-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisualize_data.php
More file actions
executable file
·218 lines (218 loc) · 6.16 KB
/
visualize_data.php
File metadata and controls
executable file
·218 lines (218 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<!DOCTYPE html>
<html lang="en" style="height: 100%">
<head>
<meta charset="UTF-8">
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
<title>Document</title>
<style>
body {
font-family: "Open Sans", sans-serif;
line-height: 1.25;
}
table {
border: 1px solid #ccc;
border-collapse: collapse;
margin: 0;
padding: 0;
width: 100%;
table-layout: fixed;
}
table caption {
font-size: 1.5em;
margin: .5em 0 .75em;
}
table tr {
background: #f8f8f8;
border: 1px solid #ddd;
padding: .35em;
}
table th,
table td {
padding: .625em;
text-align: center;
}
table th {
font-size: .85em;
letter-spacing: .1em;
text-transform: uppercase;
}
@media screen and (max-width: 600px) {
table {
border: 0;
}
table caption {
font-size: 1.3em;
}
table thead {
border: none;
clip: rect(0 0 0 0);
height: 1px;
margin: -1px;
overflow: hidden;
padding: 0;
position: absolute;
width: 1px;
}
table tr {
border-bottom: 3px solid #ddd;
display: block;
margin-bottom: .625em;
}
table td {
border-bottom: 1px solid #ddd;
display: block;
font-size: .8em;
text-align: right;
}
table td:before {
/*
* aria-label has no advantage, it won't be read inside a table
content: attr(aria-label);
*/
content: attr(data-label);
float: left;
font-weight: bold;
text-transform: uppercase;
}
table td:last-child {
border-bottom: 0;
}
}
</style>
</head>
<body style="margin: 0px;padding: 0px;height: 100%;margin-right: 5vw;margin-left: 5vw;padding-bottom: 10vw;">
<?php
require '../includes/db.php';
require '../includes/class.Meter.php';
error_reporting(-1);
ini_set('display_errors', 'On');
define('DATA_OK', 0); // green
define('DATA_NULL', 1); // orange
define('DATA_MISSING', 2); // red
define('RV_DATA', 3); // blue
define('STORAGE_DURATIONS', array('live' => 7200, 'quarterhour' => 1209600, 'hour' => 5184000, 'month' => 63113904));
define('INCREMENTS', array('live' => 60, 'quarterhour' => (60*15), 'hour' => (60*60), 'month' => (60*60*24*30)));
date_default_timezone_set('America/New_York');
if (!isset($_GET['meter_id']) || !isset($_GET['res'])) {
die('Must provide meter_id and res parameters');
}
$time = time();
$weekend = in_array(date('w'), array(0, 6));
$days = ($weekend) ? '1, 7' : '2, 3, 4, 5, 6';
$limit = ($weekend) ? 5 : 7;
$stmt = $db->prepare("SELECT id FROM meter_data WHERE meter_id = ? AND value IS NOT NULL AND resolution = 'hour' AND HOUR(FROM_UNIXTIME(recorded)) = HOUR(NOW()) AND DAYOFWEEK(FROM_UNIXTIME(recorded)) IN ({$days}) ORDER BY recorded DESC LIMIT {$limit}");
$stmt->execute(array($_GET['meter_id']));
$rv_data = array_column($stmt->fetchAll(), 'id');
$increment = INCREMENTS[$_GET['res']];
$start = $time - STORAGE_DURATIONS[$_GET['res']];
while ($start % $increment !== 0) {
++$start;
}
$expected_time = array();
$actual_time = array();
$value = array();
$status = array();
$stmt = $db->prepare('SELECT id, value, recorded FROM meter_data WHERE meter_id = ? AND resolution = ? AND recorded >= ? ORDER BY recorded ASC');
$stmt->execute(array($_GET['meter_id'], $_GET['res'], $start));
$data = $stmt->fetchAll();
$count = count($data);
$i = 0;
// while ($start > $data[$i++]['recorded']); // advance index to first point required by data
while ($start < $time) {
if (($start + $increment) < $data[$i]['recorded']) { // if the next data point does not fall in the span of the currently considered interval
$expected_time[] = $start;
$actual_time[] = 0;
$value[] = null;
$status[] = DATA_MISSING;
} else { // this data point falls within the current interval
$expected_time[] = $start;
$actual_time[] = $data[$i]['recorded'];
$value[] = $data[$i]['value'];
if (in_array($data[$i]['id'], $rv_data)) {
$status[] = RV_DATA;
} else {
if ($data[$i]['value'] === null) {
$status[] = DATA_NULL;
} else {
$status[] = DATA_OK;
}
}
$i++;
if ($i === $count) {
break;
}
}
$start += $increment;
}
if (!isset($_GET['view']) || $_GET['view'] === 'gradient') {
$linear_gradient = 'linear-gradient(to right, ';
foreach ($status as $s) {
switch ($s) {
case 0:
$linear_gradient .= ' green,';
break;
case 1:
$linear_gradient .= ' orange,';
break;
case 2:
$linear_gradient .= ' red,';
break;
case 3:
$linear_gradient .= ' blue,';
break;
}
}
$linear_gradient = substr($linear_gradient, 0, -1) . ');';
echo "<div style='height:100%;width:100%;background: {$linear_gradient}'></div>";
} elseif ($_GET['view'] === 'table') {
echo '<table class="table">
<thead>
<tr>
<th>Expected point</th>
<th>Existing point</th>
<th>Value</th>
<th>Typical</th>
</tr>
</thead>
<tbody>';
// $values = array_column($data, 'value', 'id');
// $times = array_column($data, 'recorded', 'id');
$typical = array();
for ($j = 0; $j < count($expected_time); $j++) {
echo "<tr><th scope='row'>";
echo date('n/j/y g:i.s a', $expected_time[$j]);
echo "</th><td>";
echo ($status[$j] === DATA_MISSING) ? 'None' : date('n/j/y g:i.s a', $actual_time[$j]);
echo ($status[$j] === DATA_MISSING || $status[$j] === DATA_NULL) ? "</td><td>NULL</td>" : "</td><td>{$value[$j]}</td>";
if ($status[$j] === RV_DATA) {
echo "<td>Yes</td>";
$typical[] = $value[$j];
} else {
echo "<td>No</td>";
}
echo "</tr>";
}
echo '</tbody></table>';
if ($_GET['res'] === 'hour') {
$stmt = $db->prepare('SELECT current FROM meters WHERE id = ?');
$stmt->execute(array($_GET['meter_id']));
$current = $stmt->fetchColumn();
$meter = new Meter($db);
$rv = $meter->relativeValue($typical, $current);
sort($typical);
echo "<h3>Relative value calculation</h3><p>Current: {$current}</p><p>Typical data: ".implode(', ', $typical)."</p><p>Relative value: {$rv}</p>";
}
}
// print_r($processed_data);
// $image = new Imagick();
// $image->newImage(400, 100, new ImagickPixel('white'));
// $image->setImageFormat('png');
// header('Content-type: image/png');
// echo $image;
?>
</body>
</html>