Changeset 71


Ignore:
Timestamp:
Jul 8, 2016, 11:11:07 PM (8 years ago)
Author:
chronos
Message:
  • Added: Allow add new values with specified Time parameter.
  • Modified: Use database table columns with names with first capital letter.
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Application/Version.php

    r69 r71  
    66// and system will need database update.
    77
    8 $Revision = 67;
     8$Revision = 71;
    99$DatabaseRevision = 67;
    10 $ReleaseTime = strtotime('2016-02-28');
     10$ReleaseTime = strtotime('2016-06-30');
  • trunk/Graph.php

    r69 r71  
    6565    foreach($Points as $Index => $Item)
    6666    {
    67       //$Points[$Index]['min'] =  $Points[$Index]['min'] / $Measure['Divider'];
    68       //$Points[$Index]['avg'] =  $Points[$Index]['avg'] / $Measure['Divider'];
    69       //$Points[$Index]['max'] =  $Points[$Index]['max'] / $Measure['Divider'];
    70       if($Points[$Index]['avg'] > $MaxValue) $MaxValue = $Points[$Index]['avg'];
    71       if($Points[$Index]['avg'] < $MinValue) $MinValue = $Points[$Index]['avg'];
    72       if($Points[$Index]['max'] > $MaxValue) $MaxValue = $Points[$Index]['max'];
    73       if($Points[$Index]['min'] < $MinValue) $MinValue = $Points[$Index]['min'];
    74       $AvgValue = $AvgValue + $Points[$Index]['avg'];
     67      //$Points[$Index]['Min'] =  $Points[$Index]['Min'] / $Measure['Divider'];
     68      //$Points[$Index]['Avg'] =  $Points[$Index]['Avg'] / $Measure['Divider'];
     69      //$Points[$Index]['Max'] =  $Points[$Index]['Max'] / $Measure['Divider'];
     70      if($Points[$Index]['Avg'] > $MaxValue) $MaxValue = $Points[$Index]['Avg'];
     71      if($Points[$Index]['Avg'] < $MinValue) $MinValue = $Points[$Index]['Avg'];
     72      if($Points[$Index]['Max'] > $MaxValue) $MaxValue = $Points[$Index]['Max'];
     73      if($Points[$Index]['Min'] < $MinValue) $MinValue = $Points[$Index]['Min'];
     74      $AvgValue = $AvgValue + $Points[$Index]['Avg'];
    7575    }
    7676    //$MinValue = round($MinValue * $Measure['Divider']) / $Measure['Divider'];
     
    8787      {
    8888        $PointsMin[] = $Index * $Width / $Measure->DivisionCount;
    89         $PointsMin[] = $Height - 1 - ($Points[$Index]['min'] - $MinValue) /
     89        $PointsMin[] = $Height - 1 - ($Points[$Index]['Min'] - $MinValue) /
    9090          ($MaxValue - $MinValue) * $Height * $this->ValueToImageHeigthCoefficient;
    9191        $PointsAvg[] = $Index * $Width / $Measure->DivisionCount;
    92         $PointsAvg[] = $Height - 1 - ($Points[$Index]['avg'] - $MinValue) /
     92        $PointsAvg[] = $Height - 1 - ($Points[$Index]['Avg'] - $MinValue) /
    9393          ($MaxValue - $MinValue) * $Height * $this->ValueToImageHeigthCoefficient;
    9494        $PointsMax[] = $Index * $Width / $Measure->DivisionCount;
    95         $PointsMax[] = $Height - 1 - ($Points[$Index]['max'] - $MinValue) /
     95        $PointsMax[] = $Height - 1 - ($Points[$Index]['Max'] - $MinValue) /
    9696          ($MaxValue - $MinValue) * $Height * $this->ValueToImageHeigthCoefficient;
    9797      }
  • trunk/Modules/Measure/Measure.php

    r70 r71  
    2020    $this->System->RegisterPage('', 'PageMain');
    2121  }
    22  
     22
    2323  function DoInstall()
    2424  {
     
    3131  `Continuity` tinyint(1) NOT NULL default 0,
    3232  `Period` int(11) NOT NULL default 60,
    33   `OldName` varchar(32) collate utf8_general_ci NOT NULL,
    3433  `PermissionView` varchar(255) collate utf8_general_ci NOT NULL default "all",
    3534  `PermissionAdd` varchar(255) collate utf8_general_ci NOT NULL default "localhost.localdomain",
     
    4241) ENGINE=InnoDB  DEFAULT CHARSET=utf8;');
    4342   }
    44  
     43
    4544  function DoUninstall()
    4645  {
    4746    $this->Database->query('DROP TABLE IF EXISTS `Measure`');
    4847  }
    49  
     48
    5049  function DoUpgrade()
    5150  {
    52    
     51
    5352  }
    5453}
     
    7170  {
    7271    $this->Id = 0;
    73     $this->ValueTypes = array('min', 'avg', 'max');
     72    $this->ValueTypes = array('Min', 'Avg', 'Max');
    7473    $this->Database = &$Database;
    7574    $this->LevelReducing = 5;
     
    7978    $this->DivisionCount = 500;
    8079    $this->PeriodTolerance = 0.25; // 25%
     80    $this->Data = array('Name' => '', 'Description' => '', 'Unit' => '', 'Info' => '');
    8181  }
    8282
     
    8686  }
    8787
    88   function StatTableName($Level)
    89   {
    90     if($Level == 0) return('data');
    91       else return('data_cache');
     88  function GetDataTable()
     89  {
     90    return('data_'.$this->Data['Name']);
    9291  }
    9392
     
    109108  }
    110109
    111   function AddValue($Value)
    112   {
    113     $Time = time();
    114 
     110  function AddValue($Value, $Time)
     111  {
    115112    $Result = $this->Database->select($this->Data['DataTable'], '*', '(`Measure`='.$this->Data['Id'].') AND '.
    116       '(`level`=0) ORDER BY `time` DESC LIMIT 2');
     113      '(`Level`=0) ORDER BY `Time` DESC LIMIT 2');
    117114    if($Result->num_rows == 0) {
    118115      // No measure value found. Simply insert new first value.
    119116      $this->Database->insert($this->Data['DataTable'],
    120       array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0,
    121       'measure' => $this->Data['Id'], 'time' => TimeToMysqlDateTime($Time),
    122       'continuity' => 0));
     117        array('Min' => $Value, 'Avg' => $Value, 'Max' => $Value, 'Level' => 0,
     118        'Measure' => $this->Data['Id'], 'Time' => TimeToMysqlDateTime($Time),
     119        'Continuity' => 0));
    123120    } else if($Result->num_rows == 1) {
    124121      // One value exists. Add second value.
    125122      $this->Database->insert($this->Data['DataTable'],
    126       array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0,
    127       'measure' => $this->Data['Id'], 'time' => TimeToMysqlDateTime($Time),
    128       'continuity' => 1));
     123      array('Min' => $Value, 'Avg' => $Value, 'Max' => $Value, 'Level' => 0,
     124      'Measure' => $this->Data['Id'], 'Time' => TimeToMysqlDateTime($Time),
     125      'Continuity' => 1));
    129126    } else {
    130127      // Two values already exist in measure table
    131128      $LastValue = $Result->fetch_array();
    132129      $NextToLastValue = $Result->fetch_array();
    133       if(($Time - MysqlDateTimeToTime($LastValue['time'])) < (1 - $this->PeriodTolerance) * $this->Data['Period'])
     130      if(($Time - MysqlDateTimeToTime($LastValue['Time'])) < (1 - $this->PeriodTolerance) * $this->Data['Period'])
    134131      {
    135132        // New value added too quickly. Need to wait for next measure period.
     
    138135      {
    139136        // We are near defined period and can add new value.
    140         if(($Time - MysqlDateTimeToTime($LastValue['time'])) < (1 + $this->PeriodTolerance) * $this->Data['Period']) {
     137        if(($Time - MysqlDateTimeToTime($LastValue['Time'])) < (1 + $this->PeriodTolerance) * $this->Data['Period']) {
    141138          // New value added near defined measure period inside tolerance. Keep continuity.
    142139          $Continuity = 1;
     
    146143          $Continuity = 0;
    147144        }
    148         if(($LastValue['avg'] == $NextToLastValue['avg']) and ($LastValue['avg'] == $Value) and
    149           ($LastValue['continuity'] == 1) and ($Continuity == 1))
     145        if(($LastValue['Avg'] == $NextToLastValue['Avg']) and ($LastValue['Avg'] == $Value) and
     146          ($LastValue['Continuity'] == 1) and ($Continuity == 1))
    150147        {
    151148          // New value is same as last value and continuity mode is enabled and
    152149          // continuity flag is present. Just shift forward time for last value.
    153           $this->Database->update($this->Data['DataTable'], '(`time`="'.$LastValue['time'].'") AND '.
    154           '(`level`=0) AND (`measure`='.$this->Data['Id'].')', array('time' => TimeToMysqlDateTime($Time)));
     150          $this->Database->update($this->Data['DataTable'], '(`Time`="'.$LastValue['Time'].'") AND '.
     151          '(`Level`=0) AND (`Measure`='.$this->Data['Id'].')', array('Time' => TimeToMysqlDateTime($Time)));
    155152        } else
    156153        {
    157154          // Last value is different or not with continuity flag. Need to add new value.
    158           $this->Database->insert($this->Data['DataTable'], array('min' => $Value,
    159             'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $this->Data['Id'], 'time' => TimeToMysqlDateTime($Time),
    160             'continuity' => $Continuity));
    161         }
    162       }
    163     }
    164 
    165     // Update higher levels
     155          $this->Database->insert($this->Data['DataTable'], array('Min' => $Value,
     156            'Avg' => $Value, 'max' => $Value, 'Level' => 0, 'Measure' => $this->Data['Id'],
     157            'Time' => TimeToMysqlDateTime($Time),
     158            'Continuity' => $Continuity));
     159        }
     160      }
     161    }
     162    $this->UpdateHigherLevels($Time);
     163  }
     164
     165  function UpdateHigherLevels($Time)
     166  {
    166167    for($Level = 1; $Level <= $this->MaxLevel; $Level++)
    167168    {
     
    173174      // Load values in time range
    174175      $Values = array();
    175       $Result = $this->Database->select($this->Data['DataTable'], '*', '(`time` > "'.
     176      $Result = $this->Database->select($this->Data['DataTable'], '*', '(`Time` > "'.
    176177        TimeToMysqlDateTime($StartTime).'") AND '.
    177         '(`time` < "'.TimeToMysqlDateTime($EndTime).'") AND '.
    178         '(`measure`='.$this->Data['Id'].') AND (`level`='.($Level - 1).') ORDER BY `time`');
     178        '(`Time` < "'.TimeToMysqlDateTime($EndTime).'") AND '.
     179        '(`Measure`='.$this->Data['Id'].') AND (`Level`='.($Level - 1).') ORDER BY `Time`');
    179180      while($Row = $Result->fetch_array())
    180181      {
    181         $Row['time'] = MysqlDateTimeToTime($Row['time']);
     182        $Row['Time'] = MysqlDateTimeToTime($Row['Time']);
    182183        $Values[] = $Row;
    183184      }
     
    193194      $Point = $this->ComputeOneValue($StartTime, $EndTime, $Values, $Level);
    194195
    195       $this->Database->delete($this->Data['DataTable'], '(`time` > "'.TimeToMysqlDateTime($StartTime).'") AND
    196         (`time` < "'.TimeToMysqlDateTime($EndTime).'") AND (`measure`='.$this->Data['Id'].') '.
    197         'AND (`level`='.$Level.')');
    198       $Continuity = $Values[1]['continuity'];
    199       $this->Database->insert($this->Data['DataTable'], array('level' => $Level,
    200         'measure' => $this->Data['Id'], 'min' => $Point['min'],
    201         'avg' => $Point['avg'], 'max' => $Point['max'], 'continuity' => $Continuity,
    202         'time' => TimeToMysqlDateTime($StartTime + ($EndTime - $StartTime) / 2)));
     196      $this->Database->delete($this->Data['DataTable'], '(`Time` > "'.TimeToMysqlDateTime($StartTime).'") AND
     197        (`Time` < "'.TimeToMysqlDateTime($EndTime).'") AND (`Measure`='.$this->Data['Id'].') '.
     198        'AND (`Level`='.$Level.')');
     199      $Continuity = $Values[1]['Continuity'];
     200      $this->Database->insert($this->Data['DataTable'], array('Level' => $Level,
     201        'Measure' => $this->Data['Id'], 'Min' => $Point['Min'],
     202        'Avg' => $Point['Avg'], 'Max' => $Point['Max'], 'Continuity' => $Continuity,
     203        'Time' => TimeToMysqlDateTime($StartTime + ($EndTime - $StartTime) / 2)));
    203204    }
    204205  }
     
    207208  function ComputeOneValue($LeftTime, $RightTime, $Values, $Level)
    208209  {
    209     $NewValue = array('min' => +1000000000000000000, 'avg' => 0, 'max' => -1000000000000000000);
     210    $NewValue = array('Min' => +1000000000000000000, 'Avg' => 0, 'Max' => -1000000000000000000);
    210211
    211212    // Trim outside parts
     
    213214    {
    214215      $Values[0][$ValueType] = Interpolation(
    215         NewPoint($Values[0]['time'], $Values[0][$ValueType]),
    216         NewPoint($Values[1]['time'], $Values[1][$ValueType]), $LeftTime);
    217     }
    218     $Values[0]['time'] = $LeftTime;
     216        NewPoint($Values[0]['Time'], $Values[0][$ValueType]),
     217        NewPoint($Values[1]['Time'], $Values[1][$ValueType]), $LeftTime);
     218    }
     219    $Values[0]['Time'] = $LeftTime;
    219220    foreach($this->ValueTypes as $ValueType)
    220221    {
    221222      $Values[count($Values) - 1][$ValueType] = Interpolation(
    222         NewPoint($Values[count($Values) - 2]['time'], $Values[count($Values) - 2][$ValueType]),
    223         NewPoint($Values[count($Values) - 1]['time'], $Values[count($Values) - 1][$ValueType]),
     223        NewPoint($Values[count($Values) - 2]['Time'], $Values[count($Values) - 2][$ValueType]),
     224        NewPoint($Values[count($Values) - 1]['Time'], $Values[count($Values) - 1][$ValueType]),
    224225        $RightTime);
    225226    }
    226     $Values[count($Values) - 1]['time'] = $RightTime;
     227    $Values[count($Values) - 1]['Time'] = $RightTime;
    227228
    228229    // Perform computation
     
    232233      for($I = 0; $I < (count($Values) - 1); $I++)
    233234      {
    234         if($ValueType == 'avg')
    235         {
    236           if($Values[$I + 1]['continuity'] == $this->Data['ContinuityEnabled']);
     235        if($ValueType == 'Avg')
     236        {
     237          if($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled']);
    237238            else if($this->Differential == 0)
    238239            {
    239               $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['time'] - $Values[$I]['time']) *
     240              $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['Time'] - $Values[$I]['Time']) *
    240241                (($Values[$I + 1][$ValueType] - $Values[$I][$ValueType]) / 2 + $Values[$I][$ValueType]);
    241242            } else {
    242               $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['time'] - $Values[$I]['time']) *
     243              $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['Time'] - $Values[$I]['Time']) *
    243244               (($Values[$I + 1][$ValueType] - $Values[$I][$ValueType]) / 2);
    244245            }
    245246        }
    246         else if($ValueType == 'max')
    247         {
    248           if($Values[$I + 1]['continuity'] == $this->Data['ContinuityEnabled'])
     247        else if($ValueType == 'Max')
     248        {
     249          if($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled'])
    249250          {
    250251            if(0 > $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
     
    261262          }
    262263        }
    263         else if($ValueType == 'min')
    264         {
    265           if($Values[$I + 1]['continuity'] == $this->Data['ContinuityEnabled'])
     264        else if($ValueType == 'Min')
     265        {
     266          if($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled'])
    266267          {
    267268            if(0 < $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
     
    282283    if($this->Data['Cumulative'] == 0)
    283284    {
    284       $NewValue['avg'] = $NewValue['avg'] / ($RightTime - $LeftTime);
     285      $NewValue['Avg'] = $NewValue['Avg'] / ($RightTime - $LeftTime);
    285286    }
    286287    return($NewValue);
     
    290291  {
    291292    // Get first and last time
    292     $Result = $this->Database->select($this->Data['DataTable'], '*', '(`measure`='.$this->Data['Id'].') AND '.
    293       '(`level`='.$Level.') ORDER BY `time` LIMIT 1');
     293    $Result = $this->Database->select($this->Data['DataTable'], '*', '(`Measure`='.$this->Data['Id'].') AND '.
     294      '(`Level`='.$Level.') ORDER BY `Time` LIMIT 1');
    294295    if($Result->num_rows > 0)
    295296    {
     
    298299    } else $AbsoluteLeftTime = 0;
    299300
    300     $Result = $this->Database->select($this->Data['DataTable'], '*', '(`measure`='.$this->Data['Id'].') AND '.
    301       '(`level`='.$Level.') ORDER BY `time` DESC LIMIT 1');
     301    $Result = $this->Database->select($this->Data['DataTable'], '*', '(`Measure`='.$this->Data['Id'].') AND '.
     302      '(`Level`='.$Level.') ORDER BY `Time` DESC LIMIT 1');
    302303    if($Result->num_rows > 0)
    303304    {
    304305      $Row = $Result->fetch_array();
    305       $AbsoluteRightTime = MysqlDateTimeToTime($Row['time']);
     306      $AbsoluteRightTime = MysqlDateTimeToTime($Row['Time']);
    306307    } else $AbsoluteRightTime = 0;
    307308
    308     return(array('left' => $AbsoluteLeftTime, 'right' => $AbsoluteRightTime));
     309    return(array('Left' => $AbsoluteLeftTime, 'Right' => $AbsoluteRightTime));
    309310  }
    310311
     
    314315    $Result = array();
    315316    $DbResult = $this->Database->select($this->Data['DataTable'], '*',
    316       '(`time` > "'.TimeToMysqlDateTime($Time).'") AND (`measure`='.
    317       $this->Data['Id'].') AND (`level`='.$Level.') ORDER BY `time` ASC LIMIT 1');
     317      '(`Time` > "'.TimeToMysqlDateTime($Time).'") AND (`Measure`='.
     318      $this->Data['Id'].') AND (`Level`='.$Level.') ORDER BY `Time` ASC LIMIT 1');
    318319    if($DbResult->num_rows > 0)
    319320    {
    320321      // Found one value, simply return it
    321322      $Row = $DbResult->fetch_array();
    322       $Row['time'] = MysqlDateTimeToTime($Row['time']);
     323      $Row['Time'] = MysqlDateTimeToTime($Row['Time']);
    323324      return(array($Row));
    324325    } else
     
    327328      //$Time = $Values[count($Values) - 1]['time'] + 60;
    328329      //array_push($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
    329       $Result[] = array('time' => ($Time + $this->TimeSegment($Level)), 'min' => 0,
    330         'avg' => 0, 'max' => 0, 'continuity' => 0);
     330      $Result[] = array('Time' => ($Time + $this->TimeSegment($Level)), 'Min' => 0,
     331        'Avg' => 0, 'Max' => 0, 'Continuity' => 0);
    331332      $DbResult = $this->Database->select($this->Data['DataTable'], '*',
    332         '(`time` < "'.TimeToMysqlDateTime($Time).'") AND (`measure`='.$this->Data['Id'].') '.
    333         'AND (`level`='.$Level.') ORDER BY `time` DESC LIMIT 1');
     333        '(`Time` < "'.TimeToMysqlDateTime($Time).'") AND (`Measure`='.$this->Data['Id'].') '.
     334        'AND (`Level`='.$Level.') ORDER BY `Time` DESC LIMIT 1');
    334335      if($DbResult->num_rows > 0)
    335336      {
    336337        $Row = $DbResult->fetch_array();
    337         array_unshift($Result, array('time' => (MysqlDateTimeToTime($Row['time']) + 10),
    338           'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
     338        array_unshift($Result, array('Time' => (MysqlDateTimeToTime($Row['Time']) + 10),
     339          'Min' => 0, 'Avg' => 0, 'Max' => 0, 'Continuity' => 0));
    339340      }
    340341      return($Result);
     
    347348    $Result = array();
    348349    $DbResult = $this->Database->select($this->Data['DataTable'], '*',
    349       '(`time` < "'.TimeToMysqlDateTime($Time).'") AND (`measure`='.$this->Data['Id'].') '.
    350       'AND (`level`='.$Level.') ORDER BY `time` DESC LIMIT 1');
     350      '(`Time` < "'.TimeToMysqlDateTime($Time).'") AND (`Measure`='.$this->Data['Id'].') '.
     351      'AND (`Level`='.$Level.') ORDER BY `Time` DESC LIMIT 1');
    351352    if($DbResult->num_rows > 0)
    352353    {
    353354      // Found one value, simply return it
    354355      $Row = $DbResult->fetch_array();
    355       $Row['time'] = MysqlDateTimeToTime($Row['time']);
     356      $Row['Time'] = MysqlDateTimeToTime($Row['Time']);
    356357      return(array($Row));
    357358    } else {
     
    359360      //$Time = $Values[0]['time'] - 60;
    360361      //array_unshift($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
    361       $Result[] = array('time' => ($Time - $this->TimeSegment($Level)), 'min' => 0,
    362         'avg' => 0, 'max' => 0, 'continuity' => 0);
     362      $Result[] = array('Time' => ($Time - $this->TimeSegment($Level)), 'Min' => 0,
     363        'Avg' => 0, 'Max' => 0, 'Continuity' => 0);
    363364
    364365      $DbResult = $this->Database->select($this->Data['DataTable'], '*',
    365         '(`time` > "'.TimeToMysqlDateTime($Time).'") AND (`measure`='.$this->Data['Id'].') '.
    366         'AND (`level`='.$Level.') ORDER BY `time` ASC LIMIT 1');
     366        '(`Time` > "'.TimeToMysqlDateTime($Time).'") AND (`Measure`='.$this->Data['Id'].') '.
     367        'AND (`Level`='.$Level.') ORDER BY `Time` ASC LIMIT 1');
    367368      if($DbResult->num_rows > 0)
    368369      {
    369370        $Row = $DbResult->fetch_array();
    370         array_push($Result, array('time' => (MysqlDateTimeToTime($Row['time']) - 10),
    371           'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
     371        array_push($Result, array('Time' => (MysqlDateTimeToTime($Row['Time']) - 10),
     372          'Min' => 0, 'Avg' => 0, 'Max' => 0, 'Continuity' => 0));
    372373      }
    373374      return($Result);
     
    384385
    385386    // Load values in time range
    386     $Result = $this->Database->select($this->Data['DataTable'], '`time`, `min`, '.
    387       '`avg`, `max`, `continuity`',
    388       '(`time` > "'.TimeToMysqlDateTime($TimeFrom).'") AND '.
    389       '(`time` < "'.TimeToMysqlDateTime($TimeTo).'") AND '.
    390       '(`measure`='.$this->Data['Id'].') AND (`level`='.$Level.') ORDER BY `time`');
     387    $Result = $this->Database->select($this->Data['DataTable'], '`Time`, `Min`, '.
     388      '`Avg`, `Max`, `Continuity`',
     389      '(`Time` > "'.TimeToMysqlDateTime($TimeFrom).'") AND '.
     390      '(`Time` < "'.TimeToMysqlDateTime($TimeTo).'") AND '.
     391      '(`Measure`='.$this->Data['Id'].') AND (`Level`='.$Level.') ORDER BY `Time`');
    391392    $Values = array();
    392393    while($Row = $Result->fetch_array())
    393394    {
    394       $Values[] = array('time' => MysqlDateTimeToTime($Row['time']),
    395         'min' => $Row['min'], 'avg' => $Row['avg'], 'max' => $Row['max'],
    396         'continuity' => $Row['continuity']);
     395      $Values[] = array('Time' => MysqlDateTimeToTime($Row['Time']),
     396        'Min' => $Row['Min'], 'Avg' => $Row['Avg'], 'Max' => $Row['Max'],
     397        'Continuity' => $Row['Continuity']);
    397398    }
    398399   // array_pop($Values);
     
    414415
    415416        $EndIndex = $StartIndex;
    416         while($Values[$EndIndex]['time'] < $TimeEnd) $EndIndex = $EndIndex + 1;
     417        while($Values[$EndIndex]['Time'] < $TimeEnd) $EndIndex = $EndIndex + 1;
    417418        $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
    418419        $Points[] = $this->ComputeOneValue($TimeStart, $TimeEnd, $SubValues, $Level);
    419420        $StartIndex = $EndIndex - 1;
    420421      }
    421     } else $Points[] = array('min' => 0, 'avg' => 0, 'max' => 0);
     422    } else $Points[] = array('Min' => 0, 'Avg' => 0, 'Max' => 0);
    422423    return($Points);
    423424  }
     
    430431
    431432    // Clear previous items
    432     $DbResult = $this->Database->select($this->Data['DataTable'], 'COUNT(*)', '(`level`>0) AND (`measure`='.$this->Data['Id'].')');
     433    $DbResult = $this->Database->select($this->Data['DataTable'], 'COUNT(*)', '(`Level`>0) AND (`Measure`='.$this->Data['Id'].')');
    433434    $Row = $DbResult->fetch_array();
    434435    echo("Mazu starou cache (".$Row[0]." polozek)...");
    435     $this->Database->delete($this->Data['DataTable'], '(`level`>0) AND (`measure`='.$this->Data['Id'].')');
     436    $this->Database->delete($this->Data['DataTable'], '(`Level`>0) AND (`Measure`='.$this->Data['Id'].')');
    436437    echo("<br>\n");
    437438
     
    441442      $TimeRange = $this->GetTimeRange($Level - 1);
    442443      $TimeSegment = $this->TimeSegment($Level);
    443       $StartTime = $this->AlignTime($TimeRange['left'], $TimeSegment) - $TimeSegment;
    444       $EndTime = $this->AlignTime($TimeRange['right'], $TimeSegment);
     444      $StartTime = $this->AlignTime($TimeRange['Left'], $TimeSegment) - $TimeSegment;
     445      $EndTime = $this->AlignTime($TimeRange['Right'], $TimeSegment);
    445446      $BurstCount = 500;
    446447      echo('For 0 to '.round(($EndTime - $StartTime) / $TimeSegment / $BurstCount)."<br>\n");
     
    451452        $EndTime2 = $StartTime + ($I + 1) * $BurstCount * $TimeSegment;
    452453        $Values = array();
    453         $DbResult = $this->Database->select($this->Data['DataTable'], '*', '(`time` > "'.TimeToMysqlDateTime($StartTime2).'") AND (`time` < "'.
    454           TimeToMysqlDateTime($EndTime2).'") AND (`measure`='.$this->Data['Id'].') AND (`level`='.($Level - 1).') ORDER BY `time`');
     454        $DbResult = $this->Database->select($this->Data['DataTable'], '*', '(`Time` > "'.
     455          TimeToMysqlDateTime($StartTime2).'") AND (`Time` < "'.
     456          TimeToMysqlDateTime($EndTime2).'") AND (`Measure`='.$this->Data['Id'].
     457          ') AND (`Level`='.($Level - 1).') ORDER BY `Time`');
    455458        while($Row = $DbResult->fetch_array())
    456459        {
    457           $Row['time'] = MysqlDateTimeToTime($Row['time']);
     460          $Row['Time'] = MysqlDateTimeToTime($Row['Time']);
    458461          $Values[] = $Row;
    459462        }
     
    474477
    475478            $EndIndex = $StartIndex;
    476             while($Values[$EndIndex]['time'] < $EndTime3) $EndIndex = $EndIndex + 1;
     479            while($Values[$EndIndex]['Time'] < $EndTime3) $EndIndex = $EndIndex + 1;
    477480            $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
    478481            if(count($SubValues) > 2)
    479482            {
    480483              $Point = $this->ComputeOneValue($StartTime3, $EndTime3, $SubValues, $Level);
    481               $Continuity = $SubValues[1]['continuity'];
    482               $this->Database->insert($this->Data['DataTable'], array('level' => $Level, 'measure' => $this->Data['Id'],
    483                 'min' => $Point['min'], 'avg' => $Point['avg'], 'max' => $Point['max'],
    484                 'continuity' => $Continuity, 'time' => TimeToMysqlDateTime($StartTime3 + ($EndTime3 - $StartTime3) / 2)));
     484              $Continuity = $SubValues[1]['Continuity'];
     485              $this->Database->insert($this->Data['DataTable'], array('Level' => $Level, 'Measure' => $this->Data['Id'],
     486                'Min' => $Point['Min'], 'Avg' => $Point['Avg'], 'Max' => $Point['Max'],
     487                'Continuity' => $Continuity, 'Time' => TimeToMysqlDateTime($StartTime3 + ($EndTime3 - $StartTime3) / 2)));
    485488            }
    486489            $StartIndex = $EndIndex - 1;
     
    500503  function RebuildAllMeasuresCache()
    501504  {
    502     $Result = $this->Database->select('measure', 'Id');
     505    $Result = $this->Database->select('Measure', 'Id');
    503506    while($Row = $Result->fetch_array())
    504507    {
     
    509512    }
    510513  }
    511 
    512   function InitMeasureDataTable()
    513   {
    514     $this->Database->query('CREATE TABLE `data_'.$this->Data['Name'].'` (
    515       `Time` TIMESTAMP NOT NULL ,
    516       `Avg` '.$this->Data['DataType'].' NOT NULL ,
    517       `Continuity` BOOL NOT NULL
    518       ) ENGINE = MYISAM ;');
    519 
    520     $this->Database->query('CREATE TABLE `data_'.$this->Data['Name'].'_cache` (
     514}
     515
     516class MeasureDataType
     517{
     518  const Int8 = 0;
     519  const Int16 = 1;
     520  const Int32 = 2;
     521  const Int64 = 3;
     522  const Float = 4;
     523  const Double = 5;
     524}
     525
     526class MeasureList extends Model
     527{
     528  function AddItem(Measure $Measure)
     529  {
     530    $this->Database->insert('Measure', array(
     531      'Name' => $Measure->Data['Name'],
     532      'Description' => $Measure->Data['Description'],
     533      'Unit' => $Measure->Data['Unit'],
     534      'Info' => $Measure->Data['Info'],
     535      'DataType' => $Measure->Data['DataType'],
     536      'DataTable' => $Measure->GetDataTable(),
     537    ));
     538    $Measure->Data['Id'] = $this->Database->insert_id;
     539
     540    $this->Database->query('CREATE TABLE `'.$Measure->GetDataTable().'` (
    521541`Time` TIMESTAMP NOT NULL ,
     542`Measure` INT NOT NULL ,
    522543`Level` TINYINT NOT NULL ,
    523 `Min` '.$this->Data['DataType'].' NOT NULL ,
    524 `Avg` '.$this->Data['DataType'].' NOT NULL ,
    525 `Max` '.$this->Data['DataType'].' NOT NULL ,
     544`Min` '.$Measure->Data['DataType'].' NOT NULL ,
     545`Avg` '.$Measure->Data['DataType'].' NOT NULL ,
     546`Max` '.$Measure->Data['DataType'].' NOT NULL ,
    526547`Continuity` BOOL NOT NULL
    527 ) ENGINE = MYISAM ;');
    528   }
     548) ENGINE = InnoDb ;');
     549  }
     550
     551  function RemoveItem(Measure $Measure)
     552  {
     553    $this->Database->delete('Measure', '`Id`='.$Measure->Data['Id']);
     554    $this->Database->query('DROP TABLE `'.$Measure->GetDataTable().'`');
     555  }
     556
    529557}
  • trunk/add.php

    r69 r71  
    1212  $MeasureId = addslashes($_GET['MeasureId']);
    1313  $Value = addslashes($_GET['Value']);
     14  if (array_key_exists('Time', $_GET)) $Time = $_GET['Time'] * 1;
     15    else $Time = time();
    1416  $Measure = new Measure($Core->Database);
    1517  $Measure->Load($MeasureId);
     
    1719  if(($HostName == $Measure->Data['PermissionAdd']) or ($_SERVER['REMOTE_ADDR'] == gethostbyname($Measure->Data['PermissionAdd'])))
    1820  {
    19     $Measure->AddValue($Value);
     21    $Measure->AddValue($Value, $Time);
    2022    echo('Hodnota '.$Value.' uložena');
    2123  } else echo('Nemáte oprávnění k aktualizaci zadané veličiny!<br>Vaše adresa: '.$HostName.'('.$_SERVER['REMOTE_ADDR'].')');
Note: See TracChangeset for help on using the changeset viewer.