From e21bcef240251c9fe5f204c8fe5e6d89292a56b7 Mon Sep 17 00:00:00 2001
From: Chad Barb <barb@flux.utah.edu>
Date: Fri, 28 Feb 2003 23:39:48 +0000
Subject: [PATCH] Cached thumbnails. A LOT faster and more CPU friendly.

Stored in $TB/www/thumbs/,
with filenames generated by hashing random data with experiment name.
(Stored in vis_experiments table in db.)
---
 sql/database-create.sql | 11 ++++++
 tbsetup/tbend.in        |  3 +-
 tbsetup/tbprerun.in     |  2 +-
 vis/prerender.in        | 75 +++++++++++++++++++++++++++++++++--------
 vis/prerender_all.in    | 11 ++++--
 www/showexp_list.php3   | 17 ++++++++--
 6 files changed, 98 insertions(+), 21 deletions(-)

diff --git a/sql/database-create.sql b/sql/database-create.sql
index 8abbead7b6..045417ed4f 100644
--- a/sql/database-create.sql
+++ b/sql/database-create.sql
@@ -1245,6 +1245,17 @@ CREATE TABLE virt_vtypes (
   members text
 ) TYPE=MyISAM;
 
+--
+-- Table structure for table 'vis_experiments'
+--
+
+CREATE TABLE vis_experiments (
+  eid varchar(32) NOT NULL default '',
+  pid varchar(12) NOT NULL default '',
+  thumb_hash varchar(64) NOT NULL default '',
+  PRIMARY KEY  (eid,pid)
+) TYPE=MyISAM;
+
 --
 -- Table structure for table 'vis_nodes'
 --
diff --git a/tbsetup/tbend.in b/tbsetup/tbend.in
index 35e8b29299..84eac87394 100644
--- a/tbsetup/tbend.in
+++ b/tbsetup/tbend.in
@@ -106,7 +106,7 @@ DBQueryWarn("DELETE from ipport_ranges  where pid='$pid' and eid='$eid'") or
     $errors++;
 
 print "Removing visualization data...\n";
