Code

Updated stats
[gosa.git] / gosa-core / plugins / generic / statistics / class_statistics.inc
index b615da14d848c56066aa13572f320a1f0c3ea1f5..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,6 +121,7 @@ class statistics extends plugin
                 $dates[$date] = strtotime($date);
             }
         }
+        $this->rpcHandle_Error = !$this->rpcHandle->success();
         return($dates);
     }
 
@@ -128,10 +132,12 @@ class statistics extends plugin
         $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)));
         }
@@ -150,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();
         }
@@ -167,17 +173,19 @@ 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(
                     _("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)));
     }
 
@@ -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);
     }
 
@@ -262,6 +275,19 @@ class statistics extends plugin
         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;
+                    }
+                }
+            }
+        }
     }
 
 
@@ -301,7 +327,11 @@ class statistics extends plugin
     function reloadGraphs()
     {
         new pChartInclude();
+
         $gData = $this->statisticData;
+        if(!count($gData)){
+            return;
+        }
         if(count($gData['actionsPerCategory'])){
             $this->generateCategoryPieGraph($gData['actionsPerCategory']);
         }
@@ -344,8 +374,10 @@ class statistics extends plugin
         $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);
@@ -355,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
@@ -372,29 +405,60 @@ 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])){
 
-        // Remove all graph series and add the error-series, then draw the new graph.
-        foreach($gData['actionsPerInterval'] as $category => $data){
-            $dataSet->RemoveSerie($category);
+            // 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');
+        }
+
+        
+        // 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;
     }
    
@@ -432,11 +496,10 @@ class statistics extends plugin
         $chart->drawScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, FALSE);
         $chart->drawFilledCubicCurve($dataSet->GetData(),$dataSet->GetDataDescription(),.1,50); 
 
-        $gName = "graphID_".$gID;
-        $this->$gName = preg_replace("/[^0-9]/","",microtime(TRUE));
-        $file = '/tmp/graph_'.$this->$gName;
+        $this->graphIDs[$gID] = preg_replace("/[^0-9]/","",microtime(TRUE));
+        $file = '/tmp/graph_'.$this->graphIDs[$gID];
         $chart->Render($file);
-        session::set('statistics::graphFile'.$this->$gName,$file);
+        session::set('statistics::graphFile'.$this->graphIDs[$gID],$file);
     }
 
 
@@ -446,6 +509,7 @@ class statistics extends plugin
     {
         // 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);
 
@@ -479,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);
     }
 }
 ?>