Changeset 2 for stat_functions.php


Ignore:
Timestamp:
Oct 13, 2007, 5:41:08 PM (17 years ago)
Author:
george
Message:

Přepracováno: Nově použití objektové třídy mysqli pro přístup k mysql databázi.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • stat_functions.php

    r1 r2  
    1 <?
     1<?php
    22
    33$DivisionCount = 500;
     
    4848function GetMeasureById($Id)
    4949{
    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();
    5455    if($Measure['continuity'] == 0) $Measure['ContinuityEnabled'] = 0;  // non continuous
    5556      else $Measure['ContinuityEnabled'] = 2;    // continuous graph 
     
    6061function AddValue($Measure, $Value)
    6162{
    62   global $LevelReducing, $MaxLevel;
     63  global $LevelReducing, $MaxLevel, $Database;
    6364 
    6465  $Time = time();
    6566  $Value = round($Measure['divider']*$Value);
    6667
    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),
    6970    '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),
    7172    'continuity' => 1));
    7273  else {
    73     $LastValue = DB_Row();
     74    $LastValue = $Result->fetch_array();
    7475    $NextToLastValue = DB_Row();
    7576    if(($Time - MysqlDateTimeToTime($LastValue['time'])) < 0.75 * $Measure['period'])
     
    8485      {
    8586        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()'));
    8788      } else
    8889      {
    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),
    9091          'continuity' => $Continuity));       
    9192      }
     
    108109      // Load values in time range
    109110      $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 < "'.
    111112        TimeToMysqlDateTime($EndTime).'" AND measure='.$Measure['id'].' AND level='.($Level-1).' ORDER BY time');
    112       while($Row = DB_Row())
     113      while($Row = $Result->fetch_array())
    113114      {
    114115        $Row['time'] = MysqlDateTimeToTime($Row['time']);
     
    125126      //print_r($Point);
    126127     
    127       DB_Delete(StatTableName($Level), '(time > "'.TimeToMysqlDateTime($StartTime).'") AND
     128      $Database->delete(StatTableName($Level), '(time > "'.TimeToMysqlDateTime($StartTime).'") AND
    128129        (time < "'.TimeToMysqlDateTime($EndTime).'") AND measure='.$Measure['id'].' AND level='.$Level);
    129130      $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'],
    131132        'avg' => $Point['avg'], 'max' => $Point['max'], 'continuity' => $Continuity, 'time' => TimeToMysqlDateTime($StartTime+($EndTime-$StartTime)/2)));
    132133
     
    214215  // Get first and last time
    215216  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();
    220221    $AbsoluteLeftTime = MysqlDateTimeToTime($Row['time']);
    221222  } else $AbsoluteLeftTime = 0;
    222223
    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();
    227228    $AbsoluteRightTime = MysqlDateTimeToTime($Row['time']);
    228229  } else $AbsoluteRightTime = 0;
     
    240241  global $Debug;
    241242  $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();
    246247    $Row['time'] = MysqlDateTimeToTime($Row['time']);
    247248    return(array($Row));
     
    252253    //array_push($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
    253254    $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();
    258259      array_unshift($Result, array('time' => (MysqlDateTimeToTime($Row['time'])+10), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
    259260    }
     
    268269  $Result = array();
    269270  //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();
    274275    $Row['time'] = MysqlDateTimeToTime($Row['time']);
    275276    return(array($Row));
     
    281282    $Result[] = array('time' => ($Time - TimeSegment($Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0);
    282283   
    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();
    287288      array_push($Result, array('time' => (MysqlDateTimeToTime($Row['time'])-10), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
    288289    }
     
    306307
    307308  // 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 < "'.
    309310    TimeToMysqlDateTime($TimeTo).'" AND measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time');
    310311//  echo($Level.' '.TimeToMysqlDateTime($TimeFrom).' '.TimeToMysqlDateTime($TimeTo));
     
    312313//  echo(DB_NumRows());
    313314//  $III = 0;
    314   while($Row = DB_Row())
     315  while($Row = $Result->fetch_array())
    315316  {
    316317//    echo($III.' '.$Row['time'].' '.memory_get_usage().',');
     
    362363
    363364  // 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();
    366367  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']);
    368369  echo("\n");
    369370
     
    437438    // Load measures
    438439  $Measures = array();
    439   DB_Select('stat_measure', '*');
    440   while($Measures[] = DB_Row());
     440  $Result = $Database->select('stat_measure', '*');
     441  while($Measures[] = $Result->fetch_array());
    441442  array_pop($Measures);
    442443
     
    448449}
    449450
     451function 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
    450470?>
Note: See TracChangeset for help on using the changeset viewer.