1 | <?php
|
---|
2 |
|
---|
3 | $ValueTypes = array('min', 'avg', 'max');
|
---|
4 | $PrefixMultipliers = array
|
---|
5 | (
|
---|
6 | array('y', 'yocto', pow(10, -24)),
|
---|
7 | array('z', 'zepto', pow(10, -21)),
|
---|
8 | array('a', 'atto', pow(10, -18)),
|
---|
9 | array('f', 'femto', pow(10, -15)),
|
---|
10 | array('p', 'piko', pow(10, -12)),
|
---|
11 | array('n', 'nano', pow(10, -9)),
|
---|
12 | array('u', 'mikro', pow(10, -6)),
|
---|
13 | array('m', 'mili', pow(10, -3)),
|
---|
14 | array('', '', pow(10, 0)),
|
---|
15 | array('k', 'kilo', pow(10, 3)),
|
---|
16 | array('M', 'mega', pow(10, 6)),
|
---|
17 | array('G', 'giga', pow(10, 9)),
|
---|
18 | array('T', 'tera', pow(10, 12)),
|
---|
19 | array('P', 'peta', pow(10, 15)),
|
---|
20 | array('E', 'exa', pow(10, 18)),
|
---|
21 | array('Z', 'zetta', pow(10, 21)),
|
---|
22 | array('Y', 'yotta', pow(10, 24)),
|
---|
23 | );
|
---|
24 |
|
---|
25 | function TruncateDigits($Value, $Digits = 4)
|
---|
26 | {
|
---|
27 | for($II = 2; $II > -6; $II--)
|
---|
28 | {
|
---|
29 | if($Value >= pow(10, $II))
|
---|
30 | {
|
---|
31 | if($Digits < ($II + 1)) $RealDigits = $II + 1; else $RealDigits = $Digits;
|
---|
32 | $Value = round($Value / pow(10, $II - $RealDigits + 1)) * pow(10, $II - $RealDigits + 1);
|
---|
33 | break;
|
---|
34 | }
|
---|
35 | }
|
---|
36 | return($Value);
|
---|
37 | }
|
---|
38 |
|
---|
39 | function AddPrefixMultipliers($Value, $Unit, $Digits = 4)
|
---|
40 | {
|
---|
41 | global $PrefixMultipliers;
|
---|
42 |
|
---|
43 | if($Unit == '') return(TruncateDigits($Value, $Digits));
|
---|
44 | $I = 8;
|
---|
45 | if($Value > 0) $II = 1;
|
---|
46 | else if($Value < 0) $II = -1;
|
---|
47 | else $II = 0;
|
---|
48 | while((($Value / $PrefixMultipliers[$I + $II][2]) > $II) and (($I + $II) >= 0) and (($I + $II) <= count($PrefixMultipliers))) $I = $I + $II;
|
---|
49 | $Value = $Value / $PrefixMultipliers[$I][2];
|
---|
50 |
|
---|
51 | // Truncate digits count
|
---|
52 | $Value = TruncateDigits($Value, $Digits);
|
---|
53 |
|
---|
54 | return($Value.' '.$PrefixMultipliers[$I][0].$Unit);
|
---|
55 | }
|
---|
56 |
|
---|
57 | function MysqlDateTimeToTime($Time)
|
---|
58 | {
|
---|
59 | $Parts = explode(' ', $Time);
|
---|
60 | $DateParts = explode('-', $Parts[0]);
|
---|
61 | $TimeParts = explode(':', $Parts[1]);
|
---|
62 | $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]);
|
---|
63 | return($Result);
|
---|
64 | }
|
---|
65 |
|
---|
66 | function TimeToMysqlDateTime($Time)
|
---|
67 | {
|
---|
68 | return(date('Y-m-d H:i:s', $Time));
|
---|
69 | }
|
---|
70 |
|
---|
71 | function TimeSegment($Base, $Level)
|
---|
72 | {
|
---|
73 | global $LevelReducing;
|
---|
74 |
|
---|
75 | return(pow($LevelReducing, $Level) * $Base);
|
---|
76 | }
|
---|
77 |
|
---|
78 | function StatTableName($Level)
|
---|
79 | {
|
---|
80 | if($Level == 0) return('data');
|
---|
81 | else return('data_cache');
|
---|
82 | }
|
---|
83 |
|
---|
84 | function AlignTime($Time, $TimeSegment)
|
---|
85 | {
|
---|
86 | global $ReferenceTime, $LevelReducing;
|
---|
87 | return(round(($Time - $ReferenceTime) / $TimeSegment) * $TimeSegment + $ReferenceTime);
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | function AddValue($Measure, $Value = array('min' => 0, 'avg' => 0, 'max' => 0), $Level = 0, $Time = 0)
|
---|
92 | {
|
---|
93 | global $LevelReducing, $MaxLevel, $Database;
|
---|
94 |
|
---|
95 | if($Time == 0) $Time = time();
|
---|
96 | //$Value = round($Measure['divider'] * $Value);
|
---|
97 | //echo(TimeToMysqlDateTime($Time).'|'.$Level."\n");
|
---|
98 |
|
---|
99 | $Result = $Database->select($Measure['DataTable'], '*', 'measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 2');
|
---|
100 | //echo($Database->LastQuery."\n");
|
---|
101 | if($Result->num_rows == 0)
|
---|
102 | {
|
---|
103 | $Database->insert($Measure['DataTable'], array('min' => $Value['min'], 'avg' => $Value['avg'], 'max' => $Value['max'], 'level' => $Level, 'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time), 'continuity' => 0));
|
---|
104 | //echo($Database->LastQuery."\n");
|
---|
105 | } else if($Result->num_rows == 1)
|
---|
106 | {
|
---|
107 | $Database->insert($Measure['DataTable'], array('min' => $Value['min'], 'avg' => $Value['avg'], 'max' => $Value['max'], 'level' => $Level, 'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time), 'continuity' => 1));
|
---|
108 | //echo($Database->LastQuery."\n");
|
---|
109 | } else
|
---|
110 | {
|
---|
111 | $LastValue = $Result->fetch_assoc();
|
---|
112 | $NextToLastValue = $Result->fetch_assoc();
|
---|
113 | //echo($Level.': '.$Time.' - '.MysqlDateTimeToTime($LastValue['time']).' '.($Time - MysqlDateTimeToTime($LastValue['time'])).' = '.$Measure['Period']."\n");
|
---|
114 | if((($Time - MysqlDateTimeToTime($LastValue['time'])) < 0.75 * $Measure['Period']) and ($Level == 0))
|
---|
115 | {
|
---|
116 | echo("Too short period\n");
|
---|
117 | }
|
---|
118 | else
|
---|
119 | {
|
---|
120 | if(($Time - MysqlDateTimeToTime($LastValue['time'])) < 1.25 * $Measure['Period']) $Continuity = 1;
|
---|
121 | else $Continuity = 0;
|
---|
122 | echo('('.$LastValue['avg'].'=='.$NextToLastValue['avg'].') and ('.$LastValue['avg'].' == '.$Value.') and ('.$LastValue['continuity'].' == 1) and ('.$Continuity.' == 1))'."\n");
|
---|
123 | if(($LastValue['min'] == $NextToLastValue['min']) and ($LastValue['min'] == $Value['min']) and ($LastValue['avg'] == $NextToLastValue['avg']) and ($LastValue['avg'] == $Value['avg']) and ($LastValue['max'] == $NextToLastValue['max']) and ($LastValue['max'] == $Value['max']) and ($LastValue['continuity'] == 1) and ($Continuity == 1))
|
---|
124 | {
|
---|
125 | $Database->update($Measure['DataTable'], 'time="'.$LastValue['time'].'" AND level='.$Level.' AND measure='.$Measure['Id'], array('time' => TimeToMysqlDateTime($Time)));
|
---|
126 | echo($Database->LastQuery."\n");
|
---|
127 | } else
|
---|
128 | {
|
---|
129 | $Database->insert($Measure['DataTable'], array('min' => $Value['min'], 'avg' => $Value['avg'], 'max' => $Value['max'], 'level' => $Level, 'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time), 'continuity' => $Continuity));
|
---|
130 | echo($Database->LastQuery."\n");
|
---|
131 | }
|
---|
132 | }
|
---|
133 |
|
---|
134 | // Update next level
|
---|
135 | if($Level < $MaxLevel)
|
---|
136 | {
|
---|
137 | $Level = $Level + 1;
|
---|
138 | //echo('Level '.$Level."<br>\n");
|
---|
139 | $TimeSegment = TimeSegment($Measure['Period'], 1);
|
---|
140 | $EndTime = AlignTime($Time, $TimeSegment);
|
---|
141 | //if($EndTime < $Time) $EndTime = $EndTime + $TimeSegment;
|
---|
142 | $StartTime = $EndTime - $TimeSegment;
|
---|
143 |
|
---|
144 | //echo(" ".$TimeSegment." ".$StartTime.'-'.$EndTime."<br>\n");
|
---|
145 | //flush();
|
---|
146 |
|
---|
147 | // Load values in time range
|
---|
148 | $Values = array();
|
---|
149 | //.'" AND time < "'.TimeToMysqlDateTime($EndTime).'" AND measure='.$Measure['Id'].' AND level='.($Level - 1).' ORDER BY time');
|
---|
150 | $Result = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($StartTime).'" AND time < "'.TimeToMysqlDateTime($EndTime).'" AND measure='.$Measure['Id'].' AND level='.($Level - 1).' ORDER BY time');
|
---|
151 | while($Row = $Result->fetch_assoc())
|
---|
152 | {
|
---|
153 | $Row['time'] = MysqlDateTimeToTime($Row['time']);
|
---|
154 | $Values[] = $Row;
|
---|
155 | }
|
---|
156 | //if(count($Values) > 2)
|
---|
157 | {
|
---|
158 | //print_r($Values);
|
---|
159 | //array_pop($Values);
|
---|
160 |
|
---|
161 | // Load subsidary values
|
---|
162 | print_r($Values);
|
---|
163 | $Values = array_merge(LoadLeftSideValue($Level - 1, $Measure, $StartTime), $Values, LoadRightSideValue($Level - 1, $Measure, $EndTime));
|
---|
164 | print_r($Values);
|
---|
165 |
|
---|
166 | $Point = ComputeOneValue($StartTime, $EndTime, $Values, $Measure, $Level);
|
---|
167 | //print_r($Point);
|
---|
168 |
|
---|
169 | $Database->delete($Measure['DataTable'], '(time > "'.TimeToMysqlDateTime($StartTime).'") AND (time < "'.TimeToMysqlDateTime($EndTime).'") AND measure='.$Measure['Id'].' AND level='.$Level);
|
---|
170 | $Measure['Period'] = $TimeSegment;
|
---|
171 | AddValue($Measure, array('min' => $Point['min'], 'avg' => $Point['avg'], 'max' => $Point['max']), $Level, $StartTime + ($EndTime - $StartTime) / 2);
|
---|
172 | }
|
---|
173 | }
|
---|
174 | }
|
---|
175 | }
|
---|
176 |
|
---|
177 | function Interpolation($X1, $Y1, $X2, $Y2, $X)
|
---|
178 | {
|
---|
179 | $Y = ($Y2 - $Y1) / ($X2 - $X1) * ($X - $X1) + $Y1;
|
---|
180 | //echo($Y1.'-'.$Y.'-'.$Y2.' '.$X1.'-'.$X.'-'.$X2.'<br>');
|
---|
181 | return($Y);
|
---|
182 | }
|
---|
183 |
|
---|
184 | function ComputeOneValue($LeftTime, $RightTime, $Values, $Measure, $Level)
|
---|
185 | {
|
---|
186 | global $ValueTypes, $Differential;
|
---|
187 |
|
---|
188 | $NewValue = array('min' => +1000000000000000000, 'avg' => 0, 'max' => -1000000000000000000);
|
---|
189 |
|
---|
190 | // Trim outside parts
|
---|
191 | foreach($ValueTypes as $ValueType)
|
---|
192 | {
|
---|
193 | $Values[0][$ValueType] = Interpolation($Values[0]['time'], $Values[0][$ValueType], $Values[1]['time'], $Values[1][$ValueType], $LeftTime);
|
---|
194 | }
|
---|
195 | $Values[0]['time'] = $LeftTime;
|
---|
196 | foreach($ValueTypes as $ValueType)
|
---|
197 | {
|
---|
198 | $Values[count($Values) - 1][$ValueType] = Interpolation($Values[count($Values) - 2]['time'], $Values[count($Values) - 2][$ValueType],
|
---|
199 | $Values[count($Values) - 1]['time'], $Values[count($Values) - 1][$ValueType], $RightTime);
|
---|
200 | }
|
---|
201 | $Values[count($Values) - 1]['time'] = $RightTime;
|
---|
202 |
|
---|
203 | // Perform computation
|
---|
204 | foreach($ValueTypes as $ValueType)
|
---|
205 | {
|
---|
206 | // Compute new value
|
---|
207 | for($I = 0; $I < (count($Values) - 1); $I++)
|
---|
208 | {
|
---|
209 | if($ValueType == 'avg')
|
---|
210 | {
|
---|
211 | if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled']);
|
---|
212 | else if($Differential == 0)
|
---|
213 | {
|
---|
214 | $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I+1]['time'] - $Values[$I]['time']) *
|
---|
215 | (($Values[$I+1][$ValueType] - $Values[$I][$ValueType]) / 2 + $Values[$I][$ValueType]);
|
---|
216 | } else
|
---|
217 | {
|
---|
218 | $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I+1]['time'] - $Values[$I]['time']) *
|
---|
219 | (($Values[$I+1][$ValueType] - $Values[$I][$ValueType]) / 2);
|
---|
220 | }
|
---|
221 | }
|
---|
222 | else if($ValueType == 'max')
|
---|
223 | {
|
---|
224 | if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled'])
|
---|
225 | {
|
---|
226 | if(0 > $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
|
---|
227 | }
|
---|
228 | else
|
---|
229 | {
|
---|
230 | if($Differential == 0)
|
---|
231 | {
|
---|
232 | if($Values[$I + 1][$ValueType] > $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
|
---|
233 | } else {
|
---|
234 | $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType];
|
---|
235 | if($Difference > $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
|
---|
236 | }
|
---|
237 | }
|
---|
238 | }
|
---|
239 | else if($ValueType == 'min')
|
---|
240 | {
|
---|
241 | //echo($Values[$I+1]['continuity'].'=='.$Measure['ContinuityEnabled'].'<br>');
|
---|
242 | if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled'])
|
---|
243 | {
|
---|
244 | if(0 < $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
|
---|
245 | }
|
---|
246 | else
|
---|
247 | {
|
---|
248 | if($Differential == 0)
|
---|
249 | {
|
---|
250 | if($Values[$I + 1][$ValueType] < $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
|
---|
251 | } else {
|
---|
252 | $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType];
|
---|
253 | if($Difference < $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
|
---|
254 | }
|
---|
255 | }
|
---|
256 | }
|
---|
257 | }
|
---|
258 | $NewValue[$ValueType] = $NewValue[$ValueType];
|
---|
259 | }
|
---|
260 | //if(($RightTime - $LeftTime) > 0)
|
---|
261 | if($Measure['Cumulative'] == 0)
|
---|
262 | {
|
---|
263 | $NewValue['avg'] = $NewValue['avg'] / ($RightTime - $LeftTime);
|
---|
264 | }
|
---|
265 | return($NewValue);
|
---|
266 | //echo($NewValue['avg'].'<br>');
|
---|
267 | //return(array('min' => rand(0,1), 'avg' => $NewValue['avg'], 'max' => rand(0,1)));
|
---|
268 | }
|
---|
269 |
|
---|
270 | function GetTimeRange($Measure, $Level)
|
---|
271 | {
|
---|
272 | global $Debug, $Database;
|
---|
273 |
|
---|
274 | // Get first and last time
|
---|
275 | //echo($Measure['Id'].','.$Level.','.StatTableName($Level)."\n");
|
---|
276 | $Result = $Database->select($Measure['DataTable'], '*', 'measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time LIMIT 1');
|
---|
277 | if($Result->num_rows > 0)
|
---|
278 | {
|
---|
279 | $Row = $Result->fetch_assoc();
|
---|
280 | $AbsoluteLeftTime = MysqlDateTimeToTime($Row['time']);
|
---|
281 | } else $AbsoluteLeftTime = 0;
|
---|
282 |
|
---|
283 | $Result = $Database->select($Measure['DataTable'], '*', 'measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1');
|
---|
284 | if($Result->num_rows > 0)
|
---|
285 | {
|
---|
286 | $Row = $Result->fetch_assoc();
|
---|
287 | $AbsoluteRightTime = MysqlDateTimeToTime($Row['time']);
|
---|
288 | } else $AbsoluteRightTime = 0;
|
---|
289 |
|
---|
290 | if($Debug)
|
---|
291 | {
|
---|
292 | echo('AbsoluteLeftTime: '.$AbsoluteLeftTime.'('.TimeToMysqlDateTime($AbsoluteLeftTime).')<br>');
|
---|
293 | echo('AbsoluteRightTime: '.$AbsoluteRightTime.'('.TimeToMysqlDateTime($AbsoluteRightTime).')<br>');
|
---|
294 | }
|
---|
295 | return(array('left' => $AbsoluteLeftTime, 'right' => $AbsoluteRightTime));
|
---|
296 | }
|
---|
297 |
|
---|
298 | function LoadRightSideValue($Level, $Measure, $Time)
|
---|
299 | {
|
---|
300 | global $Debug, $Database;
|
---|
301 | $Result = array();
|
---|
302 | $DbResult = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time ASC LIMIT 1');
|
---|
303 | if($DbResult->num_rows > 0)
|
---|
304 | {
|
---|
305 | $Row = $DbResult->fetch_assoc();
|
---|
306 | $Row['time'] = MysqlDateTimeToTime($Row['time']);
|
---|
307 | return(array($Row));
|
---|
308 | }
|
---|
309 | else
|
---|
310 | {
|
---|
311 | //$Time = $Values[count($Values)-1]['time'] + 60;
|
---|
312 | //array_push($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
|
---|
313 | $Result[] = array('time' => ($Time + TimeSegment($Measure['Period'], $Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0);
|
---|
314 | $DbResult = $Database->select($Measure['DataTable'], '*', 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1');
|
---|
315 | if($DbResult->num_rows > 0)
|
---|
316 | {
|
---|
317 | $Row = $DbResult->fetch_assoc();
|
---|
318 | array_unshift($Result, array('time' => (MysqlDateTimeToTime($Row['time']) + 10), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
|
---|
319 | }
|
---|
320 | // if($Debug) print_r($Result);
|
---|
321 | return($Result);
|
---|
322 | }
|
---|
323 | }
|
---|
324 |
|
---|
325 | function LoadLeftSideValue($Level, $Measure, $Time)
|
---|
326 | {
|
---|
327 | global $Debug, $Database;
|
---|
328 |
|
---|
329 | $Result = array();
|
---|
330 | //echo('SELECT * FROM '.StatTableName($Level). ' WHERE '. 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1'."<br>\n");
|
---|
331 | $DbResult = $Database->select($Measure['DataTable'], '*', 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1');
|
---|
332 | if($DbResult->num_rows > 0)
|
---|
333 | {
|
---|
334 | $Row = $DbResult->fetch_assoc();
|
---|
335 | $Row['time'] = MysqlDateTimeToTime($Row['time']);
|
---|
336 | return(array($Row));
|
---|
337 | }
|
---|
338 | else
|
---|
339 | {
|
---|
340 | //$Time = $Values[0]['time'] - 60;
|
---|
341 | //array_unshift($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
|
---|
342 | $Result[] = array('time' => ($Time - TimeSegment($Measure['Period'], $Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0);
|
---|
343 |
|
---|
344 | $DbResult = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time ASC LIMIT 1');
|
---|
345 | if($DbResult->num_rows > 0)
|
---|
346 | {
|
---|
347 | $Row = $DbResult->fetch_assoc();
|
---|
348 | array_push($Result, array('time' => (MysqlDateTimeToTime($Row['time'])-10), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
|
---|
349 | }
|
---|
350 | // if($Debug) print_r($Result);
|
---|
351 | return($Result);
|
---|
352 | }
|
---|
353 | }
|
---|
354 |
|
---|
355 | function GetValues($Measure, $TimeFrom, $TimeTo, $Level)
|
---|
356 | {
|
---|
357 | global $DivisionCount, $Debug, $Database;
|
---|
358 |
|
---|
359 | if($Debug) echo('TimeFrom: '.$TimeFrom.'('.TimeToMysqlDateTime($TimeFrom).')<br>');
|
---|
360 | if($Debug) echo('TimeTo: '.$TimeTo.'('.TimeToMysqlDateTime($TimeTo).')<br>');
|
---|
361 |
|
---|
362 | //$AbsoluteTime = GetTimeRange($MeasureId);
|
---|
363 |
|
---|
364 | // if(($TimeFrom > $AbsoluteLeftTime) and ($TimeStart < $AbsoluteRightTime) and
|
---|
365 | // ($TimeTo > $AbsoluteLeftTime) and ($TimeTo < $AbsoluteRightTime))
|
---|
366 | // {
|
---|
367 |
|
---|
368 | // Load values in time range
|
---|
369 | $Result = $Database->select($Measure['DataTable'], 'time, min, avg, max, continuity', 'time > "'.TimeToMysqlDateTime($TimeFrom).'" AND time < "'.TimeToMysqlDateTime($TimeTo).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time');
|
---|
370 | // echo($Level.' '.TimeToMysqlDateTime($TimeFrom).' '.TimeToMysqlDateTime($TimeTo));
|
---|
371 | $Values = array();
|
---|
372 | // echo(DB_NumRows());
|
---|
373 | // $III = 0;
|
---|
374 | while($Row = $Result->fetch_assoc())
|
---|
375 | {
|
---|
376 | // echo($III.' '.$Row['time'].' '.memory_get_usage().',');
|
---|
377 | // $III++;
|
---|
378 | $Values[] = array('time' => MysqlDateTimeToTime($Row['time']), 'min' => $Row['min'], 'avg' => $Row['avg'], 'max' => $Row['max'], 'continuity' => $Row['continuity']);
|
---|
379 | }
|
---|
380 | // array_pop($Values);
|
---|
381 | // echo('abc');
|
---|
382 | // die();
|
---|
383 | if($Debug) echo('Item count: '.count($Values));
|
---|
384 |
|
---|
385 | $Points = array();
|
---|
386 | if(count($Values) > 0)
|
---|
387 | {
|
---|
388 | $Values = array_merge(LoadLeftSideValue($Level, $Measure, $TimeFrom), $Values, LoadRightSideValue($Level, $Measure, $TimeTo));
|
---|
389 |
|
---|
390 | //echo(count($Values).'<br>');
|
---|
391 | //echo($TimeFrom.','.$TimeTo.'<br>');
|
---|
392 | //echo($Values[0]['time'].'<br>');
|
---|
393 | $StartIndex = 0;
|
---|
394 | $Points = array();
|
---|
395 | //echo($DivisionCount.'<br>');
|
---|
396 | for($I = 0; $I < $DivisionCount; $I++)
|
---|
397 | {
|
---|
398 | $TimeStart = $TimeFrom + (($TimeTo - $TimeFrom) / $DivisionCount) * $I;
|
---|
399 | //if($Debug) echo('TimeStart '.$I.': '.$TimeStart.'('.TimeToMysqlDateTime($TimeStart).')<br>');
|
---|
400 | $TimeEnd = $TimeFrom + (($TimeTo - $TimeFrom) / $DivisionCount) * ($I+1);
|
---|
401 | //if($Debug) echo('TimeEnd '.$I.': '.$TimeEnd.'('.TimeToMysqlDateTime($TimeEnd).')<br>');
|
---|
402 | //echo($TimeStart.','.$TimeEnd.'<br>');
|
---|
403 |
|
---|
404 | $EndIndex = $StartIndex;
|
---|
405 | while($Values[$EndIndex]['time'] < $TimeEnd) $EndIndex = $EndIndex + 1;
|
---|
406 | $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
|
---|
407 | //echo($StartIndex.','.$EndIndex.' '.count($SubValues).'<br>');
|
---|
408 | //print_r($SubValues);
|
---|
409 | $Points[] = ComputeOneValue($TimeStart, $TimeEnd, $SubValues, $Measure, $Level);
|
---|
410 | $StartIndex = $EndIndex - 1;
|
---|
411 | }
|
---|
412 | } else $Points[] = array('min' => 0, 'avg' => 0, 'max' => 0);
|
---|
413 | return($Points);
|
---|
414 | }
|
---|
415 |
|
---|
416 | function RebuildMeasureCache($Measure)
|
---|
417 | {
|
---|
418 | global $MaxLevel, $LevelReducing, $Database;
|
---|
419 |
|
---|
420 | echo('Velicina '.$Measure['Name']."<br>\n");
|
---|
421 | if($Measure['Continuity'] == 0) $Measure['ContinuityEnabled'] = 0; // non continuous
|
---|
422 | else $Measure['ContinuityEnabled'] = 2; // continuous graph
|
---|
423 |
|
---|
424 | // Clear previous items
|
---|
425 | $DbResult = $Database->select($Measure['DataTable'], 'COUNT(*)', 'level>0 AND measure='.$Measure['Id']);
|
---|
426 | $Row = $DbResult->fetch_row();
|
---|
427 | echo("Mazu starou cache (".$Row[0]." polozek)...");
|
---|
428 | $Database->delete($Measure['DataTable'], 'level>0 AND measure='.$Measure['Id']);
|
---|
429 | echo("<br>\n");
|
---|
430 |
|
---|
431 | for($Level=1; $Level <= $MaxLevel; $Level++)
|
---|
432 | {
|
---|
433 | echo('Uroven '.$Level."<br>\n");
|
---|
434 | $TimeRange = GetTimeRange($Measure, $Level - 1);
|
---|
435 | //echo($Measure['Id'].','.($Level-1)."\n");
|
---|
436 | //echo(TimeToMysqlDateTime($TimeRange['left']).'-'.TimeToMysqlDateTime($TimeRange['right'])."\n");
|
---|
437 | $TimeSegment = TimeSegment($Measure['Period'], $Level);
|
---|
438 | $StartTime = AlignTime($TimeRange['left'], $TimeSegment) - $TimeSegment;
|
---|
439 | $EndTime = AlignTime($TimeRange['right'], $TimeSegment);
|
---|
440 | $BurstCount = 500;
|
---|
441 | echo('For 0 to '.round(($EndTime - $StartTime) / $TimeSegment / $BurstCount)."<br>\n");
|
---|
442 | for($I = 0; $I <= round(($EndTime - $StartTime) / $TimeSegment / $BurstCount); $I++)
|
---|
443 | {
|
---|
444 | echo($I.' ');
|
---|
445 | $StartTime2 = $StartTime + $I * $BurstCount * $TimeSegment;
|
---|
446 | $EndTime2 = $StartTime + ($I + 1) * $BurstCount * $TimeSegment;
|
---|
447 | $Values = array();
|
---|
448 | $DbResult = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($StartTime2).'" AND time < "'.TimeToMysqlDateTime($EndTime2).'" AND measure='.$Measure['Id'].' AND level='.($Level - 1).' ORDER BY time');
|
---|
449 | while($Row = $DbResult->fetch_assoc())
|
---|
450 | {
|
---|
451 | $Row['time'] = MysqlDateTimeToTime($Row['time']);
|
---|
452 | $Values[] = $Row;
|
---|
453 | }
|
---|
454 |
|
---|
455 | if(count($Values) > 0)
|
---|
456 | {
|
---|
457 | $Values = array_merge(LoadLeftSideValue($Level - 1, $Measure, $StartTime2), $Values, LoadRightSideValue($Level - 1, $Measure, $EndTime2));
|
---|
458 |
|
---|
459 | $StartIndex = 0;
|
---|
460 | for($B = 0; $B < $BurstCount; $B++)
|
---|
461 | {
|
---|
462 | echo('.');
|
---|
463 | $StartTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * $B;
|
---|
464 | $EndTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * ($B + 1);
|
---|
465 |
|
---|
466 | $EndIndex = $StartIndex;
|
---|
467 | while($Values[$EndIndex]['time'] < $EndTime3) $EndIndex = $EndIndex + 1;
|
---|
468 | $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
|
---|
469 | //echo($StartIndex.','.$EndIndex.' '.count($SubValues).'<br>');
|
---|
470 | //print_r($SubValues);
|
---|
471 | if(count($SubValues) > 2)
|
---|
472 | {
|
---|
473 | $Point = ComputeOneValue($StartTime3, $EndTime3, $SubValues, $Measure, $Level);
|
---|
474 | $Continuity = $SubValues[1]['continuity'];
|
---|
475 | $Database->insert($Measure['DataTable'], array('level' => $Level, 'measure' => $Measure['Id'], 'min' => $Point['min'], 'avg' => $Point['avg'], 'max' => $Point['max'], 'continuity' => $Continuity, 'time' => TimeToMysqlDateTime($StartTime3 + ($EndTime3 - $StartTime3) / 2)));
|
---|
476 | }
|
---|
477 | $StartIndex = $EndIndex - 1;
|
---|
478 | }
|
---|
479 | }
|
---|
480 | // Load values in time range
|
---|
481 | //array_pop($NextValues);
|
---|
482 | }
|
---|
483 | echo("Uroven dokoncena<br>\n");
|
---|
484 | $DbResult = $Database->select($Measure['DataTable'], 'COUNT(*)', 'level='.$Level.' AND measure='.$Measure['Id']);
|
---|
485 | $Row = $DbResult->fetch_row();
|
---|
486 | echo("Vloženo ".$Row[0]." položek.<br>\n");
|
---|
487 | }
|
---|
488 | }
|
---|
489 |
|
---|
490 | function RebuildAllMeasuresCache()
|
---|
491 | {
|
---|
492 | global $Database;
|
---|
493 |
|
---|
494 | // echo("Vytvarim novou cache...\n");
|
---|
495 | // Load measures
|
---|
496 | $Measures = array();
|
---|
497 | $Result = $Database->select('measure', '*');
|
---|
498 | while($Measures[] = $Result->fetch_assoc());
|
---|
499 | array_pop($Measures);
|
---|
500 |
|
---|
501 | foreach($Measures as $Measure)
|
---|
502 | {
|
---|
503 | RebuildMeasureCache($Measure);
|
---|
504 | echo('Velicina dokoncena<br>');
|
---|
505 | }
|
---|
506 | }
|
---|
507 |
|
---|
508 | function InitMeasureDataTable($Measure)
|
---|
509 | {
|
---|
510 | global $Database;
|
---|
511 |
|
---|
512 | $Database->query('CREATE TABLE `data_'.$Measure['Name'].'` (
|
---|
513 | `Time` TIMESTAMP NOT NULL ,
|
---|
514 | `Avg` '.$Measure['DataType'].' NOT NULL ,
|
---|
515 | `Continuity` BOOL NOT NULL
|
---|
516 | ) ENGINE = MYISAM ;');
|
---|
517 |
|
---|
518 | $Database->query('CREATE TABLE `data_'.$Measure['Name'].'_cache` (
|
---|
519 | `Time` TIMESTAMP NOT NULL ,
|
---|
520 | `Level` TINYINT NOT NULL ,
|
---|
521 | `Min` '.$Measure['DataType'].' NOT NULL ,
|
---|
522 | `Avg` '.$Measure['DataType'].' NOT NULL ,
|
---|
523 | `Max` '.$Measure['DataType'].' NOT NULL ,
|
---|
524 | `Continuity` BOOL NOT NULL
|
---|
525 | ) ENGINE = MYISAM ;');
|
---|
526 | }
|
---|