Index: branches/old/stat_functions.php
===================================================================
--- branches/old/stat_functions.php	(revision 58)
+++ branches/old/stat_functions.php	(revision 59)
@@ -79,16 +79,11 @@
 
     // Update levels
-    //echo($Time."<br>\n");
     for($Level = 1; $Level <= $MaxLevel; $Level++)
     {
-      //echo('Level '.$Level."<br>\n");
       $TimeSegment = TimeSegment($Level);
       $EndTime = AlignTime($Time, $TimeSegment);
       //if($EndTime < $Time) $EndTime = $EndTime + $TimeSegment;
       $StartTime = $EndTime - $TimeSegment;
-      
-      //echo(" ".$TimeSegment." ".$StartTime.'-'.$EndTime."<br>\n");
-      //flush();
-  
+
       // Load values in time range
       $Values = array();
@@ -100,5 +95,4 @@
         $Values[] = $Row;
       }
-      //print_r($Values);
       //array_pop($Values);      
 
@@ -107,5 +101,4 @@
 
       $Point = ComputeOneValue($StartTime, $EndTime, $Values, $Measure, $Level);
-      //print_r($Point);
 
       $Database->delete($Measure['DataTable'], '(time > "'.TimeToMysqlDateTime($StartTime).'") AND 
@@ -121,66 +114,67 @@
 {
   $Y = ($Y2 - $Y1) / ($X2 - $X1) * ($X - $X1) + $Y1;
-  //echo($Y1.'-'.$Y.'-'.$Y2.' '.$X1.'-'.$X.'-'.$X2.'<br>');
   return($Y);
 }
 
+/* Compute one value for upper time level from multiple values */
 function ComputeOneValue($LeftTime, $RightTime, $Values, $Measure, $Level)
 {
   global $ValueTypes, $Differential;
-  
+
   $NewValue = array('min' => +1000000000000000000, 'avg' => 0, 'max' => -1000000000000000000); 
-  
+
   // Trim outside parts
   foreach($ValueTypes as $ValueType)
   {
-    $Values[0][$ValueType] = Interpolation($Values[0]['time'], $Values[0][$ValueType], $Values[1]['time'], $Values[1][$ValueType], $LeftTime);
+    $Values[0][$ValueType] = Interpolation($Values[0]['time'], $Values[0][$ValueType], 
+      $Values[1]['time'], $Values[1][$ValueType], $LeftTime);
   }
   $Values[0]['time'] = $LeftTime;
   foreach($ValueTypes as $ValueType)
-  {   
-    $Values[count($Values)-1][$ValueType] = Interpolation($Values[count($Values)-2]['time'], $Values[count($Values)-2][$ValueType], 
-      $Values[count($Values)-1]['time'], $Values[count($Values)-1][$ValueType], $RightTime);
-  }
-  $Values[count($Values)-1]['time'] = $RightTime;
-  
-  // Perform computation 
+  {
+    $Values[count($Values) - 1][$ValueType] = Interpolation($Values[count($Values) - 2]['time'], 
+      $Values[count($Values) - 2][$ValueType], 
+      $Values[count($Values) - 1]['time'], $Values[count($Values) - 1][$ValueType], $RightTime);
+  }
+  $Values[count($Values) - 1]['time'] = $RightTime;
+
+  // Perform computation
   foreach($ValueTypes as $ValueType)
-  { 
+  {
     // Compute new value
     for($I = 0; $I < (count($Values) - 1); $I++)
-    {      
+    {
       if($ValueType == 'avg') 
       {
-        if($Values[$I+1]['continuity'] == $Measure['ContinuityEnabled']);
+        if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled']);
           else if($Differential == 0)
           {
-            $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I+1]['time'] - $Values[$I]['time']) *
-              (($Values[$I+1][$ValueType] - $Values[$I][$ValueType]) / 2 + $Values[$I][$ValueType]);
+            $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['time'] - $Values[$I]['time']) *
+              (($Values[$I + 1][$ValueType] - $Values[$I][$ValueType]) / 2 + $Values[$I][$ValueType]);
           } else { 
-            $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I+1]['time'] - $Values[$I]['time']) *
-             (($Values[$I+1][$ValueType] - $Values[$I][$ValueType]) / 2);
+            $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['time'] - $Values[$I]['time']) *
+             (($Values[$I + 1][$ValueType] - $Values[$I][$ValueType]) / 2);
           }
       }
