Ignore:
Timestamp:
Apr 6, 2020, 11:17:40 PM (5 years ago)
Author:
chronos
Message:
  • Modified: Improved code format.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/TimeMeasure/Measure.php

    r790 r873  
    1515  {
    1616    $Result = $this->Database->select('Measure', '*', 'Id='.$Id);
    17     if($Result->num_rows > 0)
     17    if ($Result->num_rows > 0)
    1818    {
    1919      $this->Data = $Result->fetch_assoc();
    20       if($this->Data['Continuity'] == 0) $this->Data['ContinuityEnabled'] = 0;  // non continuous
     20      if ($this->Data['Continuity'] == 0) $this->Data['ContinuityEnabled'] = 0;  // non continuous
    2121        else $this->Data['ContinuityEnabled'] = 2;    // continuous graph
    2222    } else throw new Exception('Measure not found');
     
    2525  function TimeSegment($Base, $Level)
    2626  {
    27     return(pow($this->LevelReducing, $Level) * $Base);
     27    return (pow($this->LevelReducing, $Level) * $Base);
    2828  }
    2929
    3030  function StatTableName($Level)
    3131  {
    32     if($Level == 0) return('Data');
    33       else return('DataCache');
     32    if ($Level == 0) return ('Data');
     33      else return ('DataCache');
    3434  }
    3535
    3636  function AlignTime($Time, $TimeSegment)
    3737  {
    38     return(round(($Time - $this->ReferenceTime) / $TimeSegment) * $TimeSegment + $this->ReferenceTime);
     38    return (round(($Time - $this->ReferenceTime) / $TimeSegment) * $TimeSegment + $this->ReferenceTime);
    3939  }
    4040
    4141  function AddValue($Value = array('Min' => 0, 'Avg' => 0, 'Max' => 0), $Level = 0, $Time = 0)
    4242  {
    43     if($Time == 0) $Time = time();
     43    if ($Time == 0) $Time = time();
    4444
    4545    $Result = $this->Database->select($this->Data['DataTable'], '*', 'Measure='.
    4646      $this->Data['Id'].' AND Level='.$Level.' ORDER BY Time DESC LIMIT 2');
    47     if($Result->num_rows == 0)
     47    if ($Result->num_rows == 0)
    4848    {
    4949       $this->Database->insert($this->Data['DataTable'], array('Min' => $Value['Min'],
    5050         'Avg' => $Value['Avg'], 'Max' => $Value['Max'], 'Level' => $Level,
    5151         'Measure' => $this->Data['Id'], 'Time' => TimeToMysqlDateTime($Time), 'Continuity' => 0));
    52     } else if($Result->num_rows == 1)
     52    } else if ($Result->num_rows == 1)
    5353    {
    5454      $this->Database->insert($this->Data['DataTable'], array('Min' => $Value['Min'],
     
    5959      $LastValue = $Result->fetch_assoc();
    6060      $NextToLastValue = $Result->fetch_assoc();
    61       if((($Time - MysqlDateTimeToTime($LastValue['Time'])) < 0.75 * $this->Data['Period']) and ($Level == 0))
     61      if ((($Time - MysqlDateTimeToTime($LastValue['Time'])) < 0.75 * $this->Data['Period']) and ($Level == 0))
    6262      {
    6363        echo('Too short period. Minimal period is '.(0.75 * $this->Data['Period'])." seconds\n");
    6464      } else
    6565      {
    66         if(($Time - MysqlDateTimeToTime($LastValue['Time'])) < 1.25 * $this->Data['Period']) $Continuity = 1;
     66        if (($Time - MysqlDateTimeToTime($LastValue['Time'])) < 1.25 * $this->Data['Period']) $Continuity = 1;
    6767          else $Continuity = 0;
    68         if(($LastValue['Min'] == $NextToLastValue['Min']) and ($LastValue['Min'] ==
     68        if (($LastValue['Min'] == $NextToLastValue['Min']) and ($LastValue['Min'] ==
    6969          $Value['Min']) and ($LastValue['Avg'] == $NextToLastValue['Avg']) and
    7070          ($LastValue['Avg'] == $Value['Avg']) and ($LastValue['Max'] == $NextToLastValue['Max'])
     
    8282
    8383      // Update next level
    84       if($Level < $this->MaxLevel)
     84      if ($Level < $this->MaxLevel)
    8585      {
    8686        $Level = $Level + 1;
    8787        $TimeSegment = $this->TimeSegment($this->Data['Period'], 1);
    8888        $EndTime = $this->AlignTime($Time, $TimeSegment);
    89         //if($EndTime < $Time) $EndTime = $EndTime + $TimeSegment;
     89        //if ($EndTime < $Time) $EndTime = $EndTime + $TimeSegment;
    9090        $StartTime = $EndTime - $TimeSegment;
    9191
     
    9696          TimeToMysqlDateTime($StartTime).'") AND (Time < "'.TimeToMysqlDateTime($EndTime).
    9797            '") AND (Measure='.$this->Data['Id'].') AND (Level='.($Level - 1).') ORDER BY Time');
    98         while($Row = $Result->fetch_assoc())
     98        while ($Row = $Result->fetch_assoc())
    9999        {
    100100          $Row['Time'] = MysqlDateTimeToTime($Row['Time']);
    101101          $Values[] = $Row;
    102102        }
    103         //if(count($Values) > 2)
     103        //if (count($Values) > 2)
    104104        {
    105105          //array_pop($Values);
     
    124124  {
    125125    $Y = ($Y2 - $Y1) / ($X2 - $X1) * ($X - $X1) + $Y1;
    126     return($Y);
     126    return ($Y);
    127127  }
    128128
     
    132132
    133133    // Trim outside parts
    134     foreach($this->ValueTypes as $ValueType)
     134    foreach ($this->ValueTypes as $ValueType)
    135135    {
    136136      $Values[0][$ValueType] = $this->Interpolation($Values[0]['Time'], $Values[0][$ValueType], $Values[1]['Time'], $Values[1][$ValueType], $LeftTime);
    137137    }
    138138    $Values[0]['Time'] = $LeftTime;
    139     foreach($this->ValueTypes as $ValueType)
     139    foreach ($this->ValueTypes as $ValueType)
    140140    {
    141141        $Values[count($Values) - 1][$ValueType] = $this->Interpolation($Values[count($Values) - 2]['Time'], $Values[count($Values) - 2][$ValueType],
     
    145145
    146146    // Perform computation
    147     foreach($this->ValueTypes as $ValueType)
     147    foreach ($this->ValueTypes as $ValueType)
    148148    {
    149149      // Compute new value
    150       for($I = 0; $I < (count($Values) - 1); $I++)
    151       {
    152         if($ValueType == 'Avg')
    153         {
    154           if($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled']);
    155           else if($this->Differential == 0)
     150      for ($I = 0; $I < (count($Values) - 1); $I++)
     151      {
     152        if ($ValueType == 'Avg')
     153        {
     154          if ($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled']);
     155          else if ($this->Differential == 0)
    156156          {
    157157            $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['Time'] - $Values[$I]['Time']) *
     
    163163          }
    164164        }
    165         else if($ValueType == 'Max')
    166         {
    167           if($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled'])
    168           {
    169             if(0 > $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
     165        else if ($ValueType == 'Max')
     166        {
     167          if ($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled'])
     168          {
     169            if (0 > $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
    170170          } else
    171171          {
    172             if($this->Differential == 0)
     172            if ($this->Differential == 0)
    173173            {
    174               if($Values[$I + 1][$ValueType] > $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
     174              if ($Values[$I + 1][$ValueType] > $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
    175175            } else {
    176176              $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType];
    177               if($Difference > $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
     177              if ($Difference > $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
    178178            }
    179179          }
    180180        }
    181         else if($ValueType == 'Min')
    182         {
    183           if($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled'])
    184           {
    185             if(0 < $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
     181        else if ($ValueType == 'Min')
     182        {
     183          if ($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled'])
     184          {
     185            if (0 < $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
    186186          } else
    187187          {
    188             if($this->Differential == 0)
     188            if ($this->Differential == 0)
    189189            {
    190               if($Values[$I + 1][$ValueType] < $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
     190              if ($Values[$I + 1][$ValueType] < $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
    191191            } else {
    192192              $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType];
    193               if($Difference < $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
     193              if ($Difference < $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
    194194            }
    195195          }
     
    198198      $NewValue[$ValueType] = $NewValue[$ValueType];
    199199    }
    200     //if(($RightTime - $LeftTime) > 0)
    201     if($this->Data['Cumulative'] == 0)
     200    //if (($RightTime - $LeftTime) > 0)
     201    if ($this->Data['Cumulative'] == 0)
    202202    {
    203203      $NewValue['Avg'] = $NewValue['Avg'] / ($RightTime - $LeftTime);
    204204    }
    205     return($NewValue);
     205    return ($NewValue);
    206206  }
    207207
     
    210210    // Get first and last time
    211211    $Result = $this->Database->select($this->Data['DataTable'], '*', 'Measure='.$this->Data['Id'].' AND Level='.$Level.' ORDER BY Time LIMIT 1');
    212     if($Result->num_rows > 0)
     212    if ($Result->num_rows > 0)
    213213    {
    214214      $Row = $Result->fetch_assoc();
     
    217217
    218218    $Result = $this->Database->select($this->Data['DataTable'], '*', 'Measure='.$this->Data['Id'].' AND Level='.$Level.' ORDER BY Time DESC LIMIT 1');
    219     if($Result->num_rows > 0)
     219    if ($Result->num_rows > 0)
    220220    {
    221221      $Row = $Result->fetch_assoc();
     
    223223    } else $AbsoluteRightTime = 0;
    224224
    225     if($this->Debug)
     225    if ($this->Debug)
    226226    {
    227227      echo('AbsoluteLeftTime: '.$AbsoluteLeftTime.'('.TimeToMysqlDateTime($AbsoluteLeftTime).')<br>');
    228228      echo('AbsoluteRightTime: '.$AbsoluteRightTime.'('.TimeToMysqlDateTime($AbsoluteRightTime).')<br>');
    229229    }
    230     return(array('Left' => $AbsoluteLeftTime, 'Right' => $AbsoluteRightTime));
     230    return (array('Left' => $AbsoluteLeftTime, 'Right' => $AbsoluteRightTime));
    231231  }
    232232
     
    235235    $Result = array();
    236236    $DbResult = $this->Database->select($this->Data['DataTable'], '*', 'Time > "'.TimeToMysqlDateTime($Time).'" AND Measure='.$this->Data['Id'].' AND Level='.$Level.' ORDER BY Time ASC LIMIT 1');
    237     if($DbResult->num_rows > 0)
     237    if ($DbResult->num_rows > 0)
    238238    {
    239239      $Row = $DbResult->fetch_assoc();
    240240      $Row['Time'] = MysqlDateTimeToTime($Row['Time']);
    241       return(array($Row));
     241      return (array($Row));
    242242    } else
    243243    {
     
    247247      $DbResult = $this->Database->select($this->Data['DataTable'], '*', 'Time < "'.
    248248        TimeToMysqlDateTime($Time).'" AND Measure='.$this->Data['Id'].' AND Level='.$Level.' ORDER BY Time DESC LIMIT 1');
    249       if($DbResult->num_rows > 0)
     249      if ($DbResult->num_rows > 0)
    250250      {
    251251        $Row = $DbResult->fetch_assoc();
    252252        array_unshift($Result, array('Time' => (MysqlDateTimeToTime($Row['Time']) + 10), 'Min' => 0, 'Avg' => 0, 'Max' => 0, 'Continuity' => 0));
    253253      }
    254      // if($Debug) print_r($Result);
    255       return($Result);
     254     // if ($Debug) print_r($Result);
     255      return ($Result);
    256256    }
    257257  }
     
    262262    $DbResult = $this->Database->select($this->Data['DataTable'], '*', '(Time < "'.
    263263      TimeToMysqlDateTime($Time).'") AND (Measure='.$this->Data['Id'].') AND (Level='.$Level.') ORDER BY Time DESC LIMIT 1');
    264     if($DbResult->num_rows > 0)
     264    if ($DbResult->num_rows > 0)
    265265    {
    266266      $Row = $DbResult->fetch_assoc();
    267267      $Row['Time'] = MysqlDateTimeToTime($Row['Time']);
    268       return(array($Row));
     268      return (array($Row));
    269269    } else
    270270    {
    271271      //$Time = $Values[0]['Time'] - 60;
    272272      //array_unshift($Values, array('Time' => $Time, 'Min' => 0, 'Avg' => 0, 'Max' => 0, 'Continuity' => 0));
    273       if($this->Debug) echo($this->TimeSegment($this->Data['Period'], $Level));
     273      if ($this->Debug) echo($this->TimeSegment($this->Data['Period'], $Level));
    274274      $Result[] = array('Time' => ($Time - $this->TimeSegment($this->Data['Period'], $Level)), 'Min' => 0, 'Avg' => 0, 'Max' => 0, 'Continuity' => 0);
    275275
    276276      $DbResult = $this->Database->select($this->Data['DataTable'], '*', 'Time > "'.TimeToMysqlDateTime($Time).'" AND Measure='.$this->Data['Id'].' AND Level='.$Level.' ORDER BY Time ASC LIMIT 1');
    277       if($DbResult->num_rows > 0)
     277      if ($DbResult->num_rows > 0)
    278278      {
    279279        $Row = $DbResult->fetch_assoc();
    280280        array_push($Result, array('Time' => (MysqlDateTimeToTime($Row['Time']) - 10), 'Min' => 0, 'Avg' => 0, 'Max' => 0, 'Continuity' => 0));
    281281      }
    282       return($Result);
     282      return ($Result);
    283283    }
    284284  }
     
    286286  function GetValues($TimeFrom, $TimeTo, $Level)
    287287  {
    288     if($this->Debug) echo('TimeFrom: '.$TimeFrom.'('.TimeToMysqlDateTime($TimeFrom).')<br>');
    289     if($this->Debug) echo('TimeTo: '.$TimeTo.'('.TimeToMysqlDateTime($TimeTo).')<br>');
     288    if ($this->Debug) echo('TimeFrom: '.$TimeFrom.'('.TimeToMysqlDateTime($TimeFrom).')<br>');
     289    if ($this->Debug) echo('TimeTo: '.$TimeTo.'('.TimeToMysqlDateTime($TimeTo).')<br>');
    290290
    291291    //$AbsoluteTime = GetTimeRange($MeasureId);
    292292
    293     //  if(($TimeFrom > $AbsoluteLeftTime) and ($TimeStart < $AbsoluteRightTime) and
     293    //  if (($TimeFrom > $AbsoluteLeftTime) and ($TimeStart < $AbsoluteRightTime) and
    294294    //    ($TimeTo > $AbsoluteLeftTime) and ($TimeTo < $AbsoluteRightTime))
    295295    //  {
     
    304304    //  echo(DB_NumRows());
    305305    //  $III = 0;
    306     while($Row = $Result->fetch_assoc())
     306    while ($Row = $Result->fetch_assoc())
    307307    {
    308308      //    echo($III.' '.$Row['Time'].' '.memory_get_usage().',');
     
    314314    //  echo('abc');
    315315    //  die();
    316     if($this->Debug) echo('Item count: '.count($Values));
     316    if ($this->Debug) echo('Item count: '.count($Values));
    317317
    318318    $Points = array();
    319     if(count($Values) > 0)
     319    if (count($Values) > 0)
    320320    {
    321321      $Values = array_merge($this->LoadLeftSideValue($Level, $TimeFrom), $Values, $this->LoadRightSideValue($Level, $TimeTo));
    322322      $StartIndex = 0;
    323323      $Points = array();
    324       if($this->Debug) print_r($Values);
    325       for($I = 0; $I < $this->DivisionCount; $I++)
     324      if ($this->Debug) print_r($Values);
     325      for ($I = 0; $I < $this->DivisionCount; $I++)
    326326      {
    327327        $TimeStart = $TimeFrom + (($TimeTo - $TimeFrom) / $this->DivisionCount) * $I;
    328328        $TimeEnd = $TimeFrom + (($TimeTo - $TimeFrom) / $this->DivisionCount) * ($I + 1);
    329         if($this->Debug) echo('TimeEnd '.$I.': '.$TimeEnd.'('.TimeToMysqlDateTime($TimeEnd).')<br>');
     329        if ($this->Debug) echo('TimeEnd '.$I.': '.$TimeEnd.'('.TimeToMysqlDateTime($TimeEnd).')<br>');
    330330
    331331        $EndIndex = $StartIndex;
    332         while(($Values[$EndIndex]['Time'] < $TimeEnd) and ($EndIndex < count($Values))) $EndIndex = $EndIndex + 1;
    333         //while(($Values[$EndIndex]['Time'] < $TimeEnd)) $EndIndex = $EndIndex + 1;
     332        while (($Values[$EndIndex]['Time'] < $TimeEnd) and ($EndIndex < count($Values))) $EndIndex = $EndIndex + 1;
     333        //while (($Values[$EndIndex]['Time'] < $TimeEnd)) $EndIndex = $EndIndex + 1;
    334334        $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
    335335        $Points[] = $this->ComputeOneValue($TimeStart, $TimeEnd, $SubValues, $Level);
    336336        $StartIndex = $EndIndex - 1;
    337337      }
    338       if($this->Debug) print_r($Points);
     338      if ($this->Debug) print_r($Points);
    339339    } else $Points[] = array('Min' => 0, 'Avg' => 0, 'Max' => 0);
    340     return($Points);
     340    return ($Points);
    341341  }
    342342
     
    344344  {
    345345    echo('Veličina '.$this->Data['Name']."<br>\n");
    346     if($this->Data['Continuity'] == 0) $this->Data['ContinuityEnabled'] = 0;  // non continuous
     346    if ($this->Data['Continuity'] == 0) $this->Data['ContinuityEnabled'] = 0;  // non continuous
    347347      else $this->Data['ContinuityEnabled'] = 2;    // continuous graph
    348348
     
    354354    echo("<br>\n");
    355355
    356     for($Level = 1; $Level <= $this->MaxLevel; $Level++)
     356    for ($Level = 1; $Level <= $this->MaxLevel; $Level++)
    357357    {
    358358      echo('Uroven '.$Level."<br>\n");
     
    363363      $BurstCount = 500;
    364364      echo('For 0 to '.round(($EndTime - $StartTime) / $TimeSegment / $BurstCount)."<br>\n");
    365       for($I = 0; $I <= round(($EndTime - $StartTime) / $TimeSegment / $BurstCount); $I++)
     365      for ($I = 0; $I <= round(($EndTime - $StartTime) / $TimeSegment / $BurstCount); $I++)
    366366      {
    367367        echo($I.' ');
     
    371371        $DbResult = $this->Database->select($this->Data['DataTable'], '*', 'Time > "'.
    372372          TimeToMysqlDateTime($StartTime2).'" AND Time < "'.TimeToMysqlDateTime($EndTime2).'" AND Measure='.$this->Data['Id'].' AND Level='.($Level - 1).' ORDER BY Time');
    373         while($Row = $DbResult->fetch_assoc())
     373        while ($Row = $DbResult->fetch_assoc())
    374374        {
    375375          $Row['Time'] = MysqlDateTimeToTime($Row['Time']);
     
    377377        }
    378378
    379         if(count($Values) > 0)
     379        if (count($Values) > 0)
    380380        {
    381381          $Values = array_merge($this->LoadLeftSideValue($Level - 1, $StartTime2),
     
    383383
    384384          $StartIndex = 0;
    385           for($B = 0; $B < $BurstCount; $B++)
     385          for ($B = 0; $B < $BurstCount; $B++)
    386386          {
    387387            echo('.');
     
    390390
    391391            $EndIndex = $StartIndex;
    392             while($Values[$EndIndex]['Time'] < $EndTime3) $EndIndex = $EndIndex + 1;
     392            while ($Values[$EndIndex]['Time'] < $EndTime3) $EndIndex = $EndIndex + 1;
    393393            $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
    394             if(count($SubValues) > 2)
     394            if (count($SubValues) > 2)
    395395            {
    396396              $Point = $this->ComputeOneValue($StartTime3, $EndTime3, $SubValues, $Level);
     
    420420    $Measures = array();
    421421    $Result = $Database->select('Measure', '*');
    422     while($Row = $Result->fetch_assoc())
     422    while ($Row = $Result->fetch_assoc())
    423423    {
    424424      $Measures = new Measure();
     
    426426    }
    427427
    428     foreach($Measures as $Measure)
     428    foreach ($Measures as $Measure)
    429429    {
    430430      $Measure->RebuildMeasureCache();
Note: See TracChangeset for help on using the changeset viewer.