Ignore:
Timestamp:
Mar 1, 2015, 12:43:56 PM (10 years ago)
Author:
chronos
Message:
  • Moved trunk as mvc branch.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/old/stat_functions.php

    r56 r59  
    7979
    8080    // Update levels
    81     //echo($Time."<br>\n");
    8281    for($Level = 1; $Level <= $MaxLevel; $Level++)
    8382    {
    84       //echo('Level '.$Level."<br>\n");
    8583      $TimeSegment = TimeSegment($Level);
    8684      $EndTime = AlignTime($Time, $TimeSegment);
    8785      //if($EndTime < $Time) $EndTime = $EndTime + $TimeSegment;
    8886      $StartTime = $EndTime - $TimeSegment;
    89      
    90       //echo(" ".$TimeSegment." ".$StartTime.'-'.$EndTime."<br>\n");
    91       //flush();
    92  
     87
    9388      // Load values in time range
    9489      $Values = array();
     
    10095        $Values[] = $Row;
    10196      }
    102       //print_r($Values);
    10397      //array_pop($Values);     
    10498
     
    107101
    108102      $Point = ComputeOneValue($StartTime, $EndTime, $Values, $Measure, $Level);
    109       //print_r($Point);
    110103
    111104      $Database->delete($Measure['DataTable'], '(time > "'.TimeToMysqlDateTime($StartTime).'") AND
     
    121114{
    122115  $Y = ($Y2 - $Y1) / ($X2 - $X1) * ($X - $X1) + $Y1;
    123   //echo($Y1.'-'.$Y.'-'.$Y2.' '.$X1.'-'.$X.'-'.$X2.'<br>');
    124116  return($Y);
    125117}
    126118
     119/* Compute one value for upper time level from multiple values */
    127120function ComputeOneValue($LeftTime, $RightTime, $Values, $Measure, $Level)
    128121{
    129122  global $ValueTypes, $Differential;
    130  
     123
    131124  $NewValue = array('min' => +1000000000000000000, 'avg' => 0, 'max' => -1000000000000000000);
    132  
     125
    133126  // Trim outside parts
    134127  foreach($ValueTypes as $ValueType)
    135128  {
    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);
    137131  }
    138132  $Values[0]['time'] = $LeftTime;
    139133  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
    147142  foreach($ValueTypes as $ValueType)
    148   { 
     143  {
    149144    // Compute new value
    150145    for($I = 0; $I < (count($Values) - 1); $I++)
    151     {     
     146    {
    152147      if($ValueType == 'avg')
    153148      {
    154         if($Values[$I+1]['continuity'] == $Measure['ContinuityEnabled']);
     149        if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled']);
    155150          else if($Differential == 0)
    156151          {
    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]);
    159154          } 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);
    162157          }
    163158      }
    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'])
    167162        {
    168163          if(0 > $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
    169164        }
    170         else 
     165        else
    171166        {
    172167          if($Differential == 0)
    173168          {
    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];
    175170          } else {
    176             $Difference = $Values[$I+1][$ValueType] - $Values[$I][$ValueType];
     171            $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType];
    177172            if($Difference > $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
    178           } 
     173          }
    179174        }
    180175      }
    181176      else if($ValueType == 'min')
    182177      {
    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'])
    185179        {
    186180          if(0 < $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
     
    188182          if($Differential == 0)
    189183          {
    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];
    191185          } else {
    192             $Difference = $Values[$I+1][$ValueType] - $Values[$I][$ValueType];
     186            $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType];
    193187            if($Difference < $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
    194188          }
     
    196190      }
    197191    }
    198     $NewValue[$ValueType] = $NewValue[$ValueType]; 
     192    $NewValue[$ValueType] = $NewValue[$ValueType];
    199193  }
    200194  //if(($RightTime - $LeftTime) > 0)
    201   if($Measure['Cumulative'] == 0) 
     195  if($Measure['Cumulative'] == 0)
    202196  {
    203197    $NewValue['avg'] = $NewValue['avg'] / ($RightTime - $LeftTime);
    204   } 
     198  }
    205199  return($NewValue);
    206   //echo($NewValue['avg'].'<br>');
    207   //return(array('min' => rand(0,1), 'avg' => $NewValue['avg'], 'max' => rand(0,1)));
    208200}
    209201
     
    213205
    214206  // Get first and last time
    215   //echo($Measure['Id'].','.$Level.','.StatTableName($Level)."\n");
    216207  $Result = $Database->select($Measure['DataTable'], '*', 'measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time LIMIT 1');
    217208  if($Result->num_rows > 0)
     
    239230{
    240231  global $Debug, $Database;
     232 
    241233  $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');
    243235  if($DbResult->num_rows > 0)
    244236  {
     
    252244    //array_push($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
    253245    $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');
    255247    if($DbResult->num_rows > 0)
    256248    {
     
    266258{
    267259  global $Debug, $Database;
     260
    268261  $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');
    271263  if($DbResult->num_rows > 0)
    272264  {
     
    279271    $Result[] = array('time' => ($Time - TimeSegment($Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0);
    280272
    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');
    282274    if($DbResult->num_rows > 0)
    283275    {
    284276      $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    }
    288280    return($Result);
    289281  }
     
    306298  $Result = $Database->select($Measure['DataTable'], 'time, min, avg, max, continuity', 'time > "'.TimeToMysqlDateTime($TimeFrom).'" AND time < "'.
    307299    TimeToMysqlDateTime($TimeTo).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time');
    308 //  echo($Level.' '.TimeToMysqlDateTime($TimeFrom).' '.TimeToMysqlDateTime($TimeTo));
    309300  $Values = array();
    310 //  echo(DB_NumRows());
    311 //  $III = 0;
    312301  while($Row = $Result->fetch_array())
    313302  {
    314 //    echo($III.' '.$Row['time'].' '.memory_get_usage().',');
    315 //    $III++;
    316303    $Values[] = array('time' => MysqlDateTimeToTime($Row['time']), 'min' => $Row['min'], 'avg' => $Row['avg'], 'max' => $Row['max'], 'continuity' => $Row['continuity']);
    317304  }
    318305 // array_pop($Values);
    319 //  echo('abc');
    320 //  die();
    321306  if($Debug) echo('Item count: '.count($Values));
    322  
     307
    323308  $Points = array();
    324309  if(count($Values) > 0)
     
    326311    $Values = array_merge(LoadLeftSideValue($Level, $Measure, $TimeFrom), $Values, LoadRightSideValue($Level, $Measure, $TimeTo));
    327312
    328     //echo(count($Values).'<br>');
    329     //echo($TimeFrom.','.$TimeTo.'<br>');
    330     //echo($Values[0]['time'].'<br>');
    331313    $StartIndex = 0;
    332314    $Points = array();
    333     //echo($DivisionCount.'<br>');
    334315    for($I = 0; $I < $DivisionCount; $I++)
    335316    {
     
    338319      $TimeEnd = $TimeFrom + (($TimeTo - $TimeFrom) / $DivisionCount) * ($I+1);
    339320      //if($Debug) echo('TimeEnd '.$I.': '.$TimeEnd.'('.TimeToMysqlDateTime($TimeEnd).')<br>');
    340       //echo($TimeStart.','.$TimeEnd.'<br>');
    341321
    342322      $EndIndex = $StartIndex;
    343323      while($Values[$EndIndex]['time'] < $TimeEnd) $EndIndex = $EndIndex + 1;
    344324      $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
    345       //echo($StartIndex.','.$EndIndex.' '.count($SubValues).'<br>');
    346       //print_r($SubValues);
    347325      $Points[] = ComputeOneValue($TimeStart, $TimeEnd, $SubValues, $Measure, $Level);
    348326      $StartIndex = $EndIndex - 1; 
     
    371349    echo('Uroven '.$Level."<br>\n");
    372350    $TimeRange = GetTimeRange($Measure, $Level-1);
    373     //echo($Measure['Id'].','.($Level-1)."\n");
    374     //echo(TimeToMysqlDateTime($TimeRange['left']).'-'.TimeToMysqlDateTime($TimeRange['right'])."\n");
    375351    $TimeSegment = TimeSegment($Level);
    376352    $StartTime = AlignTime($TimeRange['left'], $TimeSegment) - $TimeSegment;
     
    382358      echo($I.' ');
    383359      $StartTime2 = $StartTime + $I * $BurstCount * $TimeSegment;
    384       $EndTime2 = $StartTime + ($I+1) * $BurstCount * $TimeSegment;
     360      $EndTime2 = $StartTime + ($I + 1) * $BurstCount * $TimeSegment;
    385361      $Values = array();
    386       $DbResult = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($StartTime2).'" AND time < "'.
    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');
    388364      while($Row = $DbResult->fetch_array())
    389365      {
     
    394370      if(count($Values) > 0)
    395371      {
    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));
    397373
    398374        $StartIndex = 0;
     
    401377          echo('.');
    402378          $StartTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * $B;
    403           $EndTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * ($B+1);
     379          $EndTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * ($B + 1);
    404380
    405381          $EndIndex = $StartIndex;
    406382          while($Values[$EndIndex]['time'] < $EndTime3) $EndIndex = $EndIndex + 1;
    407383          $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)
    411385          {
    412386            $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)));
    416391          }
    417392          $StartIndex = $EndIndex - 1;
     
    425400    $Row = $DbResult->fetch_array();
    426401    echo("Vlozeno ".$Row[0]." polozek.<br>\n");
    427   } 
    428 }   
     402  }
     403}
    429404
    430405function RebuildAllMeasuresCache()
     
    432407  global $Database;
    433408
    434 //  echo("Vytvarim novou cache...\n");
    435     // Load measures
     409  // Load measures
    436410  $Measures = array();
    437411  $Result = $Database->select('measure', '*');
Note: See TracChangeset for help on using the changeset viewer.