From c1509fa0831db502d22ee3170ffc35ef7b22620b Mon Sep 17 00:00:00 2001
From: Tom Lake <tswsl1989@sucs.org>
Date: Sun, 15 Jan 2012 18:59:20 +0000
Subject: [PATCH] Updates to shame lists

---
 components/disk.php          | 40 +++++++++++++++++++++++++++++++-----
 components/printer.graph.php |  2 +-
 components/printer.php       | 34 +++++++++++++++++++++++++-----
 3 files changed, 65 insertions(+), 11 deletions(-)

diff --git a/components/disk.php b/components/disk.php
index ad5b7fb..614f52c 100644
--- a/components/disk.php
+++ b/components/disk.php
@@ -1,7 +1,7 @@
 <?	exec("/usr/local/bin/shame.py -a", $Shame);
+	$max=10; //SET to number of users to display 
 	$smarty->assign('title',"The SUCS Hall of Shame");
-	$o="<div style='text-align: center;'><img src=\"/images/disk.graph.php\" alt=\"pie chart of top 12 disk users\"/></div>";
-
+//$o="<div style='text-align: center;'><img src=\"/images/disk.graph.php\" alt=\"pie chart of top 12 disk users\"/></div>";
 	$o.="<table align=\"center\">\n";
 	$o.="\t<tr><th align=\"center\">User</th><th align=\"center\">Home Directory Size</th></tr>\n";
 	for ( $i=1 ; $i < 13 /*(count($Shame) - 1)*/; $i++)
@@ -10,12 +10,42 @@
 		if ($size > 0)
 			$o.="\t<tr><td align=\"center\">$user</td><td align=\"center\">${size}B</td></tr>\n";
 	}
-	exec("df -h /home",$dfraw);
+	exec("df -m /home",$dfraw);
 	$dfout=explode("  ",$dfraw[1]);
-	$o.="\t<tr><td align=\"center\" colspan=\"2\">".$dfout[8]." used of ".$dfout[7]. " (".$dfout[9]." available)</td></tr>\n";
+	$o.="\t<tr><td align=\"center\" colspan=\"2\">".floor($dfout[8]/1024)."GB used of ".floor($dfout[7]/1024). "GB (".floor($dfout[10]/1024)."GB available)</td></tr>\n";
 	$o.="</table>\n";
 	$o.=$Shame[count($Shame) - 1];
 	$o.="<br />\n";
 	//readfile("tail.html");
-	$smarty->assign('body', $o);
+	$out="<script type=\"text/javascript\" src=\"https://www.google.com/jsapi\"></script>
+		    <script type=\"text/javascript\">
+      			google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});
+			google.setOnLoadCallback(drawChart);
+			function drawChart() {
+
+		        	var data = new google.visualization.DataTable();
+			        data.addColumn('string', 'User');
+			        data.addColumn('number', 'Megabytes in use');
+				data.addRows(".(count($Shame) - 1).");\n";
+	for( $i=0; $i<(count($Shame) - 3); $i++ ){
+		list($u,$d)=parseLine($Shame[$i+1]);
+		$out.="data.setValue(".($i+1).", 0, '".$u."');\n";
+		$out.="data.setValue(".($i+1).", 1,".$d.");\n";
+	}
+	$others=0;
+	for ( $i=$max; $i<count($data); $i++ ) {
+	    list($u,$d)=parseLine($Shame[$i+1]);
+	    $others+=$d;
+	}
+	$out.="data.setValue(0, 0, 'Free Space');\ndata.setValue(0,1,".$dfout[10]." );\n";
+	$out.="\t\tvar chart = new google.visualization.PieChart(document.getElementById('chart_div'));
+	           chart.draw(data, {width: 500, height: 360, backgroundColor: 'none', is3D: true, title: 'Disk Usage',sliceVisibilityThreshold: 1/180});
+	           }
+	</script>";
+	$out.="<div id='chart_div' align='center'></div>";
+	$smarty->assign('body', ($out.$o));
+function parseLine($l){
+	$fields=explode("|",preg_filter("/[ ]+/","|",$l));
+	return array(trim($fields[2]),trim(str_replace("M","",$fields[3])));
+}
 ?>
