Code

Added additional graphs
[gosa.git] / gosa-core / plugins / generic / statistics / class_statistics.inc
index 384af4b499d862519599206750a708b2ab081348..231db0e8e0ac621ef542eed70ee35b55a70a2f54 100644 (file)
@@ -12,11 +12,15 @@ class statistics extends plugin
 
     var $graphID_1 = 0;
     var $graphID_2 = 0;
+    var $graphID_3 = 0;
+    var $graphID_4 = 0;
+    var $graphID_5 = 0;
 
-    var $graph1Interval = 1;
     var $graph1DatePicker1 = 0;
     var $graph1DatePicker2 = 0;
 
+    var $unsbmittedFiles = array();
+
     function __construct($config)
     {
         plugin::plugin($config, NULL);
@@ -35,21 +39,86 @@ 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');
         if(isset($_POST['graph1DatePicker2'])) $this->graph1DatePicker2 = get_post('graph1DatePicker2');
-        if(isset($_POST['graph1Interval'])) $this->graph1Interval = get_post('graph1Interval');
-
-        if(!$this->graph1Interval) $this->graph1Interval = 1;
 
         $smarty = get_smarty();
         $smarty->assign('graph1DatePicker1', $this->graph1DatePicker1);
         $smarty->assign('graph1DatePicker2', $this->graph1DatePicker2);
-        $smarty->assign('graph1Interval', $this->graph1Interval);
-        $smarty->assign('intervalValues', array('1','2','5','7','30'));
 
         // Do not render anything if we are not prepared to send and receive data via rpc.
         $smarty->assign("rpcConfigured", $this->rpcConfigured);
@@ -61,17 +130,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
@@ -89,33 +163,40 @@ class statistics extends plugin
                     msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
                 }
 
