Browse code

revisions

gandolf authored on 01/19/2020 00:06:31
Showing 3 changed files
1 1
deleted file mode 100644
... ...
@@ -1,3 +0,0 @@
1
-rrdtool create /home/pi/database/arednstatData.rrd --step 60 DS:S:GAUGE:120:U:U DS:N:GAUGE:120:U:U DS:SNR:GAUGE:120:U:U DS:RX_MCS:GAUGE:120:U:U DS:TX_MCS:GAUGE:120:U:U DS:RX_RATE:GAUGE:120:U:U DS:TX_RATE:GAUGE:120:U:U RRA:LAST:0.5:1:1440 RRA:LAST:0.5:15:35520
2
-
3
-
... ...
@@ -60,10 +60,10 @@ img.chart {
60 60
     text-align: left;
61 61
     padding: 10px;
62 62
 }
63
-span.navContainer {
63
+span.chartSelector {
64 64
     margin: auto;
65 65
 }
66
-ul.navElement {
66
+ul.selectorElement {
67 67
     list-style-type: none;
68 68
     margin: 10px;
69 69
     padding: 0;
... ...
@@ -71,17 +71,17 @@ ul.navElement {
71 71
     background-color: #bbb;
72 72
     text-align: center;
73 73
 }
74
-li.navElement {
74
+li.selectorElement {
75 75
     display: inline-block;
76 76
     font: bold 18px arial, sans-serif;
77 77
     color: black;
78 78
 }
79
-span.navElement:hover {
79
+span.selectorElement:hover {
80 80
     background-color: #333;
81 81
     cursor: pointer;
82 82
     color: white;
83 83
 }
84
-span.navElement {
84
+span.selectorElement {
85 85
     display: inline-block;
86 86
     padding: 8px 12px;
87 87
 }
... ...
@@ -96,9 +96,8 @@ span.navElement {
96 96
 <body onload="main()">
97 97
 
98 98
 <div class="mainContainer">
99
-
100 99
 <h2><a href="https://github.com/fractalxaos/arednstat" 
101
-style="text-decoration:none" target="_new">
100
+  style="text-decoration:none" target="_new">
102 101
 KA7JLO Aredn Node Signal</a></h2>
103 102
 <h3>Last Updated</h3>
104 103
 <div class="datetime">
... ...
@@ -109,28 +108,25 @@ KA7JLO Aredn Node Signal</a></h2>
109 108
 
110 109
 <div class="rowContainer">
111 110
 <div class="currentDataCell">
112
-<div class="dataItems" style="text-align: right;">
113
-Status:<br>
114
-Page updates every:
115
-</div>
116
-<div class="dataItems">
117
-<span id="status"></span><br>
118
-<span id="period"></span> minutes
111
+<div class="dataItems" style="text-align: center;">
112
+Status: <span id="status"></span><br>
113
+Data updates every: <span id="period"></span> minutes
119 114
 </div>
115
+
120 116
 </div>
121 117
 </div>
122 118
 
123
-<span class="navContainer">
124
-<ul class="navElement">
125
-<li class="navElement">Select charts:</li>
126
-<li class="navElement"><span class="navElement" onclick="setChartPeriod(1)">
127
-24 hours</span></li>
128
-<li class="navElement"><span class="navElement" onclick="setChartPeriod(2)">
129
-4 weeks</span></li>
130
-<li class="navElement"><span class="navElement" onclick="setChartPeriod(3)">
131
-12 months</span></li>
132
-<li class="navElement"><span class="navElement" onclick="setChartPeriod(0)">
133
-Custom...</span></li>
119
+<span class="chartSelectors">
120
+<ul class="selectorElement">
121
+<li class="selectorElement">Select charts:</li>
122
+<li class="selectorElement"><span class="selectorElement"
123
+ onclick="setChartPeriod(1)">24 hours</span></li>
124
+<li class="selectorElement"><span class="selectorElement"
125
+ onclick="setChartPeriod(2)">4 weeks</span></li>
126
+<li class="selectorElement"><span class="selectorElement"
127
+ onclick="setChartPeriod(3)">12 months</span></li>
128
+<li class="selectorElement"><span class="selectorElement"
129
+ onclick="setChartPeriod(0)">Custom…</span></li>
134 130
 </ul>
135 131
 </span>
136 132
 
... ...
@@ -200,10 +196,7 @@ var periodElmt = document.getElementById("period");
200 196
 // Document elements
201 197
 var customChartsContainer = document.getElementById("customChartsContainer");
202 198
 var stockChartsContainer = document.getElementById("stockChartsContainer");
203
-var chartContainer = document.getElementsByClassName("chartContainer");
204 199
 var fmDateSelector = document.getElementById("fmDateSelector");
205
-var beginDate = document.getElementById("beginDate");
206
-var endDate = document.getElementById("endDate");
207 200
 var errorMsg = document.getElementById("errorMsg");
208 201
 
209 202
 /* Global objects */
... ...
@@ -213,7 +206,7 @@ var httpRequest = new XMLHttpRequest();
213 206
 /* Global variables */
214 207
 
215 208
 var chartPeriod = 1;
216
-var chartRefreshRate = 10; // chart refresh rate in minutes
209
+var chartRefreshRate = 0; // chart refresh rate in minutes
217 210
 
218 211
 function main() {
219 212
     /* Register call back function to process http requests */
... ...
@@ -315,14 +308,8 @@ function initializeDateSelector() {
315 308
     var dBegin = new Date(d.getFullYear(),
316 309
                d.getMonth(), d.getDate() - 1);
317 310
 
318
-    var strBegin = dBegin.getFullYear() + "-" + (dBegin.getMonth() + 1) + 
319
-                   "-" + dBegin.getDate();
320
-    var strEnd = dEnd.getFullYear() + "-" + (dEnd.getMonth() + 1) + 
321
-                 "-" + dEnd.getDate();
322
-
323
-    beginDate.valueAsDate = dBegin;
324
-    endDate.valueAsDate = dEnd;
325
-    //errorMsg.innerHTML = strBegin + " thru " + strEnd;
311
+    document.getElementById("beginDate").valueAsDate = dBegin;
312
+    document.getElementById("endDate").valueAsDate = dEnd;
326 313
 }
327 314
 
328 315
 function getCustomCharts() {
... ...
@@ -24,43 +24,74 @@ img.chart {
24 24
 
25 25
 <?php
26 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
27
+ Script: arednsig.php
28
+
29
+ Description: This scripts generates on the server charts showing
30
+ signal data spanning the period supplied by the user.  The script
31
+ does the following:
32
+    - converts user supplied dates to  epoch time
33
+    - gets the times of the first and last data point in the round
34
+      robin database (RRD)
35
+    - from above validates user supplied begin and end dates
36
+    - creates charts of the specified period
37
+
38
+ Copyright 2020 Jeff Owrey
39
+    This program is free software: you can redistribute it and/or modify
40
+    it under the terms of the GNU General Public License as published by
41
+    the Free Software Foundation, either version 3 of the License, or
42
+    (at your option) any later version.
43
+
44
+    This program is distributed in the hope that it will be useful,
45
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
46
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
47
+    GNU General Public License for more details.
48
+
49
+    You should have received a copy of the GNU General Public License
50
+    along with this program.  If not, see http://www.gnu.org/license.
51
+
52
+ Revision History
53
+   * v20 released 18 Jan 2020 by J L Owrey; first release
54
+*/
31 55
 
32
- use $output = shell_exec($cmd); to execute rrdtool commands
56
+# Define global constants
33 57
 
34
-*/
35 58
 # round robin database file
36
-$_RRD_FILE = "/home/pi/database/arednsigData.rrd";
59
+define("_RRD_FILE", str_replace("public_html/arednsig/arednsig.php",
60
+                                "database/arednsigData.rrd",
61
+                                $_SERVER["SCRIPT_FILENAME"]));
37 62
 # charts html directory
38
-$_GRAPH_DIRECTORY = "/home/pi/public_html/arednsig/dynamic/";
63
+define("_CHART_DIRECTORY", str_replace("arednsig.php",
64
+                                       "dynamic/",
65
+                                       $_SERVER["SCRIPT_FILENAME"]));
39 66
 # standard chart width in pixels
40
-$_GRAPH_WIDTH = 600;
67
+define("_CHART_WIDTH", 600);
41 68
 # standard chart height in pixels
42
-$_GRAPH_HEIGHT = 150;
69
+define("_CHART_HEIGHT", 150);
43 70
 # debug mode
44
-$_DEBUG = false;
71
+define("_DEBUG", false);
45 72
 
73
+# Set error handling modes.
46 74
 error_reporting(E_ALL);
47 75
 
48 76
 # Get user supplied chart begin and end dates.
49
-
50 77
 $beginDate = $_POST["beginDate"];
51 78
 $endDate =  $_POST["endDate"];
52 79
 
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
-
80
+# Convert the user supplied dates to epoch time stamps.
59 81
 $beginDateEp = strtotime($beginDate);
60 82
 $endDateEp = strtotime($endDate);
61 83
 
62
-# data entry validation and error checking
84
+# Get the time stamp of the earliest data point in the RRD file.
85
+$cmd = sprintf("rrdtool first %s --rraindex 1", _RRD_FILE);
86
+$firstDP = shell_exec($cmd);
63 87
 
88
+# Get the time stamp of the latest data point in the RRD file.
89
+$cmd = sprintf("rrdtool last %s", _RRD_FILE);
90
+$lastDP = shell_exec($cmd);
91
+
92
+# Determine validity of user supplied dates.  User supplied begin
93
+# date must be less than user supplied end date.  Furthermore both
94
+# dates must be within the range of dates stored in the RRD.
64 95
 if ($beginDateEp > $endDateEp) {
65 96
     echo "<p id=\"errorMsg\">" .
66 97
          "End date must be after begin date.</p>";
... ...
@@ -70,17 +101,18 @@ if ($beginDateEp > $endDateEp) {
70 101
           date('m / d / Y', $firstDP) . " and " . 
71 102
           date('m / d / Y', $lastDP) . ".</p>";
72 103
 } else {
104
+    # Generate charts from validated user supplied dates.
105
+    if (_DEBUG) {
106
+        echo "<p>Date range: " . $beginDateEp . " thru " .
107
+              $endDateEp . "</p>";
108
+    }
73 109
     createChart('custom_signal', 'S', 'dBm', 
74 110
                 'RSSI', $beginDateEp, $endDateEp,
75 111
                  0, 0, 2, false);
76 112
     createChart('custom_snr', 'SNR', 'dBm', 
77 113
                 'S/N', $beginDateEp, $endDateEp,
78 114
                  0, 0, 2, false);
79
-    if ($_DEBUG) {
80
-        echo "<p>Date range: " . $beginDateEp . " thru " .
81
-              $endDateEp . "</p>";
82
-    }
83
-
115
+    # Send html commands to client browser.
84 116
     echo "<div class=\"chartContainer\">" .
85 117
          "<img class=\"chart\" src=\"dynamic/custom_signal.png\">" .
86 118
          "</div>";
... ...
@@ -110,18 +142,19 @@ function createChart($chartFile, $dataItem, $label, $title, $begin,
110 142
            lower and upper parameters to set vertical axis scale
111 143
     Returns: True if successful, False otherwise
112 144
     */
113
-    global $_DEBUG, $_GRAPH_DIRECTORY, $_GRAPH_WIDTH,
114
-           $_GRAPH_HEIGHT, $_RRD_FILE;
115 145
 
116
-    $chartPath = $_GRAPH_DIRECTORY . $chartFile . ".png";
146
+    # Define path on server to chart files.
147
+    $chartPath = _CHART_DIRECTORY . $chartFile . ".png";
117 148
 
118 149
     # Format the rrdtool chart command.
119 150
 
120
-    # Set chart start time, height, and width.
151
+    # Set chart file name, start time, end time, height, and width.
121 152
     $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);
153
+    $cmd = sprintf($cmdfmt, $chartPath, $begin, $end, _CHART_WIDTH,
154
+                   _CHART_HEIGHT);
124 155
     $cmdfmt = "-l %s -u %s -r ";
156
+
157
+    # Set upper and lower ordinate bounds.
125 158
     if ($lower < $upper) {
126 159
         $cmd .= sprintf($cmdfmt, $lower, $upper);
127 160
     } elseif ($autoScale) {
... ...
@@ -133,13 +166,13 @@ function createChart($chartFile, $dataItem, $label, $title, $begin,
133 166
     $cmdfmt = "-v %s -t %s ";
134 167
     $cmd .= sprintf($cmdfmt, $label, $title);
135 168
    
169
+    # Define moving average window width.
170
+    $trendWindow = floor(($end - $begin) / 12);
171
+        
136 172
     # Show the data, or a moving average trend line over
137 173
     # the data, or both.
138
-    $trendWindow = floor(($end - $begin) / 12);
139
-    
140 174
     $cmdfmt = "DEF:dSeries=%s:%s:LAST ";
141
-    $cmd .= sprintf($cmdfmt, $_RRD_FILE, $dataItem);
142
-
175
+    $cmd .= sprintf($cmdfmt, _RRD_FILE, $dataItem);
143 176
     if ($addTrend == 0) {
144 177
         $cmd .= "LINE1:dSeries#0400ff ";
145 178
     } elseif ($addTrend == 1) {
... ...
@@ -148,15 +181,20 @@ function createChart($chartFile, $dataItem, $label, $title, $begin,
148 181
     } elseif ($addTrend == 2) {
149 182
         $cmd .= "LINE1:dSeries#0400ff ";
150 183
         $cmdfmt = "CDEF:smoothed=dSeries,%s,TREND LINE3:smoothed#ff0000 ";
184
+        #$cmdfmt = "CDEF:smoothed=dSeries,%s,XYZZY LINE3:smoothed#ff0000 ";
151 185
         $cmd .=  sprintf($cmdfmt, $trendWindow);
152 186
     }
153 187
      
154
-    if ($_DEBUG) {
188
+    # Execute the formatted rrdtool command in the shell. The rrdtool
189
+    # command will complete execution before the html image tags get
190
+    # sent to the browser.  This assures that the charts are available
191
+    # when the client browser executes the html code that loads the
192
+    # charts into the document displayed by the client browser.
193
+    if (_DEBUG) {
155 194
         echo "<p>chart command:<br>" . $cmd . "</p>";
156 195
     }
157
-
158
-    $result = shell_exec($cmd . "2>&1");
159
-    if ($_DEBUG == true) {
196
+    $result = shell_exec($cmd . " 2>&1");
197
+    if (_DEBUG) {
160 198
         echo "<p>result:<br>" . $result . "</p>";
161 199
     }
162 200
 }