doc
[icelog.git] / icecounter.cgi
1 #!/usr/bin/perl
2
3 use vars qw($dsn $username $password $dbh $query);
4 use vars qw($my_mon $my_year);
5 use vars qw($title);
6 use vars qw($min_time $max_time $min_mon $max_mon $min_year $max_year );
7 use CGI;
8 use CGI::Carp qw(fatalsToBrowser);
9 use DBI;
10 use Time::Local;
11
12 require "/etc/icelog.conf";
13
14 my $cgi = new CGI;
15 ($query) = $cgi->keywords;
16
17 $dbh = DBI->connect($dsn, $username, $password)
18   or die "Can't connect to $dsn: ". $DBI::errstr;
19
20 if ( $cgi->param('customer') ) { 
21   @customers = ( $cgi->param('customer') );
22 } else { #everybody
23   my $sth = $dbh->prepare('select distinct customer from icelog')
24     or die $dbh->errstr;
25   $sth->execute or die $sth->errstr;
26   @customers = map { $_->[0] } @{$sth->fetchall_arrayref};
27   $sth->finish;
28 }
29 ($my_mon,$my_year) = ($cgi->param('mon'), $cgi->param('year'));
30
31 my $sth = $dbh->prepare('select min(start), max(start) from icelog')
32   or die $dbh->errstr;
33 $sth->execute or die $sth->errstr;
34 ( $min_time, $max_time ) = @{$sth->fetchrow_arrayref};
35 $sth->finish;
36 ( $min_mon, $min_year ) = (localtime($min_time))[4,5];
37 $min_mon++; $min_year+=1900;
38 ( $max_mon, $max_year ) = (localtime($max_time))[4,5];
39 $max_mon++; $max_year+=1900;
40
41 my $title = 'icecast log';
42 if ( $my_mon && $my_year ) {
43   $title .= " $my_mon/$my_year";
44 } else {
45   $title .= ' (all)';
46 }
47
48 print $cgi->header, <<END;
49 <html>
50   <head>
51     <title>icecast log</title>
52   </head>
53   <body bgcolor="#e8e8e8">
54   <h2>$title</h2>
55 END
56
57 for ( my($mon,$year) = ($min_mon, $min_year);
58       $year < $max_year || ( $year == $max_year && $mon <= $max_mon);
59       do { $mon++; if ( $mon == 13 ) { $year++; $mon-=12; } }
60     ) {
61   $cgi->param('year', $year); 
62   $cgi->param('mon', $mon); 
63   print '<a href="'. $cgi->self_url. qq(">$mon/$year</a> | );
64 }
65 $cgi->param('year', '');
66 $cgi->param('mon', '');
67 print '<a href="'. $cgi->self_url. qq(">all</a>);
68 $cgi->param('year', $my_year);
69 $cgi->param('mon', $my_mon);
70
71 print <<END;
72   <br>
73   <table border>
74     <tr><th>Cust#</th><th>Minutes (live)</th><th>Minutes (archived)</th><th>Minutes (total)</th></tr>
75 END
76
77 foreach my $customer ( @customers ) {
78
79   my $liveminutes = &getminutes($customer, 'Y', $my_mon, $my_year);
80   my $archminutes = &getminutes($customer, 'N', $my_mon, $my_year);
81   my $totminutes = $liveminutes + $archminutes;
82
83   $cgi->param('customer', $customer);
84   my $self_url = $cgi->self_url;
85
86   print qq(<tr><td><a href="$self_url">$customer</a></td><td>$liveminutes</td><td>$archminutes</td><td>$totminutes</td></tr>);
87 }
88
89 print <<END;
90   </table>
91 </html>
92 END
93
94 sub getminutes {
95   my($customer, $liveflag, $mon, $year) = @_;
96   my $statement = 
97     'select sum(seconds) from icelog where customer = ? and liveflag = ?';
98   if ( $mon && $year ) {
99     my $start = timelocal(0,0,0,1,$mon-1,$year-1900);
100     $mon++; if ( $mon == 13 ) { $year++; $mon-=12; }
101     my $end = timelocal(0,0,0,1,$mon-1,$year-1900) - 1;
102     $statement .= " and start >= $start and start <= $end";
103   }
104   #warn $statement;
105   my $sth = $dbh->prepare($statement) or die $dbh->errstr;
106   $sth->execute($customer, $liveflag) or die $sth->errstr;
107   my $seconds = $sth->fetchrow_arrayref->[0];
108   $sth->finish;
109   sprintf("%.3f", $seconds / 60);
110 }