Changeset 59 for branches/old/stat_functions.php
- Timestamp:
- Mar 1, 2015, 12:43:56 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/old/stat_functions.php
r56 r59 79 79 80 80 // Update levels 81 //echo($Time."<br>\n");82 81 for($Level = 1; $Level <= $MaxLevel; $Level++) 83 82 { 84 //echo('Level '.$Level."<br>\n");85 83 $TimeSegment = TimeSegment($Level); 86 84 $EndTime = AlignTime($Time, $TimeSegment); 87 85 //if($EndTime < $Time) $EndTime = $EndTime + $TimeSegment; 88 86 $StartTime = $EndTime - $TimeSegment; 89 90 //echo(" ".$TimeSegment." ".$StartTime.'-'.$EndTime."<br>\n"); 91 //flush(); 92 87 93 88 // Load values in time range 94 89 $Values = array(); … … 100 95 $Values[] = $Row; 101 96 } 102 //print_r($Values);103 97 //array_pop($Values); 104 98 … … 107 101 108 102 $Point = ComputeOneValue($StartTime, $EndTime, $Values, $Measure, $Level); 109 //print_r($Point);110 103 111 104 $Database->delete($Measure['DataTable'], '(time > "'.TimeToMysqlDateTime($StartTime).'") AND … … 121 114 { 122 115 $Y = ($Y2 - $Y1) / ($X2 - $X1) * ($X - $X1) + $Y1; 123 //echo($Y1.'-'.$Y.'-'.$Y2.' '.$X1.'-'.$X.'-'.$X2.'<br>');124 116 return($Y); 125 117 } 126 118 119 /* Compute one value for upper time level from multiple values */ 127 120 function ComputeOneValue($LeftTime, $RightTime, $Values, $Measure, $Level) 128 121 { 129 122 global $ValueTypes, $Differential; 130 123 131 124 $NewValue = array('min' => +1000000000000000000, 'avg' => 0, 'max' => -1000000000000000000); 132 125 133 126 // Trim outside parts 134 127 foreach($ValueTypes as $ValueType) 135 128 { 136 $Values[0][$ValueType] = Interpolation($Values[0]['time'], $Values[0][$ValueType], $Values[1]['time'], $Values[1][$ValueType], $LeftTime); 129 $Values[0][$ValueType] = Interpolation($Values[0]['time'], $Values[0][$ValueType], 130 $Values[1]['time'], $Values[1][$ValueType], $LeftTime); 137 131 } 138 132 $Values[0]['time'] = $LeftTime; 139 133 foreach($ValueTypes as $ValueType) 140 { 141 $Values[count($Values)-1][$ValueType] = Interpolation($Values[count($Values)-2]['time'], $Values[count($Values)-2][$ValueType], 142 $Values[count($Values)-1]['time'], $Values[count($Values)-1][$ValueType], $RightTime); 143 } 144 $Values[count($Values)-1]['time'] = $RightTime; 145 146 // Perform computation 134 { 135 $Values[count($Values) - 1][$ValueType] = Interpolation($Values[count($Values) - 2]['time'], 136 $Values[count($Values) - 2][$ValueType], 137 $Values[count($Values) - 1]['time'], $Values[count($Values) - 1][$ValueType], $RightTime); 138 } 139 $Values[count($Values) - 1]['time'] = $RightTime; 140 141 // Perform computation 147 142 foreach($ValueTypes as $ValueType) 148 { 143 { 149 144 // Compute new value 150 145 for($I = 0; $I < (count($Values) - 1); $I++) 151 { 146 { 152 147 if($ValueType == 'avg') 153 148 { 154 if($Values[$I +1]['continuity'] == $Measure['ContinuityEnabled']);149 if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled']); 155 150 else if($Differential == 0) 156 151 { 157 $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I +1]['time'] - $Values[$I]['time']) *158 (($Values[$I +1][$ValueType] - $Values[$I][$ValueType]) / 2 + $Values[$I][$ValueType]);152 $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['time'] - $Values[$I]['time']) * 153 (($Values[$I + 1][$ValueType] - $Values[$I][$ValueType]) / 2 + $Values[$I][$ValueType]); 159 154 } else { 160 $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I +1]['time'] - $Values[$I]['time']) *161 (($Values[$I +1][$ValueType] - $Values[$I][$ValueType]) / 2);155 $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['time'] - $Values[$I]['time']) * 156 (($Values[$I + 1][$ValueType] - $Values[$I][$ValueType]) / 2); 162 157 } 163 158 } 164 else if($ValueType == 'max') 165 { 166 if($Values[$I +1]['continuity'] == $Measure['ContinuityEnabled'])159 else if($ValueType == 'max') 160 { 161 if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled']) 167 162 { 168 163 if(0 > $NewValue[$ValueType]) $NewValue[$ValueType] = 0; 169 164 } 170 else 165 else 171 166 { 172 167 if($Differential == 0) 173 168 { 174 if($Values[$I +1][$ValueType] > $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I+1][$ValueType];169 if($Values[$I + 1][$ValueType] > $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType]; 175 170 } else { 176 $Difference = $Values[$I +1][$ValueType] - $Values[$I][$ValueType];171 $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType]; 177 172 if($Difference > $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference; 178 } 173 } 179 174 } 180 175 } 181 176 else if($ValueType == 'min') 182 177 { 183 //echo($Values[$I+1]['continuity'].'=='.$Measure['ContinuityEnabled'].'<br>'); 184 if($Values[$I+1]['continuity'] == $Measure['ContinuityEnabled']) 178 if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled']) 185 179 { 186 180 if(0 < $NewValue[$ValueType]) $NewValue[$ValueType] = 0; … … 188 182 if($Differential == 0) 189 183 { 190 if($Values[$I +1][$ValueType] < $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I+1][$ValueType];184 if($Values[$I + 1][$ValueType] < $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType]; 191 185 } else { 192 $Difference = $Values[$I +1][$ValueType] - $Values[$I][$ValueType];186 $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType]; 193 187 if($Difference < $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference; 194 188 } … … 196 190 } 197 191 } 198 $NewValue[$ValueType] = $NewValue[$ValueType]; 192 $NewValue[$ValueType] = $NewValue[$ValueType]; 199 193 } 200 194 //if(($RightTime - $LeftTime) > 0) 201 if($Measure['Cumulative'] == 0) 195 if($Measure['Cumulative'] == 0) 202 196 { 203 197 $NewValue['avg'] = $NewValue['avg'] / ($RightTime - $LeftTime); 204 } 198 } 205 199 return($NewValue); 206 //echo($NewValue['avg'].'<br>');207 //return(array('min' => rand(0,1), 'avg' => $NewValue['avg'], 'max' => rand(0,1)));208 200 } 209 201 … … 213 205 214 206 // Get first and last time 215 //echo($Measure['Id'].','.$Level.','.StatTableName($Level)."\n");216 207 $Result = $Database->select($Measure['DataTable'], '*', 'measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time LIMIT 1'); 217 208 if($Result->num_rows > 0) … … 239 230 { 240 231 global $Debug, $Database; 232 241 233 $Result = array(); 242 $DbResult = $Database->select($Measure['DataTable'], '*', ' time > "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.'ORDER BY time ASC LIMIT 1');234 $DbResult = $Database->select($Measure['DataTable'], '*', '(time > "'.TimeToMysqlDateTime($Time).'") AND (measure='.$Measure['Id'].') AND (level='.$Level.') ORDER BY time ASC LIMIT 1'); 243 235 if($DbResult->num_rows > 0) 244 236 { … … 252 244 //array_push($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0)); 253 245 $Result[] = array('time' => ($Time + TimeSegment($Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0); 254 $DbResult = $Database->select($Measure['DataTable'], '*', ' time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.'ORDER BY time DESC LIMIT 1');246 $DbResult = $Database->select($Measure['DataTable'], '*', '(time < "'.TimeToMysqlDateTime($Time).'") AND (measure='.$Measure['Id'].') AND (level='.$Level.') ORDER BY time DESC LIMIT 1'); 255 247 if($DbResult->num_rows > 0) 256 248 { … … 266 258 { 267 259 global $Debug, $Database; 260 268 261 $Result = array(); 269 //echo('SELECT * FROM '.StatTableName($Level). ' WHERE '. 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1'."<br>\n"); 270 $DbResult = $Database->select($Measure['DataTable'], '*', 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1'); 262 $DbResult = $Database->select($Measure['DataTable'], '*', '(time < "'.TimeToMysqlDateTime($Time).'") AND (measure='.$Measure['Id'].') AND (level='.$Level.') ORDER BY time DESC LIMIT 1'); 271 263 if($DbResult->num_rows > 0) 272 264 { … … 279 271 $Result[] = array('time' => ($Time - TimeSegment($Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0); 280 272 281 $DbResult = $Database->select($Measure['DataTable'], '*', ' time > "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.'ORDER BY time ASC LIMIT 1');273 $DbResult = $Database->select($Measure['DataTable'], '*', '(time > "'.TimeToMysqlDateTime($Time).'") AND (measure='.$Measure['Id'].') AND (level='.$Level.') ORDER BY time ASC LIMIT 1'); 282 274 if($DbResult->num_rows > 0) 283 275 { 284 276 $Row = $DbResult->fetch_array(); 285 array_push($Result, array('time' => (MysqlDateTimeToTime($Row['time']) -10), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));286 }287 // if($Debug) print_r($Result); 277 array_push($Result, array('time' => (MysqlDateTimeToTime($Row['time']) - 10), 278 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0)); 279 } 288 280 return($Result); 289 281 } … … 306 298 $Result = $Database->select($Measure['DataTable'], 'time, min, avg, max, continuity', 'time > "'.TimeToMysqlDateTime($TimeFrom).'" AND time < "'. 307 299 TimeToMysqlDateTime($TimeTo).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time'); 308 // echo($Level.' '.TimeToMysqlDateTime($TimeFrom).' '.TimeToMysqlDateTime($TimeTo));309 300 $Values = array(); 310 // echo(DB_NumRows());311 // $III = 0;312 301 while($Row = $Result->fetch_array()) 313 302 { 314 // echo($III.' '.$Row['time'].' '.memory_get_usage().',');315 // $III++;316 303 $Values[] = array('time' => MysqlDateTimeToTime($Row['time']), 'min' => $Row['min'], 'avg' => $Row['avg'], 'max' => $Row['max'], 'continuity' => $Row['continuity']); 317 304 } 318 305 // array_pop($Values); 319 // echo('abc');320 // die();321 306 if($Debug) echo('Item count: '.count($Values)); 322 307 323 308 $Points = array(); 324 309 if(count($Values) > 0) … … 326 311 $Values = array_merge(LoadLeftSideValue($Level, $Measure, $TimeFrom), $Values, LoadRightSideValue($Level, $Measure, $TimeTo)); 327 312 328 //echo(count($Values).'<br>');329 //echo($TimeFrom.','.$TimeTo.'<br>');330 //echo($Values[0]['time'].'<br>');331 313 $StartIndex = 0; 332 314 $Points = array(); 333 //echo($DivisionCount.'<br>');334 315 for($I = 0; $I < $DivisionCount; $I++) 335 316 { … … 338 319 $TimeEnd = $TimeFrom + (($TimeTo - $TimeFrom) / $DivisionCount) * ($I+1); 339 320 //if($Debug) echo('TimeEnd '.$I.': '.$TimeEnd.'('.TimeToMysqlDateTime($TimeEnd).')<br>'); 340 //echo($TimeStart.','.$TimeEnd.'<br>');341 321 342 322 $EndIndex = $StartIndex; 343 323 while($Values[$EndIndex]['time'] < $TimeEnd) $EndIndex = $EndIndex + 1; 344 324 $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1); 345 //echo($StartIndex.','.$EndIndex.' '.count($SubValues).'<br>');346 //print_r($SubValues);347 325 $Points[] = ComputeOneValue($TimeStart, $TimeEnd, $SubValues, $Measure, $Level); 348 326 $StartIndex = $EndIndex - 1; … … 371 349 echo('Uroven '.$Level."<br>\n"); 372 350 $TimeRange = GetTimeRange($Measure, $Level-1); 373 //echo($Measure['Id'].','.($Level-1)."\n");374 //echo(TimeToMysqlDateTime($TimeRange['left']).'-'.TimeToMysqlDateTime($TimeRange['right'])."\n");375 351 $TimeSegment = TimeSegment($Level); 376 352 $StartTime = AlignTime($TimeRange['left'], $TimeSegment) - $TimeSegment; … … 382 358 echo($I.' '); 383 359 $StartTime2 = $StartTime + $I * $BurstCount * $TimeSegment; 384 $EndTime2 = $StartTime + ($I +1) * $BurstCount * $TimeSegment;360 $EndTime2 = $StartTime + ($I + 1) * $BurstCount * $TimeSegment; 385 361 $Values = array(); 386 $DbResult = $Database->select($Measure['DataTable'], '*', ' time > "'.TimeToMysqlDateTime($StartTime2).'" ANDtime < "'.387 TimeToMysqlDateTime($EndTime2).'" AND measure='.$Measure['Id'].' AND level='.($Level-1).'ORDER BY time');362 $DbResult = $Database->select($Measure['DataTable'], '*', '(time > "'.TimeToMysqlDateTime($StartTime2).'") AND (time < "'. 363 TimeToMysqlDateTime($EndTime2).'") AND (measure='.$Measure['Id'].') AND (level='.($Level - 1).') ORDER BY time'); 388 364 while($Row = $DbResult->fetch_array()) 389 365 { … … 394 370 if(count($Values) > 0) 395 371 { 396 $Values = array_merge(LoadLeftSideValue($Level -1, $Measure, $StartTime2), $Values, LoadRightSideValue($Level-1, $Measure, $EndTime2));372 $Values = array_merge(LoadLeftSideValue($Level - 1, $Measure, $StartTime2), $Values, LoadRightSideValue($Level - 1, $Measure, $EndTime2)); 397 373 398 374 $StartIndex = 0; … … 401 377 echo('.'); 402 378 $StartTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * $B; 403 $EndTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * ($B +1);379 $EndTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * ($B + 1); 404 380 405 381 $EndIndex = $StartIndex; 406 382 while($Values[$EndIndex]['time'] < $EndTime3) $EndIndex = $EndIndex + 1; 407 383 $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1); 408 //echo($StartIndex.','.$EndIndex.' '.count($SubValues).'<br>'); 409 //print_r($SubValues); 410 if(count($SubValues) > 2) 384 if(count($SubValues) > 2) 411 385 { 412 386 $Point = ComputeOneValue($StartTime3, $EndTime3, $SubValues, $Measure, $Level); 413 $Continuity = $SubValues[1]['continuity']; 414 $Database->insert($Measure['DataTable'], array('level' => $Level, 'measure' => $Measure['Id'], 'min' => $Point['min'], 415 'avg' => $Point['avg'], 'max' => $Point['max'], 'continuity' => $Continuity, 'time' => TimeToMysqlDateTime($StartTime3+($EndTime3-$StartTime3)/2))); 387 $Continuity = $SubValues[1]['continuity']; 388 $Database->insert($Measure['DataTable'], array('level' => $Level, 'measure' => $Measure['Id'], 389 'min' => $Point['min'], 'avg' => $Point['avg'], 'max' => $Point['max'], 390 'continuity' => $Continuity, 'time' => TimeToMysqlDateTime($StartTime3 + ($EndTime3 - $StartTime3) / 2))); 416 391 } 417 392 $StartIndex = $EndIndex - 1; … … 425 400 $Row = $DbResult->fetch_array(); 426 401 echo("Vlozeno ".$Row[0]." polozek.<br>\n"); 427 } 428 } 402 } 403 } 429 404 430 405 function RebuildAllMeasuresCache() … … 432 407 global $Database; 433 408 434 // echo("Vytvarim novou cache...\n"); 435 // Load measures 409 // Load measures 436 410 $Measures = array(); 437 411 $Result = $Database->select('measure', '*');
Note:
See TracChangeset
for help on using the changeset viewer.