chiark / gitweb /
cache pngs
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 28 Jun 2010 01:15:32 +0000 (02:15 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Mon, 28 Jun 2010 01:15:32 +0000 (02:15 +0100)
cgi

diff --git a/cgi b/cgi
index d6193adfab855856b43360b372c0f03afd77f687..c348508dd9b4001ff849897f5b7df2ffb87a36b1 100755 (executable)
--- a/cgi
+++ b/cgi
@@ -253,27 +253,34 @@ if (defined $elem) {
     my $sloth= param('sloth');
     die unless defined $sloth;
     $sloth =~ m/^(\d+)$/ or die;
+    $sloth= $1+0;
     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";
     
-    if (param('debug')) {
-        print((join "\n",@args),"\n"); exit 0;
-    }
-    print "Content-Type: image/png\n\n";
-
     my $title= $group;
     if (length $elem) { $title.= " $elem"; }
 
     $title .= " $g->{Units}" if $g->{Units};
-    unshift @args, '-t', $title;
+    unshift @args, '-t', $title, '-w',$width, '-h',$height;
+    unshift @args, qw(-a PNG --full-size-mode);
+
+    if (param('debug')) {
+        print((join "\n",@args),"\n"); exit 0;
+    }
 
-    exec (qw(rrdtool graph - -a PNG --full-size-mode),
-         '-w',$width, '-h',$height,
-         @args);
+#print STDERR "||| ",(join ' ', map { "'$_'" } @args)." |||\n";
+    exec(qw(sh -ec), <<'END', 'x', $cachepath, @args);
+        p="$1"; shift
+        rrdtool graph "$p" --lazy "$@" >/dev/null
+       printf "Content-Type: image/png\n\n"
+       exec cat "$p"
+END
     die $!;
 }