Code

Updated stats
[gosa.git] / gosa-core / plugins / generic / statistics / class_statistics.inc
index 2d903c0762d9616eb11d7a41f96221d46df086c4..d3f967c701b0d263f21cc67b7d4bc0c50ed6c89f 100644 (file)
@@ -12,11 +12,9 @@ class statistics extends plugin
 
     var $statisticData = array();
 
-    var $graphID_1 = 0;
-    var $graphID_2 = 0;
-    var $graphID_3 = 0;
-    var $graphID_4 = 0;
-    var $graphID_5 = 0;
+    var $graphIDs = array();
+    var $skipSeries = array();
+    var $seriesListPerGraph = array();
 
     var $legendR = 235;
     var $legendG = 235;
@@ -99,7 +97,6 @@ class statistics extends plugin
         // Exclude statistic collection from today, they are still active and cannot be submitted.
         $curDate =  date('Y-m-d');
         if(isset($unsubmitted)) unset($unsubmitted[$curDate]);
-
         return($unsubmitted);  
     }
 
@@ -109,6 +106,12 @@ class statistics extends plugin
      */ 
     function getStatisticsDatesFromServer()
     {
+        // Do not request anything while rpc isn't configured.
+        if(!$this->rpcConfigured){
+            return(array());
+        }
+        
+        // Try to gather statistic dates from the backenbd.
         $res = $this->rpcHandle->getInstanceStatDates();
         $dates = array();
         if(!$this->rpcHandle->success()){
@@ -118,23 +121,23 @@ class statistics extends plugin
                 $dates[$date] = strtotime($date);
             }
         }
+        $this->rpcHandle_Error = !$this->rpcHandle->success();
         return($dates);
     }
 
 
     function execute()
     {
-        if(isset($_POST['graph1DatePicker1'])) $this->graph1DatePicker1 = get_post('graph1DatePicker1');
-        if(isset($_POST['graph1DatePicker2'])) $this->graph1DatePicker2 = get_post('graph1DatePicker2');
-
         $smarty = get_smarty();
         $smarty->assign('graph1DatePicker1', $this->graph1DatePicker1);
         $smarty->assign('graph1DatePicker2', $this->graph1DatePicker2);
 
+        $this->reloadGraphs();
+
         // Do not render anything if we are not prepared to send and receive data via rpc.
         $smarty->assign("rpcConfigured", $this->rpcConfigured);
         $smarty->assign("validRpcHandle", TRUE);
-        if(!$this->rpcConfigured || !$this->rpcHandle){
+        if(!$this->rpcHandle){
             $smarty->assign("validRpcHandle", FALSE);
             return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
         }
@@ -153,8 +156,8 @@ class statistics extends plugin
                     msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
                 }else{
                     stats::removeStatsFile($filename);
-                    echo "Inserted ".$res." entries for date ".date('d.m.Y', $date)."<br>";
                 }
+                $this->rpcHandle_Error = !$this->rpcHandle->success();
             }
             $this->unsbmittedFiles = $this->getUnsubmittedStatistics();
         }
@@ -170,29 +173,34 @@ class statistics extends plugin
                 $this->statisticData = $this->prepareGraphData($res); 
                 $this->reloadGraphs();
             }
+            $this->rpcHandle_Error = !$this->rpcHandle->success();
         }
 
-        $smarty->assign('graphID_1', $this->graphID_1);
-        $smarty->assign('graphID_2', $this->graphID_2);
-        $smarty->assign('graphID_3', $this->graphID_3);
-        $smarty->assign('graphID_4', $this->graphID_4);
-        $smarty->assign('graphID_5', $this->graphID_5);
+
+        $smarty->assign('seriesListPerGraph', $this->seriesListPerGraph);
+        $smarty->assign('skipSeries', $this->skipSeries);
+        $smarty->assign('graphIDs', $this->graphIDs);
         $smarty->assign('unsbmittedFiles', count($this->unsbmittedFiles));