-      else if($ValueType == 'max') 
-      {
-        if($Values[$I+1]['continuity'] == $Measure['ContinuityEnabled'])
+      else if($ValueType == 'max')
+      {
+        if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled'])
         {
           if(0 > $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
         }
-        else 
+        else
         {
           if($Differential == 0)
           {
-            if($Values[$I+1][$ValueType] > $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I+1][$ValueType];
+            if($Values[$I + 1][$ValueType] > $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
           } else {
-            $Difference = $Values[$I+1][$ValueType] - $Values[$I][$ValueType]; 
+            $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType]; 
             if($Difference > $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
-          } 
+          }
         }
       }
       else if($ValueType == 'min') 
       {
-        //echo($Values[$I+1]['continuity'].'=='.$Measure['ContinuityEnabled'].'<br>'); 
-        if($Values[$I+1]['continuity'] == $Measure['ContinuityEnabled'])
+        if($Values[$I + 1]['continuity'] == $Measure['ContinuityEnabled'])
         {
           if(0 < $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
@@ -188,7 +182,7 @@
           if($Differential == 0)
           {
-            if($Values[$I+1][$ValueType] < $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I+1][$ValueType];
+            if($Values[$I + 1][$ValueType] < $NewValue[$ValueType]) $NewValue[$ValueType] = $Values[$I + 1][$ValueType];
           } else {
-            $Difference = $Values[$I+1][$ValueType] - $Values[$I][$ValueType]; 
+            $Difference = $Values[$I + 1][$ValueType] - $Values[$I][$ValueType]; 
             if($Difference < $NewValue[$ValueType]) $NewValue[$ValueType] = $Difference;
           }
@@ -196,14 +190,12 @@
       }
     }
-    $NewValue[$ValueType] = $NewValue[$ValueType]; 
+    $NewValue[$ValueType] = $NewValue[$ValueType];
   }
   //if(($RightTime - $LeftTime) > 0) 
-  if($Measure['Cumulative'] == 0) 
+  if($Measure['Cumulative'] == 0)
   {
     $NewValue['avg'] = $NewValue['avg'] / ($RightTime - $LeftTime);
-  }  
+  }
   return($NewValue);
-  //echo($NewValue['avg'].'<br>');
-  //return(array('min' => rand(0,1), 'avg' => $NewValue['avg'], 'max' => rand(0,1)));
 }
 
@@ -213,5 +205,4 @@
 
   // Get first and last time
-  //echo($Measure['Id'].','.$Level.','.StatTableName($Level)."\n"); 
   $Result = $Database->select($Measure['DataTable'], '*', 'measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time LIMIT 1');
   if($Result->num_rows > 0)
