Changeset 2
- Timestamp:
- Oct 13, 2007, 5:41:08 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
stat_functions.php
r1 r2 1 <? 1 <?php 2 2 3 3 $DivisionCount = 500; … … 48 48 function GetMeasureById($Id) 49 49 { 50 DB_Select('stat_measure', '*', 'id='.$Id); 51 if(DB_NumRows() > 0) 52 { 53 $Measure = DB_Row(); 50 global $Database; 51 $Result = $Database->select('stat_measure', '*', 'id='.$Id); 52 if($Result->num_rows > 0) 53 { 54 $Measure = $Result->fetch_array(); 54 55 if($Measure['continuity'] == 0) $Measure['ContinuityEnabled'] = 0; // non continuous 55 56 else $Measure['ContinuityEnabled'] = 2; // continuous graph … … 60 61 function AddValue($Measure, $Value) 61 62 { 62 global $LevelReducing, $MaxLevel ;63 global $LevelReducing, $MaxLevel, $Database; 63 64 64 65 $Time = time(); 65 66 $Value = round($Measure['divider']*$Value); 66 67 67 DB_Select('stat_data', '*', 'measure='.$Measure['id'].' AND level=0 ORDER BY time DESC LIMIT 2');68 if( DB_NumRows() == 0) DB_Insert('stat_data', array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['id'], 'time' => TimeToMysqlDateTime($Time),68 $Result = $Databse->select('stat_data', '*', 'measure='.$Measure['id'].' AND level=0 ORDER BY time DESC LIMIT 2'); 69 if($Result->num_rows == 0) $Database->insert('stat_data', array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['id'], 'time' => TimeToMysqlDateTime($Time), 69 70 'continuity' => 0)); 70 else if( DB_NumRows() == 1) DB_Insert('stat_data', array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['id'], 'time' => TimeToMysqlDateTime($Time),71 else if($Result->num_rows == 1) $Database->insert('stat_data', array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['id'], 'time' => TimeToMysqlDateTime($Time), 71 72 'continuity' => 1)); 72 73 else { 73 $LastValue = DB_Row();74 $LastValue = $Result->fetch_array(); 74 75 $NextToLastValue = DB_Row(); 75 76 if(($Time - MysqlDateTimeToTime($LastValue['time'])) < 0.75 * $Measure['period']) … … 84 85 { 85 86 echo('s'); 86 DB_Update('stat_data', 'time="'.$LastValue['time'].'" AND level=0 AND measure='.$Measure['id'], array('time' => 'NOW()'));87 $Database->update('stat_data', 'time="'.$LastValue['time'].'" AND level=0 AND measure='.$Measure['id'], array('time' => 'NOW()')); 87 88 } else 88 89 { 89 DB_Insert('stat_data', array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['id'], 'time' => TimeToMysqlDateTime($Time),90 $Database->insert('stat_data', array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['id'], 'time' => TimeToMysqlDateTime($Time), 90 91 'continuity' => $Continuity)); 91 92 } … … 108 109 // Load values in time range 109 110 $Values = array(); 110 DB_Select(StatTableName($Level-1), '*', 'time > "'.TimeToMysqlDateTime($StartTime).'" AND time < "'.111 $Result = $Database->select(StatTableName($Level-1), '*', 'time > "'.TimeToMysqlDateTime($StartTime).'" AND time < "'. 111 112 TimeToMysqlDateTime($EndTime).'" AND measure='.$Measure['id'].' AND level='.($Level-1).' ORDER BY time'); 112 while($Row = DB_Row())113 while($Row = $Result->fetch_array()) 113 114 { 114 115 $Row['time'] = MysqlDateTimeToTime($Row['time']); … … 125 126 //print_r($Point); 126 127 127 DB_Delete(StatTableName($Level), '(time > "'.TimeToMysqlDateTime($StartTime).'") AND128 $Database->delete(StatTableName($Level), '(time > "'.TimeToMysqlDateTime($StartTime).'") AND 128 129 (time < "'.TimeToMysqlDateTime($EndTime).'") AND measure='.$Measure['id'].' AND level='.$Level); 129 130 $Continuity = $Values[1]['continuity']; 130 DB_Insert(StatTableName($Level), array('level' => $Level, 'measure' => $Measure['id'], 'min' => $Point['min'],131 $Database->insert(StatTableName($Level), array('level' => $Level, 'measure' => $Measure['id'], 'min' => $Point['min'], 131 132 'avg' => $Point['avg'], 'max' => $Point['max'], 'continuity' => $Continuity, 'time' => TimeToMysqlDateTime($StartTime+($EndTime-$StartTime)/2))); 132 133 … … 214 215 // Get first and last time 215 216 echo($Measure['id'].','.$Level.','.StatTableName($Level)."\n"); 216 DB_Select(StatTableName($Level), '*', 'measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time LIMIT 1');217 if( DB_NumRows()> 0)218 { 219 $Row = DB_Row();217 $Result = $Database->select(StatTableName($Level), '*', 'measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time LIMIT 1'); 218 if($Result->num_rows > 0) 219 { 220 $Row = $Result->fetch_array(); 220 221 $AbsoluteLeftTime = MysqlDateTimeToTime($Row['time']); 221 222 } else $AbsoluteLeftTime = 0; 222 223 223 DB_Select(StatTableName($Level), '*', 'measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1');224 if( DB_NumRows()> 0)225 { 226 $Row = DB_Row();224 $Result = $Database->select(StatTableName($Level), '*', 'measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1'); 225 if($Result->num_rows > 0) 226 { 227 $Row = $Result->fetch_array(); 227 228 $AbsoluteRightTime = MysqlDateTimeToTime($Row['time']); 228 229 } else $AbsoluteRightTime = 0; … … 240 241 global $Debug; 241 242 $Result = array(); 242 DB_Select(StatTableName($Level), '*', 'time > "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time ASC LIMIT 1');243 if( DB_NumRows()> 0)244 { 245 $Row = DB_Row();243 $Result = $Database->select(StatTableName($Level), '*', 'time > "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time ASC LIMIT 1'); 244 if($Result->num_rows > 0) 245 { 246 $Row = $Result->fetch_array(); 246 247 $Row['time'] = MysqlDateTimeToTime($Row['time']); 247 248 return(array($Row)); … … 252 253 //array_push($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0)); 253 254 $Result[] = array('time' => ($Time + TimeSegment($Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0); 254 DB_Select(StatTableName($Level), '*', 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1');255 if( DB_NumRows()> 0)256 { 257 $Row = DB_Row();255 $Result = $Database->select(StatTableName($Level), '*', 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1'); 256 if($Result->num_rows > 0) 257 { 258 $Row = $Result->fetch_array(); 258 259 array_unshift($Result, array('time' => (MysqlDateTimeToTime($Row['time'])+10), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0)); 259 260 } … … 268 269 $Result = array(); 269 270 //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 DB_Select(StatTableName($Level), '*', 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1');271 if( DB_NumRows()> 0)272 { 273 $Row = DB_Row();271 $Result = $Database->select(StatTableName($Level), '*', 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1'); 272 if($Result->num_rows > 0) 273 { 274 $Row = $Result->fetch_array(); 274 275 $Row['time'] = MysqlDateTimeToTime($Row['time']); 275 276 return(array($Row)); … … 281 282 $Result[] = array('time' => ($Time - TimeSegment($Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0); 282 283 283 DB_Select(StatTableName($Level), '*', 'time > "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time ASC LIMIT 1');284 if( DB_NumRows()> 0)285 { 286 $Row = DB_Row();284 $Result = $Database->select(StatTableName($Level), '*', 'time > "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time ASC LIMIT 1'); 285 if($Result->num_rows > 0) 286 { 287 $Row = $Result->fetch_array(); 287 288 array_push($Result, array('time' => (MysqlDateTimeToTime($Row['time'])-10), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0)); 288 289 } … … 306 307 307 308 // Load values in time range 308 DB_Select(StatTableName($Level), 'time, min, avg, max, continuity', 'time > "'.TimeToMysqlDateTime($TimeFrom).'" AND time < "'.309 $Result = $Database->select(StatTableName($Level), 'time, min, avg, max, continuity', 'time > "'.TimeToMysqlDateTime($TimeFrom).'" AND time < "'. 309 310 TimeToMysqlDateTime($TimeTo).'" AND measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time'); 310 311 // echo($Level.' '.TimeToMysqlDateTime($TimeFrom).' '.TimeToMysqlDateTime($TimeTo)); … … 312 313 // echo(DB_NumRows()); 313 314 // $III = 0; 314 while($Row = DB_Row())315 while($Row = $Result->fetch_array()) 315 316 { 316 317 // echo($III.' '.$Row['time'].' '.memory_get_usage().','); … … 362 363 363 364 // Clear previous items 364 DB_Select('stat_data_cache', 'COUNT(*)', 'level>0 AND measure='.$Measure['id']);365 $Row = DB_Row();365 $Result = $Database->select('stat_data_cache', 'COUNT(*)', 'level>0 AND measure='.$Measure['id']); 366 $Row = $Result->fetch_array(); 366 367 echo("Mazu starou cache (".$Row[0]." polozek)..."); 367 DB_Delete('stat_data_cache', 'level>0 AND measure='.$Measure['id']);368 $Database->delete('stat_data_cache', 'level>0 AND measure='.$Measure['id']); 368 369 echo("\n"); 369 370 … … 437 438 // Load measures 438 439 $Measures = array(); 439 DB_Select('stat_measure', '*');440 while($Measures[] = DB_Row());440 $Result = $Database->select('stat_measure', '*'); 441 while($Measures[] = $Result->fetch_array()); 441 442 array_pop($Measures); 442 443 … … 448 449 } 449 450 451 function InitMeasureDataTable($Measure) 452 { 453 global $Database; 454 $Database->query('CREATE TABLE `data_'.$Measure['Name'].'` ( 455 `Time` TIMESTAMP NOT NULL , 456 `Avg` '.$Measure['DataType'].' NOT NULL , 457 `Continuity` BOOL NOT NULL 458 ) ENGINE = MYISAM ;'); 459 460 $Database->query('CREATE TABLE `data_'.$Measure['Name'].'_cache` ( 461 `Time` TIMESTAMP NOT NULL , 462 `Level` TINYINT NOT NULL , 463 `Min` '.$Measure['DataType'].' NOT NULL , 464 `Avg` '.$Measure['DataType'].' NOT NULL , 465 `Max` '.$Measure['DataType'].' NOT NULL , 466 `Continuity` BOOL NOT NULL 467 ) ENGINE = MYISAM ;'); 468 } 469 450 470 ?>
Note:
See TracChangeset
for help on using the changeset viewer.