Minor update to the README master
authorBrian Flowers <git-admn@bsflowers.net>
Sun, 9 Apr 2017 21:14:39 +0000 (17:14 -0400)
committerBrian Flowers <git-admn@bsflowers.net>
Sun, 9 Apr 2017 21:14:39 +0000 (17:14 -0400)
README
graph2.php [new file with mode: 0644]

diff --git a/README b/README
index 2f976a2..d4b45c4 100644 (file)
--- a/README
+++ b/README
@@ -2,12 +2,13 @@ bdsm-web
 A Slightly Cyberpunk project
 [git|www].slightlycyberpunk.com
 
 A Slightly Cyberpunk project
 [git|www].slightlycyberpunk.com
 
-bdsm-web is a set of PHP scripts for displaying data from bdsm using HTML5 canvas charts.
+bdsm-web is a set of PHP scripts for displaying graphs and data from bdsm
+monitors in a web interface, with graphs drawn using HTML5 canvas elements.
 
 0) PREREQUISITES
 
 a) bdsm configured using the sqlite3 processor
 
 0) PREREQUISITES
 
 a) bdsm configured using the sqlite3 processor
-b) a remotely modern web browser
+b) a somewhat modern web browser ((HTML5 support)
 
 1) INSTALLATION INSTRUCTIONS
 
 
 1) INSTALLATION INSTRUCTIONS
 
diff --git a/graph2.php b/graph2.php
new file mode 100644 (file)
index 0000000..42f9ab8
--- /dev/null
@@ -0,0 +1,114 @@
+<?php
+try
+{
+  $db = new PDO("sqlite:/home/bdsm/etc/bdsm/bdsm.db");
+  $result = $db->query("SELECT DISTINCT host FROM MEASUREMENTS");
+}
+catch(PDOException $e)
+{
+  echo 'Connection failed: ' . $e->getMessage();
+}
+
+if( !$result )
+{
+  echo "Error: No data found in database file /home/bdsm/etc/bdsm/bdsm.db";
+  echo "Query: SELECT DISTINCT host FROM MEASUREMENTS";
+}
+
+$colors = Array("#55F", "#5C5", "#F55", "#5FF", "#F5F", "#FF5", "#555");
+
+$hosts = Array();
+while($host = $result->fetch())
+  if ( $host['host'] )
+    array_push($hosts, $host['host']);
+$host = urldecode($_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="400px" height="450px"></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+.5, 0);
+      context.lineTo(i+.5, 400);
+    }
+    context.stroke();
+    context.beginPath();
+
+    context.strokeStyle = "#000";
+    for (var i = 50; i < 400; i+=50)
+    {
+      context.moveTo(0, i+.5);
+      context.lineTo(400, i+.5);
+    }
+
+    context.stroke();
+    context.beginPath();
+    context.lineWidth = 2;
+<?php
+  $result = $db->query("SELECT attribute, name FROM bdsmweb WHERE host='$host' AND graph=1;");
+  if( ! $result )
+  {
+    echo "</script><br/>";
+    echo "Error: No configuration found in database file /home/bdsm/etc/bdsm/bdsm.db for host $host<br/>";
+    echo "Query: SELECT attribute, name FROM bdsmweb WHERE host='$host' AND graph=1;";
+    die();
+  }
+        
+  $attributes = Array();
+  while($attribute = $result->fetch())
+  {
+    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->query($query);
+    $record = $result->fetch();
+    $final = $record['timestamp'];
+    $timediff = time() - $final;
+
+    $timestamp = ($timediff % 60)."s";
+    if( $timediff > 60 )
+      $timestamp = (($timediff / 60) % 60)."m ".$timestamp;
+
+    echo " context.font = '12px monospace';";
+    echo "      context.strokeStyle = '$colors[$i]';\n";
+    echo "      context.strokeText('-$timestamp', 350.5, ".(10.5+10*$i).");\n";
+
+    //echo "      context.strokeStyle = '$colors[$i]';\n";
+    echo "      context.moveTo(0, ".sprintf("%d", $record['value']).");\n";
+    echo "      context.beginPath();\n";
+    while( $record = $result->fetch() )
+      {
+       echo "      context.lineTo(".sprintf("%d", $final - $record['timestamp']).", ".(400-$record['value']*4).");\n";
+      }
+    echo "      context.stroke();\n";
+
+    $name = $attributes[$attribute];
+    echo "      context.strokeText('$name', ".(100*($i%5)+.5).", ".(425+(10*floor($i/5))+.5).");";
+    $i+=1;
+    }
+?>
+
+      </script>
+  </BODY>
+</HTML>