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