-        $smarty->assign('unsbmittedFilesMsg', 
-                sprintf(
+        $smarty->assign('unsbmittedFilesMsg', sprintf(
                     _("You have currently %s unsubmitted statistic collection, do you want to transmit them now?"),
                     count($this->unsbmittedFiles)));
+    
+        $smarty->assign('rpcHandle_Error', $this->rpcHandle_Error);
         return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
     }
 
 
+    /*! \brief      Prepares the graph data we've received from the rpc-service.
+     *              This method will construct a usable data-array with converted 
+     *               date strings.
+     */
     function prepareGraphData($res)
     { 
-        $gData = array();
 
         /* Build up array which represents the amount of errors per
          *  interval.
          */
+        $gData = array();
         foreach($res['errorsPerInterval'] as $dateStr => $data){
             $date = strtotime($dateStr);
             $gData['errorsPerInterval'][$date] = $data;
@@ -246,6 +254,11 @@ class statistics extends plugin
             }
             ksort($gData['actionsPerInterval'][$category]);
         }
+        $this->skipSeries[1] = array();
+        $this->skipSeries[2] = array();
+        $this->skipSeries[3] = array();
+        $this->skipSeries[4] = array();
+        $this->skipSeries[5] = array();
         return($gData);
     }
 
@@ -260,6 +273,21 @@ class statistics extends plugin
     function save_object()
     {
         plugin::save_object();
+        if(isset($_POST['graph1DatePicker1'])) $this->graph1DatePicker1 = get_post('graph1DatePicker1');
+        if(isset($_POST['graph1DatePicker2'])) $this->graph1DatePicker2 = get_post('graph1DatePicker2');
+
+    
+        // Get series to enable or disable
+        foreach($this->seriesListPerGraph as $graphId => $series){
+            $this->skipSeries[$graphId] = array();
+            if(isset($_POST["graphPosted_{$graphId}"])){
+                foreach($series as $seriesName => $seriesDesc){
+                    if(!isset($_POST["addSeries_{$graphId}_{$seriesName}"])){
+                        $this->skipSeries[$graphId][] = $seriesName;
+                    }
+                }
+            }
+        }
     }
 
 
@@ -294,24 +322,62 @@ class statistics extends plugin
     }
 
 
+    /*! \brief  Reload the graph images.
+     */ 
     function reloadGraphs()
     {
         new pChartInclude();
+
         $gData = $this->statisticData;
+        if(!count($gData)){
+            return;
+        }
         if(count($gData['actionsPerCategory'])){
             $this->generateCategoryPieGraph($gData['actionsPerCategory']);
         }
         $this->generateActionsGraph($gData);
+
+        // Generate graph which displays the memory usage over time
+        $series = array(
+            'max_mem' => _('Max'),
+            'avg_mem' => _('Avergae'),
+            'min_mem' => _('Min'));
+        $this->generateSystemStatsGraph($gData,'usagePerInterval',$series, _("Memory usage"),3);
+
+        // Generate graph which displays the cpu load over time
+        $series = array(
+            'max_load' => _('Max'),
+            'avg_load' => _('Avergae'),
+            'min_load' => _('Min'));
+        $this->generateSystemStatsGraph($gData,'usagePerInterval',$series, _("CPU load"),4);
+
+        // Generate graph which displays the render time
+        $series = array(
+            'max_render' => _('Max'),
+            'avg_render' => _('Avergae'),
+            'min_render' => _('Min'));
+        $this->generateSystemStatsGraph($gData,'usagePerInterval',$series, _("Render time"),5);
+
+        // Generate graph which displays the plugin duration
+        $series = array(
+            'max_dur' => _('Max'),
+            'avg_dur' => _('Avergae'),
+            'min_dur' => _('Min'));
+        $this->generateSystemStatsGraph($gData,'usagePerInterval',$series, _("Seconds per action"),6);
     }
 
 
+    /*! \brief  Generates the line-graph which displays the plugin usage over time.
+     */ 
     function generateActionsGraph($gData)
     {
         $lineMax = 100;
         $errorMax = (max($gData['errorsPerInterval']) < 100)? 100:max($gData['errorsPerInterval']);
         $dataSet = new pData;  
+        $seriesCnt = 0;
+        $gID = 2;
         foreach($gData['actionsPerInterval'] as $category => $entriesPerDate){
-            if(empty($category)) continue;
+            if(empty($category) || in_array($category, $this->skipSeries[$gID])) continue;
 
             // Add results to our data set.
             $dataSet->AddPoint($entriesPerDate, $category);
@@ -321,6 +387,7 @@ class statistics extends plugin
             // Detect maximum value, to adjust the Y-Axis
             $tmpMax = max($entriesPerDate);
             if($tmpMax > $lineMax) $lineMax = $tmpMax;
+            $seriesCnt ++;
         }
 
         // Add timeline
@@ -338,181 +405,111 @@ class statistics extends plugin
         $chart->drawTreshold(0,143,55,72,TRUE,TRUE);  
         $chart->drawTitle(50,22,"Plugin usage over time",50,50,50,585);  
         $chart->drawScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, TRUE);     
