Index: trunk/Application/Version.php
===================================================================
--- trunk/Application/Version.php	(revision 70)
+++ trunk/Application/Version.php	(revision 71)
@@ -6,5 +6,5 @@
 // and system will need database update.
 
-$Revision = 67;
+$Revision = 71;
 $DatabaseRevision = 67;
-$ReleaseTime = strtotime('2016-02-28');
+$ReleaseTime = strtotime('2016-06-30');
Index: trunk/Graph.php
===================================================================
--- trunk/Graph.php	(revision 70)
+++ trunk/Graph.php	(revision 71)
@@ -65,12 +65,12 @@
     foreach($Points as $Index => $Item)
     {
-      //$Points[$Index]['min'] =  $Points[$Index]['min'] / $Measure['Divider'];
-      //$Points[$Index]['avg'] =  $Points[$Index]['avg'] / $Measure['Divider'];
-      //$Points[$Index]['max'] =  $Points[$Index]['max'] / $Measure['Divider'];
-      if($Points[$Index]['avg'] > $MaxValue) $MaxValue = $Points[$Index]['avg'];
-      if($Points[$Index]['avg'] < $MinValue) $MinValue = $Points[$Index]['avg'];
-      if($Points[$Index]['max'] > $MaxValue) $MaxValue = $Points[$Index]['max'];
-      if($Points[$Index]['min'] < $MinValue) $MinValue = $Points[$Index]['min'];
-      $AvgValue = $AvgValue + $Points[$Index]['avg'];
+      //$Points[$Index]['Min'] =  $Points[$Index]['Min'] / $Measure['Divider'];
+      //$Points[$Index]['Avg'] =  $Points[$Index]['Avg'] / $Measure['Divider'];
+      //$Points[$Index]['Max'] =  $Points[$Index]['Max'] / $Measure['Divider'];
+      if($Points[$Index]['Avg'] > $MaxValue) $MaxValue = $Points[$Index]['Avg'];
+      if($Points[$Index]['Avg'] < $MinValue) $MinValue = $Points[$Index]['Avg'];
+      if($Points[$Index]['Max'] > $MaxValue) $MaxValue = $Points[$Index]['Max'];
+      if($Points[$Index]['Min'] < $MinValue) $MinValue = $Points[$Index]['Min'];
+      $AvgValue = $AvgValue + $Points[$Index]['Avg'];
     }
     //$MinValue = round($MinValue * $Measure['Divider']) / $Measure['Divider'];
@@ -87,11 +87,11 @@
       {
         $PointsMin[] = $Index * $Width / $Measure->DivisionCount;
-        $PointsMin[] = $Height - 1 - ($Points[$Index]['min'] - $MinValue) /
+        $PointsMin[] = $Height - 1 - ($Points[$Index]['Min'] - $MinValue) /
           ($MaxValue - $MinValue) * $Height * $this->ValueToImageHeigthCoefficient;
         $PointsAvg[] = $Index * $Width / $Measure->DivisionCount;
-        $PointsAvg[] = $Height - 1 - ($Points[$Index]['avg'] - $MinValue) /
+        $PointsAvg[] = $Height - 1 - ($Points[$Index]['Avg'] - $MinValue) /
           ($MaxValue - $MinValue) * $Height * $this->ValueToImageHeigthCoefficient;
         $PointsMax[] = $Index * $Width / $Measure->DivisionCount;
-        $PointsMax[] = $Height - 1 - ($Points[$Index]['max'] - $MinValue) /
+        $PointsMax[] = $Height - 1 - ($Points[$Index]['Max'] - $MinValue) /
           ($MaxValue - $MinValue) * $Height * $this->ValueToImageHeigthCoefficient;
       }
Index: trunk/Modules/Measure/Measure.php
===================================================================
--- trunk/Modules/Measure/Measure.php	(revision 70)
+++ trunk/Modules/Measure/Measure.php	(revision 71)
@@ -20,5 +20,5 @@
     $this->System->RegisterPage('', 'PageMain');
   }
