Changeset 65 for trunk/Measure.php
- Timestamp:
- Jan 1, 2016, 4:06:20 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Measure.php
r64 r65 13 13 var $Differential; 14 14 var $DivisionCount; 15 var $PeriodTolerance; 15 16 16 17 function __construct(Database $Database) … … 24 25 $this->Differential = 0; 25 26 $this->DivisionCount = 500; 27 $this->PeriodTolerance = 0.25; // 25% 26 28 } 27 29 … … 60 62 $Result = $this->Database->select($this->Data['DataTable'], '*', '(`measure`='.$this->Data['Id'].') AND '. 61 63 '(`level`=0) ORDER BY `time` DESC LIMIT 2'); 62 if($Result->num_rows == 0) $this->Database->insert($this->Data['DataTable'], 64 if($Result->num_rows == 0) { 65 // No measure value found. Simply insert new first value. 66 $this->Database->insert($this->Data['DataTable'], 63 67 array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 64 'measure' => $this->Data['Id'], 'time' => TimeToMysqlDateTime($Time), 'continuity' => 0)); 65 else if($Result->num_rows == 1) $this->Database->insert($this->Data['DataTable'], 68 'measure' => $this->Data['Id'], 'time' => TimeToMysqlDateTime($Time), 69 'continuity' => 0)); 70 } else if($Result->num_rows == 1) { 71 // One value exists. Add second value. 72 $this->Database->insert($this->Data['DataTable'], 66 73 array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 67 74 'measure' => $this->Data['Id'], 'time' => TimeToMysqlDateTime($Time), 68 75 'continuity' => 1)); 69 else { 76 } else { 77 // Two values already exist in measure table 70 78 $LastValue = $Result->fetch_array(); 71 79 $NextToLastValue = $Result->fetch_array(); 72 if(($Time - MysqlDateTimeToTime($LastValue['time'])) < 0.75 * $this->Data['Period']) 73 { 80 if(($Time - MysqlDateTimeToTime($LastValue['time'])) < (1 - $this->PeriodTolerance) * $this->Data['Period']) 81 { 82 // New value added too quickly. Need to wait for next measure period. 74 83 } 75 84 else 76 85 { 77 if(($Time - MysqlDateTimeToTime($LastValue['time'])) < 1.25 * $this->Data['Period']) $Continuity = 1; 78 else $Continuity = 0; 86 // We are near defined period and can add new value. 87 if(($Time - MysqlDateTimeToTime($LastValue['time'])) < (1 + $this->PeriodTolerance) * $this->Data['Period']) { 88 // New value added near defined measure period inside tolerance. Keep continuity. 89 $Continuity = 1; 90 } else { 91 // New value added too late after defined measure period. Stop 92 // continuity and let gap to be added. 93 $Continuity = 0; 94 } 79 95 if(($LastValue['avg'] == $NextToLastValue['avg']) and ($LastValue['avg'] == $Value) and 80 96 ($LastValue['continuity'] == 1) and ($Continuity == 1)) 81 97 { 82 $this->Database->update($this->Data['DataTable'], '(time="'.$LastValue['time'].'") AND '. 83 '(level=0) AND (measure='.$this->Data['Id'].')', array('time' => TimeToMysqlDateTime($Time))); 98 // New value is same as last value and continuity mode is enabled and 99 // continuity flag is present. Just shift forward time for last value. 100 $this->Database->update($this->Data['DataTable'], '(`time`="'.$LastValue['time'].'") AND '. 101 '(`level`=0) AND (`measure`='.$this->Data['Id'].')', array('time' => TimeToMysqlDateTime($Time))); 84 102 } else 85 103 { 104 // Last value is different or not with continuity flag. Need to add new value. 86 105 $this->Database->insert($this->Data['DataTable'], array('min' => $Value, 87 106 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $this->Data['Id'], 'time' => TimeToMysqlDateTime($Time), … … 91 110 } 92 111 93 // Update levels112 // Update higher levels 94 113 for($Level = 1; $Level <= $this->MaxLevel; $Level++) 95 114 { … … 101 120 // Load values in time range 102 121 $Values = array(); 103 $Result = $this->Database->select($this->Data['DataTable'], '*', '(`time` > "'.TimeToMysqlDateTime($StartTime).'") AND '. 122 $Result = $this->Database->select($this->Data['DataTable'], '*', '(`time` > "'. 123 TimeToMysqlDateTime($StartTime).'") AND '. 104 124 '(`time` < "'.TimeToMysqlDateTime($EndTime).'") AND '. 105 125 '(`measure`='.$this->Data['Id'].') AND (`level`='.($Level - 1).') ORDER BY `time`'); … … 115 135 $this->LoadLeftSideValue($Level - 1, $StartTime), 116 136 $Values, 117 $this->LoadRightSideValue($Level - 1, $EndTime)); 137 $this->LoadRightSideValue($Level - 1, $EndTime) 138 ); 118 139 119 140 $Point = $this->ComputeOneValue($StartTime, $EndTime, $Values, $Level); 120 141 121 $this->Database->delete($this->Data['DataTable'], '(time > "'.TimeToMysqlDateTime($StartTime).'") AND 122 (time < "'.TimeToMysqlDateTime($EndTime).'") AND measure='.$this->Data['Id'].' AND level='.$Level); 142 $this->Database->delete($this->Data['DataTable'], '(`time` > "'.TimeToMysqlDateTime($StartTime).'") AND 143 (`time` < "'.TimeToMysqlDateTime($EndTime).'") AND (`measure`='.$this->Data['Id'].') '. 144 'AND (`level`='.$Level.')'); 123 145 $Continuity = $Values[1]['continuity']; 124 $this->Database->insert($this->Data['DataTable'], array('level' => $Level, 'measure' => $this->Data['Id'], 'min' => $Point['min'], 146 $this->Database->insert($this->Data['DataTable'], array('level' => $Level, 147 'measure' => $this->Data['Id'], 'min' => $Point['min'], 125 148 'avg' => $Point['avg'], 'max' => $Point['max'], 'continuity' => $Continuity, 126 149 'time' => TimeToMysqlDateTime($StartTime + ($EndTime - $StartTime) / 2))); 127 128 150 } 129 151 } … … 137 159 foreach($this->ValueTypes as $ValueType) 138 160 { 139 $Values[0][$ValueType] = Interpolation(NewPoint($Values[0]['time'], $Values[0][$ValueType]), 161 $Values[0][$ValueType] = Interpolation( 162 NewPoint($Values[0]['time'], $Values[0][$ValueType]), 140 163 NewPoint($Values[1]['time'], $Values[1][$ValueType]), $LeftTime); 141 164 } … … 143 166 foreach($this->ValueTypes as $ValueType) 144 167 { 145 $Values[count($Values) - 1][$ValueType] = Interpolation( NewPoint($Values[count($Values) - 2]['time'],146 $Values[count($Values) - 2][$ValueType]),168 $Values[count($Values) - 1][$ValueType] = Interpolation( 169 NewPoint($Values[count($Values) - 2]['time'], $Values[count($Values) - 2][$ValueType]), 147 170 NewPoint($Values[count($Values) - 1]['time'], $Values[count($Values) - 1][$ValueType]), 148 171 $RightTime); … … 233 256 } 234 257 258 // Load one nearest value newer then given time 235 259 function LoadRightSideValue($Level, $Time) 236 260 { 237 261 $Result = array(); 238 $DbResult = $this->Database->select($this->Data['DataTable'], '*', '(time > "'.TimeToMysqlDateTime($Time).'") AND (measure='.$this->Data['Id'].') AND (level='.$Level.') ORDER BY time ASC LIMIT 1'); 262 $DbResult = $this->Database->select($this->Data['DataTable'], '*', 263 '(`time` > "'.TimeToMysqlDateTime($Time).'") AND (`measure`='. 264 $this->Data['Id'].') AND (`level`='.$Level.') ORDER BY `time` ASC LIMIT 1'); 239 265 if($DbResult->num_rows > 0) 240 266 { 267 // Found one value, simply return it 241 268 $Row = $DbResult->fetch_array(); 242 269 $Row['time'] = MysqlDateTimeToTime($Row['time']); 243 270 return(array($Row)); 244 } 245 else246 {247 //$Time = $Values[count($Values) -1]['time'] + 60;271 } else 272 { 273 // Not found any value. Calculate it 274 //$Time = $Values[count($Values) - 1]['time'] + 60; 248 275 //array_push($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0)); 249 276 $Result[] = array('time' => ($Time + $this->TimeSegment($Level)), 'min' => 0, … … 262 289 } 263 290 291 // Load one nearest value older then given time 264 292 function LoadLeftSideValue($Level, $Time) 265 293 { … … 270 298 if($DbResult->num_rows > 0) 271 299 { 300 // Found one value, simply return it 272 301 $Row = $DbResult->fetch_array(); 273 302 $Row['time'] = MysqlDateTimeToTime($Row['time']); 274 303 return(array($Row)); 275 304 } else { 305 // Not found any value. Calculate it 276 306 //$Time = $Values[0]['time'] - 60; 277 307 //array_unshift($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0)); … … 301 331 302 332 // Load values in time range 303 $Result = $this->Database->select($this->Data['DataTable'], '`time`, `min`, `avg`, `max`, `continuity`', 333 $Result = $this->Database->select($this->Data['DataTable'], '`time`, `min`, '. 334 '`avg`, `max`, `continuity`', 304 335 '(`time` > "'.TimeToMysqlDateTime($TimeFrom).'") AND '. 305 336 '(`time` < "'.TimeToMysqlDateTime($TimeTo).'") AND '. … … 308 339 while($Row = $Result->fetch_array()) 309 340 { 310 $Values[] = array('time' => MysqlDateTimeToTime($Row['time']), 'min' => $Row['min'], 'avg' => $Row['avg'], 'max' => $Row['max'], 'continuity' => $Row['continuity']); 341 $Values[] = array('time' => MysqlDateTimeToTime($Row['time']), 342 'min' => $Row['min'], 'avg' => $Row['avg'], 'max' => $Row['max'], 343 'continuity' => $Row['continuity']); 311 344 } 312 345 // array_pop($Values); … … 325 358 { 326 359 $TimeStart = $TimeFrom + (($TimeTo - $TimeFrom) / $this->DivisionCount) * $I; 327 $TimeEnd = $TimeFrom + (($TimeTo - $TimeFrom) / $this->DivisionCount) * ($I +1);360 $TimeEnd = $TimeFrom + (($TimeTo - $TimeFrom) / $this->DivisionCount) * ($I + 1); 328 361 329 362 $EndIndex = $StartIndex;
Note:
See TracChangeset
for help on using the changeset viewer.