-        $chart->drawFilledLineGraph($dataSet->GetData(),$dataSet->GetDataDescription(),50,TRUE);
-        $chart->setColorPalette(count($gData['actionsPerInterval']),255,0,0);   
 
-        // Draw legend
-        $dataSet->AddPoint($gData['errorsPerInterval'], 'Errors');
-        $dataSet->SetSerieName(_('Error'), 'Errors');
-        $dataSet->AddSerie('Errors');
-        $chart->drawLegend(650,30,$dataSet->GetDataDescription(),255,255,255);  
+        // Only draw this graph if we've at least one series to draw! 
+        if($seriesCnt){
+            $chart->drawFilledLineGraph($dataSet->GetData(),$dataSet->GetDataDescription(),50,TRUE);
+        }
+
+        // Do we've to add the errors series?
+        // If we have to, then add the error-data-series.
+        //  and set the color for the new error-series to red.
+        if(!in_array('errorsPerInterval', $this->skipSeries[$gID])){
+
+            // Set the color for the error Series to 'red'. 
+            // This has to be done before drawing the legend.
+            $chart->setColorPalette($seriesCnt,255,0,0);   
+
+            $dataSet->AddPoint($gData['errorsPerInterval'], 'Errors');
+            $dataSet->SetSerieName(_('Error'), 'Errors');
+            $dataSet->AddSerie('Errors');
+        }
 
-        // Remove all graph series and add the error-series, then draw the new graph.
-        foreach($gData['actionsPerInterval'] as $category => $data){
-            $dataSet->RemoveSerie($category);
+        
+        // Draw legend, but only if there is something to draw!
+        if($seriesCnt || !in_array('errorsPerInterval', $this->skipSeries[$gID])){
+            $chart->drawLegend(650,30,$dataSet->GetDataDescription(),255,255,255);  
+        }
+
+        // Draw the error graph on top of the other graphs now.
+        // But remove the category-graph before. 
+        if(!in_array('errorsPerInterval', $this->skipSeries[$gID])){
+
+            // Remove all graph series and add the error-series, then draw the new graph.
+            // (It is not relevant if it was really added before, so we simply remove all series!)
+            foreach($gData['actionsPerInterval'] as $category => $data){
+                $dataSet->RemoveSerie($category);
+            }
+            $chart->setFixedScale(0,$errorMax);
+            $chart->drawRightScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,120,150,150,TRUE,0,2, TRUE);
+            $chart->drawBarGraph($dataSet->GetData(),$dataSet->GetDataDescription());
         }
-        $chart->setFixedScale(0,$errorMax);
-        $chart->drawRightScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,120,150,150,TRUE,0,2, TRUE);
-        $chart->drawBarGraph($dataSet->GetData(),$dataSet->GetDataDescription());
 
         // Generate new and unique graph id
-        $this->graphID_2 = preg_replace("/[^0-9]/","",microtime(TRUE));
-        $file = '/tmp/graph_'.$this->graphID_2;
+        $this->graphIDs[$gID] = preg_replace("/[^0-9]/","",microtime(TRUE));
+        $file = '/tmp/graph_'.$this->graphIDs[$gID];
         $chart->Render($file);
-        session::set('statistics::graphFile'.$this->graphID_2,$file);
+        session::set('statistics::graphFile'.$this->graphIDs[$gID],$file);
 
+        // Keep a list of all selecteable data-series, to allow the user to disable
+        //  or enable series on demand.
+        $this->seriesListPerGraph[$gID] = array();
+        foreach($gData['actionsPerInterval'] as $key => $data){
+            $this->seriesListPerGraph[$gID][$key] = $this->getCategoryTranslation($key); 
+        }
+        $this->seriesListPerGraph[$gID]['errorsPerInterval'] = _("Error");
         return;
     }
