Code

Updated statistics plugin
[gosa.git] / gosa-core / plugins / generic / statistics / class_statistics.inc
index 0aa840e852b42b73f65630d03464dcaab40d1be8..ab415a01d8840947cf4d7cd9951ee3075037d640 100644 (file)
@@ -17,6 +17,8 @@ class statistics extends plugin
     var $graph1DatePicker1 = 0;
     var $graph1DatePicker2 = 0;
 
+    var $unsbmittedFiles = array();
+
     function __construct($config)
     {
         plugin::plugin($config, NULL);
@@ -35,8 +37,78 @@ class statistics extends plugin
                     "WyukwauWoid2",
                     TRUE);
         }
+
+        // Get list of unsubmitted files.
+        $this->unsbmittedFiles = $this->getUnsubmittedStatistics();
+
+        // Collect category translations
+        $this->catTranslations = array();
+        foreach($this->config->configRegistry->getListOfPlugins() as $plugin => $data){
+            if(isset($data['plCategory'])){
+                foreach($data['plCategory'] as $id => $name){
+                    if(!is_numeric($id)){
+                        $this->catTranslations[$id] = $name['description'];
+                    }
+                }
+            }
+        }
     }
 
+    /*! \brief      Returns a list local stored statistic files
+        @param      Array   A list of filenames and dates.
+     */ 
+    function getLocalStatisticsFiles()
+    {
+        
+        $res = stats::getLocalStatFiles();
+        $tmp = array();
+        if(count($res)){
+            foreach($res as $file){
+                $date = strtotime($file);
+                if($date){
+                    $tmp[$file] = $date;
+                }
+            }
+        }
+        return($tmp);
+    }
+
+   
+    /*! \brief      Returns a list of not transmitted stat files (except files for the current day)
+     *  @return     Array   A list of unsubmitted statistic files.
+     */ 
+    function getUnsubmittedStatistics()
+    {
+        $available = $this->getLocalStatisticsFiles();
+        $alreadyTransmitted = $this->getStatisticsDatesFromServer();
+        $unsubmitted = array_intersect($available,$alreadyTransmitted);
+
+        // 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);  
+    }
+
+
+    /*! \brief      Request a list of dates for which the server can return statistics.
+        @param      Array   A list of dates    $ret=[iso-str] = timestamp
+     */ 
+    function getStatisticsDatesFromServer()
+    {
+        $res = $this->rpcHandle->getInstanceStatDates();
+        $dates = array();
+        if(!$this->rpcHandle->success()){
+            msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
+        }else{
+            foreach($res as $date){
+                $dates[$date] = strtotime($date);
+            }
+        }
+        return($dates);
+    }
+
+
     function execute()
     {
         if(isset($_POST['graph1DatePicker1'])) $this->graph1DatePicker1 = get_post('graph1DatePicker1');
@@ -61,17 +133,22 @@ class statistics extends plugin
 
         // Send stats 
         if(isset($_POST['transmitStatistics'])){
-            $tmp = stats::dumpTables();
-            $dump = array();
-            foreach($tmp as $entry){
-                $dump[] = array_values($entry);
-            }
-            $res = $this->rpcHandle->updateInstanceStatus($dump);
-            if(!$this->rpcHandle->success()){
-                msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
-            }else{
-                echo $res." Entries inserted";
+            $this->unsbmittedFiles = $this->getUnsubmittedStatistics();
+            foreach($this->unsbmittedFiles as $filename => $date){
+                $tmp = stats::dumpTables($filename);
+                $dump = array();
+                foreach($tmp as $entry){
+                    $dump[] = array_values($entry);
+                }
+                $res = $this->rpcHandle->updateInstanceStatus($dump);
+                if(!$this->rpcHandle->success()){
+                    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->unsbmittedFiles = $this->getUnsubmittedStatistics();
         }
 
         // Transmit daily statistics to GOsa-Server
@@ -80,12 +157,11 @@ class statistics extends plugin
             // First try to retrieve values via RPC
             if ($this->config->get_cfg_value("core","gosaRpcServer") != ""){
 
-                $interval = $this->graph1Interval;
                 $start = strtotime($this->graph1DatePicker1);
                 $stop  = strtotime($this->graph1DatePicker2);
 
                 // Request statistics now
-                $res = $this->rpcHandle->getInstanceStats($start,$stop,$interval);
+                $res = $this->rpcHandle->getInstanceStats($start,$stop);
                 if(!$this->rpcHandle->success()){
                     msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
                 }
@@ -95,32 +171,45 @@ class statistics extends plugin
                     // Include pChart 
                     new pChartInclude();
 
-                    // --------
-                    // Generate PIE chart of most used categories
-                    // --------
-
                     // Get most used categories, but only eight at once.
-                    arsort($res['actionsPerCategory']);
-                    $mostUsedCategories = array_slice($res['actionsPerCategory'],0,8);
+                    if(count($res['actionsPerCategory'])){
+
+                        // --------
+                        // Generate PIE chart of most used categories
+                        // --------
 
-                    // Dataset definition   
-                    $DataSet = new pData;  
-                    $this->graphID_1 = preg_replace("/[^0-9]/","",microtime(TRUE));
-                    $DataSet->AddPoint(array_values($mostUsedCategories),"Serie1");  
-                    $DataSet->AddPoint(array_keys($mostUsedCategories),"Serie2");  
-                    $DataSet->AddAllSeries();  
-                    $DataSet->SetAbsciseLabelSerie("Serie2");  
+                        arsort($res['actionsPerCategory']);
 
-                    // Initialise the graph  
-                    $Test = new pChart(400,200);  
-                    $Test->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
-                    $Test->drawPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),150,90,110,PIE_PERCENTAGE,TRUE,50,20,5);  
-                    $Test->drawPieLegend(310,15,$DataSet->GetData(),$DataSet->GetDataDescription(),200,255,200);  
+                        $mostUsedCategories = array_slice($res['actionsPerCategory'],0,8);
 
-                    $file = '/tmp/graph_'.$this->graphID_1;
-                    $Test->Render($file);
-                    session::set('statistics::graphFile'.$this->graphID_1,$file);
+                        // Dataset definition   
+                        $DataSet = new pData;  
+                        $this->graphID_1 = preg_replace("/[^0-9]/","",microtime(TRUE));
 
+                        $values = array_values($mostUsedCategories);
+
+                        $keys = array_keys($mostUsedCategories);
+                        foreach($keys as $id => $cat){
+                            $keys[$id] = $this->getCategoryTranslation($cat);
+                        }
+
+                        $DataSet->AddPoint($values,"Serie1");  
+                        $DataSet->AddAllSeries();  
+                        $DataSet->AddPoint($keys,"Serie2");  
+                        $DataSet->SetAbsciseLabelSerie("Serie2");  
+
+                        // Initialise the graph  
+                        $Test = new pChart(500,200);  
+                        $Test->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
+
+
+                        $Test->drawPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),150,90,110,PIE_PERCENTAGE,TRUE,50,20,5);  
+                        $Test->drawPieLegend(310,15,$DataSet->GetData(),$DataSet->GetDataDescription(),200,255,200);  
+
+                        $file = '/tmp/graph_'.$this->graphID_1;
+                        $Test->Render($file);
+                        session::set('statistics::graphFile'.$this->graphID_1,$file);
+                    }
 
                     // --------
                     // Generate combined line and car chart of plugin usage, ldap execution time and errors 
@@ -129,60 +218,110 @@ class statistics extends plugin
                     // Generate new and unique graph id
                     $this->graphID_2 = preg_replace("/[^0-9]/","",microtime(TRUE));
 
-                    // Build up DataSet. 
+                    // Prepare transmitted data, sort it by date and collect 
+                    //  transmitted timestamps to be able to print the x-Axis labels.
+                    $dataArray = array();
+                    $dates = array();
                     $DataSet2 = new pData;  
-                    $intervalEntries = array();
-                    $max = 0;
-                    foreach($res['actionsPerInterval'] as $category => $dates){
-                        if(!isset($mostUsedCategories[$category])) continue;
-                        ksort($res['actionsPerInterval'][$category]);
-                        $DataSet2->AddPoint($res['actionsPerInterval'][$category], $category);
-                        $DataSet2->SetSerieName(_($category), $category);
-                        $tmpMax = max($res['actionsPerInterval'][$category]);
+                    $max = 1;
+                    $seriesNumber = 0;
+                    foreach($res['actionsPerInterval'] as $category => $entriesPerDate){
+
+                        // Collect data per category and store used timestamps
+                        foreach($entriesPerDate as $dateStr => $count){
+                            $date = strtotime($dateStr);
+                            $dates[$date]=$date;
+
+                            // Do not append empty data                            
+                            if(empty($category)) continue;
+                            if($count) $count = ($count);
+                            $dataArray[$category][$date] = $count;
+                        }
+                      
+                        // Do not append empty data 
+                        if(empty($category)) continue;
+
+                        // Sort results. 
+                        ksort($dataArray[$category]);
+
+                        // Add results to our data set.
+                        $DataSet2->AddPoint($dataArray[$category], $category);
+                        $DataSet2->SetSerieName($this->getCategoryTranslation($category), $category);
+                        $Test->setLabel($DataSet->GetData(),$DataSet->GetDataDescription(),"Serie1",0,"Daily incomes");  
+                        $DataSet2->AddSerie($category);
+                        $seriesNumber++;
+
+                        // Detect maximum value, to adjust the Y-Axis
+                        $tmpMax = max($dataArray[$category]);
                         if($tmpMax > $max) $max = $tmpMax;
                     }
-                    $DataSet2->AddAllSeries();  
-
-                    $Xam = 5;
-                    $cnt =0;
-                    foreach($res['actionsPerIntervalDates'] as $key => $date){
-                        $date = strtotime($date);
-                        if((count($dates) <= $Xam) || 
-                            ($cnt % (floor(count($dates) / $Xam )) == 0)){
-                            $tmp[$date] = date('d.m.Y',$date);
+                    ksort($dates);
+
+                    // Prepare date strings for X-Axis, only print a given number of 
+                    //  of labels to keep the axis readable.
+                    $Xam = 5; // Number of labels
+                    $cnt = 0;
+                    $tmp = array();
+                    foreach($dates as $stamp){
+                        if((count($dates) <= $Xam) || ($cnt % (floor(count($dates) / $Xam )) == 0)){
+                            $tmp[$stamp] = date('d.m.Y',$stamp);
                         }else{
-                            $tmp[$date] = ' ';
+                            $tmp[$stamp] = ' ';
                         }
                         $cnt ++;    
                     }
-               
+
                     $DataSet2->AddPoint($tmp, 'date');
                     $DataSet2->SetAbsciseLabelSerie('date');  
-                    $DataSet2->RemoveSerie('date');  
 
-                    $Test2 = new pChart(700,230);  
-                    $Test2->setFixedScale(0,$max);  
+                    $Test2 = new pChart(800,230);  
+                    $Test2->setFixedScale(0.0001,($max*1.1));  
                     $Test2->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
                     $Test2->setGraphArea(50,30,585,200);  
                     $Test2->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);  
                     $Test2->drawRoundedRectangle(5,5,695,225,5,230,230,230);  
                     $Test2->drawGraphArea(255,255,255,TRUE);  
-                    $Test2->drawScale($DataSet2->GetData(),$DataSet2->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2);     
-                    $Test2->drawGrid(4,TRUE,230,230,230,50);  
-
-                    // Draw the 0 line  
+                    $Test2->drawGrid(4,TRUE,200,200,200,50);  
                     $Test2->drawTreshold(0,143,55,72,TRUE,TRUE);  
+                    $Test2->drawTitle(50,22,"Plugin usage over time",50,50,50,585);  
+
+                    if(count($dates)){
+                        $Test2->drawScale($DataSet2->GetData(),$DataSet2->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2);     
+                    }
 
                     // Draw the cubic curve graph  
-                    #$Test2->drawBarGraph($DataSet2->GetData(),$DataSet2->GetDataDescription(),TRUE);  
-                    #$Test2->drawLineGraph($DataSet2->GetData(),$DataSet2->GetDataDescription(),TRUE); 
-                    $Test2->drawFilledLineGraph($DataSet2->GetData(),$DataSet2->GetDataDescription(),50,TRUE);  
+                    if(count($dataArray)){
+                        $Test2->drawFilledLineGraph($DataSet2->GetData(),$DataSet2->GetDataDescription(),50,TRUE);
+                    }
 
-                    // Finish the graph  
-                    $DataSet2->RemoveSerie('date');  
-                    $Test2->drawLegend(600,30,$DataSet2->GetDataDescription(),255,255,255);  
-                    $Test2->drawTitle(50,22,"Plugin usage over time",50,50,50,585);  
+                    // Add error series
+                    $errors = array();
+                    foreach($res['errorsPerInterval'] as $dateStr => $count){
+                        $date = strtotime($dateStr);
+                        if($count !=0) $count = ($count);
+                        $errors[$date] = $count;
+                    }
+
+                    $DataSet2->AddPoint($errors, 'Errors');
+                    $DataSet2->SetSerieName(_('Error'), 'Errors');
+                    $DataSet2->AddSerie('Errors');
+                    $seriesNumber ++;
+                   
+                    $Test2->setColorPalette($seriesNumber-1,255,0,0);   
+
+                    // Draw legend
+                    $Test2->drawLegend(650,30,$DataSet2->GetDataDescription(),255,255,255);  
+
+                    // Remove plugin usage from data series, just keep error series.
+                    foreach($dataArray as $categoryName => $list){
+                        $DataSet2->RemoveSerie($categoryName);
+                    }
+    
+                    // Draw right scale (Errors per day)
+                    $Test2->setFixedScale(0.0001,(max($errors) +1) *1.1);
+                    $Test2->drawRightScale($DataSet2->GetData(),$DataSet2->GetDataDescription(),SCALE_NORMAL,120,150,150,TRUE,0,2);
+                    $Test2->drawBarGraph($DataSet2->GetData(),$DataSet2->GetDataDescription());
 
                     $file = '/tmp/graph_'.$this->graphID_2;
                     $Test2->Render($file);
@@ -193,18 +332,55 @@ class statistics extends plugin
 
         $smarty->assign('graphID_1', $this->graphID_1);
         $smarty->assign('graphID_2', $this->graphID_2);
+        $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)));
         return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
     }
 
+
     function check()
     {
         $messages = plugin::check();
         return($messages);
     }
 
+
     function save_object()
     {
         plugin::save_object();
     }
+
+
+    /*! \brief      This method tries to translate category names.
+     *  @param      The category name to translate
+     *  @return     String  The translated category names.
+     */
+    function getCategoryTranslation($name)
+    {
+        $ret ="";
+        
+        // Extract category names from the given string.
+        $list = preg_split("/, /", $name);
+   
+        // We do not have a category for systems directly, so we've to map all system types to 'System'.
+        // If we do not map to _(Systems) the graph legend will be half screen width.
+        if(count(array_intersect(array('server','terminal','workstation', 'opsi', 'component'), $list))){
+            return(_("Systems"));
+        }
+
+        // Walk through category names and try to find a translation.
+        foreach($list as $cat){
+            $cat = trim($cat);
+            if(isset($this->catTranslations[$cat])){
+                $ret .= _($this->catTranslations[$cat]).", ";
+            }else{
+                $ret .= $cat.", ";
+            }
+        }
+        return(rtrim($ret, ', '));
+    }
 }
 ?>