-  
+
   function DoInstall()
   {
@@ -31,5 +31,4 @@
   `Continuity` tinyint(1) NOT NULL default 0,
   `Period` int(11) NOT NULL default 60,
-  `OldName` varchar(32) collate utf8_general_ci NOT NULL,
   `PermissionView` varchar(255) collate utf8_general_ci NOT NULL default "all",
   `PermissionAdd` varchar(255) collate utf8_general_ci NOT NULL default "localhost.localdomain",
@@ -42,13 +41,13 @@
 ) ENGINE=InnoDB  DEFAULT CHARSET=utf8;');
    }
-  
+
   function DoUninstall()
   {
     $this->Database->query('DROP TABLE IF EXISTS `Measure`');
   }
-  
+
   function DoUpgrade()
   {
-   
+
   }
 }
@@ -71,5 +70,5 @@
   {
     $this->Id = 0;
-    $this->ValueTypes = array('min', 'avg', 'max');
+    $this->ValueTypes = array('Min', 'Avg', 'Max');
     $this->Database = &$Database;
     $this->LevelReducing = 5;
@@ -79,4 +78,5 @@
     $this->DivisionCount = 500;
     $this->PeriodTolerance = 0.25; // 25%
+    $this->Data = array('Name' => '', 'Description' => '', 'Unit' => '', 'Info' => '');
   }
 
@@ -86,8 +86,7 @@
   }
 
-  function StatTableName($Level)
-  {
-    if($Level == 0) return('data');
-      else return('data_cache');
+  function GetDataTable()
+  {
+    return('data_'.$this->Data['Name']);
   }
 
@@ -109,27 +108,25 @@
   }
 
