Testing of independent graph module
authorBrian Flowers <git-admn@bsflowers.net>
Mon, 10 Oct 2016 22:08:44 +0000 (18:08 -0400)
committerBrian Flowers <git-admn@bsflowers.net>
Mon, 10 Oct 2016 22:08:44 +0000 (18:08 -0400)
graph.php [new file with mode: 0644]

diff --git a/graph.php b/graph.php
new file mode 100644 (file)
index 0000000..d930b49
--- /dev/null
+++ b/graph.php
@@ -0,0 +1,82 @@
+<?php
+$db_handle  = new SQLite3("/home/bdsm/bdsm.sqlite3");
+$colors = Array("#55F", "#5F5", "#F55", "#5FF", "#F5F", "#FF5", "#555");
+
+$result     = $db_handle->query("SELECT DISTINCT host FROM MEASUREMENTS");
+while($host = $result->fetchArray(SQLITE3_ASSOC))
+  if ( $host['host'] )
+    array_push($hosts, $host['host']);
+$host = $_GET['host'];
+
+if( !in_array($host, $hosts) )
+    die("Error: Host not found.");
+
+
+?>
+
+<HTML>
+  <HEAD>
+    <TITLE>bdsm at monitor.slightlycyberpunk.com | <?php echo $host; ?></TITLE>
+  </HEAD>
+  <BODY>
+    <canvas class="chart" id="chart-<?php echo $host; ?>" width="400" height="400"></canvas>
+    <script>
+    var canvas = document.getElementById("chart-<?php echo $host; ?>");
+    var context = canvas.getContext("2d");
+    context.lineWidth = 1;
+    context.strokeStyle = "#CCC";
+    for (var i = 50; i < 400; i+=50)
+    {
+      context.moveTo(i, 0);
+      context.lineTo(i, 400);
+    }
+    context.stroke();
+    context.beginPath();
+        
+    context.strokeStyle = "#000";
+    for (var i = 50; i < 400; i+=50)
+    {
+      context.moveTo(0, i);
+      context.lineTo(400, i);
+    }
+        
+    context.stroke();
+    context.beginPath();
+    context.lineWidth = 2;
+        
+<?php
+  $result = $db_handle->query("SELECT attribute, name FROM bdsmweb WHERE host='$host' AND graph=1");
+  $attributes = Array();
+  while($attribute = $result->fetchArray(SQLITE3_ASSOC))
+    if ( $attribute['attribute'] )
+      $attributes[$attribute['attribute']] = $attribute['name'];
+
+  $i = 0;
+  foreach (array_keys($attributes) as $attribute)
+    {
+    $query = "SELECT printf('%d', timestamp) AS timestamp,value ".
+              "FROM MEASUREMENTS ".
+              "WHERE host='$host' AND attribute='$attribute' ".
+              "ORDER BY timestamp DESC LIMIT 100";
+
+    $result = $db_handle->query($query);
+    echo "      context.beginPath();\n";
+    echo "      context.strokeStyle = '$colors[$i]';\n";
+    $record = $result->fetchArray(SQLITE3_ASSOC);
+    $final = $record['timestamp'];
+    echo "      context.moveTo(0, ".sprintf("%d", $record['value']).");\n";
+    while( $record = $result->fetchArray(SQLITE3_ASSOC) )
+      {
+      echo "      context.lineTo(".sprintf("%d", $final - $record['timestamp']).", ".(400-$record['value']*4).");\n";
+      }
+    echo "      context.stroke();\n";
+
+    $name = $attributes[$attribute];
+    echo "      context.strokeText('$name', 300, ".(395-15*$i).");";
+    $i+=1;
+    }
+?>
+
+      </script>
+  </BODY>
+</HTML>