Changeset 36


Ignore:
Timestamp:
Nov 11, 2008, 1:52:03 PM (16 years ago)
Author:
george
Message:
  • Upraveno: Funkce pro přidávání hodnot a vytváření ceche úrovní byla vnitřně zobecněna.
  • Přidáno: Skript pro přestavbu grafu z tabulky finančních transakcí.
  • Upraveno: Trochu zkorigována nápověda do praktičtější podoby.
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • Readme.txt

    r28 r36  
    22=================
    33
    4 Je potøeba zkopírovat soubor config.sample.php jako config.php a upravit v nìm nastavení.
    5 Ve složce SQL jsou uloženy definice tabulek measure (struktura i ukázkové data) a tabulky data, kterou je potøeba vytvoøit pro jednotlivá mìøení.
    6 Ve složce measure_scripts je ukázkový soubor monitor.sample.php, který zajišuje získávání informací o rùzných velièinách v poèítaèi.
    7  
     4Instalace
     5=========
     6
     7* Pro správnou funkci je požadován webový server (napø. apache), MySql databáze a PHP interpretr. Dále pro vykreslení grafù také knihovna php-gd.
     8* Zkopírujte soubor ukázkový config.sample.php jako config.php do stejné složky a upravte v nìm nastavení.
     9* Vytvoøete v databázi uživatele statistic a tabulku statistic a doplòte pøístupové údaje do config.php.
     10* Proveïtì import ukázkových dat ze souboru full.sql s nejvyšším èíslem revize umístìným ve složce sql. Samotná struktura tabulky measure je v measure.sql a ukázková struktura datové tabulky je v data.sql.
     11* Ve složce measure_scripts je ukázkový soubor monitor.sample.php, který zajišuje získávání informací o rùzných velièinách v poèítaèi a jejich plnìní do systému. Zkopírujte jej jako monitor.php a upravte dle potøeb.
     12
     13Poznámky k vydání
     14=================
     15
    816Verze 3.1 (13.2.2008)
    917
  • add.php

    r28 r36  
    1 <?
     1<?php
    22include('global.php');
    33
     
    99  if(gethostbyaddr($_SERVER['REMOTE_ADDR']) == $Measure['PermissionAdd'])
    1010  {
    11     AddValue($Measure, $Value);
    12     echo('Hodnota '.$Value.' uložena');
    13   } else echo('Nemáte oprávnění k aktualizaci zadané veličiny!<br>Vaše adresa: '.gethostbyaddr($_SERVER['REMOTE_ADDR']).'('.$_SERVER['REMOTE_ADDR'].')');
     11    AddValue($Measure, array('min' => $Value, 'avg' => $Value, 'max' => $Value));
     12    echo('Hodnota '.$Value.' uložena');
     13  } else echo('Nemáte oprávnění k aktualizaci zadané veličiny!<br>Vaše adresa: '.gethostbyaddr($_SERVER['REMOTE_ADDR']).'('.$_SERVER['REMOTE_ADDR'].')');
    1414} else echo('Nebyly zadány potřebné parametry');
    1515
  • stat_functions.php

    r35 r36  
    22
    33$ValueTypes = array('min', 'avg', 'max');
    4 $PrefixMultipliers = array(
     4$PrefixMultipliers = array
     5(
    56  array('y', 'yocto', pow(10, -24)),
    67  array('z', 'zepto', pow(10, -21)),
     
    1920  array('E', 'exa', pow(10, 18)),
    2021  array('Z', 'zetta', pow(10, 21)),
    21   array('Y', 'yotta', pow(10, 24)), 
     22  array('Y', 'yotta', pow(10, 24)),
    2223);
    2324
     
    2728  {
    2829    if($Value >= pow(10, $II))
    29     { 
    30       if($Digits < ($II + 1)) $RealDigits = $II + 1; else $RealDigits = $Digits; 
     30    {
     31      if($Digits < ($II + 1)) $RealDigits = $II + 1; else $RealDigits = $Digits;
    3132      $Value = round($Value / pow(10, $II - $RealDigits + 1)) * pow(10, $II - $RealDigits + 1);
    32       break;     
     33      break;
    3334    }
    3435  }
     
    4748  while((($Value / $PrefixMultipliers[$I + $II][2]) > $II) and (($I + $II) >= 0) and (($I + $II) <= count($PrefixMultipliers))) $I = $I + $II;
    4849  $Value = $Value / $PrefixMultipliers[$I][2];
    49  
     50
    5051  // Truncate digits count
    5152  $Value = TruncateDigits($Value, $Digits);
    52    
     53
    5354  return($Value.' '.$PrefixMultipliers[$I][0].$Unit);
    5455}
     
    5657function GetMicrotime()
    5758{
    58     list($Usec, $Sec) = explode(" ",microtime());
    59     return ((float)$Usec + (float)$Sec);
     59  list($Usec, $Sec) = explode(" ", microtime());
     60  return ((float)$Usec + (float)$Sec);
    6061}
    6162
     
    6364{
    6465  $Parts = explode(' ', $Time);
    65   $DateParts = explode('-', $Parts[0]); 
     66  $DateParts = explode('-', $Parts[0]);
    6667  $TimeParts = explode(':', $Parts[1]);
    67   $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]); 
    68   return($Result); 
     68  $Result = mktime($TimeParts[0], $TimeParts[1], $TimeParts[2], $DateParts[1], $DateParts[2], $DateParts[0]);
     69  return($Result);
    6970}
    7071
    7172function TimeToMysqlDateTime($Time)
    7273{
    73   return(date('Y-m-d H:i:s', $Time)); 
    74 }
    75 
    76 function TimeSegment($Level)
     74  return(date('Y-m-d H:i:s', $Time));
     75}
     76
     77function TimeSegment($Base, $Level)
    7778{
    7879  global $LevelReducing;
    79   return(pow($LevelReducing, $Level) * 60);
     80
     81  return(pow($LevelReducing, $Level) * $Base);
    8082}
    8183
     
    9597{
    9698  global $Database;
     99
    97100  $Result = $Database->select('measure', '*', 'Id='.$Id);
    98   if($Result->num_rows > 0) 
    99   {
    100     $Measure = $Result->fetch_array();
     101  if($Result->num_rows > 0)
     102  {
     103    $Measure = $Result->fetch_assoc();
    101104    if($Measure['Continuity'] == 0) $Measure['ContinuityEnabled'] = 0;  // non continuous
    102       else $Measure['ContinuityEnabled'] = 2;    // continuous graph 
    103   } else die('M��enďż˝ veliďż˝ina nenalezena');
    104   return($Measure); 
    105 }
    106 
    107 function AddValue($Measure, $Value = 0, $Time = 0)
     105      else $Measure['ContinuityEnabled'] = 2;    // continuous graph
     106  } else die('Měřená veličina nenalezena');
     107  return($Measure);
     108}
     109
     110function AddValue($Measure, $Value = array('min' => 0, 'avg' => 0, 'max' => 0), $Level = 0, $Time = 0)
    108111{
    109112  global $LevelReducing, $MaxLevel, $Database;
     
    111114  if($Time == 0) $Time = time();
    112115  //$Value = round($Measure['divider'] * $Value);
    113 
    114   echo(TimeToMysqlDateTime($Time)."\n");
    115  
    116   $Result = $Database->select($Measure['DataTable'], '*', 'measure='.$Measure['Id'].' AND level=0 ORDER BY time DESC LIMIT 2');
    117   echo($Database->LastQuery."\n");
    118   if($Result->num_rows == 0) $Database->insert($Measure['DataTable'], array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time),
    119     'continuity' => 0));
    120   else if($Result->num_rows == 1) $Database->insert($Measure['DataTable'], array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time),
    121     'continuity' => 1));
    122   else {
     116  //echo(TimeToMysqlDateTime($Time).'|'.$Level."\n");
     117
     118  $Result = $Database->select($Measure['DataTable'], '*', 'measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 2');
     119  //echo($Database->LastQuery."\n");
     120  if($Result->num_rows == 0)
     121  {
     122     $Database->insert($Measure['DataTable'], array('min' => $Value['min'], 'avg' => $Value['avg'], 'max' => $Value['max'], 'level' => $Level, 'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time), 'continuity' => 0));
     123     //echo($Database->LastQuery."\n");
     124  } else if($Result->num_rows == 1)
     125  {
     126    $Database->insert($Measure['DataTable'], array('min' => $Value['min'], 'avg' => $Value['avg'], 'max' => $Value['max'], 'level' => $Level, 'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time), 'continuity' => 1));
     127    //echo($Database->LastQuery."\n");
     128  } else
     129  {
    123130    $LastValue = $Result->fetch_array();
    124131    $NextToLastValue = $Result->fetch_array();
    125     echo($Time.' - '.MysqlDateTimeToTime($LastValue['time']).' '.($Time - MysqlDateTimeToTime($LastValue['time'])).' = '.$Measure['Period']);
    126     if(($Time - MysqlDateTimeToTime($LastValue['time'])) < 0.75 * $Measure['Period'])
    127     {
    128       echo('Too short period');
    129     }
    130     else 
     132    //echo($Level.': '.$Time.' - '.MysqlDateTimeToTime($LastValue['time']).' '.($Time - MysqlDateTimeToTime($LastValue['time'])).' = '.$Measure['Period']."\n");
     133    if((($Time - MysqlDateTimeToTime($LastValue['time'])) < 0.75 * $Measure['Period']) and ($Level == 0))
     134    {
     135      echo("Too short period\n");
     136    }
     137    else
    131138    {
    132139      if(($Time - MysqlDateTimeToTime($LastValue['time'])) < 1.25 * $Measure['Period']) $Continuity = 1;
     
    134141        //echo('('.$LastValue['avg'].'=='.$NextToLastValue['avg'].') and ('.$LastValue['avg'].' == '.$Value.') and
    135142        //('.$LastValue['continuity'].' == 1) and ('.$Continuity.' == 1))'."\n");
    136       if(($LastValue['avg'] == $NextToLastValue['avg']) and ($LastValue['avg'] == $Value) and
    137         ($LastValue['continuity'] == 1) and ($Continuity == 1))
    138       {
    139         $Database->update($Measure['DataTable'], 'time="'.$LastValue['time'].'" AND level=0 AND measure='.$Measure['Id'], array('time' => 'NOW()'));
    140         echo($Database->LastQuery."\n");
     143      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))
     144      {
     145        $Database->update($Measure['DataTable'], 'time="'.$LastValue['time'].'" AND level='.$Level.' AND measure='.$Measure['Id'], array('time' => TimeToMysqlDateTime($Time)));
     146        //echo($Database->LastQuery."\n");
    141147      } else
    142148      {
    143         $Database->insert($Measure['DataTable'], array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time),
    144           'continuity' => $Continuity));
    145         echo($Database->LastQuery."\n");
     149        $Database->insert($Measure['DataTable'], array('min' => $Value['min'], 'avg' => $Value['avg'], 'max' => $Value['max'], 'level' => $Level, 'measure' => $Measure['Id'], 'time' => TimeToMysqlDateTime($Time), 'continuity' => $Continuity));
     150        //echo($Database->LastQuery."\n");
    146151      }
    147152    }
    148153  }
    149154
    150     // Update levels
    151     //echo($Time."<br>\n");
    152     for($Level = 1; $Level <= $MaxLevel; $Level++)
    153     {
    154       //echo('Level '.$Level."<br>\n");
    155       $TimeSegment = TimeSegment($Level);
    156       $EndTime = AlignTime($Time, $TimeSegment);
    157       //if($EndTime < $Time) $EndTime = $EndTime + $TimeSegment;
    158       $StartTime = $EndTime - $TimeSegment;
    159 
    160       //echo(" ".$TimeSegment." ".$StartTime.'-'.$EndTime."<br>\n");
    161       //flush();
    162 
    163       // Load values in time range
    164       $Values = array();
    165       $Result = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($StartTime).'" AND time < "'.
    166         TimeToMysqlDateTime($EndTime).'" AND measure='.$Measure['Id'].' AND level='.($Level-1).' ORDER BY time');
    167       while($Row = $Result->fetch_array())
    168       {
    169         $Row['time'] = MysqlDateTimeToTime($Row['time']);
    170         $Values[] = $Row;
    171       }
    172       //print_r($Values);
    173       //array_pop($Values);     
    174 
    175       // Load subsidary values
    176       $Values = array_merge(LoadLeftSideValue($Level-1, $Measure, $StartTime), $Values, LoadRightSideValue($Level-1, $Measure, $EndTime));
    177 
    178       $Point = ComputeOneValue($StartTime, $EndTime, $Values, $Measure, $Level);
    179       //print_r($Point);
    180 
    181       $Database->delete($Measure['DataTable'], '(time > "'.TimeToMysqlDateTime($StartTime).'") AND
    182         (time < "'.TimeToMysqlDateTime($EndTime).'") AND measure='.$Measure['Id'].' AND level='.$Level);
    183       $Continuity = $Values[1]['continuity'];   
    184       $Database->insert($Measure['DataTable'], array('level' => $Level, 'measure' => $Measure['Id'], 'min' => $Point['min'],
    185         'avg' => $Point['avg'], 'max' => $Point['max'], 'continuity' => $Continuity, 'time' => TimeToMysqlDateTime($StartTime+($EndTime-$StartTime)/2)));
    186 
    187     }
     155  // Update next level
     156  if($Level < $MaxLevel)
     157  {
     158    $Level = $Level + 1;
     159    //echo('Level '.$Level."<br>\n");
     160    $TimeSegment = TimeSegment($Measure['Period'], 1);
     161    $EndTime = AlignTime($Time, $TimeSegment);
     162    //if($EndTime < $Time) $EndTime = $EndTime + $TimeSegment;
     163    $StartTime = $EndTime - $TimeSegment;
     164
     165    //echo(" ".$TimeSegment." ".$StartTime.'-'.$EndTime."<br>\n");
     166    //flush();
     167
     168    // Load values in time range
     169    $Values = array();
     170//.'" AND time < "'.TimeToMysqlDateTime($EndTime).'" AND measure='.$Measure['Id'].' AND level='.($Level - 1).' ORDER BY time');
     171    $Result = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($StartTime).'" AND time < "'.TimeToMysqlDateTime($EndTime).'" AND measure='.$Measure['Id'].' AND level='.($Level - 1).' ORDER BY time');
     172    while($Row = $Result->fetch_array())
     173    {
     174      $Row['time'] = MysqlDateTimeToTime($Row['time']);
     175      $Values[] = $Row;
     176    }
     177    //print_r($Values);
     178    //array_pop($Values);
     179
     180    // Load subsidary values
     181    $Values = array_merge(LoadLeftSideValue($Level - 1, $Measure, $StartTime), $Values, LoadRightSideValue($Level - 1, $Measure, $EndTime));
     182
     183    $Point = ComputeOneValue($StartTime, $EndTime, $Values, $Measure, $Level);
     184    //print_r($Point);
     185
     186    $Database->delete($Measure['DataTable'], '(time > "'.TimeToMysqlDateTime($StartTime).'") AND (time < "'.TimeToMysqlDateTime($EndTime).'") AND measure='.$Measure['Id'].' AND level='.$Level);
     187    $Measure['Period'] = $TimeSegment;
     188    AddValue($Measure, array('min' => $Point['min'], 'avg' => $Point['avg'], 'max' => $Point['max']), $Level, $StartTime + ($EndTime - $StartTime) / 2);
     189  }
    188190}
    189191
     
    198200{
    199201  global $ValueTypes, $Differential;
    200  
    201   $NewValue = array('min' => +1000000000000000000, 'avg' => 0, 'max' => -1000000000000000000); 
    202  
     202
     203  $NewValue = array('min' => +1000000000000000000, 'avg' => 0, 'max' => -1000000000000000000);
     204
    203205  // Trim outside parts
    204206  foreach($ValueTypes as $ValueType)
     
    208210  $Values[0]['time'] = $LeftTime;
    209211  foreach($ValueTypes as $ValueType)
    210   {   
    211     $Values[count($Values)-1][$ValueType] = Interpolation($Values[count($Values)-2]['time'], $Values[count($Values)-2][$ValueType],
    212       $Values[count($Values)-1]['time'], $Values[count($Values)-1][$ValueType], $RightTime);
    213   }
    214   $Values[count($Values)-1]['time'] = $RightTime;
    215  
    216   // Perform computation 
     212  {
     213    $Values[count($Values) - 1][$ValueType] = Interpolation($Values[count($Values) - 2]['time'], $Values[count($Values) - 2][$ValueType],
     214      $Values[count($Values) - 1]['time'], $Values[count($Values) - 1][$ValueType], $RightTime);
     215  }
     216  $Values[count($Values) - 1]['time'] = $RightTime;
     217
     218  // Perform computation
    217219  foreach($ValueTypes as $ValueType)
    218   { 
     220  {
    219221    // Compute new value
    220222    for($I = 0; $I < (count($Values) - 1); $I++)
    221     {     
    222       if($ValueType == 'avg') 
    223       {
    224         if($Values[$I+1]['continuity'] == $Measure['ContinuityEnabled']);
     223    {
     224      if($ValueType == 'avg')
     225      {
     226        if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled']);
    225227          else if($Differential == 0)
    226228          {
    227229            $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I+1]['time'] - $Values[$I]['time']) *
    228230              (($Values[$I+1][$ValueType] - $Values[$I][$ValueType]) / 2 + $Values[$I][$ValueType]);
    229           } else {
     231          } else
     232          {
    230233            $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I+1]['time'] - $Values[$I]['time']) *
    231234             (($Values[$I+1][$ValueType] - $Values[$I][$ValueType]) / 2);
    232235          }
    233236      }
    234       else if($ValueType == 'max') 
    235       {
    236         if($Values[$I+1]['continuity'] == $Measure['ContinuityEnabled'])
     237      else if($ValueType == 'max')
     238      {
     239        if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled'])
    237240        {
    238241          if(0 > $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
    239242        }
    240         else 
     243        else
    241244        {
    242245          if($Differential == 0)
    243246          {
    244             if($Values[$I+1][$ValueType] > $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I+1][$ValueType];
     247            if($Values[$I + 1][$ValueType] > $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
    245248          } else {
    246             $Difference = $Values[$I+1][$ValueType] - $Values[$I][$ValueType];
     249            $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType];
    247250            if($Difference > $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
    248           } 
     251          }
    249252        }
    250253      }
     
    252255      {
    253256        //echo($Values[$I+1]['continuity'].'=='.$Measure['ContinuityEnabled'].'<br>');
    254         if($Values[$I+1]['continuity'] == $Measure['ContinuityEnabled'])
     257        if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled'])
    255258        {
    256259          if(0 < $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
    257260        }
    258         else 
     261        else
    259262        {
    260263          if($Differential == 0)
    261264          {
    262             if($Values[$I+1][$ValueType] < $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I+1][$ValueType];
     265            if($Values[$I + 1][$ValueType] < $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
    263266          } else {
    264             $Difference = $Values[$I+1][$ValueType] - $Values[$I][$ValueType];
     267            $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType];
    265268            if($Difference < $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
    266           } 
     269          }
    267270        }
    268271      }
    269            
    270     } 
    271     $NewValue[$ValueType] = $NewValue[$ValueType];
     272    }
     273    $NewValue[$ValueType] = $NewValue[$ValueType];
    272274  }
    273275  //if(($RightTime - $LeftTime) > 0)
     
    275277  {
    276278    $NewValue['avg'] = $NewValue['avg'] / ($RightTime - $LeftTime);
    277   } 
     279  }
    278280  return($NewValue);
    279281  //echo($NewValue['avg'].'<br>');
     
    284286{
    285287  global $Debug, $Database;
    286  
     288
    287289  // Get first and last time
    288290  //echo($Measure['Id'].','.$Level.','.StatTableName($Level)."\n");
     
    314316  $Result = array();
    315317  $DbResult = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time ASC LIMIT 1');
    316   if($DbResult->num_rows > 0) 
     318  if($DbResult->num_rows > 0)
    317319  {
    318320    $Row = $DbResult->fetch_array();
    319321    $Row['time'] = MysqlDateTimeToTime($Row['time']);
    320322    return(array($Row));
    321   } 
    322   else 
     323  }
     324  else
    323325  {
    324326    //$Time = $Values[count($Values)-1]['time'] + 60;
    325327    //array_push($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
    326     $Result[] = array('time' => ($Time + TimeSegment($Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0);
     328    $Result[] = array('time' => ($Time + TimeSegment($Measure['Period'], $Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0);
    327329    $DbResult = $Database->select($Measure['DataTable'], '*', 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1');
    328330    if($DbResult->num_rows > 0)
    329331    {
    330332      $Row = $DbResult->fetch_array();
    331       array_unshift($Result, array('time' => (MysqlDateTimeToTime($Row['time'])+10), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
    332     } 
     333      array_unshift($Result, array('time' => (MysqlDateTimeToTime($Row['time']) + 10), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
     334    }
    333335   // if($Debug) print_r($Result);
    334336    return($Result);
    335337  }
    336 } 
     338}
    337339
    338340function LoadLeftSideValue($Level, $Measure, $Time)
    339341{
    340342  global $Debug, $Database;
     343
    341344  $Result = array();
    342345  //echo('SELECT * FROM '.StatTableName($Level). ' WHERE '. 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1'."<br>\n");
    343346  $DbResult = $Database->select($Measure['DataTable'], '*', 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1');
    344   if($DbResult->num_rows > 0) 
     347  if($DbResult->num_rows > 0)
    345348  {
    346349    $Row = $DbResult->fetch_array();
    347350    $Row['time'] = MysqlDateTimeToTime($Row['time']);
    348351    return(array($Row));
    349   } 
    350   else 
     352  }
     353  else
    351354  {
    352355    //$Time = $Values[0]['time'] - 60;
    353356    //array_unshift($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
    354     $Result[] = array('time' => ($Time - TimeSegment($Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0);
    355    
     357    $Result[] = array('time' => ($Time - TimeSegment($Measure['Period'], $Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0);
     358
    356359    $DbResult = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time ASC LIMIT 1');
    357360    if($DbResult->num_rows > 0)
    358361    {
    359362      $Row = $DbResult->fetch_array();
    360       array_push($Result, array('time' => (MysqlDateTimeToTime($Row['time'])-10), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0)); 
    361     } 
     363      array_push($Result, array('time' => (MysqlDateTimeToTime($Row['time'])-10), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
     364    }
    362365//    if($Debug) print_r($Result);
    363366    return($Result);
     
    368371{
    369372  global $DivisionCount, $Debug, $Database;
    370  
     373
    371374  if($Debug) echo('TimeFrom: '.$TimeFrom.'('.TimeToMysqlDateTime($TimeFrom).')<br>');
    372375  if($Debug) echo('TimeTo: '.$TimeTo.'('.TimeToMysqlDateTime($TimeTo).')<br>');
    373376
    374377  //$AbsoluteTime = GetTimeRange($MeasureId);
    375  
     378
    376379//  if(($TimeFrom > $AbsoluteLeftTime) and ($TimeStart < $AbsoluteRightTime) and
    377380//    ($TimeTo > $AbsoluteLeftTime) and ($TimeTo < $AbsoluteRightTime))
     
    379382
    380383  // Load values in time range
    381   $Result = $Database->select($Measure['DataTable'], 'time, min, avg, max, continuity', 'time > "'.TimeToMysqlDateTime($TimeFrom).'" AND time < "'.
    382     TimeToMysqlDateTime($TimeTo).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time');
     384  $Result = $Database->select($Measure['DataTable'], 'time, min, avg, max, continuity', 'time > "'.TimeToMysqlDateTime($TimeFrom).'" AND time < "'.TimeToMysqlDateTime($TimeTo).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time');
    383385//  echo($Level.' '.TimeToMysqlDateTime($TimeFrom).' '.TimeToMysqlDateTime($TimeTo));
    384386  $Values = array();
     
    395397//  die();
    396398  if($Debug) echo('Item count: '.count($Values));
    397  
     399
    398400  $Points = array();
    399401  if(count($Values) > 0)
    400402  {
    401403    $Values = array_merge(LoadLeftSideValue($Level, $Measure, $TimeFrom), $Values, LoadRightSideValue($Level, $Measure, $TimeTo));
    402    
     404
    403405    //echo(count($Values).'<br>');
    404406    //echo($TimeFrom.','.$TimeTo.'<br>');
     
    421423      //print_r($SubValues);
    422424      $Points[] = ComputeOneValue($TimeStart, $TimeEnd, $SubValues, $Measure, $Level);
    423       $StartIndex = $EndIndex - 1; 
     425      $StartIndex = $EndIndex - 1;
    424426    }
    425427  } else $Points[] = array('min' => 0, 'avg' => 0, 'max' => 0);
     
    445447  {
    446448    echo('Uroven '.$Level."<br>\n");
    447     $TimeRange = GetTimeRange($Measure, $Level-1);
     449    $TimeRange = GetTimeRange($Measure, $Level - 1);
    448450    //echo($Measure['Id'].','.($Level-1)."\n");
    449451    //echo(TimeToMysqlDateTime($TimeRange['left']).'-'.TimeToMysqlDateTime($TimeRange['right'])."\n");
    450     $TimeSegment = TimeSegment($Level);
     452    $TimeSegment = TimeSegment($Measure['Period'], $Level);
    451453    $StartTime = AlignTime($TimeRange['left'], $TimeSegment) - $TimeSegment;
    452454    $EndTime = AlignTime($TimeRange['right'], $TimeSegment);
    453     $BurstCount = 500; 
     455    $BurstCount = 500;
    454456    echo('For 0 to '.round(($EndTime - $StartTime) / $TimeSegment / $BurstCount)."<br>\n");
    455457    for($I = 0; $I <= round(($EndTime - $StartTime) / $TimeSegment / $BurstCount); $I++)
     
    459461      $EndTime2 = $StartTime + ($I+1) * $BurstCount * $TimeSegment;
    460462      $Values = array();
    461       $DbResult = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($StartTime2).'" AND time < "'.
    462         TimeToMysqlDateTime($EndTime2).'" AND measure='.$Measure['Id'].' AND level='.($Level-1).' ORDER BY time');
     463      $DbResult = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($StartTime2).'" AND time < "'.TimeToMysqlDateTime($EndTime2).'" AND measure='.$Measure['Id'].' AND level='.($Level-1).' ORDER BY time');
    463464      while($Row = $DbResult->fetch_array())
    464465      {
     
    466467        $Values[] = $Row;
    467468      }
    468      
     469
    469470      if(count($Values) > 0)
    470       {       
    471         $Values = array_merge(LoadLeftSideValue($Level-1, $Measure, $StartTime2), $Values, LoadRightSideValue($Level-1, $Measure, $EndTime2));
    472  
     471      {
     472        $Values = array_merge(LoadLeftSideValue($Level - 1, $Measure, $StartTime2), $Values, LoadRightSideValue($Level - 1, $Measure, $EndTime2));
     473
    473474        $StartIndex = 0;
    474475        for($B = 0; $B < $BurstCount; $B++)
    475         {         
     476        {
    476477          echo('.');
    477478          $StartTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * $B;
    478           $EndTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * ($B+1);
    479          
     479          $EndTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * ($B + 1);
     480
    480481          $EndIndex = $StartIndex;
    481482          while($Values[$EndIndex]['time'] < $EndTime3) $EndIndex = $EndIndex + 1;
     
    484485          //print_r($SubValues);
    485486          if(count($SubValues) > 2)
    486           {       
     487          {
    487488            $Point = ComputeOneValue($StartTime3, $EndTime3, $SubValues, $Measure, $Level);
    488             $Continuity = $SubValues[1]['continuity'];   
    489             $Database->insert($Measure['DataTable'], array('level' => $Level, 'measure' => $Measure['Id'], 'min' => $Point['min'],
    490               'avg' => $Point['avg'], 'max' => $Point['max'], 'continuity' => $Continuity, 'time' => TimeToMysqlDateTime($StartTime3+($EndTime3-$StartTime3)/2)));
    491           }       
    492           $StartIndex = $EndIndex - 1; 
     489            $Continuity = $SubValues[1]['continuity'];
     490            $Database->insert($Measure['DataTable'], array('level' => $Level, 'measure' => $Measure['Id'], 'min' => $Point['min'], 'avg' => $Point['avg'], 'max' => $Point['max'], 'continuity' => $Continuity, 'time' => TimeToMysqlDateTime($StartTime3 + ($EndTime3 - $StartTime3) / 2)));
     491          }
     492          $StartIndex = $EndIndex - 1;
    493493        }
    494494      }
     
    496496      //array_pop($NextValues);
    497497    } 
    498     echo("Uroven dokoncena<br>\n");         
     498    echo("Uroven dokoncena<br>\n");
    499499    $DbResult = $Database->select($Measure['DataTable'], 'COUNT(*)', 'level='.$Level.' AND measure='.$Measure['Id']);
    500500    $Row = $DbResult->fetch_array();
    501     echo("Vlozeno ".$Row[0]." polozek.<br>\n");
    502   } 
    503 }   
     501    echo("Vloženo ".$Row[0]." položek.<br>\n");
     502  }
     503}
    504504
    505505function RebuildAllMeasuresCache()
    506506{
    507507  global $Database;
    508  
    509  
    510 //  echo("Vytvarim novou cache...\n");
     508
     509  //  echo("Vytvarim novou cache...\n");
    511510    // Load measures
    512511  $Measures = array();
     
    517516  foreach($Measures as $Measure)
    518517  {
    519     RebuildMeasureCache($Measure); 
    520     echo('Velicina dokoncena<br>');         
     518    RebuildMeasureCache($Measure);
     519    echo('Velicina dokoncena<br>');
    521520  }
    522521}
     
    525524{
    526525  global $Database;
     526
    527527  $Database->query('CREATE TABLE `data_'.$Measure['Name'].'` (
    528528`Time` TIMESTAMP NOT NULL ,
Note: See TracChangeset for help on using the changeset viewer.