-  function AddValue($Value)
-  {
-    $Time = time();
-
+  function AddValue($Value, $Time)
+  {
     $Result = $this->Database->select($this->Data['DataTable'], '*', '(`Measure`='.$this->Data['Id'].') AND '.
-      '(`level`=0) ORDER BY `time` DESC LIMIT 2');
+      '(`Level`=0) ORDER BY `Time` DESC LIMIT 2');
     if($Result->num_rows == 0) {
       // No measure value found. Simply insert new first value.
       $this->Database->insert($this->Data['DataTable'],
-      array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0,
-      'measure' => $this->Data['Id'], 'time' => TimeToMysqlDateTime($Time),
-      'continuity' => 0));
+        array('Min' => $Value, 'Avg' => $Value, 'Max' => $Value, 'Level' => 0,
+        'Measure' => $this->Data['Id'], 'Time' => TimeToMysqlDateTime($Time),
+        'Continuity' => 0));
     } else if($Result->num_rows == 1) {
       // One value exists. Add second value.
       $this->Database->insert($this->Data['DataTable'],
-      array('min' => $Value, 'avg' => $Value, 'max' => $Value, 'level' => 0,
-      'measure' => $this->Data['Id'], 'time' => TimeToMysqlDateTime($Time),
-      'continuity' => 1));
+      array('Min' => $Value, 'Avg' => $Value, 'Max' => $Value, 'Level' => 0,
+      'Measure' => $this->Data['Id'], 'Time' => TimeToMysqlDateTime($Time),
+      'Continuity' => 1));
     } else {
       // Two values already exist in measure table
       $LastValue = $Result->fetch_array();
       $NextToLastValue = $Result->fetch_array();
-      if(($Time - MysqlDateTimeToTime($LastValue['time'])) < (1 - $this->PeriodTolerance) * $this->Data['Period'])
+      if(($Time - MysqlDateTimeToTime($LastValue['Time'])) < (1 - $this->PeriodTolerance) * $this->Data['Period'])
       {
         // New value added too quickly. Need to wait for next measure period.
@@ -138,5 +135,5 @@
       {
         // We are near defined period and can add new value.
-        if(($Time - MysqlDateTimeToTime($LastValue['time'])) < (1 + $this->PeriodTolerance) * $this->Data['Period']) {
+        if(($Time - MysqlDateTimeToTime($LastValue['Time'])) < (1 + $this->PeriodTolerance) * $this->Data['Period']) {
           // New value added near defined measure period inside tolerance. Keep continuity.
           $Continuity = 1;
@@ -146,22 +143,26 @@
           $Continuity = 0;
         }
-        if(($LastValue['avg'] == $NextToLastValue['avg']) and ($LastValue['avg'] == $Value) and
-          ($LastValue['continuity'] == 1) and ($Continuity == 1))
+        if(($LastValue['Avg'] == $NextToLastValue['Avg']) and ($LastValue['Avg'] == $Value) and
+          ($LastValue['Continuity'] == 1) and ($Continuity == 1))
         {
           // New value is same as last value and continuity mode is enabled and
           // continuity flag is present. Just shift forward time for last value.
-          $this->Database->update($this->Data['DataTable'], '(`time`="'.$LastValue['time'].'") AND '.
-          '(`level`=0) AND (`measure`='.$this->Data['Id'].')', array('time' => TimeToMysqlDateTime($Time)));
+          $this->Database->update($this->Data['DataTable'], '(`Time`="'.$LastValue['Time'].'") AND '.
+          '(`Level`=0) AND (`Measure`='.$this->Data['Id'].')', array('Time' => TimeToMysqlDateTime($Time)));
         } else
         {
           // Last value is different or not with continuity flag. Need to add new value.
-          $this->Database->insert($this->Data['DataTable'], array('min' => $Value,
-            'avg' => $Value, 'max' => $Value, 'level' => 0, 'measure' => $this->Data['Id'], 'time' => TimeToMysqlDateTime($Time),
-            'continuity' => $Continuity));
-        }
-      }
-    }
-
-    // Update higher levels
+          $this->Database->insert($this->Data['DataTable'], array('Min' => $Value,
+            'Avg' => $Value, 'max' => $Value, 'Level' => 0, 'Measure' => $this->Data['Id'],
+            'Time' => TimeToMysqlDateTime($Time),
+            'Continuity' => $Continuity));
+        }
+      }
+    }
+    $this->UpdateHigherLevels($Time);
+  }
+
+  function UpdateHigherLevels($Time)
+  {
     for($Level = 1; $Level <= $this->MaxLevel; $Level++)
     {
@@ -173,11 +174,11 @@
       // Load values in time range
       $Values = array();
-      $Result = $this->Database->select($this->Data['DataTable'], '*', '(`time` > "'.
+      $Result = $this->Database->select($this->Data['DataTable'], '*', '(`Time` > "'.
         TimeToMysqlDateTime($StartTime).'") AND '.
-        '(`time` < "'.TimeToMysqlDateTime($EndTime).'") AND '.
-        '(`measure`='.$this->Data['Id'].') AND (`level`='.($Level - 1).') ORDER BY `time`');
+        '(`Time` < "'.TimeToMysqlDateTime($EndTime).'") AND '.
+        '(`Measure`='.$this->Data['Id'].') AND (`Level`='.($Level - 1).') ORDER BY `Time`');
       while($Row = $Result->fetch_array())
       {
-        $Row['time'] = MysqlDateTimeToTime($Row['time']);
+        $Row['Time'] = MysqlDateTimeToTime($Row['Time']);
         $Values[] = $Row;
       }
@@ -193,12 +194,12 @@
       $Point = $this->ComputeOneValue($StartTime, $EndTime, $Values, $Level);
 
-      $this->Database->delete($this->Data['DataTable'], '(`time` > "'.TimeToMysqlDateTime($StartTime).'") AND
-        (`time` < "'.TimeToMysqlDateTime($EndTime).'") AND (`measure`='.$this->Data['Id'].') '.
-        'AND (`level`='.$Level.')');
-      $Continuity = $Values[1]['continuity'];
-      $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' => TimeToMysqlDateTime($StartTime + ($EndTime - $StartTime) / 2)));
+      $this->Database->delete($this->Data['DataTable'], '(`Time` > "'.TimeToMysqlDateTime($StartTime).'") AND
+        (`Time` < "'.TimeToMysqlDateTime($EndTime).'") AND (`Measure`='.$this->Data['Id'].') '.
+        'AND (`Level`='.$Level.')');
+      $Continuity = $Values[1]['Continuity'];
+      $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' => TimeToMysqlDateTime($StartTime + ($EndTime - $StartTime) / 2)));
     }
   }
