chiark / gitweb /
copyright notices and licences
[rrd-graphs.git] / cgi
diff --git a/cgi b/cgi
index e0e90b8d9de18ca00dab42af5e16aed0a769a9e2..96282376aabff29acc3ff6421d20df427acd574c 100755 (executable)
--- a/cgi
+++ b/cgi
@@ -1,9 +1,29 @@
 #!/usr/bin/speedy -w -- -t100 -M1
+#
+# Main CGI program's logic; must be run inside a lock.
+
+# rrd-graphs/cgi - part of rrd-graphs, a tool for online graphs
+# Copyright 2010, 2012 Ian Jackson
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
 
 use strict qw(vars);
 use CGI::SpeedyCGI qw/:standard -no_xhtml/;
 use CGI qw/:standard -no_xhtml/;
 use POSIX;
+use MD5;
 
 sub fail ($) {
     print(header(-status=>500),
@@ -161,7 +181,7 @@ if (!open NM, '<', "$SELF/data/news/name-map") {
     }
 }
 
-our @news_graphs;
+our %news_sources;
 
 foreach my $src (<$SELF/data/news/*.rrd>) {
     my $site= $src;
@@ -174,23 +194,52 @@ foreach my $src (<$SELF/data/news/*.rrd>) {
     my $newsite= $news_name_map{$site,$inout};
     $site= $newsite if defined $newsite;
     next if $site eq '-';
-    #my $sk= join '.', reverse split /\./, $site;
+    push @{ $news_sources{$site}{$inout} }, $src;
+}
+
+our @news_graphs;
+
+foreach my $site (keys %news_sources) {
     my $sk= $site;
-    $sk .= " $&" if $sk =~ s/^[^.]*(?:news|nntp|peer)[^.]*\.//;
-    $sk .= " $inout";
-    push @news_graphs, [ $sk, $site, $inout, $src ];
+    for (;;) {
+        last unless $sk =~
+            s/^[^.]*(?:chiark|greenend|news|nntp|peer|feed|in|out)[^.]*\.//;
+        $sk .= " $&";
+    }
+    foreach my $inout (keys %{ $news_sources{$site} }) {
+        push @news_graphs, [ "$sk $inout", $site, $inout ];
+    }
 }
 
 foreach my $siteinfo (sort { $a->[0] cmp $b->[0] } @news_graphs) {
-    my ($sortkey, $site, $inout, $src)= @$siteinfo;
+    my ($sortkey, $site, $inout)= @$siteinfo;
+    my @sources= @{ $news_sources{$site}{$inout} };
+
+    my @vals= $inout eq 'out'
+        ? qw(missing deferred unwanted accepted rejected body_missing)
+        : qw(accepted refused rejected duplicate
+             accepted_size duplicate_size);
+    my @defs;
+    foreach my $val (@vals) {
+        my $def= "CDEF:$val=0";
+        foreach my $si (0..$#sources) {
+            my $src= $sources[$si];
+            my $tvar= "${val}_${si}";
+            push @defs, "DEF:$tvar=$src:$val:AVERAGE";
+            $def .= ",$tvar,ADDNAN";
+        }
+        push @defs, $def;
+        if ($val =~ m/_size$/) {
+            push @defs, "CDEF:kb_$`=$val,1024,/";
+        }
+    }
     graph_of_group("News", $site, $inout,
          {
                Units => '[art/s]',
                TimeRanges => [ map { $_*86400 } qw(1 7 31), 366, 366*3 ]
            }, $inout eq 'out' ?
          [
-          (map { "DEF:$_=$src:$_:AVERAGE" }
-               qw(missing deferred unwanted accepted rejected body_missing)),
+          @defs,
           "AREA:accepted#00f:ok",
           "AREA:body_missing#ff0:miss:STACK",
           "AREA:rejected#f00:rej:STACK",
@@ -198,11 +247,7 @@ foreach my $siteinfo (sort { $a->[0] cmp $b->[0] } @news_graphs) {
           "AREA:deferred#ddd:defer:STACK",
           ] :
          [
-          (map { "DEF:$_=$src:$_:AVERAGE" }
-               qw(accepted refused rejected duplicate)),
-          (map { ("DEF:bytes_$_=$src:${_}_size:AVERAGE",
-                  "CDEF:kb_$_=bytes_$_,1024,/")
-             } qw(accepted duplicate)),
+          @defs,
           "AREA:accepted#00f:ok:STACK",
           "AREA:rejected#f00:rej:STACK",
           "AREA:duplicate#000:dupe:STACK",
@@ -371,13 +416,16 @@ sub num_param ($$$$) {
 
 our $group= param('graph');
 
+my $defwidth= 370;
+my $defheight= 200;
+
 our $elem= param('elem');
 if (defined $elem) {
     my $g= $graphs{$section,$group,$elem};
     die unless $g;
 
-    my $width= num_param('w',370,100,1600);
-    my $height= num_param('h',200,100,1600);
+    my $width= num_param('w',$defwidth,100,1600);
+    my $height= num_param('h',$defheight,100,1600);
 
     my $sloth= param('sloth');
     die unless defined $sloth;
@@ -386,9 +434,6 @@ if (defined $elem) {
     my $end= $g->{TimeRanges}[$sloth];
     die unless defined $end;
 
-    my $cacheid= "$section!$group!$elem!$sloth!$width!$height";
-    my $cachepath= "cache/$cacheid.png";
-
     my @args= @{ $g->{Args} };
     s,\<interval/(\d+)\>, $end/$1 ,ge foreach @args;
     unshift @args, qw(--end now --start), "end-${end}s";
@@ -400,6 +445,10 @@ if (defined $elem) {
     unshift @args, '-t', $title, '-w',$width, '-h',$height;
     unshift @args, qw(-a PNG --full-size-mode);
 
+    my $cacheid= "$section!$group!$elem!$sloth!$width!$height!";
+    $cacheid .= unpack "H*", MD5->hash(join '\0', @args);
+    my $cachepath= "cache/$cacheid.png";
+
     if (param('debug')) {
         print((join "\n",@args),"\n"); exit 0;
     }
@@ -462,7 +511,8 @@ if (defined $detail) {
            my $imgurl= "$self?graph=$detail&section=$section".
                "&sloth=$tsloth&elem=$elem";
            print a({href=>"$imgurl&w=780&h=800"},
-                   img({src=>$imgurl, alt=>''}));
+                   img({src=>$imgurl, alt=>'',
+                        width=>$defwidth, height=>$defheight}));
        }
     }
     print end_html();
@@ -508,7 +558,7 @@ foreach my $group (@{ $section_groups{$section} }) {
     foreach my $elem (@$elems) {
        my $g= $graphs{$ref_section,$ref_group,$elem};
        print img({src=>"$imgurl&elem=$elem&sloth=".($sloth + $g->{Slower}),
-                  alt=>''});
+                  alt=>'', width=>$defwidth, height=>$defheight});
     }
     print "</span>";
     print "</a>\n";