-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemit-graphic-logs.php
192 lines (178 loc) · 5.87 KB
/
emit-graphic-logs.php
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
<?php
$barHeight = 20; // px
$barWidthPerHour = 51; // must be the same as in the CSS file, plus 1 for the border
?>
<style>
div.graphic-log td,
div.graphic-log div.bar {
min-height: <?= max( 20, $barHeight * count( $devices )) ?>px;
}
</style>
<div class="graphic-log">
<?php
$page = $_GET['page'];
printLogFileLines( "graphic-logs.php", $page );
?>
<table>
<thead>
<tr>
<th>Day</th>
<th colspan="24">Time</th>
</tr>
<tr>
<th></th>
<?php
for( $i=0 ; $i<24 ; ++$i ) {
print( " <th class=\"h\">$i</th>\n" );
}
?>
</tr>
</thead>
<tbody>
<?php
if( $page ) {
foreach( $oldLogFilesPrintf as $candidate ) {
$candidateFile = sprintf( $candidate, $page );
if( substr( $candidateFile, -3 ) === ".gz" ) {
$logFh = @gzopen( $candidateFile, "r" );
} else {
$logFh = @fopen( $candidateFile, "r" );
}
if( $logFh ) {
break;
}
}
} else {
$logFh = @fopen( $logFile, "r" );
}
if( $logFh ) {
function drawBar( $deviceIndex, $timeOn, $timeOff, $dotdotdot=0 ) {
global $barHeight;
global $barWidthPerHour;
$left = round( $timeOn/3600.0*$barWidthPerHour );
$width = max( 1, round( ( $timeOff - $timeOn )/3600.0*$barWidthPerHour ));
print( "<span style=\"left: ${left}px; width: ${width}px;\" class=\"d${deviceIndex}\"></span>\n" );
if( $dotdotdot ) {
$both = $left + $width;
print( "<span style=\"left: ${both}px;\" class=\"d${deviceIndex} dotdotdot\">...</span>" ); // \n" );
}
}
$reverseDevices = array_flip( $devices );
$days = array();
while( $line = fgets( $logFh )) {
if( ( $parsedLine = parseLogLine( $line ))) {
if( $parsedLine == NULL ) {
continue;
}
$currentDay = GregorianToJD( $parsedLine[2], $parsedLine[3], $parsedLine[1] );
$timeOfDay = $parsedLine[4]*60*60 + $parsedLine[5]*60 + $parsedLine[6];
$deviceIndex = 0;
foreach( $devices as $devicePin ) {
if( $devicePin[0] == $parsedLine[7] ) {
break;
}
++$deviceIndex;
}
if( $deviceIndex == count( $devices ) ) {
continue;
}
$days[$currentDay][$deviceIndex][$timeOfDay] = $parsedLine[8];
}
}
fclose( $logFh );
if( count( $days ) > 0 ) {
ksort( $days, SORT_NUMERIC );
$today = strtotime('today');
$now = time() - $today;
$today = unixtojd( $today );
$deviceStatus = array(); // keyed by deviceIndex, valued by start time
foreach( $days as $day => $deviceEvents ) {
preg_match( "!(\d+)/(\d+)/(\d+)!", jdtogregorian( $day ), $matched );
print( "<tr>\n" );
printf( " <td>%04d-%02d-%02d</td>\n", $matched[3], $matched[1], $matched[2] );
print( " <td colspan=\"24\"><div class=\"bar\">\n" );
foreach( $deviceEvents as $deviceIndex => $events ) {
foreach( $events as $timeOfDay => $event ) {
if( $event == 1 ) {
if( !isset( $deviceStatus[$deviceIndex] )) {
$deviceStatus[$deviceIndex] = $timeOfDay;
} // else switched on again -- ignore
} else if( $event == 0 ) {
if( isset( $deviceStatus[$deviceIndex] )) {
drawBar( $deviceIndex, $deviceStatus[$deviceIndex], $timeOfDay );
unset( $deviceStatus[$deviceIndex] );
} // else switched off again -- ignore
}
}
}
if( $day == $today ) {
foreach( $deviceStatus as $deviceIndex => $timeOfDay ) {
drawBar( $deviceIndex, $timeOfDay, $now, 1 );
}
} else {
foreach( $deviceStatus as $deviceIndex => $timeOfDay ) {
drawBar( $deviceIndex, $timeOfDay, 24*60*60 );
$deviceStatus[$deviceIndex] = 0; // midnight
}
}
print( " </div></td>\n" );
print( "</tr>\n" );
}
if( $day < $today && count( $deviceStatus ) > 0 ) {
for( ; $day < $today ; ++$day ) {
preg_match( "!(\d+)/(\d+)/(\d+)!", jdtogregorian( $day ), $matched );
print( "<tr>\n" );
printf( " <td>%04d-%02d-%02d</td>\n", $matched[3], $matched[1], $matched[2] );
print( " <td colspan=\"24\"><div class=\"bar\">\n" );
foreach( $deviceStatus as $deviceIndex => $timeOfDay ) {
drawBar( $deviceIndex, 0, 24*60*60 );
}
print( " </div></td>\n" );
print( "</tr>\n" );
}
print( "<tr>\n" );
print( strftime( " <td>%G-%m-%d</td>\n" ));
print( " <td colspan=\"24\"><div class=\"bar\">\n" );
foreach( $deviceStatus as $deviceIndex => $timeOfDay ) {
drawBar( $deviceIndex, 0, $now, 1 );
}
print( " </div></td>\n" );
print( "</tr>\n" );
}
}
}
if( !isset( $parsedLine ) || !$parsedLine ) {
?>
<tr>
<td colspan="25">No events logged so far.</td>
</tr>
<?php
}
?>
<tr>
<th></th>
<?php // added hour legend to base graph
for( $i=0 ; $i<24 ; ++$i ) {
print( " <th class=\"h\">$i</th>\n" );
}
?>
</tr>
</tbody>
</table>
<?php
if( isset( $parsedLine ) && $parsedLine ) {
?>
<div class="legend">Legend:
<?php
$deviceIndex = 0;
foreach( $devices as $deviceName => $devicePin ) {
print( "<span class=\"d${deviceIndex}\">$deviceName</span>\n" );
++$deviceIndex;
}
?>
</div><p>
<?php
}
require( 'emit-current-time.php' );
?>
</div>