@@ -207,5 +208,5 @@
   function ComputeOneValue($LeftTime, $RightTime, $Values, $Level)
   {
-    $NewValue = array('min' => +1000000000000000000, 'avg' => 0, 'max' => -1000000000000000000);
+    $NewValue = array('Min' => +1000000000000000000, 'Avg' => 0, 'Max' => -1000000000000000000);
 
     // Trim outside parts
@@ -213,16 +214,16 @@
     {
       $Values[0][$ValueType] = Interpolation(
-        NewPoint($Values[0]['time'], $Values[0][$ValueType]),
-        NewPoint($Values[1]['time'], $Values[1][$ValueType]), $LeftTime);
-    }
-    $Values[0]['time'] = $LeftTime;
+        NewPoint($Values[0]['Time'], $Values[0][$ValueType]),
+        NewPoint($Values[1]['Time'], $Values[1][$ValueType]), $LeftTime);
+    }
+    $Values[0]['Time'] = $LeftTime;
     foreach($this->ValueTypes as $ValueType)
     {
       $Values[count($Values) - 1][$ValueType] = Interpolation(
-        NewPoint($Values[count($Values) - 2]['time'], $Values[count($Values) - 2][$ValueType]),
-        NewPoint($Values[count($Values) - 1]['time'], $Values[count($Values) - 1][$ValueType]),
+        NewPoint($Values[count($Values) - 2]['Time'], $Values[count($Values) - 2][$ValueType]),
+        NewPoint($Values[count($Values) - 1]['Time'], $Values[count($Values) - 1][$ValueType]),
         $RightTime);
     }
-    $Values[count($Values) - 1]['time'] = $RightTime;
+    $Values[count($Values) - 1]['Time'] = $RightTime;
 
     // Perform computation