-system("prerender -r $pid $eid");
+system("prerender -r -t $pid $eid");
 
 if ($errors == 0) {
     SetExpState($pid, $eid, EXPTSTATE_TERMINATED) or
@@ -117,3 +117,4 @@ print "Cleanup finished! " . TBTimeStamp() . "\n";
 
 # We exit with whether we had errors or not.
 exit $errors;
+
diff --git a/tbsetup/tbprerun.in b/tbsetup/tbprerun.in
index 90df915ec9..65ac4dd9bd 100644
--- a/tbsetup/tbprerun.in
+++ b/tbsetup/tbprerun.in
@@ -112,7 +112,7 @@ print "Parser done! " . TBTimeStamp() . "\n";
 
 TBDebugTimeStamp("prerender started in background");
 print "Precomputing visualization (in background)...\n";
-system("prerender $pid $eid &");
+system("prerender -t $pid $eid &");
 
 TBDebugTimeStamp("routes started");
 print "Setting up static routes (if requested) ... \n";
diff --git a/vis/prerender.in b/vis/prerender.in
index b150eed6c3..b7fbee8a0d 100644
--- a/vis/prerender.in
+++ b/vis/prerender.in
@@ -8,6 +8,7 @@
 
 use English;
 use Getopt::Std;
+use Digest::MD5  qw(md5 md5_hex md5_base64);
 
 # Need this module to use mktemp (commented out below)
 # use File::MkTemp;
@@ -22,7 +23,7 @@ use libdb;
 $| = 1;
 
 # Untaint the path
-$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
+$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin:@prefix@/libexec/vis';
 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
 
 #my $NEATO_CMD = "neato -Gstart=5 -Gepsilon=0.0001 -Goverlap=scale -Gsep=1";
@@ -30,15 +31,17 @@ delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
 #my $NEATO_CMD = "neato -Gstart=rand -Gepsilon=0.001 -Goverlap=scale -Gpack=true -Gsep=4 -Gmclimit=30";
 #my $NEATO_CMD = "neato -Gstart=rand -Gepsilon=0.005 -Goverlap=scale -Gmaxiter=20000 -Gpack=true";
 my $NEATO_CMD = "neato -Gstart=rand -Gepsilon=0.005 -Gmaxiter=20000 -Gpack=true";
+my $THUMB_CMD = "render -t 128";
 
 sub dprint($);
 
 sub usage {
-    die "Usage:\nprerender [-v] [-r] <pid> <eid>\n" .
-        "    -r  Just remove vis info from DB\n";
+    die "Usage:\nprerender [-v] [-r] [-t] <pid> <eid>\n" .
+        "    -r  Just remove vis info from DB\n".
+        "    -t  Generate (or remove) thumbnail\n";
 }
 
-my $optlist = "rv";
+my $optlist = "rvt";
 
 %options = ();
 
@@ -46,22 +49,55 @@ if (!getopts($optlist, \%options)) { usage; }
 if (@ARGV != 2) { usage; }
 
 my $debug = 0;
-if ( defined($options{"v"}) ) { $debug++; }
 
-# for the mungeName function, below.
-$mungeUID = 0;
+if ( defined($options{"v"}) ) { 
+    $debug++; 
+    # pass verbosity along to thumbnail render.
+    $THUMB_CMD .= " -v"; 
+}
 
-my $pid = $ARGV[0];
-my $eid = $ARGV[1];
+#my ($pid) = $ARGV[0];
+#my ($eid) = $ARGV[1];
+my ($pid) = $ARGV[0] =~ /([0-9a-zA-Z\-]+)/;
+my ($eid) = $ARGV[1] =~ /([0-9a-zA-Z\-]+)/;
 
-### First, wipe out the old layout.
+if ( defined($options{"t"}) ) { 
+    my $result = DBQueryFatal("SELECT thumb_hash FROM vis_experiments " .
+			      "WHERE pid='$pid' AND eid='$eid'");
+    ($thumb) = $result->fetchrow;
 
-DBQueryFatal("DELETE FROM vis_nodes WHERE pid='$pid' AND eid='$eid'");    
+    if (!$thumb) {
+	if (defined($options{"r"})) {
+	    print STDERR "Warning! No thumbnail found to remove.\n";
+	} else {
+	    # If we're not removing the experiment, we need to generate a thumbhash.
+	    # Hash a lot of random junk.
+	    $thumb = md5_hex( rand(99999) . "-$$-$pid-$eid-XYzzY" );
+	    DBQueryFatal("INSERT INTO vis_experiments (pid,eid,thumb_hash) VALUES ".
+			 "('$pid','$eid','$thumb')");
+	}
+    }
 
-### If they specified -r, meaning they just wanted to remove vis info 
-### from the DB, we quit now.
+    if ($thumb) { 
+	dprint "Thumb hash is '$thumb'.\n"; 
+    } else {
+	dprint "No thumb hash.\n";
+    }
+}
 
-if (defined($options{"r"})) { exit; }
+# for the mungeName function, below.
+$mungeUID = 0;
+
+### If they specified -r, meaning they just wanted to remove vis info 
+### from the DB, we do it and quit.
+if (defined($options{"r"})) {
+    if ($thumb) {
+	system("/bin/rm @prefix@/www/thumbs/tn$thumb.png");
+    }
+    DBQueryFatal("DELETE FROM vis_nodes WHERE pid='$pid' AND eid='$eid'");    
+    DBQueryFatal("DELETE FROM vis_experiments WHERE pid='$pid' AND eid='$eid'");
+    exit; 
+}
 
 ### Now, read experiment virtual info from the DB.
 
@@ -377,6 +413,9 @@ unlink $tempfile;
 
 ### Put new data into db.
 
+# First, wipe out the old layout.
+DBQueryFatal("DELETE FROM vis_nodes WHERE pid='$pid' AND eid='$eid'");    
+
 foreach $node (keys %nodes) {
     if (exists $nodes{ $node }{"type"} &&
         exists $nodes{ $node }{"x_best"} &&
@@ -390,6 +429,14 @@ foreach $node (keys %nodes) {
     }
 }
 
+### Generate thumbnail, if called for.
+
+if ($thumb) {
+    if (system("$THUMB_CMD $pid $eid > @prefix@/www/thumbs/tn$thumb.png")) {
+	print STDERR "Error generating thumbnail.\n";
+    }
+}
+
 ### Success!!
 
 exit;
diff --git a/vis/prerender_all.in b/vis/prerender_all.in
index 98eca274af..9252ca53e0 100644
--- a/vis/prerender_all.in
+++ b/vis/prerender_all.in
@@ -23,12 +23,13 @@ $ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';
 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
 
 my $PRERENDER_CMD = "@prefix@/libexec/vis/prerender";
+my $THUMBRENDER_CMD = "@prefix@/bin/dbvistopology -t 128";
 
 sub usage {
-    die "Usage:\prerender_all [-v]";
+    die "Usage:\prerender_all [-v] [-t]";
 }
 
-my $optlist = "v";
+my $optlist = "vt";
 
 %options = ();
 
@@ -39,6 +40,10 @@ if ( defined($options{"v"}) ) {
     $PRERENDER_CMD .= " -v";
 }
 
+if ( defined($options{"t"}) ) { 
+    $PRERENDER_CMD .= " -t";
+}
+
 my $result = DBQueryFatal("SELECT pid,eid FROM experiments");
 
 while (my ($pid,$eid) = $result->fetchrow) {
@@ -48,4 +53,6 @@ while (my ($pid,$eid) = $result->fetchrow) {
     }
 }
 
+
+
 exit(0);
diff --git a/www/showexp_list.php3 b/www/showexp_list.php3
index ef7f4ec64d..092545f680 100644
--- a/www/showexp_list.php3
+++ b/www/showexp_list.php3
@@ -121,8 +121,11 @@ if ($isadmin) {
 		     "(to_days(now())-to_days(expt_swapped)) as lastswap, ".
                      "count(r.node_id) as ncount, swap_requests, ".
 		     "round((unix_timestamp(now()) - ".
-		     "unix_timestamp(last_swap_req))/3600,1) as lastreq ".
+		     "unix_timestamp(last_swap_req))/3600,1) as lastreq, ".
+		     "ve.thumb_hash as thumb_hash ".
 		     "from experiments as e ".
+		     "left join vis_experiments as ve on ".
+		     "ve.pid=e.pid and ve.eid=e.eid ".
                      "left join reserved as r on e.pid=r.pid and e.eid=r.eid ".
                      "left join nodes as n on r.node_id=n.node_id ".
                      "where (n.type!='dnard' or n.type is null) $clause ".
@@ -165,10 +168,13 @@ else {
     $experiments_result =
 	DBQueryFatal("select distinct e.*, ".
                      "date_format(expt_swapped,'%Y-%m-%d') as d, ".
-                     "count(r.node_id) as ncount ".
+                     "count(r.node_id) as ncount, ".
+		     "ve.thumb_hash as thumb_hash ".
                      "from group_membership as g ".
                      "left join experiments as e on ".
                      "  g.pid=e.pid and g.pid=g.gid ".
+		     "left join vis_experiments as ve on ".
+		     "ve.pid=e.pid and ve.eid=e.eid ".
                      "left join reserved as r on e.pid=r.pid and e.eid=r.eid ".
                      "left join nodes as n on r.node_id=n.node_id ".
                      "where (n.type!='dnard' or n.type is null) and ".
@@ -238,6 +244,7 @@ if ($thumb && !$idle) {
 	$huid = $row["expt_head_uid"];
 	$name = stripslashes($row["expt_name"]);
 	$date = $row["d"];
+	$thumb_hash = $row["thumb_hash"];
 	
 	if ($idle && ($str=="&nbsp;" || !$pcs)) { continue; }
 #	echo "<table style=\"float: none;\" width=256 height=192><tr><td>".
@@ -246,12 +253,16 @@ if ($thumb && !$idle) {
 #	echo "<td width=50%>".
 #	echo "<tr
 
+#	Should create on demand.
+
 	echo "<td>".
              "<table border=0 cellpadding=4 cellspacing=0>".
 	     "<tr>".
 	     "<td width=128 align=center>".
 	     "<img border=1 width=128 height=128 class='stealth' ".
-	     " src='top2image.php3?pid=$pid&eid=$eid&thumb=128' />".
+#	     " src='top2image.php3?pid=$pid&eid=$eid&thumb=128' />".
+#	     " src='thumbs/$pid.$eid.png' />".
+	     " src='thumbs/tn$thumb_hash.png' />".
              "</td>" .
 	     "<td align=left class='paddedcell'>".
 	     "<b>".
-- 
GitLab