Code

Updated stats dump
[gosa.git] / gosa-core / include / class_stats.inc
index 9d6517c3d3cf4d882bfc58987afd4303c96af44e..c9b7dd80f08fa379c1cbe4ec947a1665e7d65d6e 100644 (file)
@@ -12,6 +12,15 @@ class stats
     static protected $lastHandle = NULL;
     static protected $statsEnabled = FALSE;
 
+
+    static function prepareFloatForWriting($float){
+        return(floor($float * 1000));
+    }
+
+    static function prepareFloatForReading($int){
+        return($int / 1000);
+    }
+
     static function getDatabaseHandle()
     {
         // Try to return last valid handle.
@@ -70,6 +79,8 @@ class stats
         $TABLE_NAME = stats::$tableName;
         $query = "DROP TABLE '{$TABLE_NAME}'";
         $ret = sqlite_query($query, $handle);
+        stats::$lastHandle = NULL;
+        stats::getDatabaseHandle();
     }
 
 
@@ -147,6 +158,7 @@ class stats
 
         // Get database handle, if it is invalid (NULL) return without creating stats
         $res = stats::getDatabaseHandle();
+#        stats::dropTable($res);
         if(!$res) return;
 
         // Ensure that 'clicks' and 'overallRenderTimer' are present and set correctly, 
@@ -154,7 +166,7 @@ class stats
         //   -- 'clicks' is a counter wich is set in main.php -> Number of page reloads 
         //   -- 'overallRenderTimer' is set in main.php -> timestamp of rendering start.
         if(!isset($clicks) || empty($clicks)) $clicks = 0;
-        if(!isset($overallRenderTimer) || empty($overallRenderTimer)){
+        if(!isset($overallRenderTimer)){
             $renderTime = 0;
         }else{
             $renderTime = microtime(TRUE) - $overallRenderTimer;
@@ -164,6 +176,9 @@ class stats
             $overallRenderTimer = microtime(TRUE);
         }
 
+        $duration   = stats::prepareFloatForWriting($duration);
+        $renderTime = stats::prepareFloatForWriting($renderTime);
+
         // Prepare values to be useable within a database
         $uuid = $config->getGOsaUUID();
         $type           = sqlite_escape_string($type);
@@ -177,7 +192,7 @@ class stats
         $info           = sqlite_escape_string($info);
         $clicks         = sqlite_escape_string($clicks);
         $memory_usage   = sqlite_escape_string(stats::get_memory_usage());
-        $cpu_load       = sqlite_escape_string(stats::get_cpu_load());
+        $cpu_load       = sqlite_escape_string(sprintf("%0.6f",stats::get_cpu_load()));
 
         // Clean up category, which usally comes from acl_category and may still contain 
         //  some special chars like /
@@ -199,7 +214,37 @@ class stats
                     '{$memory_usage}','{$cpu_load}','{$info}')";
         sqlite_query($query, $res);
     }
-   
+  
+    /*! \brief      |
+     *  @param      |
+     *  @return     |
+     */  
+    static function dumpTables($from = NULL, $to = NULL)
+    {
+        // Get database connection
+        $TABLE_NAME = stats::$tableName;
+        $handle = stats::getDatabaseHandle();
+        if(!$handle) return;        
+
+        // Build up filter to limit dumped entries to the given range.
+        $tim = "";
+        if($from != NULL){
+            $from = sqlite_escape_string($from);
+            $tim.= "AND TIMESTAMP >= '{$from}' ";
+        }
+        if($to != NULL){
+            $to = sqlite_escape_string($to);
+            $tim.= "AND TIMESTAMP <= '{$to}' ";
+        }
+        $tim = preg_replace("/^AND /"," WHERE ",$tim);
+
+        // Create Filter and start query
+        $filter = "SELECT * FROM {$TABLE_NAME}{$tim}";
+        $ret = sqlite_array_query($filter, $handle, SQLITE_ASSOC);
+        return($ret);
+    }
+
  
     /*! \brief      |
      *  @param      |
@@ -207,7 +252,13 @@ class stats
      */  
     static function show()
     {
+
+        stats::dumpTables();
+
+        return;
+
         $res = stats::getDatabaseHandle();
+#       stats::dropTable($res);
         if(!$res) return;        
 
         $TABLE_NAME = stats::$tableName;
@@ -543,6 +594,31 @@ class stats
         }
         echo sqlite_error_string($ret);
 
+
+        echo "\n------ \n";
+        echo "Rendertime per plugin\n";
+        echo "------ \n";
+
+        $query = "
+            SELECT PLUGIN, RENDER_TIME AS RM
+            FROM {$TABLE_NAME}
+            GROUP BY PLUGIN
+            ORDER BY RM DESC
+            LIMIT 10
+            ";
+        $ret = sqlite_query($query, $res);
+
+        $colSize = 16;
+        $title = FALSE;
+        foreach(sqlite_fetch_all($ret) as $entry){
+            foreach($entry as $key => $str){
+                if(is_numeric($key)) continue;
+                echo str_pad($str,$colSize,' ')."|"; 
+            }
+            echo "\n";
+        }
+        echo sqlite_error_string($ret);
+
         echo "</pre>";
     }
 }