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