aboutsummaryrefslogtreecommitdiffstats
path: root/web/stream.gathering.org/streamstats.pl
blob: e4c20f986f7b601bc4d6b75d41e808ca2ac72c79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#! /usr/bin/perl
use strict;
use warnings;
use POSIX;
use CGI qw(fatalsToBrowser);
	
my %port_spec = prepare_spec(CGI::param('port'));
my %proto_spec = prepare_spec(CGI::param('proto'));
my %audience_spec = prepare_spec(CGI::param('audience'));

#open LOG, "<", "/home/techserver/count_datacube.log"
open LOG, "-|", "/home/techserver/fix_count.pl"
#open LOG, "<", "/home/techserver/cleaned_datacube.log"
	or die "count_datacube.log: $!";

our %desc = (
	3013 => 'main (3013)',
	3014 => 'main-sd (3014)',
	3015 => 'webcam (3015)',
	3016 => 'webcam-south (3016)',
	3017 => 'webcam-south-transcode (3017)',
	3018 => 'webcam-fisheye (3018)',
	5013 => 'main-transcode (5013)',
	5015 => 'webcam-transcode (5015)',
);

my $lines = {};
my %streams = ();

while (<LOG>) {
	chomp;
	my ($date, $port, $proto, $audience, $count) = split /\s+/;
	next if (filter($port, $proto, $audience));
	my $stream_id = get_stream_id($port, $proto, $audience);
	$streams{$stream_id} = 1;
	$lines->{$date}{$stream_id} += $count;
}

close LOG;

print CGI::header(-type=>'image/png');

my $tmpfile = POSIX::tmpnam();
open GRAPH, ">", $tmpfile
	or die "$tmpfile: $!";
for my $date (sort keys %$lines) {
	my @cols = ();
	for my $stream (keys %streams) {
		push @cols, ($lines->{$date}{$stream} // "0");
	}
	print GRAPH "$date ", join(' ', @cols), "\n";
}
close GRAPH;

my $tmpfile2 = POSIX::tmpnam();
open GNUPLOT, ">", $tmpfile2
	or die "$tmpfile2: $!";
print GNUPLOT "set terminal png\n";
print GNUPLOT "set xdata time\n";
print GNUPLOT "set timefmt \"20%y-%m-%d-%H:%M:%S\"\n";
print GNUPLOT "set xtics axis \"2000-00-00-01:00:00\"\n";
print GNUPLOT "set format x \"%H\"\n";

my @plots = ();
my $idx = 2;
for my $stream (keys %streams) {
	push @plots, "\"$tmpfile\" using 1:$idx title \"$stream\" with lines";
	++$idx;
}
print GNUPLOT "plot ", join(', ', @plots);

# \"$tmpfile\" using 0:2 with lines, \"$tmpfile\" using 0:3 with lines\n";
close GNUPLOT;

system("gnuplot < $tmpfile2");

sub prepare_spec {
	my $spec = shift;
	return () if ($spec eq 'compare' || $spec eq 'dontcare');
	$spec =~ s/^compare://;

	my %ret = ();
	for my $s (split /,/, $spec) {
		$ret{$s} = 1;
	}
	return %ret;
}

sub filter {
	my ($port, $proto, $audience) = @_;
	return 1 if (filter_list(\%port_spec, $port));
	return 1 if (filter_list(\%proto_spec, $proto));
	return 1 if (filter_list(\%audience_spec, $audience));
	return 0;
}

sub filter_list {
	my ($spec, $candidate) = @_;
	return 0 if ((scalar keys %$spec) == 0);
	return !exists($spec->{$candidate});
}	

sub get_stream_id {
	my ($port, $proto, $audience) = @_;
	my @keys = ();
	if (CGI::param('port') =~ /^compare/) {
		if (exists($desc{$port})) {
			push @keys, $desc{$port};
		} else {
			push @keys, "___" . $port . "___";
		}
	}
	push @keys, $proto if (CGI::param('proto') =~ /^compare/);
	push @keys, $audience if (CGI::param('audience') =~ /^compare/);
	return join(',', @keys);
}