Changeset 43


Ignore:
Timestamp:
Nov 24, 2009, 2:03:29 PM (14 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
Files:
12 added
3 deleted
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/Application/Controller/Graph.php

    r40 r43  
    11<?php
     2
     3include_once(dirname(__FILE__).'/../../Base/Controller.php');
     4include_once(dirname(__FILE__).'/../View/Graph.php');
    25
    36class GraphController extends Controller
     
    69  {
    710    $View = new GraphView($this->System);
    8     retunr($View->Render());
     11    return($View->Render());
    912  }
    1013}
  • 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)
  • trunk/Application/View/Graph.php

    r40 r43  
    11<?php
     2
     3include_once(dirname(__FILE__).'/../../Base/View.php');
     4include_once(dirname(__FILE__).'/../Model/Measure.php');
    25
    36class GraphView extends View
     
    58  function Render()
    69  {
    7 if(array_key_exists('Debug', $_GET)) $Debug = $_GET['Debug'];
    8   else $Debug = 0;
    9  
    10 if(!array_key_exists('From',$_GET)) die('Musíte zadat čas počátku');
    11 $StartTime = addslashes($_GET['From']);
    12 if(!array_key_exists('To',$_GET)) die('Musíte zadat čas konce');
    13 $EndTime = addslashes($_GET['To']);
    14 if($EndTime < $StartTime) $EndTime = $StartTime + 60;
    15 $TimeDifference = $EndTime - $StartTime;
    16 if(!array_key_exists('Measure',$_GET)) die('Musíte zadat měřenou veličinu');
    17 $MeasureId = addslashes($_GET['Measure']);
    18 if(!array_key_exists('Width', $_GET)) $Width = $DefaultWidth;
    19 else $Width = addslashes($_GET['Width']);
    20 if(!array_key_exists('Height', $_GET)) $Height = $DefaultHeight;
    21 else $Height = addslashes($_GET['Height']);
    22 if(!array_key_exists('Differential', $_GET)) $Differential = $Config['DefaultVariables']['Differential'];
    23 else $Differential = addslashes($_GET['Differential']);
    24 $VerticalLinesCount = round($Height / ($FontSize + 4));
    25 
    26 $StopWatchStart = GetMicrotime();
    27 
    28 $Measure = GetMeasureById($MeasureId);
    29 
    30 $Level = floor(log(($EndTime - $StartTime) / $DivisionCount / 60) / log($LevelReducing)) - 1;
    31 if($Level < 0) $Level = 0;
    32 if($Level > $MaxLevel) $Level = $MaxLevel;
    33 //$Level = 0;
    34 
    35 
    36 $Points = GetValues($Measure, $StartTime, $EndTime, $Level);
    37 
    38 if($Debug) echo('Points count: '.count($Points).'<br>');
    39 //if($Debug) foreach($Points as $Index => $Item)
    40 //  echo($Index.': '.$Item['min'].'<br>');
    41 
    42 // Calculate total max, avg, min value
    43 $MaxValue = -1000000000000000000;
    44 $AvgValue = 0;
    45 $MinValue = 1000000000000000000;
    46 foreach($Points as $Index => $Item)
    47 {
    48   //$Points[$Index]['min'] =  $Points[$Index]['min'] / $Measure['Divider'];
    49   //$Points[$Index]['avg'] =  $Points[$Index]['avg'] / $Measure['Divider'];
    50   //$Points[$Index]['max'] =  $Points[$Index]['max'] / $Measure['Divider'];
    51   if($Points[$Index]['avg'] > $MaxValue) $MaxValue = $Points[$Index]['avg'];
    52   if($Points[$Index]['avg'] < $MinValue) $MinValue = $Points[$Index]['avg'];
    53   if($Points[$Index]['max'] > $MaxValue) $MaxValue = $Points[$Index]['max'];
    54   if($Points[$Index]['min'] < $MinValue) $MinValue = $Points[$Index]['min'];
    55   $AvgValue = $AvgValue + $Points[$Index]['avg'];
    56 }
    57 //$MinValue = round($MinValue * $Measure['Divider']) / $Measure['Divider'];
    58 //$MaxValue = round($MaxValue * $Measure['Divider']) / $Measure['Divider'];
    59 $AvgValue = $AvgValue / count($Points); //round( * $Measure['Divider']) / $Measure['Divider'];
    60 
    61 // Generate polygon and recalculate y values to fit graph height
    62 $PointsMin = array(0, $Height-1);
    63 $PointsAvg = array(0, $Height-1);
    64 $PointsMax = array(0, $Height-1);
    65 if(($MaxValue - $MinValue) == 0) $MaxValue = $MinValue + 1;
    66 {
    67   foreach($Points as $Index => $Item)
    68   {
    69     $PointsMin[] = $Index*$Width/$DivisionCount;
    70     $PointsMin[] = $Height - 1 - ($Points[$Index]['min'] - $MinValue) / ($MaxValue - $MinValue) * $Height * $K;
    71     $PointsAvg[] = $Index*$Width/$DivisionCount;
    72     $PointsAvg[] = $Height - 1 - ($Points[$Index]['avg'] - $MinValue) / ($MaxValue - $MinValue) * $Height * $K;
    73     $PointsMax[] = $Index*$Width/$DivisionCount;
    74     $PointsMax[] = $Height - 1 - ($Points[$Index]['max'] - $MinValue) / ($MaxValue - $MinValue) * $Height * $K;
    75     //echo($Index.' - '.$Item.' '.$Points[$Index].'<br>');
     10    if(array_key_exists('Debug', $_GET)) $Debug = $_GET['Debug'];
     11      else $Debug = 0;
     12 
     13    if(!array_key_exists('From', $_GET)) die('Musíte zadat čas počátku');
     14    $StartTime = addslashes($_GET['From']);
     15    if(!array_key_exists('To', $_GET)) die('Musíte zadat čas konce');
     16    $EndTime = addslashes($_GET['To']);
     17    if($EndTime < $StartTime) $EndTime = $StartTime + 60;
     18    $TimeDifference = $EndTime - $StartTime;
     19    if(!array_key_exists('Measure', $_GET)) die('Musíte zadat měřenou veličinu');
     20    $MeasureId = addslashes($_GET['Measure']);   
     21    if(!array_key_exists('Width', $_GET)) $Width = $DefaultWidth;
     22      else $Width = addslashes($_GET['Width']);
     23    if(!array_key_exists('Height', $_GET)) $Height = $DefaultHeight;
     24      else $Height = addslashes($_GET['Height']);
     25    if(!array_key_exists('Differential', $_GET)) $Differential = $this->Config['Application']['DefaultVariables']['Differential'];
     26      else $Differential = addslashes($_GET['Differential']);
     27    $VerticalLinesCount = round($Height / ($this->Config['Application']['FontSize'] + 4));
     28
     29    $StopWatchStart = $this->System->GetMicrotime();
     30
     31    $Measure = new Measure($this->System);
     32    $Measure->Load($MeasureId);
     33    $Measure->LevelReducing = $this->Config['Application']['LevelReducing'];
     34    $Measure->ReferenceTime = $this->Config['Application']['ReferenceTime'];
     35    $Measure->MaxLevel = $this->Config['Application']['MaxLevel'];
     36    $Measure->DivisionCount = $this->Config['Application']['DivisionCount'];
     37    $Measure->Differential = $this->Config['Application']['DefaultVariables']['Differential'];
     38    $Measure->Debug = $Debug;
     39
     40    $FontSize = $this->Config['Application']['FontSize'];
     41    $FontFile = dirname(__FILE__).'/../Style/'.$this->Config['System']['Style'].'/'.$this->Config['Application']['FontFileName'];
     42
     43    $Level = floor(log(($EndTime - $StartTime) / $Measure->DivisionCount / 60) / log($Measure->LevelReducing)) - 1;
     44    if($Level < 0) $Level = 0;
     45    if($Level > $Measure->MaxLevel) $Level = $Measure->MaxLevel;
     46    //$Level = 0;
     47
     48    $Points = $Measure->GetValues($StartTime, $EndTime, $Level);
     49
     50    if($Debug) echo('Points count: '.count($Points).'<br/>');
     51    //if($Debug) foreach($Points as $Index => $Item)
     52    // echo($Index.': '.$Item['min'].'<br>');
     53
     54    // Calculate total max, avg, min value
     55    $MaxValue = -1000000000000000000;
     56    $AvgValue = 0;
     57    $MinValue = 1000000000000000000;
     58    foreach($Points as $Index => $Item)
     59    {
     60      //$Points[$Index]['min'] =  $Points[$Index]['min'] / $Measure['Divider'];
     61      //$Points[$Index]['avg'] =  $Points[$Index]['avg'] / $Measure['Divider'];
     62      //$Points[$Index]['max'] =  $Points[$Index]['max'] / $Measure['Divider'];
     63      if($Points[$Index]['Avg'] > $MaxValue) $MaxValue = $Points[$Index]['Avg'];
     64      if($Points[$Index]['Avg'] < $MinValue) $MinValue = $Points[$Index]['Avg'];
     65      if($Points[$Index]['Max'] > $MaxValue) $MaxValue = $Points[$Index]['Max'];
     66      if($Points[$Index]['Min'] < $MinValue) $MinValue = $Points[$Index]['Min'];
     67      $AvgValue = $AvgValue + $Points[$Index]['Avg'];
     68    }
     69    //$MinValue = round($MinValue * $Measure['Divider']) / $Measure['Divider'];
     70    //$MaxValue = round($MaxValue * $Measure['Divider']) / $Measure['Divider'];
     71    $AvgValue = $AvgValue / count($Points); //round( * $Measure['Divider']) / $Measure['Divider'];
     72
     73    // Generate polygon and recalculate y values to fit graph height
     74    $PointsMin = array(0, $Height - 1);
     75    $PointsAvg = array(0, $Height - 1);
     76    $PointsMax = array(0, $Height - 1);
     77    if(($MaxValue - $MinValue) == 0) $MaxValue = $MinValue + 1;
     78    {
     79      foreach($Points as $Index => $Item)
     80      {
     81        $PointsMin[] = $Index * $Width / $Measure->DivisionCount;
     82        $PointsMin[] = $Height - 1 - ($Points[$Index]['Min'] - $MinValue) / ($MaxValue - $MinValue) * $Height * $this->Config['Application']['ValueToImageHeigthCoefficient'];
     83        $PointsAvg[] = $Index * $Width / $Measure->DivisionCount;
     84        $PointsAvg[] = $Height - 1 - ($Points[$Index]['Avg'] - $MinValue) / ($MaxValue - $MinValue) * $Height * $this->Config['Application']['ValueToImageHeigthCoefficient'];
     85        $PointsMax[] = $Index * $Width / $Measure->DivisionCount;
     86        $PointsMax[] = $Height - 1 - ($Points[$Index]['Max'] - $MinValue) / ($MaxValue - $MinValue) * $Height * $this->Config['Application']['ValueToImageHeigthCoefficient'];
     87        //echo($Index.' - '.$Item.' '.$Points[$Index].'<br>');
     88      }
     89    }
     90    $PointsMin[] = $Width - 1;
     91    $PointsMin[] = $Height - 1;
     92    $PointsAvg[] = $Width - 1;
     93    $PointsAvg[] = $Height - 1;
     94    $PointsMax[] = $Width - 1;
     95    $PointsMax[] = $Height - 1;
     96    $PointsMin[] = $Width - 1;
     97    $PointsMin[] = $Height - 1;
     98    $PointsAvg[] = $Width - 1;
     99    $PointsAvg[] = $Height - 1;
     100    $PointsMax[] = $Width - 1;
     101    $PointsMax[] = $Height - 1;
     102
     103
     104    //array_unshift($Points, $Height - 1);
     105    //array_unshift($Points, 0);
     106    //$Points[] = $Width - 1;
     107    //$Points[] = $Height - 1;
     108
     109    //print_r($PointsMax);
     110 
     111    // Generate image
     112    if(!$Debug)
     113    {
     114      Header('Content-type: image/png');
     115      Header('Cache-Control: no-cache');  // Dynamic graph - no cache
     116      $Image = @imagecreate($Width, $Height);
     117      $BackgroundColor = imagecolorallocate($Image, 255, 255, 255);
     118      $Black = imagecolorallocate($Image, 0, 0, 0);
     119      $White = imagecolorallocate($Image, 255, 255, 255);
     120      $Gray = imagecolorallocate($Image, 200, 200, 200);
     121      $DarkGray = imagecolorallocate($Image, 100, 100, 100);
     122      $LightBlue = imagecolorallocate($Image, 150, 150, 255);
     123      $Blue = imagecolorallocate($Image, 0, 0, 255);
     124      $LightRed = imagecolorallocate($Image, 255, 150, 150);
     125      $Red = imagecolorallocate($Image, 255, 0, 0);
     126      $Green = imagecolorallocate($Image, 0, 200, 0);
     127      $LightGreen = imagecolorallocate($Image, 150, 255, 150);
     128
     129      imagefilledpolygon($Image, $PointsMax, count($PointsMax) / 2, $LightRed);
     130      imagefilledpolygon($Image, $PointsAvg, count($PointsAvg) / 2, $LightGreen);
     131      imagefilledpolygon($Image, $PointsMin, count($PointsMin) / 2, $LightBlue);
     132 
     133      $TimeMarks = array(1, 60, 60*60, 60*60*24, 60*60*24*7, 60*60*24*30, 60*60*24*365, 60*60*24*365*10);
     134 
     135      $TimeRange = $EndTime - $StartTime;
     136      $TimeMarksIndex = 0;
     137      while(($TimeRange / $TimeMarks[$TimeMarksIndex]) > 1) $TimeMarksIndex += 1;
     138      if($TimeMarksIndex < 2) $TimeMarksIndex = 2;
     139      $MajorTimeMarks = $TimeMarks[$TimeMarksIndex - 1];
     140      $MinorTimeMarks = $TimeMarks[$TimeMarksIndex - 2]; 
     141   
     142      $TimeShift = $Measure->AlignTime($StartTime, $MajorTimeMarks) - $StartTime;
     143      //imagestring($Image, 10, 40, 50, $TimeShift, $Black);
     144 
     145      // Zobraz měřítko Y
     146      $VerticalLinesDistance = $Height / $VerticalLinesCount;
     147      for($I = 1; $I <= $VerticalLinesCount; $I++)
     148      {
     149        $Y = $Height - 1 - ($VerticalLinesDistance * $I);
     150        for($X = 1; $X < $Width; $X = $X + 3) imagesetpixel($Image, $X, $Y, $Gray);
     151        //imageline($Image, 30, $Y, $Width-1, $Y, IMG_COLOR_STYLED);
     152      }
     153
     154      $TimeShift = $Measure->AlignTime($StartTime, $MinorTimeMarks) - $StartTime;
     155 
     156      // Zobraz měřítko X
     157      $LastTextEnd = 0;
     158      for($Time = $StartTime; $Time < $EndTime; $Time += $MajorTimeMarks)
     159      {
     160        $X = round(($Time - $StartTime + $TimeShift) / $TimeRange * $Width) % $Width;
     161        //imageline($Image, 30, $Y, $Width-1, $Y, IMG_COLOR_STYLED);
     162        if(($MajorTimeMarks > 60 * 60 * 24)) $Text = date('j.n.Y', $Time + $TimeShift);
     163          else $Text = date('j.n.Y G:i', $Time + $TimeShift);
     164        $BoundBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
     165        if($LastTextEnd < ($X - ($BoundBox[2] - $BoundBox[0] + 20) / 2))
     166        {
     167          for($Y = 0; $Y < $Height; $Y = $Y + 1) imagesetpixel($Image, $X, $Y, $Gray);
     168          imagettftext($Image, $FontSize, 0, $X - ($BoundBox[2] - $BoundBox[0]) / 2,  $Height - 2, $Black, $FontFile, $Text);
     169          $LastTextEnd = $X + ($BoundBox[2] - $BoundBox[0]) / 2;
     170        }
     171        else for($Y = 0; $Y < $Height; $Y = $Y + 3) imagesetpixel($Image, $X, $Y, $Gray);
     172      }
     173 
     174      // Popisky osy Y
     175      for($I = 1; $I <= $VerticalLinesCount; $I++)
     176      {
     177        $Y = $Height - 1 - ($VerticalLinesDistance * $I);
     178        //$Y = $Height - 1 - ($VerticalLinesDistance * $I / ($MaxValue - $MinValue) * $this->Config['Application']['ValueToImageHeigthCoefficient'] * $Height);
     179        $Text = $this->System->AddPrefixMultipliers(round(($I * $VerticalLinesDistance / $Height / $this->Config['Application']['ValueToImageHeigthCoefficient'] * ($MaxValue - $MinValue) + $MinValue)), $Measure->Data['Unit'], 3);
     180        $BoundBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
     181        if(($Y - ($BoundBox[5] - $BoundBox[1]) / 2) > 10)
     182          imagettftext($Image, $FontSize, 0, 2,  $Y - ($BoundBox[5] - $BoundBox[1]) / 2, $Black, $FontFile, $Text);
     183      }
     184      $GenerationTime = floor(($this->System->GetMicrotime() - $StopWatchStart) * 1000  ) / 1000;
     185
     186      $Left = $Width - 10;
     187      $Text = '    Max. '.$this->System->AddPrefixMultipliers($MaxValue, $Measure->Data['Unit']);
     188      $BoundingBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
     189      $Left -= ($BoundingBox[2] - $BoundingBox[0]);
     190      imagettftext($Image, $FontSize, 0, $Left, 14, $Red, $FontFile, $Text);
     191
     192      $Text = '    Avg.  '.$this->System->AddPrefixMultipliers($AvgValue, $Measure->Data['Unit']);
     193      $BoundingBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
     194      $Left -= ($BoundingBox[2] - $BoundingBox[0]);
     195      imagettftext($Image, $FontSize, 0, $Left, 14, $Green, $FontFile, $Text);
     196
     197      $Text = '    Min.  '.$this->System->AddPrefixMultipliers($MinValue, $Measure->Data['Unit']);
     198      $BoundingBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
     199      $Left -= ($BoundingBox[2] - $BoundingBox[0]);
     200      imagettftext($Image, $FontSize, 0, $Left, 14, $Blue, $FontFile, $Text);
     201      //imagestring($Image, 2, 70, 20, 'Vygenerováno za '.$GenerationTime.' sekund', $Black);
     202      //imagestring($Image, 2, 50, 30, 'Level: '.$Level, $Black);
     203
     204      imagettftext($Image, $FontSize, 0, 70, 14, $Black, $FontFile, $Measure->Data['Description']);
     205      imagerectangle($Image, 0, 0, $Width - 1, $Height - 1, $Black);   // Frame border
     206      imagepng($Image);
     207      imagedestroy($Image);
     208    }   
    76209  }
    77210}
    78 $PointsMin[] = $Width-1;
    79 $PointsMin[] = $Height-1;
    80 $PointsAvg[] = $Width-1;
    81 $PointsAvg[] = $Height-1;
    82 $PointsMax[] = $Width-1;
    83 $PointsMax[] = $Height-1;
    84 $PointsMin[] = $Width-1;
    85 $PointsMin[] = $Height-1;
    86 $PointsAvg[] = $Width-1;
    87 $PointsAvg[] = $Height-1;
    88 $PointsMax[] = $Width-1;
    89 $PointsMax[] = $Height-1;
    90 
    91 
    92 //array_unshift($Points, $Height - 1);
    93 //array_unshift($Points, 0);
    94 //$Points[] = $Width - 1;
    95 //$Points[] = $Height - 1;
    96 
    97 //print_r($PointsMax);
    98 
    99 // Generate image
    100 if(!$Debug)
    101 {
    102   header("Content-type: image/png");
    103   header("Cache-Control: no-cache");  // Dynamic graph - no cache
    104   $Image = @imagecreate($Width, $Height);
    105   $BackgroundColor = imagecolorallocate($Image, 255, 255, 255);
    106   $Black = imagecolorallocate($Image, 0, 0, 0);
    107   $White = imagecolorallocate($Image, 255, 255, 255);
    108   $Gray = imagecolorallocate($Image, 200, 200, 200);
    109   $DarkGray = imagecolorallocate($Image, 100, 100, 100);
    110   $LightBlue = imagecolorallocate($Image, 150, 150, 255);
    111   $Blue = imagecolorallocate($Image, 0, 0, 255);
    112   $LightRed = imagecolorallocate($Image, 255, 150, 150);
    113   $Red = imagecolorallocate($Image, 255, 0, 0);
    114   $Green = imagecolorallocate($Image, 0, 200, 0);
    115   $LightGreen = imagecolorallocate($Image, 150, 255, 150);
    116 
    117   imagefilledpolygon($Image, $PointsMax, count($PointsMax) / 2, $LightRed);
    118   imagefilledpolygon($Image, $PointsAvg, count($PointsAvg) / 2, $LightGreen);
    119   imagefilledpolygon($Image, $PointsMin, count($PointsMin) / 2, $LightBlue);
    120  
    121   $TimeMarks = array(1, 60, 60*60, 60*60*24, 60*60*24*7, 60*60*24*30, 60*60*24*365, 60*60*24*365*10);
    122  
    123   $TimeRange = $EndTime - $StartTime;
    124   $TimeMarksIndex = 0;
    125   while(($TimeRange / $TimeMarks[$TimeMarksIndex]) > 1) $TimeMarksIndex += 1;
    126   if($TimeMarksIndex < 2) $TimeMarksIndex = 2;
    127   $MajorTimeMarks = $TimeMarks[$TimeMarksIndex - 1];
    128   $MinorTimeMarks = $TimeMarks[$TimeMarksIndex - 2]; 
    129    
    130   $TimeShift = AlignTime($StartTime, $MajorTimeMarks) - $StartTime;
    131   //imagestring($Image, 10, 40, 50, $TimeShift, $Black);
    132  
    133   // Zobraz měřítko Y
    134   $VerticalLinesDistance = $Height / $VerticalLinesCount;
    135   for($I=1; $I<=$VerticalLinesCount; $I++)
    136   {
    137     $Y = $Height - 1 - ($VerticalLinesDistance * $I);
    138     for($X=1; $X < $Width; $X = $X + 3) imagesetpixel($Image, $X, $Y, $Gray);
    139     //imageline($Image, 30, $Y, $Width-1, $Y, IMG_COLOR_STYLED);
    140   }
    141 
    142   $TimeShift = AlignTime($StartTime, $MinorTimeMarks) - $StartTime;
    143  
    144   // Zobraz měřítko X
    145   $LastTextEnd = 0;
    146   for($Time = $StartTime; $Time < $EndTime; $Time += $MajorTimeMarks)
    147   {
    148     $X = round(($Time - $StartTime + $TimeShift) / $TimeRange * $Width) % $Width;
    149     //imageline($Image, 30, $Y, $Width-1, $Y, IMG_COLOR_STYLED);
    150     if(($MajorTimeMarks > 60*60*24)) $Text = date('j.n.Y', $Time + $TimeShift);
    151       else $Text = date('j.n.Y G:i', $Time + $TimeShift);
    152     $BoundBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
    153     if($LastTextEnd < ($X - ($BoundBox[2] - $BoundBox[0] + 20) / 2))
    154     {
    155       for($Y=0; $Y < $Height; $Y = $Y + 1) imagesetpixel($Image, $X, $Y, $Gray);
    156       imagettftext($Image, $FontSize, 0, $X - ($BoundBox[2] - $BoundBox[0]) / 2,  $Height - 2, $Black, $FontFile, $Text);
    157       $LastTextEnd = $X + ($BoundBox[2] - $BoundBox[0]) / 2;
    158     }
    159     else for($Y=0; $Y < $Height; $Y = $Y + 3) imagesetpixel($Image, $X, $Y, $Gray);
    160   }
    161  
    162   // Popisky osy Y
    163   for($I=1; $I<=$VerticalLinesCount; $I++)
    164   {
    165     $Y = $Height - 1 - ($VerticalLinesDistance * $I);
    166     //$Y = $Height - 1 - ($VerticalLinesDistance * $I / ($MaxValue - $MinValue) * $K * $Height);
    167     $Text = AddPrefixMultipliers(round(($I * $VerticalLinesDistance / $Height / $K * ($MaxValue - $MinValue) + $MinValue)), $Measure['Unit'], 3);
    168     $BoundBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
    169     if(($Y - ($BoundBox[5] - $BoundBox[1]) / 2) > 10)
    170       imagettftext($Image, $FontSize, 0, 2,  $Y - ($BoundBox[5] - $BoundBox[1]) / 2, $Black, $FontFile, $Text);
    171   }
    172   $GenerationTime = floor((GetMicrotime() - $StopWatchStart) * 1000  ) / 1000;
    173 
    174   $Left = $Width - 10;
    175   $Text = "    Max. ".AddPrefixMultipliers($MaxValue, $Measure['Unit']);
    176   $BoundingBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
    177   $Left -= ($BoundingBox[2] - $BoundingBox[0]);
    178   imagettftext($Image, $FontSize, 0, $Left, 14, $Red, $FontFile, $Text);
    179 
    180   $Text = "    Avg.  ".AddPrefixMultipliers($AvgValue, $Measure['Unit']);
    181   $BoundingBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
    182   $Left -= ($BoundingBox[2] - $BoundingBox[0]);
    183   imagettftext($Image, $FontSize, 0, $Left, 14, $Green, $FontFile, $Text);
    184 
    185   $Text = "    Min.  ".AddPrefixMultipliers($MinValue, $Measure['Unit']);
    186   $BoundingBox = imagettfbbox($FontSize, 0, $FontFile, $Text);
    187   $Left -= ($BoundingBox[2] - $BoundingBox[0]);
    188   imagettftext($Image, $FontSize, 0, $Left, 14, $Blue, $FontFile, $Text);
    189   //imagestring($Image, 2, 70, 20, 'Vygenerováno za '.$GenerationTime.' sekund', $Black);
    190   //imagestring($Image, 2, 50, 30, 'Level: '.$Level, $Black);
    191 
    192   imagettftext($Image, $FontSize, 0, 70, 14, $Black, $FontFile, $Measure['Description']);
    193   imagerectangle($Image, 0, 0, $Width - 1, $Height - 1, $Black);   // Frame border
    194   imagepng($Image);
    195   imagedestroy($Image);
    196 }
    197    
    198   }
    199 }
    200211
    201212?>
  • trunk/Application/View/Main.php

    r42 r43  
    100100    $Debug = 0;
    101101
    102     foreach($this->Config['DefaultVariables'] as $Index => $Variable)
     102    foreach($this->Config['Application']['DefaultVariables'] as $Index => $Variable)
    103103    {
    104104      if(!array_key_exists($Index, $_SESSION)) $_SESSION[$Index] = $Variable;
     
    147147    {
    148148      $Output .= 'Délka úseku: ';
    149       foreach($GrafTimeRanges as $Index => $Item)
     149      foreach($this->GraphTimeRanges as $Index => $Item)
    150150        $Output .= '<a href="?Period='.$Index.'">'.$Item['caption'].'</a>&nbsp;';
    151151      $Output .= '<br/>';
     
    167167  {
    168168    $Output = '<strong>Graf:</strong><br/>';
    169     $Output .= '<img alt="Graf" src="?Module=Graph&amp;Measure='.$_SESSION['Measure'].'&amp;From='.$_SESSION['TimeStart'].'&amp;To='.$_SESSION['TimeEnd'].'&amp;Width=750&amp;Height=200&amp;Differential='.$_SESSION['Differential'].'" width="750" height="200"><br>';
     169    $Output .= '<img alt="Graf" src="?M=Graph&amp;Measure='.$_SESSION['Measure'].'&amp;From='.$_SESSION['TimeStart'].'&amp;To='.$_SESSION['TimeEnd'].'&amp;Width='.$this->Config['Application']['GraphSize']['Width'].'&amp;Height='.$this->Config['Application']['GraphSize']['Height'].'&amp;Differential='.$_SESSION['Differential'].'" width="'.$this->Config['Application']['GraphSize']['Width'].'" height="'.$this->Config['Application']['GraphSize']['Height'].'"><br>';
    170170    $Output .= '<a href="?Measure='.$_SESSION['Measure'].'&amp;TimeStart='.$_SESSION['TimeStart'].'&amp;TimeEnd='.$_SESSION['TimeEnd'].'&amp;TimeSpecify=1&amp;Differential='.$_SESSION['Differential'].'">Odkaz na vybraný graf</a><br>';
    171171//print_r(GetValues($Measure, $TimeStart, $TimeEnd));
     
    191191        $RowCount = $RowCount[0];
    192192      }
    193       $Result2 = $this->Database->select($Measure['DataTable'], 'Time, Average', 'Measure='.$Measure['Id'].' AND Level=0 ORDER BY Time DESC LIMIT 1');
     193      $Result2 = $this->Database->select($Measure['DataTable'], 'Time, Avg', 'Measure='.$Measure['Id'].' AND Level=0 ORDER BY Time DESC LIMIT 1');
    194194      if($Result2->num_rows > 0)
    195195      {
    196196        $Row = $Result2->fetch_array();
    197         $LastMeasureTime = date('j.n.Y G:i:s', MysqlDateTimeToTime($Row['Time']));
    198         $LastMeasureValue = AddPrefixMultipliers($Row['Average'], $Measure['Unit']);
     197        $LastMeasureTime = date('j.n.Y G:i:s', $this->Database->MysqlDateTimeToTime($Row['Time']));
     198        $LastMeasureValue = $this->System->AddPrefixMultipliers($Row['Avg'], $Measure['Unit']);
    199199      } else
    200200      {
     
    202202        $LastMeasureValue = '&nbsp;';
    203203      }
    204       if($Measure['Continuity'] == 1) $Interpolate = 'Ano'; else $Interpolate = 'Ne';
     204      if($Measure['Continuity'] == 1) $Interpolate = 'Ano';
     205        else $Interpolate = 'Ne';
    205206      if($Measure['Info'] == '') $Measure['Info'] = '&nbsp;';
    206207      $GenerationTime = floor(($this->System->GetMicrotime() - $StopWatchStart) * 1000  ) / 1000;
Note: See TracChangeset for help on using the changeset viewer.