Browse code

custom charts added

gandolf authored on 01/17/2020 00:21:04
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,167 @@
1
+<html>
2
+<!-- Courtsey ruler
3
+12345678901234567890123456789012345678901234567890123456789012345678901234567890
4
+-->
5
+<head>
6
+<style>
7
+p {
8
+    font: 14px ariel, sans serif;
9
+}
10
+#errorMsg {
11
+    font:bold 18px arial,sans-serif;
12
+    color:red;
13
+    text-align:center;
14
+}
15
+.chartContainer {
16
+    padding: 2px;
17
+}
18
+img.chart {
19
+    width:100%;
20
+}
21
+</style>
22
+</head>
23
+<body>
24
+
25
+<?php
26
+/*
27
+ done - Convert dates to  epoch time
28
+ done - Rrdtool - get first in time and last in time
29
+ From above validate begin and end dates
30
+ Rrdtool - create charts of time period
31
+
32
+ use $output = shell_exec($cmd); to execute rrdtool commands
33
+
34
+*/
35
+# round robin database file
36
+$_RRD_FILE = "/home/pi/database/arednsigData.rrd";
37
+# charts html directory
38
+$_GRAPH_DIRECTORY = "/home/pi/public_html/arednsig/dynamic/";
39
+# standard chart width in pixels
40
+$_GRAPH_WIDTH = 600;
41
+# standard chart height in pixels
42
+$_GRAPH_HEIGHT = 150;
43
+# debug mode
44
+$_DEBUG = false;
45
+
46
+error_reporting(E_ALL);
47
+
48
+# Get user supplied chart begin and end dates.
49
+
50
+$beginDate = $_POST["beginDate"];
51
+$endDate =  $_POST["endDate"];
52
+
53
+$cmd = sprintf("rrdtool first %s --rraindex 1",$_RRD_FILE);
54
+$firstDP = shell_exec($cmd);
55
+
56
+$cmd = sprintf("rrdtool last %s", $_RRD_FILE);
57
+$lastDP = shell_exec($cmd);
58
+
59
+$beginDateEp = strtotime($beginDate);
60
+$endDateEp = strtotime($endDate);
61
+
62
+# data entry validation and error checking
63
+
64
+if ($beginDateEp > $endDateEp) {
65
+    echo "<p id=\"errorMsg\">" .
66
+         "End date must be after begin date.</p>";
67
+} elseif ($beginDateEp < $firstDP || $endDateEp > $lastDP) {
68
+    echo "<p id=\"errorMsg\">" .
69
+          "Date range must be between " .
70
+          date('m / d / Y', $firstDP) . " and " . 
71
+          date('m / d / Y', $lastDP) . ".</p>";
72
+} else {
73
+    createChart('custom_signal', 'S', 'dBm', 
74
+                'RSSI', $beginDateEp, $endDateEp,
75
+                 0, 0, 2, false);
76
+    createChart('custom_snr', 'SNR', 'dBm', 
77
+                'S/N', $beginDateEp, $endDateEp,
78
+                 0, 0, 2, false);
79
+    if ($_DEBUG) {
80
+        echo "<p>Date range: " . $beginDateEp . " thru " .
81
+              $endDateEp . "</p>";
82
+    }
83
+
84
+    echo "<div class=\"chartContainer\">" .
85
+         "<img class=\"chart\" src=\"dynamic/custom_signal.png\">" .
86
+         "</div>";
87
+    echo "<div class=\"chartContainer\">" .
88
+         "<img class=\"chart\" src=\"dynamic/custom_snr.png\">" .
89
+         "</div>";
90
+}
91
+
92
+function createChart($chartFile, $dataItem, $label, $title, $begin,
93
+                     $end, $lower, $upper, $addTrend, $autoScale) {
94
+    /*
95
+    Uses rrdtool to create a chart of specified aredn node data item.
96
+    Parameters:
97
+       fileName - name of the created chart file
98
+       dataItem - data item to be charted
99
+       label - string containing a label for the item to be charted
100
+       title - string containing a title for the chart
101
+       begin - beginning time of the chart data
102
+       end   - ending time of the data to be charted
103
+       lower - lower bound for chart ordinate #NOT USED
104
+       upper - upper bound for chart ordinate #NOT USED
105
+       addTrend - 0, show only chart data
106
+                  1, show only a trend line
107
+                  2, show a trend line and the chart data
108
+       autoScale - if True, then use vertical axis auto scaling
109
+           (lower and upper parameters are ignored), otherwise use
110
+           lower and upper parameters to set vertical axis scale
111
+    Returns: True if successful, False otherwise
112
+    */
113
+    global $_DEBUG, $_GRAPH_DIRECTORY, $_GRAPH_WIDTH,
114
+           $_GRAPH_HEIGHT, $_RRD_FILE;
115
+
116
+    $chartPath = $_GRAPH_DIRECTORY . $chartFile . ".png";
117
+
118
+    # Format the rrdtool chart command.
119
+
120
+    # Set chart start time, height, and width.
121
+    $cmdfmt = "rrdtool graph %s -a PNG -s %s -e %s -w %s -h %s ";
122
+    $cmd = sprintf($cmdfmt, $chartPath, $begin, $end, $_GRAPH_WIDTH,
123
+                   $_GRAPH_HEIGHT);
124
+    $cmdfmt = "-l %s -u %s -r ";
125
+    if ($lower < $upper) {
126
+        $cmd .= sprintf($cmdfmt, $lower, $upper);
127
+    } elseif ($autoScale) {
128
+        $cmd .= "-A ";
129
+    }
130
+    $cmd .= "-Y ";
131
+
132
+    # Set the chart ordinate label and chart title. 
133
+    $cmdfmt = "-v %s -t %s ";
134
+    $cmd .= sprintf($cmdfmt, $label, $title);
135
+   
136
+    # Show the data, or a moving average trend line over
137
+    # the data, or both.
138
+    $trendWindow = floor(($end - $begin) / 12);
139
+    
140
+    $cmdfmt = "DEF:dSeries=%s:%s:LAST ";
141
+    $cmd .= sprintf($cmdfmt, $_RRD_FILE, $dataItem);
142
+
143
+    if ($addTrend == 0) {
144
+        $cmd .= "LINE1:dSeries#0400ff ";
145
+    } elseif ($addTrend == 1) {
146
+        $cmdfmt = "CDEF:smoothed=dSeries,%s,TREND LINE3:smoothed#ff0000 ";
147
+        $cmd .= sprintf($cmdfmt, $trendWindow);
148
+    } elseif ($addTrend == 2) {
149
+        $cmd .= "LINE1:dSeries#0400ff ";
150
+        $cmdfmt = "CDEF:smoothed=dSeries,%s,TREND LINE3:smoothed#ff0000 ";
151
+        $cmd .=  sprintf($cmdfmt, $trendWindow);
152
+    }
153
+     
154
+    if ($_DEBUG) {
155
+        echo "<p>chart command:<br>" . $cmd . "</p>";
156
+    }
157
+
158
+    $result = shell_exec($cmd . "2>&1");
159
+    if ($_DEBUG == true) {
160
+        echo "<p>result:<br>" . $result . "</p>";
161
+    }
162
+}
163
+
164
+?>
165
+
166
+</body>
167
+</html>