-                if($res){
-
+                if($res && $this->rpcHandle->success()){
 
                     // Include pChart 
                     new pChartInclude();
 
                     // Get most used categories, but only eight at once.
-                    if(count($res['actionsPerCategory']) && 0){
+                    if(count($res['actionsPerCategory'])){
 
                         // --------
                         // Generate PIE chart of most used categories
                         // --------
 
                         arsort($res['actionsPerCategory']);
+
                         $mostUsedCategories = array_slice($res['actionsPerCategory'],0,8);
 
                         // 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");  
+                        $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(400,200);  
+                        $Test = new pChart(500,200);  
                         $Test->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
 
 
@@ -140,6 +221,7 @@ class statistics extends plugin
                     $dates = array();
                     $DataSet2 = new pData;  
                     $max = 1;
+                    $seriesNumber = 0;
                     foreach($res['actionsPerInterval'] as $category => $entriesPerDate){
 
                         // Collect data per category and store used timestamps
@@ -149,7 +231,7 @@ class statistics extends plugin
 
                             // Do not append empty data                            
                             if(empty($category)) continue;
-                            if($count) $count = log($count);
+                            if($count) $count = ($count);
                             $dataArray[$category][$date] = $count;
                         }
                       
@@ -162,7 +244,10 @@ class statistics extends plugin
 
                         // Add results to our data set.
                         $DataSet2->AddPoint($dataArray[$category], $category);
-                        $DataSet2->SetSerieName(_($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]);
@@ -170,72 +255,291 @@ class statistics extends plugin
                     }
                     ksort($dates);
 
-
-
-                    $DataSet2->AddAllSeries();  
-
                     // 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();
+                    $dateSeries = array();
                     foreach($dates as $stamp){
                         if((count($dates) <= $Xam) || ($cnt % (floor(count($dates) / $Xam )) == 0)){
-                            $tmp[$stamp] = date('d.m.Y',$stamp);
+                            $dateSeries[$stamp] = date('d.m.Y',$stamp);
                         }else{
-                            $tmp[$stamp] = ' ';
+                            $dateSeries[$stamp] = ' ';
                         }
                         $cnt ++;    
                     }
 
-                    $DataSet2->AddPoint($tmp, 'date');
+                    $DataSet2->AddPoint($dateSeries, 'date');
                     $DataSet2->SetAbsciseLabelSerie('date');  
-                    $DataSet2->RemoveSerie('date');  
 
-                    $Test2 = new pChart(700,230);  
-                    $Test2->setFixedScale(1,$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->drawGrid(4,TRUE,230,230,230,50);  
+                    $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);     
-                        $DataSet2->RemoveSerie('date');
                     }
 
                     // Draw the cubic curve graph  
                     if(count($dataArray)){
-                        $Test2->drawFilledCubicCurve($DataSet2->GetData(),$DataSet2->GetDataDescription(),.1,20);  
- #                       $Test2->drawLineGraph($DataSet2->GetData(),$DataSet2->GetDataDescription());
-                        $Test2->drawLegend(600,30,$DataSet2->GetDataDescription(),255,255,255);  
+                        $Test2->drawFilledLineGraph($DataSet2->GetData(),$DataSet2->GetDataDescription(),50,TRUE);
+                    }
+
+                    // 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);
                     session::set('statistics::graphFile'.$this->graphID_2,$file);
+
+
+
+
+
+
+
+
+
+
+                        
+                
+
+
+
+
+                    // Prepare Data 
+                    $graphData = array();
+                    foreach($res['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.
+                    $DataSet3 = new pData;  
+
+                    $max = max($graphData['max_mem']);
+
+                    $DataSet3->AddPoint(array_values($graphData['max_mem']), 'max_mem');
+                    $DataSet3->AddPoint(array_values($graphData['avg_mem']), 'avg_mem');
+                    $DataSet3->AddPoint(array_values($graphData['min_mem']), 'min_mem');
+
+                    $DataSet3->SetSerieName('Min Memory', 'min_mem');
+                    $DataSet3->SetSerieName('Max Memory', 'max_mem');
+                    $DataSet3->SetSerieName('Average Memory', 'avg_mem');
+
+                    $DataSet3->AddAllSeries();  
+                    $DataSet3->AddPoint($dateSeries, 'date');
+                    $DataSet3->SetAbsciseLabelSerie('date');
+
+                    $Test3 = new pChart(800,230);  
+                    $Test3->setFixedScale(0.0001,($max*1.1));  
+                    $Test3->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
+                    $Test3->setGraphArea(50,30,585,200);  
+                    $Test3->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);  
+                    $Test3->drawRoundedRectangle(5,5,695,225,5,230,230,230);  
+                    $Test3->drawGraphArea(255,255,255,TRUE);  
+                    $Test3->drawGrid(4,TRUE,200,200,200,50);  
+                    $Test3->drawTreshold(0,143,55,72,TRUE,TRUE);  
+                    $Test3->drawTitle(50,22,"Memory usage",50,50,50,585);  
+
+                    $Test3->drawScale($DataSet3->GetData(),$DataSet3->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, FALSE);
+                    $Test3->drawFilledCubicCurve($DataSet3->GetData(),$DataSet3->GetDataDescription(),.1,50); 
+
+                    $file = '/tmp/graph_'.$this->graphID_3;
+                    $Test3->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.
+                    $DataSet4 = new pData;  
+
+                    $max = max($graphData['max_dur']);
+
+                    $DataSet4->AddPoint(array_values($graphData['max_dur']), 'max_dur');
+                    $DataSet4->AddPoint(array_values($graphData['avg_dur']), 'avg_dur');
+                    $DataSet4->AddPoint(array_values($graphData['min_dur']), 'min_dur');
+
+                    $DataSet4->SetSerieName('Min dur', 'min_dur');
+                    $DataSet4->SetSerieName('Max dur', 'max_dur');
+                    $DataSet4->SetSerieName('Average dur', 'avg_dur');
+
+                    $DataSet4->AddAllSeries();  
+                    $DataSet4->AddPoint($dateSeries, 'date');
+                    $DataSet4->SetAbsciseLabelSerie('date');
+
+                    $Test4 = new pChart(800,230);  
+                    $Test4->setFixedScale(0.0001,($max*1.1));  
+                    $Test4->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
+                    $Test4->setGraphArea(50,30,585,200);  
+                    $Test4->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);  
+                    $Test4->drawRoundedRectangle(5,5,695,225,5,230,230,230);  
+                    $Test4->drawGraphArea(255,255,255,TRUE);  
+                    $Test4->drawGrid(4,TRUE,200,200,200,50);  
+                    $Test4->drawTreshold(0,143,55,72,TRUE,TRUE);  
+                    $Test4->drawTitle(50,22,"Render time",50,50,50,585);  
+
+                    $Test4->drawScale($DataSet4->GetData(),$DataSet4->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, FALSE);
+                    $Test4->drawFilledCubicCurve($DataSet4->GetData(),$DataSet4->GetDataDescription(),.1,50); 
+
+                    $file= '/tmp/graph_'.$this->graphID_4;
+                    $Test4->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.
+                    $DataSet5 = new pData;  
+
+                    $max = max($graphData['max_load']);
+
+                    $DataSet5->AddPoint(array_values($graphData['max_load']), 'max_load');
+                    $DataSet5->AddPoint(array_values($graphData['avg_load']), 'avg_load');
+                    $DataSet5->AddPoint(array_values($graphData['min_load']), 'min_load');
+
+                    $DataSet5->SetSerieName('Min Load', 'min_load');
+                    $DataSet5->SetSerieName('Max Load', 'max_load');
+                    $DataSet5->SetSerieName('Average Load', 'avg_load');
+
+                    $DataSet5->AddAllSeries();  
+                    $DataSet5->AddPoint($dateSeries, 'date');
+                    $DataSet5->SetAbsciseLabelSerie('date');
+
+                    $Test5 = new pChart(800,230);  
+                    $Test5->setFixedScale(0.0001,($max*1.1));  
+                    $Test5->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
+                    $Test5->setGraphArea(50,30,585,200);  
+                    $Test5->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);  
+                    $Test5->drawRoundedRectangle(5,5,695,225,5,230,230,230);  
+                    $Test5->drawGraphArea(255,255,255,TRUE);  
+                    $Test5->drawGrid(4,TRUE,200,200,200,50);  
+                    $Test5->drawTreshold(0,143,55,72,TRUE,TRUE);  
+                    $Test5->drawTitle(50,22,"CPU load",50,50,50,585);  
+
+                    $Test5->drawScale($DataSet5->GetData(),$DataSet5->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, FALSE);
+                    $Test5->drawFilledCubicCurve($DataSet5->GetData(),$DataSet5->GetDataDescription(),.1,50); 
+
+                    $file = '/tmp/graph_'.$this->graphID_5;
+                    $Test5->Render($file);
+                    session::set('statistics::graphFile'.$this->graphID_5,$file);
+
                 }
             }
         }
 
         $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('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, ', '));
+    }
 }
 ?>