Changeset 6 for stat_functions.php


Ignore:
Timestamp:
Oct 16, 2007, 10:12:26 PM (17 years ago)
Author:
george
Message:

Přepracovaná vývojová verze s oddělenými datovými tabulkami a různými datovými typy.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • stat_functions.php

    r2 r6  
    11<?php
    2 
    3 $DivisionCount = 500;
    4 $ReferenceTime = 0;
    5 $LevelReducing = 5;
    6 $MaxLevel = 4;
    72
    83$ValueTypes = array('min', 'avg', 'max');
     
    3631function StatTableName($Level)
    3732{
    38   if($Level == 0) return('stat_data');
    39     else return('stat_data_cache');
     33  if($Level == 0) return('data');
     34    else return('data_cache');
    4035}
    4136
     
    4944{
    5045  global $Database;
    51   $Result = $Database->select('stat_measure', '*', 'id='.$Id);
     46  $Result = $Database->select('measure', '*', 'Id='.$Id);
    5247  if($Result->num_rows > 0)
    5348  {
    5449    $Measure = $Result->fetch_array();
    55     if($Measure['continuity'] == 0) $Measure['ContinuityEnabled'] = 0;  // non continuous
     50    if($Measure['Continuity'] == 0) $Measure['ContinuityEnabled'] = 0;  // non continuous
    5651      else $Measure['ContinuityEnabled'] = 2;    // continuous graph 
    5752  } else die('Mìøená velièina nenalezena');
     
    6459 
    6560  $Time = time();
    66   $Value = round($Measure['divider']*$Value);
    67 
    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),
     61  //$Value = round($Measure['divider'] * $Value);
     62
     63  $Result = $Databse->select($Measure['DataTable'], '*', 'measure='.$Measure['Id'].' AND level=0 ORDER BY time DESC LIMIT 2');
     64  if($Result->num_rows == 0) $Database->insert('stat_data', array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time),
    7065    'continuity' => 0));
    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),
     66  else if($Result->num_rows == 1) $Database->insert($Measure['DataTable'], array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time),
    7267    'continuity' => 1));
    7368  else {
     
    8580      {
    8681        echo('s');
    87         $Database->update('stat_data', 'time="'.$LastValue['time'].'" AND level=0 AND measure='.$Measure['id'], array('time' => 'NOW()'));
     82        $Database->update($Measure['DataTable'], 'time="'.$LastValue['time'].'" AND level=0 AND measure='.$Measure['Id'], array('time' => 'NOW()'));
    8883      } else
    8984      {
    90         $Database->insert('stat_data', array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['id'], 'time' => TimeToMysqlDateTime($Time),
     85        $Database->insert($Measure['DataTable'], array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time),
    9186          'continuity' => $Continuity));       
    9287      }
     
    109104      // Load values in time range
    110105      $Values = array();
    111       $Result = $Database->select(StatTableName($Level-1), '*', 'time > "'.TimeToMysqlDateTime($StartTime).'" AND time < "'.
    112         TimeToMysqlDateTime($EndTime).'" AND measure='.$Measure['id'].' AND level='.($Level-1).' ORDER BY time');
     106      $Result = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($StartTime).'" AND time < "'.
     107        TimeToMysqlDateTime($EndTime).'" AND measure='.$Measure['Id'].' AND level='.($Level-1).' ORDER BY time');
    113108      while($Row = $Result->fetch_array())
    114109      {
     
    126121      //print_r($Point);
    127122     
    128       $Database->delete(StatTableName($Level), '(time > "'.TimeToMysqlDateTime($StartTime).'") AND
    129         (time < "'.TimeToMysqlDateTime($EndTime).'") AND measure='.$Measure['id'].' AND level='.$Level);
     123      $Database->delete($Measure['DataTable'], '(time > "'.TimeToMysqlDateTime($StartTime).'") AND
     124        (time < "'.TimeToMysqlDateTime($EndTime).'") AND measure='.$Measure['Id'].' AND level='.$Level);
    130125      $Continuity = $Values[1]['continuity'];   
    131       $Database->insert(StatTableName($Level), array('level' => $Level, 'measure' => $Measure['id'], 'min' => $Point['min'],
     126      $Database->insert($DataTable, array('level' => $Level, 'measure' => $Measure['Id'], 'min' => $Point['min'],
    132127        'avg' => $Point['avg'], 'max' => $Point['max'], 'continuity' => $Continuity, 'time' => TimeToMysqlDateTime($StartTime+($EndTime-$StartTime)/2)));
    133128
     
    203198  }
    204199  //if(($RightTime - $LeftTime) > 0)
    205   if($Measure['cumulative'] == 0) $NewValue['avg'] = $NewValue['avg'] / ($RightTime - $LeftTime); 
     200  if($Measure['Cumulative'] == 0) $NewValue['avg'] = $NewValue['avg'] / ($RightTime - $LeftTime); 
    206201  return($NewValue);
    207202  //echo($NewValue['avg'].'<br>');
     
    214209 
    215210  // Get first and last time
    216   echo($Measure['id'].','.$Level.','.StatTableName($Level)."\n");
    217   $Result = $Database->select(StatTableName($Level), '*', 'measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time LIMIT 1');
     211  echo($Measure['Id'].','.$Level.','.StatTableName($Level)."\n");
     212  $Result = $Database->select($Measure['DataTable'], '*', 'measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time LIMIT 1');
    218213  if($Result->num_rows > 0)
    219214  {
     
    222217  } else $AbsoluteLeftTime = 0;
    223218
    224   $Result = $Database->select(StatTableName($Level), '*', 'measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1');
     219  $Result = $Database->select($Measure['DataTable'], '*', 'measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1');
    225220  if($Result->num_rows > 0)
    226221  {
     
    239234function LoadRightSideValue($Level, $Measure, $Time)
    240235{
    241   global $Debug;
     236  global $Debug, $Database;
    242237  $Result = array();
    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();
     238  $DbResult = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time ASC LIMIT 1');
     239  if($DbResult->num_rows > 0)
     240  {
     241    $Row = $DbResult->fetch_array();
    247242    $Row['time'] = MysqlDateTimeToTime($Row['time']);
    248243    return(array($Row));
     
    253248    //array_push($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
    254249    $Result[] = array('time' => ($Time + TimeSegment($Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0);
    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();
     250    $DbResult = $Database->select($Measure['DataTable'], '*', 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1');
     251    if($DbResult->num_rows > 0)
     252    {
     253      $Row = $DbResult->fetch_array();
    259254      array_unshift($Result, array('time' => (MysqlDateTimeToTime($Row['time'])+10), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
    260255    }
     
    266261function LoadLeftSideValue($Level, $Measure, $Time)
    267262{
    268   global $Debug;
     263  global $Debug, $Database;
    269264  $Result = array();
    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");
    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();
     265  //echo('SELECT * FROM '.StatTableName($Level). ' WHERE '. 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1'."<br>\n");
     266  $DbResult = $Database->select($Measure['DataTable'], '*', 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1');
     267  if($DbResult->num_rows > 0)
     268  {
     269    $Row = $DbResult->fetch_array();
    275270    $Row['time'] = MysqlDateTimeToTime($Row['time']);
    276271    return(array($Row));
     
    282277    $Result[] = array('time' => ($Time - TimeSegment($Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0);
    283278   
    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();
     279    $DbResult = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time ASC LIMIT 1');
     280    if($DbResult->num_rows > 0)
     281    {
     282      $Row = $DbResult->fetch_array();
    288283      array_push($Result, array('time' => (MysqlDateTimeToTime($Row['time'])-10), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
    289284    }
     
    295290function GetValues($Measure, $TimeFrom, $TimeTo, $Level)
    296291{
    297   global $DivisionCount, $Debug;
     292  global $DivisionCount, $Debug, $Database;
    298293 
    299294  if($Debug) echo('TimeFrom: '.$TimeFrom.'('.TimeToMysqlDateTime($TimeFrom).')<br>');
     
    307302
    308303  // Load values in time range
    309   $Result = $Database->select(StatTableName($Level), 'time, min, avg, max, continuity', 'time > "'.TimeToMysqlDateTime($TimeFrom).'" AND time < "'.
    310     TimeToMysqlDateTime($TimeTo).'" AND measure='.$Measure['id'].' AND level='.$Level.' ORDER BY time');
     304  $Result = $Database->select($Measure['DataTable'], 'time, min, avg, max, continuity', 'time > "'.TimeToMysqlDateTime($TimeFrom).'" AND time < "'.
     305    TimeToMysqlDateTime($TimeTo).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time');
    311306//  echo($Level.' '.TimeToMysqlDateTime($TimeFrom).' '.TimeToMysqlDateTime($TimeTo));
    312307  $Values = array();
     
    334329    $StartIndex = 0;
    335330    $Points = array();
     331    //echo($DivisionCount.'<br>');
    336332    for($I = 0; $I < $DivisionCount; $I++)
    337333    {
     
    358354  global $MaxLevel, $LevelReducing;
    359355
    360   echo('Velicina '.$Measure['name']."\n");
    361   if($Measure['continuity'] == 0) $Measure['ContinuityEnabled'] = 0;  // non continuous
     356  echo('Velicina '.$Measure['Name']."\n");
     357  if($Measure['Continuity'] == 0) $Measure['ContinuityEnabled'] = 0;  // non continuous
    362358    else $Measure['ContinuityEnabled'] = 2;    // continuous graph
    363359
    364360  // Clear previous items
    365   $Result = $Database->select('stat_data_cache', 'COUNT(*)', 'level>0 AND measure='.$Measure['id']);
     361  $Result = $Database->select($Measure['DataTable'], 'COUNT(*)', 'level>0 AND measure='.$Measure['Id']);
    366362  $Row = $Result->fetch_array();
    367363  echo("Mazu starou cache (".$Row[0]." polozek)...");
    368   $Database->delete('stat_data_cache', 'level>0 AND measure='.$Measure['id']);
     364  $Database->delete($Measure['DataTable'], 'level>0 AND measure='.$Measure['Id']);
    369365  echo("\n");
    370366
     
    373369    echo('Uroven '.$Level."\n");
    374370    $TimeRange = GetTimeRange($Measure, $Level-1);
    375     //echo($Measure['id'].','.($Level-1)."\n");
     371    //echo($Measure['Id'].','.($Level-1)."\n");
    376372    //echo(TimeToMysqlDateTime($TimeRange['left']).'-'.TimeToMysqlDateTime($TimeRange['right'])."\n");
    377373    $TimeSegment = TimeSegment($Level);
     
    386382      $EndTime2 = $StartTime + ($I+1) * $BurstCount * $TimeSegment;
    387383      $Values = array();
    388       DB_Select(StatTableName($Level-1), '*', 'time > "'.TimeToMysqlDateTime($StartTime2).'" AND time < "'.
    389         TimeToMysqlDateTime($EndTime2).'" AND measure='.$Measure['id'].' AND level='.($Level-1).' ORDER BY time');
     384      DB_Select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($StartTime2).'" AND time < "'.
     385        TimeToMysqlDateTime($EndTime2).'" AND measure='.$Measure['Id'].' AND level='.($Level-1).' ORDER BY time');
    390386      while($Row = DB_Row())
    391387      {
     
    414410            $Point = ComputeOneValue($StartTime3, $EndTime3, $SubValues, $Measure, $Level);
    415411            $Continuity = $SubValues[1]['continuity'];   
    416             DB_Insert(StatTableName($Level), array('level' => $Level, 'measure' => $Measure['id'], 'min' => $Point['min'],
     412            DB_Insert($Measure['DataTable'], array('level' => $Level, 'measure' => $Measure['Id'], 'min' => $Point['min'],
    417413              'avg' => $Point['avg'], 'max' => $Point['max'], 'continuity' => $Continuity, 'time' => TimeToMysqlDateTime($StartTime3+($EndTime3-$StartTime3)/2)));
    418414          }       
     
    424420    } 
    425421    echo("Uroven dokoncena\n");         
    426     DB_Select('stat_data', 'COUNT(*)', 'level='.$Level.' AND measure='.$Measure['id']);
     422    DB_Select($Measure['DataTable'], 'COUNT(*)', 'level='.$Level.' AND measure='.$Measure['Id']);
    427423    $Row = DB_Row();
    428424    echo("Vlozeno ".$Row[0]." polozek.\n");
     
    438434    // Load measures
    439435  $Measures = array();
    440   $Result = $Database->select('stat_measure', '*');
     436  $Result = $Database->select('measure', '*');
    441437  while($Measures[] = $Result->fetch_array());
    442438  array_pop($Measures);
Note: See TracChangeset for help on using the changeset viewer.