-6; $II--) { if($Value >= pow(10, $II)) { if($Digits < ($II + 1)) $RealDigits = $II + 1; else $RealDigits = $Digits; $Value = round($Value / pow(10, $II - $RealDigits + 1)) * pow(10, $II - $RealDigits + 1); break; } } return($Value); } function AddPrefixMultipliers($Value, $Unit, $Digits = 4) { global $PrefixMultipliers; if($Unit == '') return(TruncateDigits($Value, $Digits)); $I = 8; if($Value > 0) $II = 1; else if($Value < 0) $II = -1; else $II = 0; while((($Value / $PrefixMultipliers[$I + $II][2]) > $II) and (($I + $II) >= 0) and (($I + $II) <= count($PrefixMultipliers))) $I = $I + $II; $Value = $Value / $PrefixMultipliers[$I][2]; // Truncate digits count $Value = TruncateDigits($Value, $Digits); return($Value.' '.$PrefixMultipliers[$I][0].$Unit); } function MysqlDateTimeToTime($Time) { $Parts = explode(' ', $Time); $DateParts = explode('-', $Parts[0]); $TimeParts = explode(':', $Parts[1]); $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]); return($Result); } function TimeToMysqlDateTime($Time) { return(date('Y-m-d H:i:s', $Time)); } function TimeSegment($Base, $Level) { global $LevelReducing; return(pow($LevelReducing, $Level) * $Base); } function StatTableName($Level) { if($Level == 0) return('data'); else return('data_cache'); } function AlignTime($Time, $TimeSegment) { global $ReferenceTime, $LevelReducing; return(round(($Time - $ReferenceTime) / $TimeSegment) * $TimeSegment + $ReferenceTime); } function AddValue($Measure, $Value = array('min' => 0, 'avg' => 0, 'max' => 0), $Level = 0, $Time = 0) { global $LevelReducing, $MaxLevel, $Database; if($Time == 0) $Time = time(); //$Value = round($Measure['divider'] * $Value); //echo(TimeToMysqlDateTime($Time).'|'.$Level."\n"); $Result = $Database->select($Measure['DataTable'], '*', 'measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 2'); //echo($Database->LastQuery."\n"); if($Result->num_rows == 0) { $Database->insert($Measure['DataTable'], array('min' => $Value['min'], 'avg' => $Value['avg'], 'max' => $Value['max'], 'level' => $Level, 'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time), 'continuity' => 0)); //echo($Database->LastQuery."\n"); } else if($Result->num_rows == 1) { $Database->insert($Measure['DataTable'], array('min' => $Value['min'], 'avg' => $Value['avg'], 'max' => $Value['max'], 'level' => $Level, 'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time), 'continuity' => 1)); //echo($Database->LastQuery."\n"); } else { $LastValue = $Result->fetch_assoc(); $NextToLastValue = $Result->fetch_assoc(); //echo($Level.': '.$Time.' - '.MysqlDateTimeToTime($LastValue['time']).' '.($Time - MysqlDateTimeToTime($LastValue['time'])).' = '.$Measure['Period']."\n"); if((($Time - MysqlDateTimeToTime($LastValue['time'])) < 0.75 * $Measure['Period']) and ($Level == 0)) { echo("Too short period\n"); } else { if(($Time - MysqlDateTimeToTime($LastValue['time'])) < 1.25 * $Measure['Period']) $Continuity = 1; else $Continuity = 0; echo('('.$LastValue['avg'].'=='.$NextToLastValue['avg'].') and ('.$LastValue['avg'].' == '.$Value.') and ('.$LastValue['continuity'].' == 1) and ('.$Continuity.' == 1))'."\n"); if(($LastValue['min'] == $NextToLastValue['min']) and ($LastValue['min'] == $Value['min']) and ($LastValue['avg'] == $NextToLastValue['avg']) and ($LastValue['avg'] == $Value['avg']) and ($LastValue['max'] == $NextToLastValue['max']) and ($LastValue['max'] == $Value['max']) and ($LastValue['continuity'] == 1) and ($Continuity == 1)) { $Database->update($Measure['DataTable'], 'time="'.$LastValue['time'].'" AND level='.$Level.' AND measure='.$Measure['Id'], array('time' => TimeToMysqlDateTime($Time))); echo($Database->LastQuery."\n"); } else { $Database->insert($Measure['DataTable'], array('min' => $Value['min'], 'avg' => $Value['avg'], 'max' => $Value['max'], 'level' => $Level, 'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time), 'continuity' => $Continuity)); echo($Database->LastQuery."\n"); } } // Update next level if($Level < $MaxLevel) { $Level = $Level + 1; //echo('Level '.$Level."
\n"); $TimeSegment = TimeSegment($Measure['Period'], 1); $EndTime = AlignTime($Time, $TimeSegment); //if($EndTime < $Time) $EndTime = $EndTime + $TimeSegment; $StartTime = $EndTime - $TimeSegment; //echo(" ".$TimeSegment." ".$StartTime.'-'.$EndTime."
\n"); //flush(); // Load values in time range $Values = array(); //.'" AND time < "'.TimeToMysqlDateTime($EndTime).'" AND measure='.$Measure['Id'].' AND level='.($Level - 1).' ORDER BY time'); $Result = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($StartTime).'" AND time < "'.TimeToMysqlDateTime($EndTime).'" AND measure='.$Measure['Id'].' AND level='.($Level - 1).' ORDER BY time'); while($Row = $Result->fetch_assoc()) { $Row['time'] = MysqlDateTimeToTime($Row['time']); $Values[] = $Row; } //if(count($Values) > 2) { //print_r($Values); //array_pop($Values); // Load subsidary values print_r($Values); $Values = array_merge(LoadLeftSideValue($Level - 1, $Measure, $StartTime), $Values, LoadRightSideValue($Level - 1, $Measure, $EndTime)); print_r($Values); $Point = ComputeOneValue($StartTime, $EndTime, $Values, $Measure, $Level); //print_r($Point); $Database->delete($Measure['DataTable'], '(time > "'.TimeToMysqlDateTime($StartTime).'") AND (time < "'.TimeToMysqlDateTime($EndTime).'") AND measure='.$Measure['Id'].' AND level='.$Level); $Measure['Period'] = $TimeSegment; AddValue($Measure, array('min' => $Point['min'], 'avg' => $Point['avg'], 'max' => $Point['max']), $Level, $StartTime + ($EndTime - $StartTime) / 2); } } } } function Interpolation($X1, $Y1, $X2, $Y2, $X) { $Y = ($Y2 - $Y1) / ($X2 - $X1) * ($X - $X1) + $Y1; //echo($Y1.'-'.$Y.'-'.$Y2.' '.$X1.'-'.$X.'-'.$X2.'
'); return($Y); } function ComputeOneValue($LeftTime, $RightTime, $Values, $Measure, $Level) { global $ValueTypes, $Differential; $NewValue = array('min' => +1000000000000000000, 'avg' => 0, 'max' => -1000000000000000000); // Trim outside parts foreach($ValueTypes as $ValueType) { $Values[0][$ValueType] = Interpolation($Values[0]['time'], $Values[0][$ValueType], $Values[1]['time'], $Values[1][$ValueType], $LeftTime); } $Values[0]['time'] = $LeftTime; foreach($ValueTypes as $ValueType) { $Values[count($Values) - 1][$ValueType] = Interpolation($Values[count($Values) - 2]['time'], $Values[count($Values) - 2][$ValueType], $Values[count($Values) - 1]['time'], $Values[count($Values) - 1][$ValueType], $RightTime); } $Values[count($Values) - 1]['time'] = $RightTime; // Perform computation foreach($ValueTypes as $ValueType) { // Compute new value for($I = 0; $I < (count($Values) - 1); $I++) { if($ValueType == 'avg') { if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled']); else if($Differential == 0) { $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I+1]['time'] - $Values[$I]['time']) * (($Values[$I+1][$ValueType] - $Values[$I][$ValueType]) / 2 + $Values[$I][$ValueType]); } else { $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I+1]['time'] - $Values[$I]['time']) * (($Values[$I+1][$ValueType] - $Values[$I][$ValueType]) / 2); } } else if($ValueType == 'max') { if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled']) { if(0 > $NewValue[$ValueType]) $NewValue[$ValueType] = 0; } else { if($Differential == 0) { if($Values[$I + 1][$ValueType] > $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType]; } else { $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType]; if($Difference > $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference; } } } else if($ValueType == 'min') { //echo($Values[$I+1]['continuity'].'=='.$Measure['ContinuityEnabled'].'
'); if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled']) { if(0 < $NewValue[$ValueType]) $NewValue[$ValueType] = 0; } else { if($Differential == 0) { if($Values[$I + 1][$ValueType] < $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType]; } else { $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType]; if($Difference < $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference; } } } } $NewValue[$ValueType] = $NewValue[$ValueType]; } //if(($RightTime - $LeftTime) > 0) if($Measure['Cumulative'] == 0) { $NewValue['avg'] = $NewValue['avg'] / ($RightTime - $LeftTime); } return($NewValue); //echo($NewValue['avg'].'
'); //return(array('min' => rand(0,1), 'avg' => $NewValue['avg'], 'max' => rand(0,1))); } function GetTimeRange($Measure, $Level) { global $Debug, $Database; // Get first and last time //echo($Measure['Id'].','.$Level.','.StatTableName($Level)."\n"); $Result = $Database->select($Measure['DataTable'], '*', 'measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time LIMIT 1'); if($Result->num_rows > 0) { $Row = $Result->fetch_assoc(); $AbsoluteLeftTime = MysqlDateTimeToTime($Row['time']); } else $AbsoluteLeftTime = 0; $Result = $Database->select($Measure['DataTable'], '*', 'measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1'); if($Result->num_rows > 0) { $Row = $Result->fetch_assoc(); $AbsoluteRightTime = MysqlDateTimeToTime($Row['time']); } else $AbsoluteRightTime = 0; if($Debug) { echo('AbsoluteLeftTime: '.$AbsoluteLeftTime.'('.TimeToMysqlDateTime($AbsoluteLeftTime).')
'); echo('AbsoluteRightTime: '.$AbsoluteRightTime.'('.TimeToMysqlDateTime($AbsoluteRightTime).')
'); } return(array('left' => $AbsoluteLeftTime, 'right' => $AbsoluteRightTime)); } function LoadRightSideValue($Level, $Measure, $Time) { global $Debug, $Database; $Result = array(); $DbResult = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time ASC LIMIT 1'); if($DbResult->num_rows > 0) { $Row = $DbResult->fetch_assoc(); $Row['time'] = MysqlDateTimeToTime($Row['time']); return(array($Row)); } else { //$Time = $Values[count($Values)-1]['time'] + 60; //array_push($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0)); $Result[] = array('time' => ($Time + TimeSegment($Measure['Period'], $Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0); $DbResult = $Database->select($Measure['DataTable'], '*', 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1'); if($DbResult->num_rows > 0) { $Row = $DbResult->fetch_assoc(); array_unshift($Result, array('time' => (MysqlDateTimeToTime($Row['time']) + 10), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0)); } // if($Debug) print_r($Result); return($Result); } } function LoadLeftSideValue($Level, $Measure, $Time) { global $Debug, $Database; $Result = array(); //echo('SELECT * FROM '.StatTableName($Level). ' WHERE '. 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1'."
\n"); $DbResult = $Database->select($Measure['DataTable'], '*', 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1'); if($DbResult->num_rows > 0) { $Row = $DbResult->fetch_assoc(); $Row['time'] = MysqlDateTimeToTime($Row['time']); return(array($Row)); } else { //$Time = $Values[0]['time'] - 60; //array_unshift($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0)); $Result[] = array('time' => ($Time - TimeSegment($Measure['Period'], $Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0); $DbResult = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time ASC LIMIT 1'); if($DbResult->num_rows > 0) { $Row = $DbResult->fetch_assoc(); array_push($Result, array('time' => (MysqlDateTimeToTime($Row['time'])-10), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0)); } // if($Debug) print_r($Result); return($Result); } } function GetValues($Measure, $TimeFrom, $TimeTo, $Level) { global $DivisionCount, $Debug, $Database; if($Debug) echo('TimeFrom: '.$TimeFrom.'('.TimeToMysqlDateTime($TimeFrom).')
'); if($Debug) echo('TimeTo: '.$TimeTo.'('.TimeToMysqlDateTime($TimeTo).')
'); //$AbsoluteTime = GetTimeRange($MeasureId); // if(($TimeFrom > $AbsoluteLeftTime) and ($TimeStart < $AbsoluteRightTime) and // ($TimeTo > $AbsoluteLeftTime) and ($TimeTo < $AbsoluteRightTime)) // { // Load values in time range $Result = $Database->select($Measure['DataTable'], 'time, min, avg, max, continuity', 'time > "'.TimeToMysqlDateTime($TimeFrom).'" AND time < "'.TimeToMysqlDateTime($TimeTo).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time'); // echo($Level.' '.TimeToMysqlDateTime($TimeFrom).' '.TimeToMysqlDateTime($TimeTo)); $Values = array(); // echo(DB_NumRows()); // $III = 0; while($Row = $Result->fetch_assoc()) { // echo($III.' '.$Row['time'].' '.memory_get_usage().','); // $III++; $Values[] = array('time' => MysqlDateTimeToTime($Row['time']), 'min' => $Row['min'], 'avg' => $Row['avg'], 'max' => $Row['max'], 'continuity' => $Row['continuity']); } // array_pop($Values); // echo('abc'); // die(); if($Debug) echo('Item count: '.count($Values)); $Points = array(); if(count($Values) > 0) { $Values = array_merge(LoadLeftSideValue($Level, $Measure, $TimeFrom), $Values, LoadRightSideValue($Level, $Measure, $TimeTo)); //echo(count($Values).'
'); //echo($TimeFrom.','.$TimeTo.'
'); //echo($Values[0]['time'].'
'); $StartIndex = 0; $Points = array(); //echo($DivisionCount.'
'); for($I = 0; $I < $DivisionCount; $I++) { $TimeStart = $TimeFrom + (($TimeTo - $TimeFrom) / $DivisionCount) * $I; //if($Debug) echo('TimeStart '.$I.': '.$TimeStart.'('.TimeToMysqlDateTime($TimeStart).')
'); $TimeEnd = $TimeFrom + (($TimeTo - $TimeFrom) / $DivisionCount) * ($I+1); //if($Debug) echo('TimeEnd '.$I.': '.$TimeEnd.'('.TimeToMysqlDateTime($TimeEnd).')
'); //echo($TimeStart.','.$TimeEnd.'
'); $EndIndex = $StartIndex; while($Values[$EndIndex]['time'] < $TimeEnd) $EndIndex = $EndIndex + 1; $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1); //echo($StartIndex.','.$EndIndex.' '.count($SubValues).'
'); //print_r($SubValues); $Points[] = ComputeOneValue($TimeStart, $TimeEnd, $SubValues, $Measure, $Level); $StartIndex = $EndIndex - 1; } } else $Points[] = array('min' => 0, 'avg' => 0, 'max' => 0); return($Points); } function RebuildMeasureCache($Measure) { global $MaxLevel, $LevelReducing, $Database; echo('Velicina '.$Measure['Name']."
\n"); if($Measure['Continuity'] == 0) $Measure['ContinuityEnabled'] = 0; // non continuous else $Measure['ContinuityEnabled'] = 2; // continuous graph // Clear previous items $DbResult = $Database->select($Measure['DataTable'], 'COUNT(*)', 'level>0 AND measure='.$Measure['Id']); $Row = $DbResult->fetch_row(); echo("Mazu starou cache (".$Row[0]." polozek)..."); $Database->delete($Measure['DataTable'], 'level>0 AND measure='.$Measure['Id']); echo("
\n"); for($Level=1; $Level <= $MaxLevel; $Level++) { echo('Uroven '.$Level."
\n"); $TimeRange = GetTimeRange($Measure, $Level - 1); //echo($Measure['Id'].','.($Level-1)."\n"); //echo(TimeToMysqlDateTime($TimeRange['left']).'-'.TimeToMysqlDateTime($TimeRange['right'])."\n"); $TimeSegment = TimeSegment($Measure['Period'], $Level); $StartTime = AlignTime($TimeRange['left'], $TimeSegment) - $TimeSegment; $EndTime = AlignTime($TimeRange['right'], $TimeSegment); $BurstCount = 500; echo('For 0 to '.round(($EndTime - $StartTime) / $TimeSegment / $BurstCount)."
\n"); for($I = 0; $I <= round(($EndTime - $StartTime) / $TimeSegment / $BurstCount); $I++) { echo($I.' '); $StartTime2 = $StartTime + $I * $BurstCount * $TimeSegment; $EndTime2 = $StartTime + ($I + 1) * $BurstCount * $TimeSegment; $Values = array(); $DbResult = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($StartTime2).'" AND time < "'.TimeToMysqlDateTime($EndTime2).'" AND measure='.$Measure['Id'].' AND level='.($Level - 1).' ORDER BY time'); while($Row = $DbResult->fetch_assoc()) { $Row['time'] = MysqlDateTimeToTime($Row['time']); $Values[] = $Row; } if(count($Values) > 0) { $Values = array_merge(LoadLeftSideValue($Level - 1, $Measure, $StartTime2), $Values, LoadRightSideValue($Level - 1, $Measure, $EndTime2)); $StartIndex = 0; for($B = 0; $B < $BurstCount; $B++) { echo('.'); $StartTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * $B; $EndTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * ($B + 1); $EndIndex = $StartIndex; while($Values[$EndIndex]['time'] < $EndTime3) $EndIndex = $EndIndex + 1; $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1); //echo($StartIndex.','.$EndIndex.' '.count($SubValues).'
'); //print_r($SubValues); if(count($SubValues) > 2) { $Point = ComputeOneValue($StartTime3, $EndTime3, $SubValues, $Measure, $Level); $Continuity = $SubValues[1]['continuity']; $Database->insert($Measure['DataTable'], array('level' => $Level, 'measure' => $Measure['Id'], 'min' => $Point['min'], 'avg' => $Point['avg'], 'max' => $Point['max'], 'continuity' => $Continuity, 'time' => TimeToMysqlDateTime($StartTime3 + ($EndTime3 - $StartTime3) / 2))); } $StartIndex = $EndIndex - 1; } } // Load values in time range //array_pop($NextValues); } echo("Uroven dokoncena
\n"); $DbResult = $Database->select($Measure['DataTable'], 'COUNT(*)', 'level='.$Level.' AND measure='.$Measure['Id']); $Row = $DbResult->fetch_row(); echo("Vloženo ".$Row[0]." položek.
\n"); } } function RebuildAllMeasuresCache() { global $Database; // echo("Vytvarim novou cache...\n"); // Load measures $Measures = array(); $Result = $Database->select('measure', '*'); while($Measures[] = $Result->fetch_assoc()); array_pop($Measures); foreach($Measures as $Measure) { RebuildMeasureCache($Measure); echo('Velicina dokoncena
'); } } function InitMeasureDataTable($Measure) { global $Database; $Database->query('CREATE TABLE `data_'.$Measure['Name'].'` ( `Time` TIMESTAMP NOT NULL , `Avg` '.$Measure['DataType'].' NOT NULL , `Continuity` BOOL NOT NULL ) ENGINE = MYISAM ;'); $Database->query('CREATE TABLE `data_'.$Measure['Name'].'_cache` ( `Time` TIMESTAMP NOT NULL , `Level` TINYINT NOT NULL , `Min` '.$Measure['DataType'].' NOT NULL , `Avg` '.$Measure['DataType'].' NOT NULL , `Max` '.$Measure['DataType'].' NOT NULL , `Continuity` BOOL NOT NULL ) ENGINE = MYISAM ;'); }