-    
-#       // Prepare Data 
-#       $graphData = array();
-#       foreach($gData['usagePerInterval'] as $dateStr => $data){
-#           $date = strtotime($dateStr);
-#           foreach($data as $name => $val){
-#               $graphData[$name][$date] = $val;
-#           }
-#       }
-#
-#       // Sort Data 
-#       foreach($graphData as $key => $data)
-#           ksort($graphData[$key]);
-#
-#       // Generate new and unique graph id
-#       $this->graphID_3 = preg_replace("/[^0-9]/","",microtime(TRUE));
-#
-#       // Prepare transmitted data, sort it by date and collect 
-#       //  transmitted timestamps to be able to print the x-Axis labels.
-#       $dataSet = new pData;  
-#
-#       $max = max($graphData['max_mem']);
-#
-#       $dataSet->AddPoint(array_values($graphData['max_mem']), 'max_mem');
-#       $dataSet->AddPoint(array_values($graphData['avg_mem']), 'avg_mem');
-#       $dataSet->AddPoint(array_values($graphData['min_mem']), 'min_mem');
-#
-#       $dataSet->SetSerieName('Min Memory', 'min_mem');
-#       $dataSet->SetSerieName('Max Memory', 'max_mem');
-#       $dataSet->SetSerieName('Average Memory', 'avg_mem');
-#
-#       $dataSet->AddAllSeries();  
-#       $dataSet->AddPoint($dateSeries, 'date');
-#       $dataSet->SetAbsciseLabelSerie('date');
-#
-#       $chart = new pChart(800,230);  
-#       $chart->setFixedScale(0.0001,($max*1.1));  
-#       $chart->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
-#       $chart->setGraphArea(50,30,585,200);  
-#       $chart->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);  
-#       $chart->drawRoundedRectangle(5,5,695,225,5,230,230,230);  
-#       $chart->drawGraphArea(255,255,255,TRUE);  
-#       $chart->drawGrid(4,TRUE,200,200,200,50);  
-#       $chart->drawTreshold(0,143,55,72,TRUE,TRUE);  
-#       $chart->drawTitle(50,22,"Memory usage",50,50,50,585);  
-#
-#       $chart->drawScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, FALSE);
-#       $chart->drawFilledCubicCurve($dataSet->GetData(),$dataSet->GetDataDescription(),.1,50); 
-#
-#       $file = '/tmp/graph_'.$this->graphID_3;
-#       $chart->Render($file);
-#       session::set('statistics::graphFile'.$this->graphID_3,$file);
-#
-#
-#
-#
-#
-#
-#
-#       // Generate new and unique graph id
-#       $this->graphID_4 = preg_replace("/[^0-9]/","",microtime(TRUE));
-#
-#       // Prepare transmitted data, sort it by date and collect 
-#       //  transmitted timestamps to be able to print the x-Axis labels.
-#       $dataSet = new pData;  
-#
-#       $max = max($graphData['max_dur']);
-#
-#       $dataSet->AddPoint(array_values($graphData['max_dur']), 'max_dur');
-#       $dataSet->AddPoint(array_values($graphData['avg_dur']), 'avg_dur');
-#       $dataSet->AddPoint(array_values($graphData['min_dur']), 'min_dur');
-#
-#       $dataSet->SetSerieName('Min dur', 'min_dur');
-#       $dataSet->SetSerieName('Max dur', 'max_dur');
-#       $dataSet->SetSerieName('Average dur', 'avg_dur');
-#
-#       $dataSet->AddAllSeries();  
-#       $dataSet->AddPoint($dateSeries, 'date');
-#       $dataSet->SetAbsciseLabelSerie('date');
-#
-#       $chart = new pChart(800,230);  
-#       $chart->setFixedScale(0.0001,($max*1.1));  
-#       $chart->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
-#       $chart->setGraphArea(50,30,585,200);  
-#       $chart->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);  
-#       $chart->drawRoundedRectangle(5,5,695,225,5,230,230,230);  
-#       $chart->drawGraphArea(255,255,255,TRUE);  
-#       $chart->drawGrid(4,TRUE,200,200,200,50);  
-#       $chart->drawTreshold(0,143,55,72,TRUE,TRUE);  
-#       $chart->drawTitle(50,22,"Render time",50,50,50,585);  
-#
-#       $chart->drawScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, FALSE);
-#       $chart->drawFilledCubicCurve($dataSet->GetData(),$dataSet->GetDataDescription(),.1,50); 
-#
-#       $file= '/tmp/graph_'.$this->graphID_4;
-#       $chart->Render($file);
-#       session::set('statistics::graphFile'.$this->graphID_4,$file);
-#
-#
-#
-#
-#
-#
-#
-#
-#       // Generate new and unique graph id
-#       $this->graphID_5 = preg_replace("/[^0-9]/","",microtime(TRUE));
-#
-#       // Prepare transmitted data, sort it by date and collect 
-#       //  transmitted timestamps to be able to print the x-Axis labels.
-#       $dataSet = new pData;  
-#
-#       $max = max($graphData['max_load']);
-#
-#       $dataSet->AddPoint(array_values($graphData['max_load']), 'max_load');
-#       $dataSet->AddPoint(array_values($graphData['avg_load']), 'avg_load');
-#       $dataSet->AddPoint(array_values($graphData['min_load']), 'min_load');
-#
-#       $dataSet->SetSerieName('Min Load', 'min_load');
-#       $dataSet->SetSerieName('Max Load', 'max_load');
-#       $dataSet->SetSerieName('Average Load', 'avg_load');
-#
-#       $dataSet->AddAllSeries();  
-#       $dataSet->AddPoint($dateSeries, 'date');
-#       $dataSet->SetAbsciseLabelSerie('date');
-#
-#       $chart = new pChart(800,230);  
-#       $chart->setFixedScale(0.0001,($max*1.1));  
-#       $chart->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
-#       $chart->setGraphArea(50,30,585,200);  
-#       $chart->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);  
-#       $chart->drawRoundedRectangle(5,5,695,225,5,230,230,230);  
-#       $chart->drawGraphArea(255,255,255,TRUE);  
-#       $chart->drawGrid(4,TRUE,200,200,200,50);  
-#       $chart->drawTreshold(0,143,55,72,TRUE,TRUE);  
-#       $chart->drawTitle(50,22,"CPU load",50,50,50,585);  
-#
-#       $chart->drawScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, FALSE);
-#       $chart->drawFilledCubicCurve($dataSet->GetData(),$dataSet->GetDataDescription(),.1,50); 
-#
-#       $file = '/tmp/graph_'.$this->graphID_5;
-#       $chart->Render($file);
-#       session::set('statistics::graphFile'.$this->graphID_5,$file);
-#
+   
+
+    /*! \brief  Generates a graph about system informations.
+     */ 
+    function generateSystemStatsGraph($gData, $key = "", $series= array(), $title = "", $gID=0 )
+    { 
+        // Add series data to dataSet        
+        $dataSet = new pData;  
+        $max = 0;
+        foreach($series as $seriesName => $seriesDesc){
+            if(isset($gData[$key][$seriesName])){
+                $dataSet->AddPoint($gData[$key][$seriesName],$seriesName);
+                $dataSet->SetSerieName($seriesDesc,$seriesName);
+                if($max < max($gData[$key][$seriesName])) $max = max($gData[$key][$seriesName]);
+            }
+        }
+        $dataSet->AddAllSeries();  
+        $dataSet->AddPoint($gData['dates'], 'date');
+        $dataSet->SetAbsciseLabelSerie('date');  
+        
+        $chart = new pChart(800,230);  
+        $chart->setFixedScale(0.0001,($max*1.1));  
+        $chart->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
+        $chart->setGraphArea(50,30,585,200);  
+        $chart->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);  
+        $chart->drawRoundedRectangle(5,5,695,225,5,230,230,230);  
+        $chart->drawGraphArea(255,255,255,TRUE);  
+        $chart->drawGrid(4,TRUE,200,200,200,50);  
+        $chart->drawTreshold(0,143,55,72,TRUE,TRUE);  
+        $chart->drawTitle(50,22,$title,50,50,50,585);  
+        $chart->drawLegend(650,30,$dataSet->GetDataDescription(),255,255,255);  
+
+        $chart->drawScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, FALSE);
+        $chart->drawFilledCubicCurve($dataSet->GetData(),$dataSet->GetDataDescription(),.1,50); 
 
