Changeset 43 for trunk/Application/Model


Ignore:
Timestamp:
Nov 24, 2009, 2:03:29 PM (15 years ago)
Author:
george
Message:
  • Upraveno: Skripty pro načítání hodnot byly pročištěny a přepsány na třídy. Pro definici místně měřených veličin nyní použita tabulky MeasureClient odkazující se na tabulku MeasureMethod obsahující globální registr měřících metod.
  • Opraveno: Různé chyby vzniklé při přepisu třídy Measure a použité pro zobrazení obrázku grafu.
Location:
trunk/Application/Model
Files:
5 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Application/Model/Measure.php

    r40 r43  
    11<?php
     2
     3include_once(dirname(__FILE__).'/../../Base/Model.php');
    24
    35class Measure extends Model
    46{
    5   function Add()
    6   {
    7     if(array_key_exists('MeasureId', $_GET) and array_key_exists('Value', $_GET))
    8     {
    9       $MeasureId = addslashes($_GET['MeasureId']);
    10       $Value = addslashes($_GET['Value']);
    11       $Measure = GetMeasureById($MeasureId);
    12       if(gethostbyaddr($_SERVER['REMOTE_ADDR']) == $Measure['PermissionAdd'])
    13       {
    14         AddValue($Measure, array('min' => $Value, 'avg' => $Value, 'max' => $Value));
    15         $Output = 'Hodnota '.$Value.' uložena';
    16       } else $Output = 'Nemáte oprávnění k aktualizaci zadané veličiny!<br>Vaše adresa: '.gethostbyaddr($_SERVER['REMOTE_ADDR']).'('.$_SERVER['REMOTE_ADDR'].')';
    17     } else $Output = 'Nebyly zadány potřebné parametry';
    18     return($Output);
     7  var $Data;
     8  var $LevelReducing = 5;
     9  var $ReferenceTime = 0;
     10  var $MaxLevel = 4;
     11  var $Differential = 0;
     12  var $Debug = 0;
     13  var $DivisionCount = 500;
     14  var $ValueTypes = array('Min', 'Avg', 'Max');
     15 
     16  function Add($Value)
     17  {
     18    if(gethostbyaddr($_SERVER['REMOTE_ADDR']) == $this->Data['PermissionAdd'])
     19    {
     20      $this->AddValue(array('Min' => $Value, 'Avg' => $Value, 'Max' => $Value));
     21      $Output = 'Hodnota '.$Value.' uložena';
     22    } else throw new Exception('Access denied');
     23  }
     24 
     25  function Load($Id)
     26  {
     27    $Result = $this->Database->select('Measure', '*', 'Id='.$Id);
     28    if($Result->num_rows > 0)
     29    {
     30      $this->Data = $Result->fetch_assoc();
     31      if($this->Data['Continuity'] == 0) $this->Data['ContinuityEnabled'] = 0;  // non continuous
     32        else $this->Data['ContinuityEnabled'] = 2;    // continuous graph
     33    } else throw new Exception('Measure not found');
     34  }
     35 
     36  function TimeSegment($Base, $Level)
     37  {
     38    return(pow($this->LevelReducing, $Level) * $Base);
     39  }
     40
     41  function StatTableName($Level)
     42  {
     43    if($Level == 0) return('Data');
     44      else return('DataCache');
     45  }
     46
     47  function AlignTime($Time, $TimeSegment)
     48  {
     49    return(round(($Time - $this->ReferenceTime) / $TimeSegment) * $TimeSegment + $this->ReferenceTime);
     50  }
     51
     52
     53  function AddValue($Value = array('Min' => 0, 'Avg' => 0, 'Max' => 0), $Level = 0, $Time = 0)
     54  {
     55    if($Time == 0) $Time = time();
     56    //$Value = round($Measure['divider'] * $Value);
     57    //echo(TimeToMysqlDateTime($Time).'|'.$Level."\n");
     58
     59    $Result = $Database->select($this->Data['DataTable'], '*', 'Measure='.$this->Data['Id'].' AND Level='.$Level.' ORDER BY Time DESC LIMIT 2');
     60    //echo($Database->LastQuery."\n");
     61    if($Result->num_rows == 0)
     62    {
     63       $Database->insert($this->Data['DataTable'], array('Min' => $Value['Min'], 'Avg' => $Value['Avg'], 'Max' => $Value['Max'], 'Level' => $Level, 'Measure' => $this->Data['Id'], 'Time' => $this->Database->TimeToMysqlDateTime($Time), 'Continuity' => 0));
     64       //echo($Database->LastQuery."\n");
     65    } else if($Result->num_rows == 1)
     66    {
     67      $Database->insert($this->Data['DataTable'], array('Min' => $Value['Min'], 'Avg' => $Value['Avg'], 'Max' => $Value['Max'], 'Level' => $Level, 'Measure' => $this->Data['Id'], 'Time' => $this->Database->TimeToMysqlDateTime($Time), 'Continuity' => 1));
     68      //echo($Database->LastQuery."\n");
     69    } else
     70    {
     71      $LastValue = $Result->fetch_assoc();
     72      $NextToLastValue = $Result->fetch_assoc();
     73      //echo($Level.': '.$Time.' - '.MysqlDateTimeToTime($LastValue['Time']).' '.($Time - MysqlDateTimeToTime($LastValue['Time'])).' = '.$Measure['Period']."\n");
     74      if((($Time - $this->Database->MysqlDateTimeToTime($LastValue['Time'])) < 0.75 * $this->Data['Period']) and ($Level == 0))
     75      {
     76        echo('Too short period\n');
     77      } else
     78      {
     79        if(($Time - MysqlDateTimeToTime($LastValue['Time'])) < 1.25 * $this->Data['Period']) $Continuity = 1;
     80          else $Continuity = 0;
     81        echo('('.$LastValue['Avg'].'=='.$NextToLastValue['Avg'].') and ('.$LastValue['Avg'].' == '.$Value.') and ('.$LastValue['Continuity'].' == 1) and ('.$Continuity.' == 1))'."\n");
     82        if(($LastValue['Min'] == $NextToLastValue['Min']) and ($LastValue['Min'] == $Value['Min']) and ($LastValue['Avg'] == $NextToLastValue['Avg']) and ($LastValue['Avg'] == $Value['Avg']) and ($LastValue['Max'] == $NextToLastValue['Max']) and ($LastValue['Max'] == $Value['Max']) and ($LastValue['Continuity'] == 1) and ($Continuity == 1))
     83        {
     84          $this->Database->update($this->Data['DataTable'], '(Time="'.$LastValue['Time'].'") AND (Level='.$Level.') AND (Measure='.$Measure['Id'].')', array('Time' => $this->Database->TimeToMysqlDateTime($Time)));
     85          echo($this->Database->LastQuery."\n");
     86        } else
     87        {
     88          $Database->insert($this->Data['DataTable'], array('Min' => $Value['Min'], 'Avg' => $Value['Avg'], 'Max' => $Value['Max'], 'Level' => $Level, 'Measure' => $Measure['Id'], 'Time' => $this->Database->TimeToMysqlDateTime($Time), 'Continuity' => $Continuity));
     89          echo($this->Database->LastQuery."\n");
     90        }
     91      } 
     92
     93      // Update next level
     94      if($Level < $MaxLevel)
     95      {
     96        $Level = $Level + 1;
     97        //echo('Level '.$Level."<br>\n");
     98        $TimeSegment = $this->TimeSegment($this->Data['Period'], 1);
     99        $EndTime = $this->AlignTime($Time, $TimeSegment);
     100        //if($EndTime < $Time) $EndTime = $EndTime + $TimeSegment;
     101        $StartTime = $EndTime - $TimeSegment;
     102
     103        //echo(" ".$TimeSegment." ".$StartTime.'-'.$EndTime."<br>\n");
     104        //flush();
     105
     106        // Load values in time range
     107        $Values = array();
     108        //.'" AND Time < "'.TimeToMysqlDateTime($EndTime).'" AND Measure='.$Measure['Id'].' AND Level='.($Level - 1).' ORDER BY Time');
     109        $Result = $this->Database->select($this->Data['DataTable'], '*', '(Time > "'.$this->Database->TimeToMysqlDateTime($StartTime).'") AND (Time < "'.$this->Database->TimeToMysqlDateTime($EndTime).'") AND (Measure='.$this->Data['Id'].') AND (Level='.($Level - 1).') ORDER BY Time');
     110        while($Row = $Result->fetch_assoc())
     111        {
     112          $Row['Time'] = $this->Database->MysqlDateTimeToTime($Row['Time']);
     113          $Values[] = $Row;
     114        }
     115        //if(count($Values) > 2)
     116        {
     117          //print_r($Values);
     118          //array_pop($Values);
     119
     120          // Load subsidary values
     121          print_r($Values);
     122          $Values = array_merge($this->LoadLeftSideValue($Level - 1, $this->Data, $StartTime), $Values, $this->LoadRightSideValue($Level - 1, $Measure, $EndTime));
     123          print_r($Values);
     124
     125          $Point = $this->ComputeOneValue($StartTime, $EndTime, $Values, $this->Data, $Level);
     126        //print_r($Point);
     127
     128          $this->Database->delete($this->Data['DataTable'], '(Time > "'.$this->Database->TimeToMysqlDateTime($StartTime).'") AND (Time < "'.$this->Database->TimeToMysqlDateTime($EndTime).'") AND Measure='.$this->Data['Id'].' AND Level='.$Level);
     129          $this->Data['Period'] = $TimeSegment;
     130          $this->AddValue($this->Data, array('Min' => $Point['Min'], 'Avg' => $Point['Avg'], 'Max' => $Point['Max']), $Level, $StartTime + ($EndTime - $StartTime) / 2);
     131        }
     132      }
     133    }
     134  }
     135
     136  function Interpolation($X1, $Y1, $X2, $Y2, $X)
     137  {
     138    $Y = ($Y2 - $Y1) / ($X2 - $X1) * ($X - $X1) + $Y1;
     139    //echo($Y1.'-'.$Y.'-'.$Y2.' '.$X1.'-'.$X.'-'.$X2.'<br>');
     140    return($Y);
     141  }
     142
     143  function ComputeOneValue($LeftTime, $RightTime, $Values, $Level)
     144  {
     145    $NewValue = array('Min' => +1000000000000000000, 'Avg' => 0, 'Max' => -1000000000000000000);
     146
     147    // Trim outside parts
     148    foreach($this->ValueTypes as $ValueType)
     149    {
     150      $Values[0][$ValueType] = $this->Interpolation($Values[0]['Time'], $Values[0][$ValueType], $Values[1]['Time'], $Values[1][$ValueType], $LeftTime);
     151    }
     152    $Values[0]['Time'] = $LeftTime;
     153    foreach($this->ValueTypes as $ValueType)
     154    {
     155        $Values[count($Values) - 1][$ValueType] = $this->Interpolation($Values[count($Values) - 2]['Time'], $Values[count($Values) - 2][$ValueType],
     156        $Values[count($Values) - 1]['Time'], $Values[count($Values) - 1][$ValueType], $RightTime);
     157    }
     158    $Values[count($Values) - 1]['Time'] = $RightTime;
     159
     160    // Perform computation
     161    foreach($this->ValueTypes as $ValueType)
     162    {
     163      // Compute new value
     164      for($I = 0; $I < (count($Values) - 1); $I++)
     165      {
     166        if($ValueType == 'Avg')
     167        {
     168          if($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled']);
     169          else if($this->Differential == 0)
     170          {
     171            $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['Time'] - $Values[$I]['Time']) *
     172              (($Values[$I + 1][$ValueType] - $Values[$I][$ValueType]) / 2 + $Values[$I][$ValueType]);
     173          } else
     174          {
     175            $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['Time'] - $Values[$I]['Time']) *
     176              (($Values[$I + 1][$ValueType] - $Values[$I][$ValueType]) / 2);
     177          }
     178        }
     179        else if($ValueType == 'Max')
     180        {
     181          if($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled'])
     182          {
     183            if(0 > $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
     184          } else
     185          {
     186            if($this->Differential == 0)
     187            {
     188              if($Values[$I + 1][$ValueType] > $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
     189            } else {
     190              $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType];
     191              if($Difference > $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
     192            }
     193          }
     194        }
     195        else if($ValueType == 'Min')
     196        {
     197          //echo($Values[$I+1]['continuity'].'=='.$Measure['ContinuityEnabled'].'<br>');
     198          if($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled'])
     199          {
     200            if(0 < $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
     201          } else
     202          {
     203            if($this->Differential == 0)
     204            {
     205              if($Values[$I + 1][$ValueType] < $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
     206            } else {
     207              $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType];
     208              if($Difference < $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
     209            }
     210          }
     211        }
     212      }
     213      $NewValue[$ValueType] = $NewValue[$ValueType];
     214    }
     215    //if(($RightTime - $LeftTime) > 0)
     216    if($this->Data['Cumulative'] == 0)
     217    {
     218      $NewValue['Avg'] = $NewValue['Avg'] / ($RightTime - $LeftTime);
     219    }
     220    return($NewValue);
     221    //echo($NewValue['avg'].'<br>');
     222    //return(array('min' => rand(0,1), 'avg' => $NewValue['avg'], 'max' => rand(0,1)));
     223  }
     224
     225  function GetTimeRange($Level)
     226  {
     227    // Get first and last time
     228    //echo($Measure['Id'].','.$Level.','.StatTableName($Level)."\n");
     229    $Result = $this->Database->select($this->Data['DataTable'], '*', 'Measure='.$this->Data['Id'].' AND Level='.$Level.' ORDER BY Time LIMIT 1');
     230    if($Result->num_rows > 0)
     231    {
     232      $Row = $Result->fetch_assoc();
     233      $AbsoluteLeftTime = MysqlDateTimeToTime($Row['Time']);
     234    } else $AbsoluteLeftTime = 0;
     235
     236    $Result = $this->Database->select($this->Data['DataTable'], '*', 'Measure='.$this->Data['Id'].' AND Level='.$Level.' ORDER BY Time DESC LIMIT 1');
     237    if($Result->num_rows > 0)
     238    {
     239      $Row = $Result->fetch_assoc();
     240      $AbsoluteRightTime = MysqlDateTimeToTime($Row['Time']);
     241    } else $AbsoluteRightTime = 0;
     242
     243    if($this->Debug)
     244    {
     245      echo('AbsoluteLeftTime: '.$AbsoluteLeftTime.'('.$this->Database->TimeToMysqlDateTime($AbsoluteLeftTime).')<br>');
     246      echo('AbsoluteRightTime: '.$AbsoluteRightTime.'('.$this->Database->TimeToMysqlDateTime($AbsoluteRightTime).')<br>');
     247    }
     248    return(array('Left' => $AbsoluteLeftTime, 'Right' => $AbsoluteRightTime));
     249  }
     250
     251  function LoadRightSideValue($Level, $Time)
     252  {
     253    $Result = array();
     254    $DbResult = $this->Database->select($this->Data['DataTable'], '*', 'Time > "'.$this->Database->TimeToMysqlDateTime($Time).'" AND Measure='.$this->Data['Id'].' AND Level='.$Level.' ORDER BY Time ASC LIMIT 1');
     255    if($DbResult->num_rows > 0)
     256    {
     257      $Row = $DbResult->fetch_assoc();
     258      $Row['Time'] = $this->Database->MysqlDateTimeToTime($Row['Time']);
     259      return(array($Row));
     260    } else
     261    {
     262      //$Time = $Values[count($Values)-1]['Time'] + 60;
     263      //array_push($Values, array('Time' => $Time, 'Min' => 0, 'Avg' => 0, 'Max' => 0, 'Continuity' => 0));
     264      $Result[] = array('Time' => ($Time + $this->TimeSegment($this->Data['Period'], $Level)), 'Min' => 0, 'Avg' => 0, 'Max' => 0, 'Continuity' => 0);
     265      $DbResult = $this->Database->select($this->Data['DataTable'], '*', 'Time < "'.$this->Database->TimeToMysqlDateTime($Time).'" AND Measure='.$this->Data['Id'].' AND Level='.$Level.' ORDER BY Time DESC LIMIT 1');
     266      if($DbResult->num_rows > 0)
     267      {
     268        $Row = $DbResult->fetch_assoc();
     269        array_unshift($Result, array('Time' => ($this->Database->MysqlDateTimeToTime($Row['Time']) + 10), 'Min' => 0, 'Avg' => 0, 'Max' => 0, 'Continuity' => 0));
     270      }
     271     // if($Debug) print_r($Result);
     272      return($Result);
     273    }
     274  }
     275
     276  function LoadLeftSideValue($Level, $Time)
     277  {
     278    $Result = array();
     279    //echo('SELECT * FROM '.StatTableName($Level). ' WHERE '. 'Time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY Time DESC LIMIT 1'."<br>\n");
     280    $DbResult = $this->Database->select($this->Data['DataTable'], '*', 'Time < "'.$this->Database->TimeToMysqlDateTime($Time).'" AND Measure='.$this->Data['Id'].' AND Level='.$Level.' ORDER BY Time DESC LIMIT 1');
     281    if($DbResult->num_rows > 0)
     282    {
     283      $Row = $DbResult->fetch_assoc();
     284      $Row['Time'] = $this->Database->MysqlDateTimeToTime($Row['Time']);
     285      return(array($Row));
     286    } else
     287    {
     288      //$Time = $Values[0]['Time'] - 60;
     289      //array_unshift($Values, array('Time' => $Time, 'Min' => 0, 'Avg' => 0, 'Max' => 0, 'Continuity' => 0));
     290      $Result[] = array('Time' => ($Time - $this->TimeSegment($this->Data['Period'], $Level)), 'Min' => 0, 'Avg' => 0, 'Max' => 0, 'Continuity' => 0);
     291
     292      $DbResult = $this->Database->select($this->Data['DataTable'], '*', 'Time > "'.$this->Database->TimeToMysqlDateTime($Time).'" AND Measure='.$this->Data['Id'].' AND Level='.$Level.' ORDER BY Time ASC LIMIT 1');
     293      if($DbResult->num_rows > 0)
     294      {
     295        $Row = $DbResult->fetch_assoc();
     296        array_push($Result, array('Time' => ($this->Database->MysqlDateTimeToTime($Row['Time']) - 10), 'Min' => 0, 'Avg' => 0, 'Max' => 0, 'Continuity' => 0));
     297      }
     298//    if($Debug) print_r($Result);
     299      return($Result);
     300    }
     301  }
     302
     303  function GetValues($TimeFrom, $TimeTo, $Level)
     304  {
     305    if($this->Debug) echo('TimeFrom: '.$TimeFrom.'('.$this->Database->TimeToMysqlDateTime($TimeFrom).')<br>');
     306    if($this->Debug) echo('TimeTo: '.$TimeTo.'('.$this->Database->TimeToMysqlDateTime($TimeTo).')<br>');
     307
     308    //$AbsoluteTime = GetTimeRange($MeasureId);
     309
     310    //  if(($TimeFrom > $AbsoluteLeftTime) and ($TimeStart < $AbsoluteRightTime) and
     311    //    ($TimeTo > $AbsoluteLeftTime) and ($TimeTo < $AbsoluteRightTime))
     312    //  {
     313
     314    // Load values in time range
     315    $Result = $this->Database->select($this->Data['DataTable'], 'Time, Min, Avg, Max, Continuity', '(Time > "'.$this->Database->TimeToMysqlDateTime($TimeFrom).'") AND (Time < "'.$this->Database->TimeToMysqlDateTime($TimeTo).'") AND (Measure='.$this->Data['Id'].') AND (Level='.$Level.') ORDER BY Time');
     316    //  echo($Level.' '.TimeToMysqlDateTime($TimeFrom).' '.TimeToMysqlDateTime($TimeTo));
     317    $Values = array();
     318    //  echo(DB_NumRows());
     319    //  $III = 0;
     320    while($Row = $Result->fetch_assoc())
     321    {
     322      //    echo($III.' '.$Row['Time'].' '.memory_get_usage().',');
     323      //    $III++;
     324      $Values[] = array('Time' => $this->Database->MysqlDateTimeToTime($Row['Time']), 'Min' => $Row['Min'], 'Avg' => $Row['Avg'], 'Max' => $Row['Max'], 'Continuity' => $Row['Continuity']);
     325    }
     326    // array_pop($Values);
     327    //  echo('abc');
     328    //  die();
     329    if($this->Debug) echo('Item count: '.count($Values));
     330 
     331    $Points = array();
     332    if(count($Values) > 0)
     333    {
     334      $Values = array_merge($this->LoadLeftSideValue($Level, $TimeFrom), $Values, $this->LoadRightSideValue($Level, $TimeTo));
     335      //echo(count($Values).'<br>');
     336      //echo($TimeFrom.','.$TimeTo.'<br>');
     337      //echo($Values[0]['Time'].'<br>');
     338      $StartIndex = 0;
     339      $Points = array();
     340      //echo($DivisionCount.'<br>');
     341      if($this->Debug) print_r($Values);
     342      //die();
     343      for($I = 0; $I < $this->DivisionCount; $I++)
     344      {
     345        $TimeStart = $TimeFrom + (($TimeTo - $TimeFrom) / $this->DivisionCount) * $I;
     346        //if($Debug) echo('TimeStart '.$I.': '.$TimeStart.'('.TimeToMysqlDateTime($TimeStart).')<br>');
     347        $TimeEnd = $TimeFrom + (($TimeTo - $TimeFrom) / $this->DivisionCount) * ($I + 1);
     348        if($this->Debug) echo('TimeEnd '.$I.': '.$TimeEnd.'('.$this->Database->TimeToMysqlDateTime($TimeEnd).')<br>');
     349        //echo($TimeStart.','.$TimeEnd.'<br>');
     350
     351        $EndIndex = $StartIndex;
     352        //while(($Values[$EndIndex]['Time'] < $TimeEnd) and ($EndIndex < count($Values))) $EndIndex = $EndIndex + 1;
     353        while(($Values[$EndIndex]['Time'] < $TimeEnd)) $EndIndex = $EndIndex + 1;
     354        $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
     355        //echo($StartIndex.','.$EndIndex.' '.count($SubValues).'<br>');
     356        //print_r($SubValues);
     357        $Points[] = $this->ComputeOneValue($TimeStart, $TimeEnd, $SubValues, $Level);
     358        $StartIndex = $EndIndex - 1;
     359      }
     360      if($this->Debug) print_r($Points);
     361    } else $Points[] = array('Min' => 0, 'Avg' => 0, 'Max' => 0);
     362    return($Points);
     363  }
     364
     365  function RebuildMeasureCache()
     366  {
     367    echo('Velicina '.$this->Data['Name']."<br>\n");
     368    if($this->Data['Continuity'] == 0) $this->Data['ContinuityEnabled'] = 0;  // non continuous
     369      else $this->Data['ContinuityEnabled'] = 2;    // continuous graph
     370
     371    // Clear previous items
     372    $DbResult = $this->Database->select($this->Data['DataTable'], 'COUNT(*)', 'Level > 0 AND Measure='.$this->Data['Id']);
     373    $Row = $DbResult->fetch_row();
     374    echo("Mazu starou cache (".$Row[0]." polozek)...");
     375    $this->Database->delete($this->Data['DataTable'], 'Level > 0 AND Measure='.$this->Data['Id']);
     376    echo("<br>\n");
     377
     378    for($Level = 1; $Level <= $this->MaxLevel; $Level++)
     379    {
     380      echo('Uroven '.$Level."<br>\n");
     381      $TimeRange = $this->GetTimeRange($Level - 1);
     382      //echo($Measure['Id'].','.($Level-1)."\n");
     383      //echo(TimeToMysqlDateTime($TimeRange['left']).'-'.TimeToMysqlDateTime($TimeRange['right'])."\n");
     384      $TimeSegment = $this->TimeSegment($this->Data['Period'], $Level);
     385      $StartTime = $this->AlignTime($TimeRange['Left'], $TimeSegment) - $TimeSegment;
     386      $EndTime = $this->AlignTime($TimeRange['Right'], $TimeSegment);
     387      $BurstCount = 500;
     388      echo('For 0 to '.round(($EndTime - $StartTime) / $TimeSegment / $BurstCount)."<br>\n");
     389      for($I = 0; $I <= round(($EndTime - $StartTime) / $TimeSegment / $BurstCount); $I++)
     390      {
     391        echo($I.' ');
     392        $StartTime2 = $StartTime + $I * $BurstCount * $TimeSegment;
     393        $EndTime2 = $StartTime + ($I + 1) * $BurstCount * $TimeSegment;
     394        $Values = array();
     395        $DbResult = $this->Database->select($this->Data['DataTable'], '*', 'Time > "'.$this->Database->TimeToMysqlDateTime($StartTime2).'" AND Time < "'.$this->Database->TimeToMysqlDateTime($EndTime2).'" AND Measure='.$Measure['Id'].' AND Level='.($Level - 1).' ORDER BY Time');
     396        while($Row = $DbResult->fetch_assoc())
     397        {
     398          $Row['Time'] = $this->Database->MysqlDateTimeToTime($Row['Time']);
     399          $Values[] = $Row;
     400        }
     401
     402        if(count($Values) > 0)
     403        {
     404          $Values = array_merge($this->LoadLeftSideValue($Level - 1, $StartTime2), $Values, $this->LoadRightSideValue($Level - 1, $Measure, $EndTime2));
     405
     406          $StartIndex = 0;
     407          for($B = 0; $B < $BurstCount; $B++)
     408          {
     409            echo('.');
     410            $StartTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * $B;
     411            $EndTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * ($B + 1);
     412
     413            $EndIndex = $StartIndex;
     414            while($Values[$EndIndex]['Time'] < $EndTime3) $EndIndex = $EndIndex + 1;
     415            $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
     416            //echo($StartIndex.','.$EndIndex.' '.count($SubValues).'<br>');
     417            //print_r($SubValues);
     418            if(count($SubValues) > 2)
     419            {
     420              $Point = $this->ComputeOneValue($StartTime3, $EndTime3, $SubValues, $Level);
     421              $Continuity = $SubValues[1]['Continuity'];
     422              $this->Database->insert($this->Data['DataTable'], array('Level' => $Level, 'Measure' => $this->Data['Id'], 'Min' => $Point['Min'], 'Avg' => $Point['avg'], 'max' => $Point['Max'], 'Continuity' => $Continuity, 'Time' => $this->Database->TimeToMysqlDateTime($StartTime3 + ($EndTime3 - $StartTime3) / 2)));
     423            }
     424            $StartIndex = $EndIndex - 1;
     425          }
     426        }
     427        // Load values in time range
     428        //array_pop($NextValues);
     429      } 
     430      echo("Uroven dokoncena<br>\n");
     431      $DbResult = $this->Database->select($this->Data['DataTable'], 'COUNT(*)', 'Level='.$Level.' AND Measure='.$this->Data['Id']);
     432      $Row = $DbResult->fetch_row();
     433      echo("Vloženo ".$Row[0]." položek.<br>\n");
     434    }
     435  }
     436
     437  function RebuildAllMeasuresCache()
     438  {
     439    // echo("Vytvarim novou cache...\n");
     440    // Load measures
     441    $Measures = array();
     442    $Result = $Database->select('Measure', '*');
     443    while($Row = $Result->fetch_assoc())
     444    {
     445      $Measures = new Measure();
     446      $Measure->Load($Row['Id']);
     447    }
     448
     449    foreach($Measures as $Measure)
     450    {
     451      $Measure->RebuildMeasureCache();
     452      echo('Velicina dokoncena<br>');
     453    }
     454  }
     455
     456  function InitMeasureDataTable()
     457  {
     458    $this->Database->query('CREATE TABLE `Data'.$this->Data['Name'].'` (
     459`Time` TIMESTAMP NOT NULL ,
     460`Avg` '.$this->Data['DataType'].' NOT NULL ,
     461`Continuity` BOOL NOT NULL
     462) ENGINE = MYISAM ;');
     463
     464    $this->Database->query('CREATE TABLE `Data'.$this->Data['Name'].'Cache` (
     465`Time` TIMESTAMP NOT NULL ,
     466`Level` TINYINT NOT NULL ,
     467`Min` '.$this->Data['DataType'].' NOT NULL ,
     468`Avg` '.$this->Data['DataType'].' NOT NULL ,
     469`Max` '.$this->Data['DataType'].' NOT NULL ,
     470`Continuity` BOOL NOT NULL
     471) ENGINE = MYISAM ;');
    19472  }
    20473}
  • trunk/Application/Model/stat_functions.php

    r42 r43  
    5555}
    5656
    57 function GetMicrotime()
    58 {
    59   list($Usec, $Sec) = explode(" ", microtime());
    60   return ((float)$Usec + (float)$Sec);
    61 }
    62 
    6357function MysqlDateTimeToTime($Time)
    6458{
     
    9488}
    9589
    96 function GetMeasureById($Id)
    97 {
    98   global $Database;
    99 
    100   $Result = $Database->select('measure', '*', 'Id='.$Id);
    101   if($Result->num_rows > 0)
    102   {
    103     $Measure = $Result->fetch_assoc();
    104     if($Measure['Continuity'] == 0) $Measure['ContinuityEnabled'] = 0;  // non continuous
    105       else $Measure['ContinuityEnabled'] = 2;    // continuous graph
    106   } else die('Měřená veličina nenalezena');
    107   return($Measure);
    108 }
    10990
    11091function AddValue($Measure, $Value = array('min' => 0, 'avg' => 0, 'max' => 0), $Level = 0, $Time = 0)
Note: See TracChangeset for help on using the changeset viewer.