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