source: trunk/Modules/Measure/Measure.php

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