source: www/statistic/new_/stat_functions.php@ 1

Last change on this file since 1 was 1, checked in by george, 17 years ago

Prvotní import všeho

File size: 15.8 KB
Line 
1<?
2
3$DivisionCount = 500;
4$ReferenceTime = 0;
5$LevelReducing = 5;
6$MaxLevel = 4;
7
8$ValueTypes = array('min', 'avg', 'max');
9
10function MysqlDateTimeToTime($Time)
11{
12 $Parts = explode(' ', $Time);
13 $DateParts = explode('-', $Parts[0]);
14 $TimeParts = explode(':', $Parts[1]);
15 $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]);
16 return($Result);
17}
18
19function TimeToMysqlDateTime($Time)
20{
21 return(date('Y-m-d H:i:s', $Time));
22}
23
24function TimeSegment($Level)
25{
26 global $LevelReducing;
27 return(pow($LevelReducing, $Level) * 60);
28}
29
30function StatTableName($Level)
31{
32 if($Level == 0) return('stat_data');
33 else return('stat_data_cache');
34}
35
36function AlignTime($Time, $TimeSegment)
37{
38 global $ReferenceTime, $LevelReducing;
39 return(round(($Time - $ReferenceTime) / $TimeSegment) * $TimeSegment + $ReferenceTime);
40}
41
42function GetMeasureById($Id)
43{
44 DB_Select('stat_measure', '*', 'id='.$Id);
45 if(DB_NumRows() > 0)
46 {
47 $Measure = DB_Row();
48 if($Measure['continuity'] == 0) $Measure['ContinuityEnabled'] = 0; // non continuous
49 else $Measure['ContinuityEnabled'] = 2; // continuous graph
50 } else die('Mìøená velièina nenalezena');
51 return($Measure);
52}
53
54function AddValue($Measure, $Value)
55{
56 global $LevelReducing, $MaxLevel;
57
58 $Time = time();
59 $Value = round($Measure['divider']*$Value);
60
61 DB_Select('stat_data', '*', 'measure='.$Measure['id'].' AND level=0 ORDER BY time DESC LIMIT 2');
62 if(DB_NumRows() == 0) DB_Insert('stat_data', array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['id'], 'time' => TimeToMysqlDateTime($Time),
63 'continuity' => 0));
64 else if(DB_NumRows() == 1) DB_Insert('stat_data', array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['id'], 'time' => TimeToMysqlDateTime($Time),
65 'continuity' => 1));
66 else {
67 $LastValue = DB_Row();
68 $NextToLastValue = DB_Row();
69 if(($Time - MysqlDateTimeToTime($LastValue['time'])) < 0.75 * $Measure['period'])
70 {
71 }
72 else
73 {
74 if(($Time - MysqlDateTimeToTime($LastValue['time'])) < 1.25 * $Measure['period']) $Continuity = 1;
75 else $Continuity = 0;
76 if(($LastValue['avg'] == $NextToLastValue['avg']) and ($LastValue['avg'] == $Value) and
77 ($LastValue['continuity'] == 1) and ($Continuity == 1))
78 {
79 echo('s');
80 DB_Update('stat_data', 'time="'.$LastValue['time'].'" AND level=0 AND measure='.$Measure['id'], array('time' => 'NOW()'));
81 } else
82 {
83 DB_Insert('stat_data', array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['id'], 'time' => TimeToMysqlDateTime($Time),
84 'continuity' => $Continuity));
85 }
86 }
87 }
88
89 // Update levels
90 //echo($Time."<br>\n");
91 for($Level = 1; $Level <= $MaxLevel; $Level++)
92 {
93 //echo('Level '.$Level."<br>\n");
94 $TimeSegment = TimeSegment($Level);
95 $EndTime = AlignTime($Time, $TimeSegment);
96 //if($EndTime < $Time) $EndTime = $EndTime + $TimeSegment;
97 $StartTime = $EndTime - $TimeSegment;
98
99 //echo(" ".$TimeSegment." ".$StartTime.'-'.$EndTime."<br>\n");
100 //flush();
101
102 // Load values in time range
103 $Values = array();
104 DB_Select(StatTableName($Level-1), '*', 'time > "'.TimeToMysqlDateTime($StartTime).'" AND time < "'.
105 TimeToMysqlDateTime($EndTime).'" AND measure='.$Measure['id'].' AND level='.($Level-1).' ORDER BY time');
106 while($Row = DB_Row())
107 {
108 $Row['time'] = MysqlDateTimeToTime($Row['time']);
109 $Values[] = $Row;
110 }
111 //print_r($Values);
112 //array_pop($Values);
113
114 // Load subsidary values
115 array_unshift($Values, LoadLeftSideValue($Level-1, $Measure, $StartTime));
116 array_push($Values, LoadRightSideValue($Level-1, $Measure, $EndTime));
117
118 $Point = ComputeOneValue($StartTime, $EndTime, $Values, $Measure, $Level);
119 //print_r($Point);
120
121 DB_Delete(StatTableName($Level), '(time > "'.TimeToMysqlDateTime($StartTime).'") AND
122 (time < "'.TimeToMysqlDateTime($EndTime).'") AND measure='.$Measure['id'].' AND level='.$Level);
123 $Continuity = $Values[1]['continuity'];
124 DB_Insert(StatTableName($Level), array('level' => $Level, 'measure' => $Measure['id'], 'min' => $Point['min'],
125 'avg' => $Point['avg'], 'max' => $Point['max'], 'continuity' => $Continuity, 'time' => TimeToMysqlDateTime($StartTime+($EndTime-$StartTime)/2)));
126
127 }
128}
129
130function Interpolation($X1, $Y1, $X2, $Y2, $X)
131{
132 $Y = ($Y2 - $Y1) / ($X2 - $X1) * ($X - $X1) + $Y1;
133 //echo($Y1.'-'.$Y.'-'.$Y2.' '.$X1.'-'.$X.'-'.$X2.'<br>');
134 return($Y);
135}
136
137function ComputeOneValue($LeftTime, $RightTime, $Values, $Measure, $Level)
138{
139 global $ValueTypes;
140
141 $NewValue = array('min' => +10000000000, 'avg' => 0, 'max' => -10000000000);
142 foreach($ValueTypes as $ValueType)
143 {
144 // Trim outside parts
145 $Values[0][$ValueType] = Interpolation($Values[0]['time'], $Values[0][$ValueType], $Values[1]['time'], $Values[1][$ValueType], $LeftTime);
146 }
147 $Values[0]['time'] = $LeftTime;
148 foreach($ValueTypes as $ValueType)
149 {
150 $Values[count($Values)-1][$ValueType] = Interpolation($Values[count($Values)-2]['time'], $Values[count($Values)-2][$ValueType],
151 $Values[count($Values)-1]['time'], $Values[count($Values)-1][$ValueType], $RightTime);
152 }
153 $Values[count($Values)-1]['time'] = $RightTime;
154
155 foreach($ValueTypes as $ValueType)
156 {
157 // Compute new value
158 for($I=0; $I < (count($Values) - 1); $I++)
159 {
160 if($ValueType == 'avg')
161 {
162 if($Values[$I+1]['continuity'] == $Measure['ContinuityEnabled']) ;
163 else $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I+1]['time'] - $Values[$I]['time']) *
164 (($Values[$I+1][$ValueType] - $Values[$I][$ValueType]) / 2 + $Values[$I][$ValueType]);
165 }
166 else if($ValueType == 'max')
167 {
168 if($Values[$I+1]['continuity'] == $Measure['ContinuityEnabled'])
169 {
170 if(0 > $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
171 }
172 else
173 {
174 //if($Values[$I][$ValueType] > $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I][$ValueType];
175 if($Values[$I+1][$ValueType] > $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I+1][$ValueType];
176 }
177 }
178 else if($ValueType == 'min')
179 {
180 //echo($Values[$I+1]['continuity'].'=='.$Measure['ContinuityEnabled'].'<br>');
181 if($Values[$I+1]['continuity'] == $Measure['ContinuityEnabled'])
182 {
183 if(0 < $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
184 }
185 else
186 {
187 //if($Values[$I][$ValueType] < $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I][$ValueType];
188 if($Values[$I+1][$ValueType] < $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I+1][$ValueType];
189 }
190 }
191
192 }
193 $NewValue[$ValueType] = $NewValue[$ValueType];
194 }
195 //if(($RightTime - $LeftTime) > 0)
196 $NewValue['avg'] = $NewValue['avg'] / ($RightTime - $LeftTime);
197 return($NewValue);
198 //echo($NewValue['avg'].'<br>');
199 //return(array('min' => rand(0,1), 'avg' => $NewValue['avg'], 'max' => rand(0,1)));
200}
201
202function GetTimeRange($Measure, $Level)
203{
204 global $Debug;
205
206 // Get first and last time
207 echo($Measure['id'].','.$Level.','.StatTableName($Level)."\n");
208 DB_Select(StatTableName($Level), '*', 'measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time LIMIT 1');
209 if(DB_NumRows() > 0)
210 {
211 $Row = DB_Row();
212 $AbsoluteLeftTime = MysqlDateTimeToTime($Row['time']);
213 } else $AbsoluteLeftTime = 0;
214
215 DB_Select(StatTableName($Level), '*', 'measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1');
216 if(DB_NumRows() > 0)
217 {
218 $Row = DB_Row();
219 $AbsoluteRightTime = MysqlDateTimeToTime($Row['time']);
220 } else $AbsoluteRightTime = 0;
221
222 if($Debug)
223 {
224 echo('AbsoluteLeftTime: '.$AbsoluteLeftTime.'('.TimeToMysqlDateTime($AbsoluteLeftTime).')<br>');
225 echo('AbsoluteRightTime: '.$AbsoluteRightTime.'('.TimeToMysqlDateTime($AbsoluteRightTime).')<br>');
226 }
227 return(array('left' => $AbsoluteLeftTime, 'right' => $AbsoluteRightTime));
228}
229
230function LoadRightSideValue($Level, $Measure, $Time)
231{
232 DB_Select(StatTableName($Level), '*', 'time > "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time LIMIT 1');
233 if(DB_NumRows() > 0)
234 {
235 $Row = DB_Row();
236 $Row['time'] = MysqlDateTimeToTime($Row['time']);
237 return($Row);
238 }
239 else
240 {
241 //$Time = $Values[count($Values)-1]['time'] + 60;
242 //array_push($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
243 return(array('time' => ($Time + TimeSegment($Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
244 }
245}
246
247function LoadLeftSideValue($Level, $Measure, $Time)
248{
249 //echo('SELECT * FROM '.StatTableName($Level). ' WHERE '. 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1'."<br>\n");
250 DB_Select(StatTableName($Level), '*', 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1');
251 if(DB_NumRows() > 0)
252 {
253 $Row = DB_Row();
254 $Row['time'] = MysqlDateTimeToTime($Row['time']);
255 return($Row);
256 }
257 else
258 {
259 //$Time = $Values[0]['time'] - 60;
260 //array_unshift($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
261 return(array('time' => ($Time - TimeSegment($Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
262 }
263}
264
265function GetValues($Measure, $TimeFrom, $TimeTo, $Level)
266{
267 global $DivisionCount, $Debug;
268
269 if($Debug) echo('TimeFrom: '.$TimeFrom.'('.TimeToMysqlDateTime($TimeFrom).')<br>');
270 if($Debug) echo('TimeTo: '.$TimeTo.'('.TimeToMysqlDateTime($TimeTo).')<br>');
271
272 //$AbsoluteTime = GetTimeRange($MeasureId);
273
274 $Values = array();
275
276// if(($TimeFrom > $AbsoluteLeftTime) and ($TimeStart < $AbsoluteRightTime) and
277// ($TimeTo > $AbsoluteLeftTime) and ($TimeTo < $AbsoluteRightTime))
278// {
279
280 // Load values in time range
281 DB_Select(StatTableName($Level), '*', 'time > "'.TimeToMysqlDateTime($TimeFrom).'" AND time < "'.
282 TimeToMysqlDateTime($TimeTo).'" AND measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time');
283 while($Row = DB_Row())
284 {
285 $Row['time'] = MysqlDateTimeToTime($Row['time']);
286 $Values[] = $Row;
287 }
288 array_pop($Values);
289 if($Debug) echo('Item count: '.count($Values));
290
291 $Points = array();
292
293 if(count($Values) > 0)
294 {
295 array_push($Values, LoadRightSideValue($Level, $Measure, $TimeTo));
296 array_unshift($Values, LoadLeftSideValue($Level, $Measure, $TimeFrom));
297
298 //echo(count($Values).'<br>');
299 //echo($TimeFrom.','.$TimeTo.'<br>');
300 //echo($Values[0]['time'].'<br>');
301 $StartIndex = 0;
302 $Points = array();
303 for($I = 0; $I < $DivisionCount; $I++)
304 {
305 $TimeStart = $TimeFrom + (($TimeTo - $TimeFrom) / $DivisionCount) * $I;
306 if($Debug) echo('TimeStart '.$I.': '.$TimeStart.'('.TimeToMysqlDateTime($TimeStart).')<br>');
307 $TimeEnd = $TimeFrom + (($TimeTo - $TimeFrom) / $DivisionCount) * ($I+1);
308 if($Debug) echo('TimeEnd '.$I.': '.$TimeEnd.'('.TimeToMysqlDateTime($TimeEnd).')<br>');
309 //echo($TimeStart.','.$TimeEnd.'<br>');
310
311 $EndIndex = $StartIndex;
312 while($Values[$EndIndex]['time'] < $TimeEnd) $EndIndex = $EndIndex + 1;
313 $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
314 //echo($StartIndex.','.$EndIndex.' '.count($SubValues).'<br>');
315 //print_r($SubValues);
316 $Points[] = ComputeOneValue($TimeStart, $TimeEnd, $SubValues, $Measure, $Level);
317 $StartIndex = $EndIndex - 1;
318 }
319 } else $Points[] = array('min' => 0, 'avg' => 0, 'max' => 0);
320 return($Points);
321}
322
323function RebuildMeasureCache($Measure)
324{
325 global $MaxLevel, $LevelReducing;
326
327 echo('Velicina '.$Measure['name']."\n");
328 if($Measure['continuity'] == 0) $Measure['ContinuityEnabled'] = 0; // non continuous
329 else $Measure['ContinuityEnabled'] = 2; // continuous graph
330
331 // Clear previous items
332 DB_Select('stat_data_cache', 'COUNT(*)', 'level>0 AND measure='.$Measure['id']);
333 $Row = DB_Row();
334 echo("Mazu starou cache (".$Row[0]." polozek)...");
335 DB_Delete('stat_data_cache', 'level>0 AND measure='.$Measure['id']);
336 echo("\n");
337
338 for($Level=1; $Level <= $MaxLevel; $Level++)
339 {
340 echo('Uroven '.$Level."\n");
341 $TimeRange = GetTimeRange($Measure, $Level-1);
342 //echo($Measure['id'].','.($Level-1)."\n");
343 //echo(TimeToMysqlDateTime($TimeRange['left']).'-'.TimeToMysqlDateTime($TimeRange['right'])."\n");
344 $TimeSegment = TimeSegment($Level);
345 $StartTime = AlignTime($TimeRange['left'], $TimeSegment) - $TimeSegment;
346 $EndTime = AlignTime($TimeRange['right'], $TimeSegment);
347 $BurstCount = 500;
348 echo('For 0 to '.round(($EndTime - $StartTime) / $TimeSegment / $BurstCount)."\n");
349 for($I = 0; $I <= round(($EndTime - $StartTime) / $TimeSegment / $BurstCount); $I++)
350 {
351 echo($I.' ');
352 $StartTime2 = $StartTime + $I * $BurstCount * $TimeSegment;
353 $EndTime2 = $StartTime + ($I+1) * $BurstCount * $TimeSegment;
354 $Values = array();
355 DB_Select(StatTableName($Level-1), '*', 'time > "'.TimeToMysqlDateTime($StartTime2).'" AND time < "'.
356 TimeToMysqlDateTime($EndTime2).'" AND measure='.$Measure['id'].' AND level='.($Level-1).' ORDER BY time');
357 while($Row = DB_Row())
358 {
359 $Row['time'] = MysqlDateTimeToTime($Row['time']);
360 $Values[] = $Row;
361 }
362
363 if(count($Values) > 0)
364 {
365 array_push($Values, LoadRightSideValue($Level-1, $Measure, $EndTime2));
366 array_unshift($Values, LoadLeftSideValue($Level-1, $Measure, $StartTime2));
367
368 $StartIndex = 0;
369 for($B = 0; $B < $BurstCount; $B++)
370 {
371 echo('.');
372 $StartTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * $B;
373 $EndTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * ($B+1);
374
375 $EndIndex = $StartIndex;
376 while($Values[$EndIndex]['time'] < $EndTime3) $EndIndex = $EndIndex + 1;
377 $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
378 //echo($StartIndex.','.$EndIndex.' '.count($SubValues).'<br>');
379 //print_r($SubValues);
380 if(count($SubValues) > 2)
381 {
382 $Point = ComputeOneValue($StartTime3, $EndTime3, $SubValues, $Measure, $Level);
383 $Continuity = $SubValues[1]['continuity'];
384 DB_Insert(StatTableName($Level), array('level' => $Level, 'measure' => $Measure['id'], 'min' => $Point['min'],
385 'avg' => $Point['avg'], 'max' => $Point['max'], 'continuity' => $Continuity, 'time' => TimeToMysqlDateTime($StartTime3+($EndTime3-$StartTime3)/2)));
386 }
387 $StartIndex = $EndIndex - 1;
388 }
389 }
390 // Load values in time range
391 //array_pop($NextValues);
392 }
393 echo("Uroven dokoncena\n");
394 DB_Select('stat_data', 'COUNT(*)', 'level='.$Level.' AND measure='.$Measure['id']);
395 $Row = DB_Row();
396 echo("Vlozeno ".$Row[0]." polozek.\n");
397 }
398}
399
400function RebuildAllMeasuresCache()
401{
402 global $ReferenceTime, $LevelReducing, $MaxLevel;
403
404
405// echo("Vytvarim novou cache...\n");
406 // Load measures
407 $Measures = array();
408 DB_Select('stat_measure', '*');
409 while($Measures[] = DB_Row());
410 array_pop($Measures);
411
412 foreach($Measures as $Measure)
413 {
414 RebuildMeasureCache($Measure);
415 echo('Velicina dokoncena<br>');
416 }
417}
418
419?>
Note: See TracBrowser for help on using the repository browser.