Changeset 65


Ignore:
Timestamp:
Jan 1, 2016, 4:06:20 PM (9 years ago)
Author:
chronos
Message:
  • Added: Comment to Measure methods.
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Measure.php

    r64 r65  
    1313  var $Differential;
    1414  var $DivisionCount;
     15  var $PeriodTolerance;
    1516
    1617  function __construct(Database $Database)
     
    2425    $this->Differential = 0;
    2526    $this->DivisionCount = 500;
     27    $this->PeriodTolerance = 0.25; // 25%
    2628  }
    2729
     
    6062    $Result = $this->Database->select($this->Data['DataTable'], '*', '(`measure`='.$this->Data['Id'].') AND '.
    6163      '(`level`=0) ORDER BY `time` DESC LIMIT 2');
    62     if($Result->num_rows == 0) $this->Database->insert($this->Data['DataTable'],
     64    if($Result->num_rows == 0) {
     65      // No measure value found. Simply insert new first value.
     66      $this->Database->insert($this->Data['DataTable'],
    6367      array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0,
    64       'measure' => $this->Data['Id'], 'time' => TimeToMysqlDateTime($Time), 'continuity' => 0));
    65     else if($Result->num_rows == 1) $this->Database->insert($this->Data['DataTable'],
     68      'measure' => $this->Data['Id'], 'time' => TimeToMysqlDateTime($Time),
     69      'continuity' => 0));
     70    } else if($Result->num_rows == 1) {
     71      // One value exists. Add second value.
     72      $this->Database->insert($this->Data['DataTable'],
    6673      array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0,
    6774      'measure' => $this->Data['Id'], 'time' => TimeToMysqlDateTime($Time),
    6875      'continuity' => 1));
    69     else {
     76    } else {
     77      // Two values already exist in measure table
    7078      $LastValue = $Result->fetch_array();
    7179      $NextToLastValue = $Result->fetch_array();
    72       if(($Time - MysqlDateTimeToTime($LastValue['time'])) < 0.75 * $this->Data['Period'])
    73       {
     80      if(($Time - MysqlDateTimeToTime($LastValue['time'])) < (1 - $this->PeriodTolerance) * $this->Data['Period'])
     81      {
     82        // New value added too quickly. Need to wait for next measure period.
    7483      }
    7584      else
    7685      {
    77         if(($Time - MysqlDateTimeToTime($LastValue['time'])) < 1.25 * $this->Data['Period']) $Continuity = 1;
    78           else $Continuity = 0;
     86        // We are near defined period and can add new value.
     87        if(($Time - MysqlDateTimeToTime($LastValue['time'])) < (1 + $this->PeriodTolerance) * $this->Data['Period']) {
     88          // New value added near defined measure period inside tolerance. Keep continuity.
     89          $Continuity = 1;
     90        } else {
     91          // New value added too late after defined measure period. Stop
     92          // continuity and let gap to be added.
     93          $Continuity = 0;
     94        }
    7995        if(($LastValue['avg'] == $NextToLastValue['avg']) and ($LastValue['avg'] == $Value) and
    8096          ($LastValue['continuity'] == 1) and ($Continuity == 1))
    8197        {
    82           $this->Database->update($this->Data['DataTable'], '(time="'.$LastValue['time'].'") AND '.
    83           '(level=0) AND (measure='.$this->Data['Id'].')', array('time' => TimeToMysqlDateTime($Time)));
     98          // New value is same as last value and continuity mode is enabled and
     99          // continuity flag is present. Just shift forward time for last value.
     100          $this->Database->update($this->Data['DataTable'], '(`time`="'.$LastValue['time'].'") AND '.
     101          '(`level`=0) AND (`measure`='.$this->Data['Id'].')', array('time' => TimeToMysqlDateTime($Time)));
    84102        } else
    85103        {
     104          // Last value is different or not with continuity flag. Need to add new value.
    86105          $this->Database->insert($this->Data['DataTable'], array('min' => $Value,
    87106            'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $this->Data['Id'], 'time' => TimeToMysqlDateTime($Time),
     
    91110    }
    92111
    93     // Update levels
     112    // Update higher levels
    94113    for($Level = 1; $Level <= $this->MaxLevel; $Level++)
    95114    {
     
    101120      // Load values in time range
    102121      $Values = array();
    103       $Result = $this->Database->select($this->Data['DataTable'], '*', '(`time` > "'.TimeToMysqlDateTime($StartTime).'") AND '.
     122      $Result = $this->Database->select($this->Data['DataTable'], '*', '(`time` > "'.
     123        TimeToMysqlDateTime($StartTime).'") AND '.
    104124        '(`time` < "'.TimeToMysqlDateTime($EndTime).'") AND '.
    105125        '(`measure`='.$this->Data['Id'].') AND (`level`='.($Level - 1).') ORDER BY `time`');
     
    115135        $this->LoadLeftSideValue($Level - 1, $StartTime),
    116136        $Values,
    117         $this->LoadRightSideValue($Level - 1, $EndTime));
     137        $this->LoadRightSideValue($Level - 1, $EndTime)
     138      );
    118139
    119140      $Point = $this->ComputeOneValue($StartTime, $EndTime, $Values, $Level);
    120141
    121       $this->Database->delete($this->Data['DataTable'], '(time > "'.TimeToMysqlDateTime($StartTime).'") AND
    122         (time < "'.TimeToMysqlDateTime($EndTime).'") AND measure='.$this->Data['Id'].' AND level='.$Level);
     142      $this->Database->delete($this->Data['DataTable'], '(`time` > "'.TimeToMysqlDateTime($StartTime).'") AND
     143        (`time` < "'.TimeToMysqlDateTime($EndTime).'") AND (`measure`='.$this->Data['Id'].') '.
     144        'AND (`level`='.$Level.')');
    123145      $Continuity = $Values[1]['continuity'];
    124       $this->Database->insert($this->Data['DataTable'], array('level' => $Level, 'measure' => $this->Data['Id'], 'min' => $Point['min'],
     146      $this->Database->insert($this->Data['DataTable'], array('level' => $Level,
     147        'measure' => $this->Data['Id'], 'min' => $Point['min'],
    125148        'avg' => $Point['avg'], 'max' => $Point['max'], 'continuity' => $Continuity,
    126149        'time' => TimeToMysqlDateTime($StartTime + ($EndTime - $StartTime) / 2)));
    127 
    128150    }
    129151  }
     
    137159    foreach($this->ValueTypes as $ValueType)
    138160    {
    139       $Values[0][$ValueType] = Interpolation(NewPoint($Values[0]['time'], $Values[0][$ValueType]),
     161      $Values[0][$ValueType] = Interpolation(
     162        NewPoint($Values[0]['time'], $Values[0][$ValueType]),
    140163        NewPoint($Values[1]['time'], $Values[1][$ValueType]), $LeftTime);
    141164    }
     
    143166    foreach($this->ValueTypes as $ValueType)
    144167    {
    145       $Values[count($Values) - 1][$ValueType] = Interpolation(NewPoint($Values[count($Values) - 2]['time'],
    146         $Values[count($Values) - 2][$ValueType]),
     168      $Values[count($Values) - 1][$ValueType] = Interpolation(
     169        NewPoint($Values[count($Values) - 2]['time'], $Values[count($Values) - 2][$ValueType]),
    147170        NewPoint($Values[count($Values) - 1]['time'], $Values[count($Values) - 1][$ValueType]),
    148171        $RightTime);
     
    233256  }
    234257
     258  // Load one nearest value newer then given time
    235259  function LoadRightSideValue($Level, $Time)
    236260  {
    237261    $Result = array();
    238     $DbResult = $this->Database->select($this->Data['DataTable'], '*', '(time > "'.TimeToMysqlDateTime($Time).'") AND (measure='.$this->Data['Id'].') AND (level='.$Level.') ORDER BY time ASC LIMIT 1');
     262    $DbResult = $this->Database->select($this->Data['DataTable'], '*',
     263      '(`time` > "'.TimeToMysqlDateTime($Time).'") AND (`measure`='.
     264      $this->Data['Id'].') AND (`level`='.$Level.') ORDER BY `time` ASC LIMIT 1');
    239265    if($DbResult->num_rows > 0)
    240266    {
     267      // Found one value, simply return it
    241268      $Row = $DbResult->fetch_array();
    242269      $Row['time'] = MysqlDateTimeToTime($Row['time']);
    243270      return(array($Row));
    244     }
    245     else
    246     {
    247       //$Time = $Values[count($Values)-1]['time'] + 60;
     271    } else
     272    {
     273      // Not found any value. Calculate it
     274      //$Time = $Values[count($Values) - 1]['time'] + 60;
    248275      //array_push($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
    249276      $Result[] = array('time' => ($Time + $this->TimeSegment($Level)), 'min' => 0,
     
    262289  }
    263290
     291  // Load one nearest value older then given time
    264292  function LoadLeftSideValue($Level, $Time)
    265293  {
     
    270298    if($DbResult->num_rows > 0)
    271299    {
     300      // Found one value, simply return it
    272301      $Row = $DbResult->fetch_array();
    273302      $Row['time'] = MysqlDateTimeToTime($Row['time']);
    274303      return(array($Row));
    275304    } else {
     305      // Not found any value. Calculate it
    276306      //$Time = $Values[0]['time'] - 60;
    277307      //array_unshift($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
     
    301331
    302332    // Load values in time range
    303     $Result = $this->Database->select($this->Data['DataTable'], '`time`, `min`, `avg`, `max`, `continuity`',
     333    $Result = $this->Database->select($this->Data['DataTable'], '`time`, `min`, '.
     334      '`avg`, `max`, `continuity`',
    304335      '(`time` > "'.TimeToMysqlDateTime($TimeFrom).'") AND '.
    305336      '(`time` < "'.TimeToMysqlDateTime($TimeTo).'") AND '.
     
    308339    while($Row = $Result->fetch_array())
    309340    {
    310       $Values[] = array('time' => MysqlDateTimeToTime($Row['time']), 'min' => $Row['min'], 'avg' => $Row['avg'], 'max' => $Row['max'], 'continuity' => $Row['continuity']);
     341      $Values[] = array('time' => MysqlDateTimeToTime($Row['time']),
     342        'min' => $Row['min'], 'avg' => $Row['avg'], 'max' => $Row['max'],
     343        'continuity' => $Row['continuity']);
    311344    }
    312345   // array_pop($Values);
     
    325358      {
    326359        $TimeStart = $TimeFrom + (($TimeTo - $TimeFrom) / $this->DivisionCount) * $I;
    327         $TimeEnd = $TimeFrom + (($TimeTo - $TimeFrom) / $this->DivisionCount) * ($I+1);
     360        $TimeEnd = $TimeFrom + (($TimeTo - $TimeFrom) / $this->DivisionCount) * ($I + 1);
    328361
    329362        $EndIndex = $StartIndex;
  • trunk/Version.php

    r64 r65  
    11<?php
    22
    3 $Revision = 64;
     3$Revision = 65;
    44$ReleaseTime = strtotime('2016-01-01');
Note: See TracChangeset for help on using the changeset viewer.