diff --git a/components/printer.graph.php b/components/printer.graph.php
index 5375f47..db4f309 100644
--- a/components/printer.graph.php
+++ b/components/printer.graph.php
@@ -7,7 +7,7 @@ $sucsDB->SetFetchMode(ADODB_FETCH_ASSOC);
 $res = $sucsDB->Execute("select username, sum(pages) as pages from printer where username != 'old' group by username order by sum(pages) desc");
 $num = $res->RecordCount();
 	for ($i=0;$i<$num;$i++) {
-	$row[$i] = $res->FetchRow();
+		$row[$i] = $res->FetchRow();
 		if ($i < 12)
 		{
 			$item[]=$row[$i]['username'];
diff --git a/components/printer.php b/components/printer.php
index 4f74242..e7fb8f7 100644
--- a/components/printer.php
+++ b/components/printer.php
@@ -4,13 +4,14 @@
  * Ported from old site by tswsl1989
  *
  */
+$max=10; //Set this to number of users to display in chart
 
 $smarty->assign('title',"Printer Statistics");
 $sucsDB = NewADOConnection('postgres8');
 $sucsDB->Connect('dbname=sucs user=apache');
 $sucsDB->SetFetchMode(ADODB_FETCH_ASSOC);
 
-$out="<img src=\"/images/printer.graph.php\" alt=\"pie chart of top 12 printer users\"/>";
+// $out="<img src=\"/images/printer.graph.php\" alt=\"pie chart of top 12 printer users\"/>";
 $table="<table border=1 cellpadding=3>\n<tr>\n<th>Username</th>\n<th>Pages</th>\n<th>Projected Monthly Usage</th>\n<th>Printed This Month</th>\n<th>What it would cost in the library</th></tr>\n";
 
 //pull in all the current users (ie not old) who have printed stuff
@@ -112,16 +113,39 @@ $table.="<tr align=\"center\">\n\t<th>Total</th>\n\t<th>$total</th>\n\t<th>".rou
 $table.="</table>\n";
 
 //output the dates of the first and last prints recorded
-$out.="<p>First Data -  ".$date['first'][weekday].", ".$date['first'][mday]." ".$date['first'][month]." ".$date['first'][year].", at ".$date['first'][hours].":".$date['first'][minutes].":".$date['first'][seconds]."<br />";
-$out.="Last Data -  ".$date['last'][weekday].", ".$date['last'][mday]." ".$date['last'][month]." ".$date['last'][year].", at ".$date['last'][hours].":".$date['last'][minutes].":".$date['last'][seconds]."</p>";
+$out.="<script type=\"text/javascript\" src=\"https://www.google.com/jsapi\"></script>
+    <script type=\"text/javascript\">
+      google.load(\"visualization\", \"1\", {packages:[\"corechart\"]});
+      google.setOnLoadCallback(drawChart);
+      function drawChart() {
+        var data = new google.visualization.DataTable();
+        data.addColumn('string', 'User');
+	data.addColumn('number', 'Pages Printed');
+        data.addRows(".($max+2).");\n";
+for( $i=0; $i<($max+1); $i++ ){
+	$out.="data.setValue(".($i).", 0, '".$data[$i]['username']."');\n";
+	$out.="data.setValue(".($i).", 1,".$data[$i]['pages'].");\n";
+}
+$others=0;
+for ( $i=$max; $i<count($data); $i++ ) {
+	$others+=$data[$i]['pages'];
+}
+$out.="data.setValue(".($max+1).", 0, 'Others');\ndata.setValue(".($max+1).",1, $others);\n";
+$out.="\t\tvar chart = new google.visualization.PieChart(document.getElementById('chart_div'));
+        chart.draw(data, {width: 500, height: 360, backgroundColor: 'none', is3D: true, title: 'Printer Usage'});
+      }
+    </script>";
+$out.="<div id=\"chart_div\" style=\"background: none;\"></div>";
+$out.="<p>First Data -  ".$date['first'][weekday].", ".$date['first'][mday]." ".$date['first'][month]." ".$date['first'][year].", at ".sprintf("%02d:%02d:%02d",$date['first'][hours],$date['first'][minutes],$date['first'][seconds])."<br />";
+$out.="Last Data -  ".$date['last'][weekday].", ".$date['last'][mday]." ".$date['last'][month]." ".$date['last'][year].", at ".sprintf("%02d:%02d:%02d",$date['last'][hours],$date['last'][minutes],$date['last'][seconds])."</p>";
 //do some silly maths to work out lots of silly things
 $paperarea = round(0.21*0.297*$total,3); //area of paper
 $paperweight = round($paperarea*.08, 3); //how much that would weigh
 $numtrees = round($paperweight/730.296, 6); //*very* roughly how many trees that would be
-$cost = round(0.1*$total,2);
+$cost = sprintf("£%01.2f",round(0.1*$total,2));
 $out.="<p>That's ".$paperarea."m<sup>2</sup> of paper, weighing ".$paperweight."kg!<br>\n";
 $out.="This is equivalent to approximately ".$numtrees." trees.<br>\n";
-$out.="That would have cost our members a grand total of £".$cost." if it were printed in the library.. not bad for £5!";
+$out.="That would have cost our members a grand total of ".$cost." if it were printed in the library.. not bad for £5!";
 
 //done
 $out.="<p>Note: The number of pages is the number spooled and may be more than the actual number printed</p>";
-- 
GitLab