@@ -239,6 +230,7 @@
 {
   global $Debug, $Database;
+  
   $Result = array();
-  $DbResult = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time ASC LIMIT 1');
+  $DbResult = $Database->select($Measure['DataTable'], '*', '(time > "'.TimeToMysqlDateTime($Time).'") AND (measure='.$Measure['Id'].') AND (level='.$Level.') ORDER BY time ASC LIMIT 1');
   if($DbResult->num_rows > 0) 
   {
@@ -252,5 +244,5 @@
     //array_push($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
     $Result[] = array('time' => ($Time + TimeSegment($Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0);
-    $DbResult = $Database->select($Measure['DataTable'], '*', 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1');
+    $DbResult = $Database->select($Measure['DataTable'], '*', '(time < "'.TimeToMysqlDateTime($Time).'") AND (measure='.$Measure['Id'].') AND (level='.$Level.') ORDER BY time DESC LIMIT 1');
     if($DbResult->num_rows > 0)
     {
@@ -266,7 +258,7 @@
 {
   global $Debug, $Database;
+
   $Result = array();
-  //echo('SELECT * FROM '.StatTableName($Level). ' WHERE '. 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1'."<br>\n");
-  $DbResult = $Database->select($Measure['DataTable'], '*', 'time < "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time DESC LIMIT 1');
+  $DbResult = $Database->select($Measure['DataTable'], '*', '(time < "'.TimeToMysqlDateTime($Time).'") AND (measure='.$Measure['Id'].') AND (level='.$Level.') ORDER BY time DESC LIMIT 1');
   if($DbResult->num_rows > 0) 
   {
@@ -279,11 +271,11 @@
     $Result[] = array('time' => ($Time - TimeSegment($Level)), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0);
 
-    $DbResult = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($Time).'" AND measure='.$Measure['Id'].' AND level='.$Level.' ORDER BY time ASC LIMIT 1');
+    $DbResult = $Database->select($Measure['DataTable'], '*', '(time > "'.TimeToMysqlDateTime($Time).'") AND (measure='.$Measure['Id'].') AND (level='.$Level.') ORDER BY time ASC LIMIT 1');
     if($DbResult->num_rows > 0)
     {
       $Row = $DbResult->fetch_array();
-      array_push($Result, array('time' => (MysqlDateTimeToTime($Row['time'])-10), 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0)); 
-    } 
-//    if($Debug) print_r($Result);
+      array_push($Result, array('time' => (MysqlDateTimeToTime($Row['time']) - 10), 
+        'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
+    }
     return($Result);
   }
@@ -306,19 +298,12 @@
   $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');
-//  echo($Level.' '.TimeToMysqlDateTime($TimeFrom).' '.TimeToMysqlDateTime($TimeTo));
   $Values = array();
-//  echo(DB_NumRows());
-//  $III = 0;
   while($Row = $Result->fetch_array())
   {
-//    echo($III.' '.$Row['time'].' '.memory_get_usage().',');
-//    $III++;
     $Values[] = array('time' => MysqlDateTimeToTime($Row['time']), 'min' => $Row['min'], 'avg' => $Row['avg'], 'max' => $Row['max'], 'continuity' => $Row['continuity']);
   }
  // array_pop($Values);
-//  echo('abc');
-//  die();
   if($Debug) echo('Item count: '.count($Values));
-  
+
   $Points = array();
   if(count($Values) > 0)
@@ -326,10 +311,6 @@
     $Values = array_merge(LoadLeftSideValue($Level, $Measure, $TimeFrom), $Values, LoadRightSideValue($Level, $Measure, $TimeTo));
 
-    //echo(count($Values).'<br>');
-    //echo($TimeFrom.','.$TimeTo.'<br>');
-    //echo($Values[0]['time'].'<br>');
     $StartIndex = 0;
     $Points = array();
-    //echo($DivisionCount.'<br>');
     for($I = 0; $I < $DivisionCount; $I++)
     {
@@ -338,11 +319,8 @@
       $TimeEnd = $TimeFrom + (($TimeTo - $TimeFrom) / $DivisionCount) * ($I+1);
       //if($Debug) echo('TimeEnd '.$I.': '.$TimeEnd.'('.TimeToMysqlDateTime($TimeEnd).')<br>');
-      //echo($TimeStart.','.$TimeEnd.'<br>');
 
       $EndIndex = $StartIndex;
       while($Values[$EndIndex]['time'] < $TimeEnd) $EndIndex = $EndIndex + 1;
       $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
-      //echo($StartIndex.','.$EndIndex.' '.count($SubValues).'<br>');
-      //print_r($SubValues);
       $Points[] = ComputeOneValue($TimeStart, $TimeEnd, $SubValues, $Measure, $Level);
       $StartIndex = $EndIndex - 1;  
@@ -371,6 +349,4 @@
     echo('Uroven '.$Level."<br>\n");
     $TimeRange = GetTimeRange($Measure, $Level-1);
-    //echo($Measure['Id'].','.($Level-1)."\n");
-    //echo(TimeToMysqlDateTime($TimeRange['left']).'-'.TimeToMysqlDateTime($TimeRange['right'])."\n");
     $TimeSegment = TimeSegment($Level);
     $StartTime = AlignTime($TimeRange['left'], $TimeSegment) - $TimeSegment;
@@ -382,8 +358,8 @@
       echo($I.' ');
       $StartTime2 = $StartTime + $I * $BurstCount * $TimeSegment;
-      $EndTime2 = $StartTime + ($I+1) * $BurstCount * $TimeSegment;
+      $EndTime2 = $StartTime + ($I + 1) * $BurstCount * $TimeSegment;
       $Values = array();
-      $DbResult = $Database->select($Measure['DataTable'], '*', 'time > "'.TimeToMysqlDateTime($StartTime2).'" AND time < "'.
-        TimeToMysqlDateTime($EndTime2).'" AND measure='.$Measure['Id'].' AND level='.($Level-1).' ORDER BY time');
+      $DbResult = $Database->select($Measure['DataTable'], '*', '(time > "'.TimeToMysqlDateTime($StartTime2).'") AND (time < "'.
+        TimeToMysqlDateTime($EndTime2).'") AND (measure='.$Measure['Id'].') AND (level='.($Level - 1).') ORDER BY time');
       while($Row = $DbResult->fetch_array())
       {
@@ -394,5 +370,5 @@
       if(count($Values) > 0)
       {
-        $Values = array_merge(LoadLeftSideValue($Level-1, $Measure, $StartTime2), $Values, LoadRightSideValue($Level-1, $Measure, $EndTime2));
+        $Values = array_merge(LoadLeftSideValue($Level - 1, $Measure, $StartTime2), $Values, LoadRightSideValue($Level - 1, $Measure, $EndTime2));
 
         $StartIndex = 0;
@@ -401,17 +377,16 @@
           echo('.');
           $StartTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * $B;
-          $EndTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * ($B+1);
+          $EndTime3 = $StartTime2 + (($EndTime2 - $StartTime2) / $BurstCount) * ($B + 1);
 
           $EndIndex = $StartIndex;
           while($Values[$EndIndex]['time'] < $EndTime3) $EndIndex = $EndIndex + 1;
           $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
-          //echo($StartIndex.','.$EndIndex.' '.count($SubValues).'<br>');
-          //print_r($SubValues);
-          if(count($SubValues) > 2)
+         if(count($SubValues) > 2)
           {
             $Point = ComputeOneValue($StartTime3, $EndTime3, $SubValues, $Measure, $Level);
-            $Continuity = $SubValues[1]['continuity'];   
-            $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)));
+            $Continuity = $SubValues[1]['continuity'];
+            $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)));
           }
           $StartIndex = $EndIndex - 1;
@@ -425,6 +400,6 @@
     $Row = $DbResult->fetch_array();
     echo("Vlozeno ".$Row[0]." polozek.<br>\n");
-  } 
-}   
+  }
+}
 
 function RebuildAllMeasuresCache()
@@ -432,6 +407,5 @@
   global $Database;
 
-//  echo("Vytvarim novou cache...\n");
-    // Load measures
+  // Load measures
   $Measures = array();
   $Result = $Database->select('measure', '*');
