1 | <?php
|
---|
2 |
|
---|
3 | include_once(dirname(__FILE__).'/Page.php');
|
---|
4 |
|
---|
5 | class ModuleMeasure extends AppModule
|
---|
6 | {
|
---|
7 | function __construct($System)
|
---|
8 | {
|
---|
9 | parent::__construct($System);
|
---|
10 | $this->Name = 'Measure';
|
---|
11 | $this->Version = '1.0';
|
---|
12 | $this->Creator = 'Chronos';
|
---|
13 | $this->License = 'GNU/GPL';
|
---|
14 | $this->Description = 'Measurement processing';
|
---|
15 | $this->Dependencies = array();
|
---|
16 | }
|
---|
17 |
|
---|
18 | function DoStart()
|
---|
19 | {
|
---|
20 | $this->System->RegisterPage('', 'PageMain');
|
---|
21 | }
|
---|
22 |
|
---|
23 | function DoInstall()
|
---|
24 | {
|
---|
25 | $this->Database->query('CREATE TABLE IF NOT EXISTS `Measure` (
|
---|
26 | `Id` int(11) NOT NULL auto_increment,
|
---|
27 | `Name` varchar(255) collate utf8_general_ci NOT NULL,
|
---|
28 | `Description` varchar(255) collate utf8_general_ci NOT NULL,
|
---|
29 | `Divider` int(11) NOT NULL default 1,
|
---|
30 | `Unit` varchar(16) collate utf8_general_ci NOT NULL,
|
---|
31 | `Continuity` tinyint(1) NOT NULL default 0,
|
---|
32 | `Period` int(11) NOT NULL default 60,
|
---|
33 | `PermissionView` varchar(255) collate utf8_general_ci NOT NULL default "all",
|
---|
34 | `PermissionAdd` varchar(255) collate utf8_general_ci NOT NULL default "localhost.localdomain",
|
---|
35 | `Info` varchar(255) collate utf8_general_ci NOT NULL,
|
---|
36 | `Enabled` int(11) NOT NULL default 1,
|
---|
37 | `Cumulative` int(11) NOT NULL default 0,
|
---|
38 | `DataTable` varchar(32) collate utf8_general_ci NOT NULL default "data",
|
---|
39 | `DataType` varchar(32) collate utf8_general_ci NOT NULL,
|
---|
40 | PRIMARY KEY (`Id`)
|
---|
41 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8;');
|
---|
42 | }
|
---|
43 |
|
---|
44 | function DoUninstall()
|
---|
45 | {
|
---|
46 | $this->Database->query('DROP TABLE IF EXISTS `Measure`');
|
---|
47 | }
|
---|
48 |
|
---|
49 | function DoUpgrade()
|
---|
50 | {
|
---|
51 |
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | include_once('Point.php');
|
---|
56 |
|
---|
57 | class Measure
|
---|
58 | {
|
---|
59 | var $Data;
|
---|
60 | var $ValueTypes;
|
---|
61 | var $LevelReducing;
|
---|
62 | var $Database;
|
---|
63 | var $MaxLevel;
|
---|
64 | var $ReferenceTime;
|
---|
65 | var $Differential;
|
---|
66 | var $DivisionCount;
|
---|
67 | var $PeriodTolerance;
|
---|
68 |
|
---|
69 | function __construct(Database $Database)
|
---|
70 | {
|
---|
71 | $this->Id = 0;
|
---|
72 | $this->ValueTypes = array('Min', 'Avg', 'Max');
|
---|
73 | $this->Database = &$Database;
|
---|
74 | $this->LevelReducing = 5;
|
---|
75 | $this->MaxLevel = 4;
|
---|
76 | $this->ReferenceTime = 0;
|
---|
77 | $this->Differential = 0;
|
---|
78 | $this->DivisionCount = 500;
|
---|
79 | $this->PeriodTolerance = 0.25; // 25%
|
---|
80 | $this->Data = array('Name' => '', 'Description' => '', 'Unit' => '', 'Info' => '');
|
---|
81 | }
|
---|
82 |
|
---|
83 | function TimeSegment($Level)
|
---|
84 | {
|
---|
85 | return(pow($this->LevelReducing, $Level) * 60);
|
---|
86 | }
|
---|
87 |
|
---|
88 | function GetDataTable()
|
---|
89 | {
|
---|
90 | return('data_'.$this->Data['Name']);
|
---|
91 | }
|
---|
92 |
|
---|
93 | function AlignTime($Time, $TimeSegment)
|
---|
94 | {
|
---|
95 | return(round(($Time - $this->ReferenceTime) / $TimeSegment) * $TimeSegment +
|
---|
96 | $this->ReferenceTime);
|
---|
97 | }
|
---|
98 |
|
---|
99 | function Load($Id)
|
---|
100 | {
|
---|
101 | $Result = $this->Database->select('Measure', '*', '`Id`='.$Id);
|
---|
102 | if($Result->num_rows > 0)
|
---|
103 | {
|
---|
104 | $this->Data = $Result->fetch_array();
|
---|
105 | if($this->Data['Continuity'] == 0) $this->Data['ContinuityEnabled'] = 0; // non continuous
|
---|
106 | else $this->Data['ContinuityEnabled'] = 2; // continuous graph
|
---|
107 | } else die('Měřená veličina nenalezena');
|
---|
108 | }
|
---|
109 |
|
---|
110 | function AddValue($Value, $Time)
|
---|
111 | {
|
---|
112 | $Result = $this->Database->select($this->Data['DataTable'], '*', '(`Measure`='.$this->Data['Id'].') AND '.
|
---|
113 | '(`Level`=0) ORDER BY `Time` DESC LIMIT 2');
|
---|
114 | if($Result->num_rows == 0) {
|
---|
115 | // No measure value found. Simply insert new first value.
|
---|
116 | $this->Database->insert($this->Data['DataTable'],
|
---|
117 | array('Min' => $Value, 'Avg' => $Value, 'Max' => $Value, 'Level' => 0,
|
---|
118 | 'Measure' => $this->Data['Id'], 'Time' => TimeToMysqlDateTime($Time),
|
---|
119 | 'Continuity' => 0));
|
---|
120 | } else if($Result->num_rows == 1) {
|
---|
121 | // One value exists. Add second value.
|
---|
122 | $this->Database->insert($this->Data['DataTable'],
|
---|
123 | array('Min' => $Value, 'Avg' => $Value, 'Max' => $Value, 'Level' => 0,
|
---|
124 | 'Measure' => $this->Data['Id'], 'Time' => TimeToMysqlDateTime($Time),
|
---|
125 | 'Continuity' => 1));
|
---|
126 | } else {
|
---|
127 | // Two values already exist in measure table
|
---|
128 | $LastValue = $Result->fetch_array();
|
---|
129 | $NextToLastValue = $Result->fetch_array();
|
---|
130 | if(($Time - MysqlDateTimeToTime($LastValue['Time'])) < (1 - $this->PeriodTolerance) * $this->Data['Period'])
|
---|
131 | {
|
---|
132 | // New value added too quickly. Need to wait for next measure period.
|
---|
133 | }
|
---|
134 | else
|
---|
135 | {
|
---|
136 | // We are near defined period and can add new value.
|
---|
137 | if(($Time - MysqlDateTimeToTime($LastValue['Time'])) < (1 + $this->PeriodTolerance) * $this->Data['Period']) {
|
---|
138 | // New value added near defined measure period inside tolerance. Keep continuity.
|
---|
139 | $Continuity = 1;
|
---|
140 | } else {
|
---|
141 | // New value added too late after defined measure period. Stop
|
---|
142 | // continuity and let gap to be added.
|
---|
143 | $Continuity = 0;
|
---|
144 | }
|
---|
145 | if(($LastValue['Avg'] == $NextToLastValue['Avg']) and ($LastValue['Avg'] == $Value) and
|
---|
146 | ($LastValue['Continuity'] == 1) and ($Continuity == 1))
|
---|
147 | {
|
---|
148 | // New value is same as last value and continuity mode is enabled and
|
---|
149 | // continuity flag is present. Just shift forward time for last value.
|
---|
150 | $this->Database->update($this->Data['DataTable'], '(`Time`="'.$LastValue['Time'].'") AND '.
|
---|
151 | '(`Level`=0) AND (`Measure`='.$this->Data['Id'].')', array('Time' => TimeToMysqlDateTime($Time)));
|
---|
152 | } else
|
---|
153 | {
|
---|
154 | // Last value is different or not with continuity flag. Need to add new value.
|
---|
155 | $this->Database->insert($this->Data['DataTable'], array('Min' => $Value,
|
---|
156 | 'Avg' => $Value, 'max' => $Value, 'Level' => 0, 'Measure' => $this->Data['Id'],
|
---|
157 | 'Time' => TimeToMysqlDateTime($Time),
|
---|
158 | 'Continuity' => $Continuity));
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 | $this->UpdateHigherLevels($Time);
|
---|
163 | }
|
---|
164 |
|
---|
165 | function UpdateHigherLevels($Time)
|
---|
166 | {
|
---|
167 | for($Level = 1; $Level <= $this->MaxLevel; $Level++)
|
---|
168 | {
|
---|
169 | $TimeSegment = $this->TimeSegment($Level);
|
---|
170 | $EndTime = $this->AlignTime($Time, $TimeSegment);
|
---|
171 | //if($EndTime < $Time) $EndTime = $EndTime + $TimeSegment;
|
---|
172 | $StartTime = $EndTime - $TimeSegment;
|
---|
173 |
|
---|
174 | // Load values in time range
|
---|
175 | $Values = array();
|
---|
176 | $Result = $this->Database->select($this->Data['DataTable'], '*', '(`Time` > "'.
|
---|
177 | TimeToMysqlDateTime($StartTime).'") AND '.
|
---|
178 | '(`Time` < "'.TimeToMysqlDateTime($EndTime).'") AND '.
|
---|
179 | '(`Measure`='.$this->Data['Id'].') AND (`Level`='.($Level - 1).') ORDER BY `Time`');
|
---|
180 | while($Row = $Result->fetch_array())
|
---|
181 | {
|
---|
182 | $Row['Time'] = MysqlDateTimeToTime($Row['Time']);
|
---|
183 | $Values[] = $Row;
|
---|
184 | }
|
---|
185 | //array_pop($Values);
|
---|
186 |
|
---|
187 | // Load subsidary values
|
---|
188 | $Values = array_merge(
|
---|
189 | $this->LoadLeftSideValue($Level - 1, $StartTime),
|
---|
190 | $Values,
|
---|
191 | $this->LoadRightSideValue($Level - 1, $EndTime)
|
---|
192 | );
|
---|
193 |
|
---|
194 | $Point = $this->ComputeOneValue($StartTime, $EndTime, $Values, $Level);
|
---|
195 |
|
---|
196 | $this->Database->delete($this->Data['DataTable'], '(`Time` > "'.TimeToMysqlDateTime($StartTime).'") AND
|
---|
197 | (`Time` < "'.TimeToMysqlDateTime($EndTime).'") AND (`Measure`='.$this->Data['Id'].') '.
|
---|
198 | 'AND (`Level`='.$Level.')');
|
---|
199 | $Continuity = $Values[1]['Continuity'];
|
---|
200 | $this->Database->insert($this->Data['DataTable'], array('Level' => $Level,
|
---|
201 | 'Measure' => $this->Data['Id'], 'Min' => $Point['Min'],
|
---|
202 | 'Avg' => $Point['Avg'], 'Max' => $Point['Max'], 'Continuity' => $Continuity,
|
---|
203 | 'Time' => TimeToMysqlDateTime($StartTime + ($EndTime - $StartTime) / 2)));
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | /* Compute one value for upper time level from multiple values */
|
---|
208 | function ComputeOneValue($LeftTime, $RightTime, $Values, $Level)
|
---|
209 | {
|
---|
210 | $NewValue = array('Min' => +1000000000000000000, 'Avg' => 0, 'Max' => -1000000000000000000);
|
---|
211 |
|
---|
212 | // Trim outside parts
|
---|
213 | foreach($this->ValueTypes as $ValueType)
|
---|
214 | {
|
---|
215 | $Values[0][$ValueType] = Interpolation(
|
---|
216 | NewPoint($Values[0]['Time'], $Values[0][$ValueType]),
|
---|
217 | NewPoint($Values[1]['Time'], $Values[1][$ValueType]), $LeftTime);
|
---|
218 | }
|
---|
219 | $Values[0]['Time'] = $LeftTime;
|
---|
220 | foreach($this->ValueTypes as $ValueType)
|
---|
221 | {
|
---|
222 | $Values[count($Values) - 1][$ValueType] = Interpolation(
|
---|
223 | NewPoint($Values[count($Values) - 2]['Time'], $Values[count($Values) - 2][$ValueType]),
|
---|
224 | NewPoint($Values[count($Values) - 1]['Time'], $Values[count($Values) - 1][$ValueType]),
|
---|
225 | $RightTime);
|
---|
226 | }
|
---|
227 | $Values[count($Values) - 1]['Time'] = $RightTime;
|
---|
228 |
|
---|
229 | // Perform computation
|
---|
230 | foreach($this->ValueTypes as $ValueType)
|
---|
231 | {
|
---|
232 | // Compute new value
|
---|
233 | for($I = 0; $I < (count($Values) - 1); $I++)
|
---|
234 | {
|
---|
235 | if($ValueType == 'Avg')
|
---|
236 | {
|
---|
237 | if($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled']);
|
---|
238 | else if($this->Differential == 0)
|
---|
239 | {
|
---|
240 | $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['Time'] - $Values[$I]['Time']) *
|
---|
241 | (($Values[$I + 1][$ValueType] - $Values[$I][$ValueType]) / 2 + $Values[$I][$ValueType]);
|
---|
242 | } else {
|
---|
243 | $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['Time'] - $Values[$I]['Time']) *
|
---|
244 | (($Values[$I + 1][$ValueType] - $Values[$I][$ValueType]) / 2);
|
---|
245 | }
|
---|
246 | }
|
---|
247 | else if($ValueType == 'Max')
|
---|
248 | {
|
---|
249 | if($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled'])
|
---|
250 | {
|
---|
251 | if(0 > $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
|
---|
252 | }
|
---|
253 | else
|
---|
254 | {
|
---|
255 | if($this->Differential == 0)
|
---|
256 | {
|
---|
257 | if($Values[$I + 1][$ValueType] > $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
|
---|
258 | } else {
|
---|
259 | $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType];
|
---|
260 | if($Difference > $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
|
---|
261 | }
|
---|
262 | }
|
---|
263 | }
|
---|
264 | else if($ValueType == 'Min')
|
---|
265 | {
|
---|
266 | if($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled'])
|
---|
267 | {
|
---|
268 | if(0 < $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
|
---|
269 | } else {
|
---|
270 | if($this->Differential == 0)
|
---|
271 | {
|
---|
272 | if($Values[$I + 1][$ValueType] < $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
|
---|
273 | } else {
|
---|
274 | $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType];
|
---|
275 | if($Difference < $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
|
---|
276 | }
|
---|
277 | }
|
---|
278 | }
|
---|
279 | }
|
---|
280 | $NewValue[$ValueType] = $NewValue[$ValueType];
|
---|
281 | }
|
---|
282 | //if(($RightTime - $LeftTime) > 0)
|
---|
283 | if($this->Data['Cumulative'] == 0)
|
---|
284 | {
|
---|
285 | $NewValue['Avg'] = $NewValue['Avg'] / ($RightTime - $LeftTime);
|
---|
286 | }
|
---|
287 | return($NewValue);
|
---|
288 | }
|
---|
289 |
|
---|
290 | function GetTimeRange($Level)
|
---|
291 | {
|
---|
292 | // Get first and last time
|
---|
293 | $Result = $this->Database->select($this->Data['DataTable'], '*', '(`Measure`='.$this->Data['Id'].') AND '.
|
---|
294 | '(`Level`='.$Level.') ORDER BY `Time` LIMIT 1');
|
---|
295 | if($Result->num_rows > 0)
|
---|
296 | {
|
---|
297 | $Row = $Result->fetch_array();
|
---|
298 | $AbsoluteLeftTime = MysqlDateTimeToTime($Row['time']);
|
---|
299 | } else $AbsoluteLeftTime = 0;
|
---|
300 |
|
---|
301 | $Result = $this->Database->select($this->Data['DataTable'], '*', '(`Measure`='.$this->Data['Id'].') AND '.
|
---|
302 | '(`Level`='.$Level.') ORDER BY `Time` DESC LIMIT 1');
|
---|
303 | if($Result->num_rows > 0)
|
---|
304 | {
|
---|
305 | $Row = $Result->fetch_array();
|
---|
306 | $AbsoluteRightTime = MysqlDateTimeToTime($Row['Time']);
|
---|
307 | } else $AbsoluteRightTime = 0;
|
---|
308 |
|
---|
309 | return(array('Left' => $AbsoluteLeftTime, 'Right' => $AbsoluteRightTime));
|
---|
310 | }
|
---|
311 |
|
---|
312 | // Load one nearest value newer then given time
|
---|
313 | function LoadRightSideValue($Level, $Time)
|
---|
314 | {
|
---|
315 | $Result = array();
|
---|
316 | $DbResult = $this->Database->select($this->Data['DataTable'], '*',
|
---|
317 | '(`Time` > "'.TimeToMysqlDateTime($Time).'") AND (`Measure`='.
|
---|
318 | $this->Data['Id'].') AND (`Level`='.$Level.') ORDER BY `Time` ASC LIMIT 1');
|
---|
319 | if($DbResult->num_rows > 0)
|
---|
320 | {
|
---|
321 | // Found one value, simply return it
|
---|
322 | $Row = $DbResult->fetch_array();
|
---|
323 | $Row['Time'] = MysqlDateTimeToTime($Row['Time']);
|
---|
324 | return(array($Row));
|
---|
325 | } else
|
---|
326 | {
|
---|
327 | // Not found any value. Calculate it
|
---|
328 | //$Time = $Values[count($Values) - 1]['time'] + 60;
|
---|
329 | //array_push($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
|
---|
330 | $Result[] = array('Time' => ($Time + $this->TimeSegment($Level)), 'Min' => 0,
|
---|
331 | 'Avg' => 0, 'Max' => 0, 'Continuity' => 0);
|
---|
332 | $DbResult = $this->Database->select($this->Data['DataTable'], '*',
|
---|
333 | '(`Time` < "'.TimeToMysqlDateTime($Time).'") AND (`Measure`='.$this->Data['Id'].') '.
|
---|
334 | 'AND (`Level`='.$Level.') ORDER BY `Time` DESC LIMIT 1');
|
---|
335 | if($DbResult->num_rows > 0)
|
---|
336 | {
|
---|
337 | $Row = $DbResult->fetch_array();
|
---|
338 | array_unshift($Result, array('Time' => (MysqlDateTimeToTime($Row['Time']) + 10),
|
---|
339 | 'Min' => 0, 'Avg' => 0, 'Max' => 0, 'Continuity' => 0));
|
---|
340 | }
|
---|
341 | return($Result);
|
---|
342 | }
|
---|
343 | }
|
---|
344 |
|
---|
345 | // Load one nearest value older then given time
|
---|
346 | function LoadLeftSideValue($Level, $Time)
|
---|
347 | {
|
---|
348 | $Result = array();
|
---|
349 | $DbResult = $this->Database->select($this->Data['DataTable'], '*',
|
---|
350 | '(`Time` < "'.TimeToMysqlDateTime($Time).'") AND (`Measure`='.$this->Data['Id'].') '.
|
---|
351 | 'AND (`Level`='.$Level.') ORDER BY `Time` DESC LIMIT 1');
|
---|
352 | if($DbResult->num_rows > 0)
|
---|
353 | {
|
---|
354 | // Found one value, simply return it
|
---|
355 | $Row = $DbResult->fetch_array();
|
---|
356 | $Row['Time'] = MysqlDateTimeToTime($Row['Time']);
|
---|
357 | return(array($Row));
|
---|
358 | } else {
|
---|
359 | // Not found any value. Calculate it
|
---|
360 | //$Time = $Values[0]['time'] - 60;
|
---|
361 | //array_unshift($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
|
---|
362 | $Result[] = array('Time' => ($Time - $this->TimeSegment($Level)), 'Min' => 0,
|
---|
363 | 'Avg' => 0, 'Max' => 0, 'Continuity' => 0);
|
---|
364 |
|
---|
365 | $DbResult = $this->Database->select($this->Data['DataTable'], '*',
|
---|
366 | '(`Time` > "'.TimeToMysqlDateTime($Time).'") AND (`Measure`='.$this->Data['Id'].') '.
|
---|
367 | 'AND (`Level`='.$Level.') ORDER BY `Time` ASC LIMIT 1');
|
---|
368 | if($DbResult->num_rows > 0)
|
---|
369 | {
|
---|
370 | $Row = $DbResult->fetch_array();
|
---|
371 | array_push($Result, array('Time' => (MysqlDateTimeToTime($Row['Time']) - 10),
|
---|
372 | 'Min' => 0, 'Avg' => 0, 'Max' => 0, 'Continuity' => 0));
|
---|
373 | }
|
---|
374 | return($Result);
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 | function GetValues($TimeFrom, $TimeTo, $Level)
|
---|
379 | {
|
---|
380 | //$AbsoluteTime = GetTimeRange($this->DataId);
|
---|
381 |
|
---|
382 | // if(($TimeFrom > $AbsoluteLeftTime) and ($TimeStart < $AbsoluteRightTime) and
|
---|
383 | // ($TimeTo > $AbsoluteLeftTime) and ($TimeTo < $AbsoluteRightTime))
|
---|
384 | // {
|
---|
385 |
|
---|
386 | // Load values in time range
|
---|
387 | $Result = $this->Database->select($this->Data['DataTable'], '`Time`, `Min`, '.
|
---|
388 | '`Avg`, `Max`, `Continuity`',
|
---|
389 | '(`Time` > "'.TimeToMysqlDateTime($TimeFrom).'") AND '.
|
---|
390 | '(`Time` < "'.TimeToMysqlDateTime($TimeTo).'") AND '.
|
---|
391 | '(`Measure`='.$this->Data['Id'].') AND (`Level`='.$Level.') ORDER BY `Time`');
|
---|
392 | $Values = array();
|
---|
393 | while($Row = $Result->fetch_array())
|
---|
394 | {
|
---|
395 | $Values[] = array('Time' => MysqlDateTimeToTime($Row['Time']),
|
---|
396 | 'Min' => $Row['Min'], 'Avg' => $Row['Avg'], 'Max' => $Row['Max'],
|
---|
397 | 'Continuity' => $Row['Continuity']);
|
---|
398 | }
|
---|
399 | // array_pop($Values);
|
---|
400 |
|
---|
401 | $Points = array();
|
---|
402 | if(count($Values) > 0)
|
---|
403 | {
|
---|
404 | $Values = array_merge(
|
---|
405 | $this->LoadLeftSideValue($Level, $TimeFrom),
|
---|
406 | $Values,
|
---|
407 | $this->LoadRightSideValue($Level, $TimeTo));
|
---|
408 |
|
---|
409 | $StartIndex = 0;
|
---|
410 | $Points = array();
|
---|
411 | for($I = 0; $I < $this->DivisionCount; $I++)
|
---|
412 | {
|
---|
413 | $TimeStart = $TimeFrom + (($TimeTo - $TimeFrom) / $this->DivisionCount) * $I;
|
---|
414 | $TimeEnd = $TimeFrom + (($TimeTo - $TimeFrom) / $this->DivisionCount) * ($I + 1);
|
---|
415 |
|
---|
416 | $EndIndex = $StartIndex;
|
---|
417 | while($Values[$EndIndex]['Time'] < $TimeEnd) $EndIndex = $EndIndex + 1;
|
---|
418 | $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
|
---|
419 | $Points[] = $this->ComputeOneValue($TimeStart, $TimeEnd, $SubValues, $Level);
|
---|
420 | $StartIndex = $EndIndex - 1;
|
---|
421 | }
|
---|
422 | } else $Points[] = array('Min' => 0, 'Avg' => 0, 'Max' => 0);
|
---|
423 | return($Points);
|
---|
424 | }
|
---|
425 |
|
---|
426 | function RebuildMeasureCache()
|
---|
427 | {
|
---|
428 | echo('Velicina '.$this->Data['Name']."<br>\n");
|
---|
429 | if($this->Data['Continuity'] == 0) $this->Data['ContinuityEnabled'] = 0; // non continuous
|
---|
430 | else $this->Data['ContinuityEnabled'] = 2; // continuous graph
|
---|
431 |
|
---|
432 | // Clear previous items
|
---|
433 | $DbResult = $this->Database->select($this->Data['DataTable'], 'COUNT(*)', '(`Level`>0) AND (`Measure`='.$this->Data['Id'].')');
|
---|
434 | $Row = $DbResult->fetch_array();
|
---|
435 | echo("Mazu starou cache (".$Row[0]." polozek)...");
|
---|
436 | $this->Database->delete($this->Data['DataTable'], '(`Level`>0) AND (`Measure`='.$this->Data['Id'].')');
|
---|
437 | echo("<br>\n");
|
---|
438 |
|
---|
439 | for($Level = 1; $Level <= $this->MaxLevel; $Level++)
|
---|
440 | {
|
---|
441 | echo('Uroven '.$Level."<br>\n");
|
---|
442 | $TimeRange = $this->GetTimeRange($Level - 1);
|
---|
443 | $TimeSegment = $this->TimeSegment($Level);
|
---|
444 | $StartTime = $this->AlignTime($TimeRange['Left'], $TimeSegment) - $TimeSegment;
|
---|
445 | $EndTime = $this->AlignTime($TimeRange['Right'], $TimeSegment);
|
---|
446 | $BurstCount = 500;
|
---|
447 | echo('For 0 to '.round(($EndTime - $StartTime) / $TimeSegment / $BurstCount)."<br>\n");
|
---|
448 | for($I = 0; $I <= round(($EndTime - $StartTime) / $TimeSegment / $BurstCount); $I++)
|
---|
449 | {
|
---|
450 | echo($I.' ');
|
---|
451 | $StartTime2 = $StartTime + $I * $BurstCount * $TimeSegment;
|
---|
452 | $EndTime2 = $StartTime + ($I + 1) * $BurstCount * $TimeSegment;
|
---|
453 | $Values = array();
|
---|
454 | $DbResult = $this->Database->select($this->Data['DataTable'], '*', '(`Time` > "'.
|
---|
455 | TimeToMysqlDateTime($StartTime2).'") AND (`Time` < "'.
|
---|
456 | TimeToMysqlDateTime($EndTime2).'") AND (`Measure`='.$this->Data['Id'].
|
---|
457 | ') AND (`Level`='.($Level - 1).') ORDER BY `Time`');
|
---|
458 | while($Row = $DbResult->fetch_array())
|
---|
459 | {
|
---|
460 | $Row['Time'] = MysqlDateTimeToTime($Row['Time']);
|
---|
461 | $Values[] = $Row;
|
---|
462 | }
|
---|
463 |
|
---|
464 | if(count($Values) > 0)
|
---|
465 | {
|
---|
466 | $Values = array_merge(
|
---|
467 | $this->LoadLeftSideValue($Level - 1, $StartTime2),
|
---|
468 | $Values,
|
---|
469 | $this->LoadRightSideValue($Level - 1, $EndTime2));
|
---|
470 |
|
---|
471 | $StartIndex = 0;
|
---|
472 | for($B = 0; $B < $BurstCount; $B++)
|
---|
473 | {
|
---|
474 | echo('.');
|
---|
475 | $StartTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * $B;
|
---|
476 | $EndTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * ($B + 1);
|
---|
477 |
|
---|
478 | $EndIndex = $StartIndex;
|
---|
479 | while($Values[$EndIndex]['Time'] < $EndTime3) $EndIndex = $EndIndex + 1;
|
---|
480 | $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
|
---|
481 | if(count($SubValues) > 2)
|
---|
482 | {
|
---|
483 | $Point = $this->ComputeOneValue($StartTime3, $EndTime3, $SubValues, $Level);
|
---|
484 | $Continuity = $SubValues[1]['Continuity'];
|
---|
485 | $this->Database->insert($this->Data['DataTable'], array('Level' => $Level, 'Measure' => $this->Data['Id'],
|
---|
486 | 'Min' => $Point['Min'], 'Avg' => $Point['Avg'], 'Max' => $Point['Max'],
|
---|
487 | 'Continuity' => $Continuity, 'Time' => TimeToMysqlDateTime($StartTime3 + ($EndTime3 - $StartTime3) / 2)));
|
---|
488 | }
|
---|
489 | $StartIndex = $EndIndex - 1;
|
---|
490 | }
|
---|
491 | }
|
---|
492 | // Load values in time range
|
---|
493 | //array_pop($NextValues);
|
---|
494 | }
|
---|
495 | echo("Uroven dokoncena<br>\n");
|
---|
496 | $DbResult = $this->Database->select($this->Data['DataTable'], 'COUNT(*)',
|
---|
497 | '(`level`='.$Level.') AND (`measure`='.$this->Data['Id'].')');
|
---|
498 | $Row = $DbResult->fetch_array();
|
---|
499 | echo("Vlozeno ".$Row[0]." polozek.<br>\n");
|
---|
500 | }
|
---|
501 | }
|
---|
502 |
|
---|
503 | function RebuildAllMeasuresCache()
|
---|
504 | {
|
---|
505 | $Result = $this->Database->select('Measure', 'Id');
|
---|
506 | while($Row = $Result->fetch_array())
|
---|
507 | {
|
---|
508 | $Measure = new Measure();
|
---|
509 | $Measure->Load($Row['Id']);
|
---|
510 | $Measure->RebuildMeasureCache();
|
---|
511 | echo('Velicina id '.$Row['Id'].' dokoncena<br/>');
|
---|
512 | }
|
---|
513 | }
|
---|
514 |
|
---|
515 | function ClearData()
|
---|
516 | {
|
---|
517 | $this->Database->delete($this->Data['DataTable'], '1');
|
---|
518 | }
|
---|
519 | }
|
---|
520 |
|
---|
521 | class MeasureDataType
|
---|
522 | {
|
---|
523 | const Int8 = 0;
|
---|
524 | const Int16 = 1;
|
---|
525 | const Int32 = 2;
|
---|
526 | const Int64 = 3;
|
---|
527 | const Float = 4;
|
---|
528 | const Double = 5;
|
---|
529 | }
|
---|
530 |
|
---|
531 | class MeasureList extends Model
|
---|
532 | {
|
---|
533 | function AddItem(Measure $Measure)
|
---|
534 | {
|
---|
535 | $this->Database->insert('Measure', array(
|
---|
536 | 'Name' => $Measure->Data['Name'],
|
---|
537 | 'Description' => $Measure->Data['Description'],
|
---|
538 | 'Unit' => $Measure->Data['Unit'],
|
---|
539 | 'Info' => $Measure->Data['Info'],
|
---|
540 | 'DataType' => $Measure->Data['DataType'],
|
---|
541 | 'DataTable' => $Measure->GetDataTable(),
|
---|
542 | ));
|
---|
543 | $Measure->Data['Id'] = $this->Database->insert_id;
|
---|
544 |
|
---|
545 | $this->Database->query('CREATE TABLE `'.$Measure->GetDataTable().'` (
|
---|
546 | `Time` TIMESTAMP NOT NULL ,
|
---|
547 | `Measure` INT NOT NULL ,
|
---|
548 | `Level` TINYINT NOT NULL ,
|
---|
549 | `Min` '.$Measure->Data['DataType'].' NOT NULL ,
|
---|
550 | `Avg` '.$Measure->Data['DataType'].' NOT NULL ,
|
---|
551 | `Max` '.$Measure->Data['DataType'].' NOT NULL ,
|
---|
552 | `Continuity` BOOL NOT NULL
|
---|
553 | ) ENGINE = InnoDb ;');
|
---|
554 | }
|
---|
555 |
|
---|
556 | function RemoveItem(Measure $Measure)
|
---|
557 | {
|
---|
558 | $this->Database->delete('Measure', '`Id`='.$Measure->Data['Id']);
|
---|
559 | $this->Database->query('DROP TABLE `'.$Measure->GetDataTable().'`');
|
---|
560 | }
|
---|
561 |
|
---|
562 | }
|
---|