+        $this->graphIDs[$gID] = preg_replace("/[^0-9]/","",microtime(TRUE));
+        $file = '/tmp/graph_'.$this->graphIDs[$gID];
+        $chart->Render($file);
+        session::set('statistics::graphFile'.$this->graphIDs[$gID],$file);
+    }
 
+
+    /*! \brief  Generate the pie-chart which displays the overall-plugin-usage
+     */
     function generateCategoryPieGraph($data)
     {
         // Sort data by usage count and slice array to get 
         //  the eight most used categories
+        $gID = 1;
         arsort($data);
         $mostUsedCategories = array_slice($data,0,7);
 
@@ -546,10 +543,10 @@ class statistics extends plugin
                 $this->legendR,$this->legendG,$this->legendB);
 
         // Store graph data
-        $this->graphID_1 = preg_replace("/[^0-9]/","",microtime(TRUE));
-        $file = '/tmp/graph_'.$this->graphID_1;
+        $this->graphIDs[$gID] = preg_replace("/[^0-9]/","",microtime(TRUE));
+        $file = '/tmp/graph_'.$this->graphIDs[$gID];
         $chart->Render($file);
-        session::set('statistics::graphFile'.$this->graphID_1,$file);
+        session::set('statistics::graphFile'.$this->graphIDs[$gID],$file);
     }
 }
 ?>