@@ -232,19 +233,19 @@
       for($I = 0; $I < (count($Values) - 1); $I++)
       {
-        if($ValueType == 'avg')
-        {
-          if($Values[$I + 1]['continuity'] == $this->Data['ContinuityEnabled']);
+        if($ValueType == 'Avg')
+        {
+          if($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled']);
             else if($this->Differential == 0)
             {
-              $NewValue[$ValueType] = $NewValue[$ValueType] + ($Values[$I + 1]['time'] - $Values[$I]['time']) *
+              $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']) *
+              $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'] == $this->Data['ContinuityEnabled'])
+        else if($ValueType == 'Max')
+        {
+          if($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled'])
           {
             if(0 > $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
@@ -261,7 +262,7 @@
           }
         }
-        else if($ValueType == 'min')
-        {
-          if($Values[$I + 1]['continuity'] == $this->Data['ContinuityEnabled'])
+        else if($ValueType == 'Min')
+        {
+          if($Values[$I + 1]['Continuity'] == $this->Data['ContinuityEnabled'])
           {
             if(0 < $NewValue[$ValueType]) $NewValue[$ValueType] = 0;
@@ -282,5 +283,5 @@
     if($this->Data['Cumulative'] == 0)
     {
-      $NewValue['avg'] = $NewValue['avg'] / ($RightTime - $LeftTime);
+      $NewValue['Avg'] = $NewValue['Avg'] / ($RightTime - $LeftTime);
     }
     return($NewValue);
@@ -290,6 +291,6 @@
   {
     // Get first and last time
-    $Result = $this->Database->select($this->Data['DataTable'], '*', '(`measure`='.$this->Data['Id'].') AND '.
-      '(`level`='.$Level.') ORDER BY `time` LIMIT 1');
+    $Result = $this->Database->select($this->Data['DataTable'], '*', '(`Measure`='.$this->Data['Id'].') AND '.
+      '(`Level`='.$Level.') ORDER BY `Time` LIMIT 1');
     if($Result->num_rows > 0)
     {
@@ -298,13 +299,13 @@
     } else $AbsoluteLeftTime = 0;
 
-    $Result = $this->Database->select($this->Data['DataTable'], '*', '(`measure`='.$this->Data['Id'].') AND '.
-      '(`level`='.$Level.') ORDER BY `time` DESC LIMIT 1');
+    $Result = $this->Database->select($this->Data['DataTable'], '*', '(`Measure`='.$this->Data['Id'].') AND '.
+      '(`Level`='.$Level.') ORDER BY `Time` DESC LIMIT 1');
     if($Result->num_rows > 0)
     {
       $Row = $Result->fetch_array();
-      $AbsoluteRightTime = MysqlDateTimeToTime($Row['time']);
+      $AbsoluteRightTime = MysqlDateTimeToTime($Row['Time']);
     } else $AbsoluteRightTime = 0;
 
-    return(array('left' => $AbsoluteLeftTime, 'right' => $AbsoluteRightTime));
+    return(array('Left' => $AbsoluteLeftTime, 'Right' => $AbsoluteRightTime));
   }
 
@@ -314,11 +315,11 @@
     $Result = array();
     $DbResult = $this->Database->select($this->Data['DataTable'], '*',
-      '(`time` > "'.TimeToMysqlDateTime($Time).'") AND (`measure`='.
-      $this->Data['Id'].') AND (`level`='.$Level.') ORDER BY `time` ASC LIMIT 1');
+      '(`Time` > "'.TimeToMysqlDateTime($Time).'") AND (`Measure`='.
+      $this->Data['Id'].') AND (`Level`='.$Level.') ORDER BY `Time` ASC LIMIT 1');
     if($DbResult->num_rows > 0)
     {
       // Found one value, simply return it
       $Row = $DbResult->fetch_array();
-      $Row['time'] = MysqlDateTimeToTime($Row['time']);
+      $Row['Time'] = MysqlDateTimeToTime($Row['Time']);
       return(array($Row));
     } else
@@ -327,14 +328,14 @@
       //$Time = $Values[count($Values) - 1]['time'] + 60;
       //array_push($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
-      $Result[] = array('time' => ($Time + $this->TimeSegment($Level)), 'min' => 0,
-        'avg' => 0, 'max' => 0, 'continuity' => 0);
+      $Result[] = array('Time' => ($Time + $this->TimeSegment($Level)), 'Min' => 0,
+        'Avg' => 0, 'Max' => 0, 'Continuity' => 0);
       $DbResult = $this->Database->select($this->Data['DataTable'], '*',
-        '(`time` < "'.TimeToMysqlDateTime($Time).'") AND (`measure`='.$this->Data['Id'].') '.
-        'AND (`level`='.$Level.') ORDER BY `time` DESC LIMIT 1');
+        '(`Time` < "'.TimeToMysqlDateTime($Time).'") AND (`Measure`='.$this->Data['Id'].') '.
+        'AND (`Level`='.$Level.') ORDER BY `Time` DESC LIMIT 1');
       if($DbResult->num_rows > 0)
       {
         $Row = $DbResult->fetch_array();
-        array_unshift($Result, array('time' => (MysqlDateTimeToTime($Row['time']) + 10),
-          'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
+        array_unshift($Result, array('Time' => (MysqlDateTimeToTime($Row['Time']) + 10),
+          'Min' => 0, 'Avg' => 0, 'Max' => 0, 'Continuity' => 0));
       }
       return($Result);
@@ -347,11 +348,11 @@
     $Result = array();
     $DbResult = $this->Database->select($this->Data['DataTable'], '*',
-      '(`time` < "'.TimeToMysqlDateTime($Time).'") AND (`measure`='.$this->Data['Id'].') '.
-      'AND (`level`='.$Level.') ORDER BY `time` DESC LIMIT 1');
+      '(`Time` < "'.TimeToMysqlDateTime($Time).'") AND (`Measure`='.$this->Data['Id'].') '.
+      'AND (`Level`='.$Level.') ORDER BY `Time` DESC LIMIT 1');
     if($DbResult->num_rows > 0)
     {
       // Found one value, simply return it
       $Row = $DbResult->fetch_array();
-      $Row['time'] = MysqlDateTimeToTime($Row['time']);
+      $Row['Time'] = MysqlDateTimeToTime($Row['Time']);
       return(array($Row));
     } else {
@@ -359,15 +360,15 @@
       //$Time = $Values[0]['time'] - 60;
       //array_unshift($Values, array('time' => $Time, 'min' => 0, 'avg' => 0, 'max' => 0, 'continuity' => 0));
-      $Result[] = array('time' => ($Time - $this->TimeSegment($Level)), 'min' => 0,
-        'avg' => 0, 'max' => 0, 'continuity' => 0);
+      $Result[] = array('Time' => ($Time - $this->TimeSegment($Level)), 'Min' => 0,
+        'Avg' => 0, 'Max' => 0, 'Continuity' => 0);
 
       $DbResult = $this->Database->select($this->Data['DataTable'], '*',
-        '(`time` > "'.TimeToMysqlDateTime($Time).'") AND (`measure`='.$this->Data['Id'].') '.
-        'AND (`level`='.$Level.') ORDER BY `time` ASC LIMIT 1');
+        '(`Time` > "'.TimeToMysqlDateTime($Time).'") AND (`Measure`='.$this->Data['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));
+        array_push($Result, array('Time' => (MysqlDateTimeToTime($Row['Time']) - 10),
+          'Min' => 0, 'Avg' => 0, 'Max' => 0, 'Continuity' => 0));
       }
       return($Result);
@@ -384,15 +385,15 @@
 
     // Load values in time range
-    $Result = $this->Database->select($this->Data['DataTable'], '`time`, `min`, '.
-      '`avg`, `max`, `continuity`',
-      '(`time` > "'.TimeToMysqlDateTime($TimeFrom).'") AND '.
-      '(`time` < "'.TimeToMysqlDateTime($TimeTo).'") AND '.
-      '(`measure`='.$this->Data['Id'].') AND (`level`='.$Level.') ORDER BY `time`');
+    $Result = $this->Database->select($this->Data['DataTable'], '`Time`, `Min`, '.
+      '`Avg`, `Max`, `Continuity`',
+      '(`Time` > "'.TimeToMysqlDateTime($TimeFrom).'") AND '.
+      '(`Time` < "'.TimeToMysqlDateTime($TimeTo).'") AND '.
+      '(`Measure`='.$this->Data['Id'].') AND (`Level`='.$Level.') ORDER BY `Time`');
     $Values = array();
     while($Row = $Result->fetch_array())
     {
-      $Values[] = array('time' => MysqlDateTimeToTime($Row['time']),
-        'min' => $Row['min'], 'avg' => $Row['avg'], 'max' => $Row['max'],
-        'continuity' => $Row['continuity']);
+      $Values[] = array('Time' => MysqlDateTimeToTime($Row['Time']),
+        'Min' => $Row['Min'], 'Avg' => $Row['Avg'], 'Max' => $Row['Max'],
+        'Continuity' => $Row['Continuity']);
     }
    // array_pop($Values);
@@ -414,10 +415,10 @@
 
         $EndIndex = $StartIndex;
-        while($Values[$EndIndex]['time'] < $TimeEnd) $EndIndex = $EndIndex + 1;
+        while($Values[$EndIndex]['Time'] < $TimeEnd) $EndIndex = $EndIndex + 1;
         $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
         $Points[] = $this->ComputeOneValue($TimeStart, $TimeEnd, $SubValues, $Level);
         $StartIndex = $EndIndex - 1;
       }
-    } else $Points[] = array('min' => 0, 'avg' => 0, 'max' => 0);
+    } else $Points[] = array('Min' => 0, 'Avg' => 0, 'Max' => 0);
     return($Points);
   }
@@ -430,8 +431,8 @@
 
     // Clear previous items
-    $DbResult = $this->Database->select($this->Data['DataTable'], 'COUNT(*)', '(`level`>0) AND (`measure`='.$this->Data['Id'].')');
+    $DbResult = $this->Database->select($this->Data['DataTable'], 'COUNT(*)', '(`Level`>0) AND (`Measure`='.$this->Data['Id'].')');
     $Row = $DbResult->fetch_array();
     echo("Mazu starou cache (".$Row[0]." polozek)...");
-    $this->Database->delete($this->Data['DataTable'], '(`level`>0) AND (`measure`='.$this->Data['Id'].')');
+    $this->Database->delete($this->Data['DataTable'], '(`Level`>0) AND (`Measure`='.$this->Data['Id'].')');
     echo("<br>\n");
 
@@ -441,6 +442,6 @@
       $TimeRange = $this->GetTimeRange($Level - 1);
       $TimeSegment = $this->TimeSegment($Level);
-      $StartTime = $this->AlignTime($TimeRange['left'], $TimeSegment) - $TimeSegment;
-      $EndTime = $this->AlignTime($TimeRange['right'], $TimeSegment);
+      $StartTime = $this->AlignTime($TimeRange['Left'], $TimeSegment) - $TimeSegment;
+      $EndTime = $this->AlignTime($TimeRange['Right'], $TimeSegment);
       $BurstCount = 500;
       echo('For 0 to '.round(($EndTime - $StartTime) / $TimeSegment / $BurstCount)."<br>\n");
@@ -451,9 +452,11 @@
         $EndTime2 = $StartTime + ($I + 1) * $BurstCount * $TimeSegment;
         $Values = array();
-        $DbResult = $this->Database->select($this->Data['DataTable'], '*', '(`time` > "'.TimeToMysqlDateTime($StartTime2).'") AND (`time` < "'.
-          TimeToMysqlDateTime($EndTime2).'") AND (`measure`='.$this->Data['Id'].') AND (`level`='.($Level - 1).') ORDER BY `time`');
+        $DbResult = $this->Database->select($this->Data['DataTable'], '*', '(`Time` > "'.
+          TimeToMysqlDateTime($StartTime2).'") AND (`Time` < "'.
+          TimeToMysqlDateTime($EndTime2).'") AND (`Measure`='.$this->Data['Id'].
+          ') AND (`Level`='.($Level - 1).') ORDER BY `Time`');
         while($Row = $DbResult->fetch_array())
         {
-          $Row['time'] = MysqlDateTimeToTime($Row['time']);
+          $Row['Time'] = MysqlDateTimeToTime($Row['Time']);
           $Values[] = $Row;
         }
@@ -474,13 +477,13 @@
 
             $EndIndex = $StartIndex;
-            while($Values[$EndIndex]['time'] < $EndTime3) $EndIndex = $EndIndex + 1;
+            while($Values[$EndIndex]['Time'] < $EndTime3) $EndIndex = $EndIndex + 1;
             $SubValues = array_slice($Values, $StartIndex, $EndIndex - $StartIndex + 1);
             if(count($SubValues) > 2)
             {
               $Point = $this->ComputeOneValue($StartTime3, $EndTime3, $SubValues, $Level);
-              $Continuity = $SubValues[1]['continuity'];
-              $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' => TimeToMysqlDateTime($StartTime3 + ($EndTime3 - $StartTime3) / 2)));
+              $Continuity = $SubValues[1]['Continuity'];
+              $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' => TimeToMysqlDateTime($StartTime3 + ($EndTime3 - $StartTime3) / 2)));
             }
             $StartIndex = $EndIndex - 1;
@@ -500,5 +503,5 @@
   function RebuildAllMeasuresCache()
   {
-    $Result = $this->Database->select('measure', 'Id');
+    $Result = $this->Database->select('Measure', 'Id');
     while($Row = $Result->fetch_array())
     {
@@ -509,21 +512,46 @@
     }
   }
-
-  function InitMeasureDataTable()
-  {
-    $this->Database->query('CREATE TABLE `data_'.$this->Data['Name'].'` (
-      `Time` TIMESTAMP NOT NULL ,
-      `Avg` '.$this->Data['DataType'].' NOT NULL ,
-      `Continuity` BOOL NOT NULL
-      ) ENGINE = MYISAM ;');
-
-    $this->Database->query('CREATE TABLE `data_'.$this->Data['Name'].'_cache` (
+}
+
+class MeasureDataType
+{
+  const Int8 = 0;
+  const Int16 = 1;
+  const Int32 = 2;
+  const Int64 = 3;
+  const Float = 4;
+  const Double = 5;
+}
+
+class MeasureList extends Model
+{
+  function AddItem(Measure $Measure)
+  {
+    $this->Database->insert('Measure', array(
+      'Name' => $Measure->Data['Name'],
+      'Description' => $Measure->Data['Description'],
+      'Unit' => $Measure->Data['Unit'],
+      'Info' => $Measure->Data['Info'],
+      'DataType' => $Measure->Data['DataType'],
+      'DataTable' => $Measure->GetDataTable(),
+    ));
+    $Measure->Data['Id'] = $this->Database->insert_id;
+
+    $this->Database->query('CREATE TABLE `'.$Measure->GetDataTable().'` (
 `Time` TIMESTAMP NOT NULL ,
+`Measure` INT NOT NULL ,
 `Level` TINYINT NOT NULL ,
-`Min` '.$this->Data['DataType'].' NOT NULL ,
-`Avg` '.$this->Data['DataType'].' NOT NULL ,
-`Max` '.$this->Data['DataType'].' NOT NULL ,
+`Min` '.$Measure->Data['DataType'].' NOT NULL ,
+`Avg` '.$Measure->Data['DataType'].' NOT NULL ,
+`Max` '.$Measure->Data['DataType'].' NOT NULL ,
 `Continuity` BOOL NOT NULL
-) ENGINE = MYISAM ;');
-  }
+) ENGINE = InnoDb ;');
+  }
+
+  function RemoveItem(Measure $Measure)
+  {
+    $this->Database->delete('Measure', '`Id`='.$Measure->Data['Id']);
+    $this->Database->query('DROP TABLE `'.$Measure->GetDataTable().'`');
+  }
+
 }
Index: trunk/add.php
===================================================================
--- trunk/add.php	(revision 70)
+++ trunk/add.php	(revision 71)
@@ -12,4 +12,6 @@
   $MeasureId = addslashes($_GET['MeasureId']);
   $Value = addslashes($_GET['Value']);
+  if (array_key_exists('Time', $_GET)) $Time = $_GET['Time'] * 1;
+    else $Time = time();
   $Measure = new Measure($Core->Database);
   $Measure->Load($MeasureId);
@@ -17,5 +19,5 @@
   if(($HostName == $Measure->Data['PermissionAdd']) or ($_SERVER['REMOTE_ADDR'] == gethostbyname($Measure->Data['PermissionAdd'])))
   {
-    $Measure->AddValue($Value);
+    $Measure->AddValue($Value, $Time);
     echo('Hodnota '.$Value.' uložena');
   } else echo('Nemáte oprávnění k aktualizaci zadané veličiny!<br>Vaše adresa: '.$HostName.'('.$_SERVER['REMOTE_ADDR'].')');
