Code

2fcae6a14c8cf88a7be562150027a9f485bab9fb
[gosa.git] / gosa-core / plugins / generic / statistics / graphClasses / class_categoryActionOverTime.inc
1 <?php
3 class categoryActionsOverTime extends statChart
4 {
5     var $title = 'Plugin usage over time';
6     var $gID = 0;
8     var $graphData = array();    
9     var $seriesList = array();
11     var $enableCaching = FALSE;
13     function __construct(&$config)
14     {
15         parent::__construct($config);
16         $this->gID = get_class();
17     }
20     /*! \brief  Generates the line-graph which displays the plugin usage over time.
21      */ 
22     function render()
23     {
24         $lineMax = 100;
25         $gData = $this->graphData;
26         $errorMax = (max($gData['errorsPerInterval']) < 100)? 100:max($gData['errorsPerInterval']);
27         $dataSet = new pData;  
28         $seriesCnt = 0;
29         foreach($gData['actionsPerInterval'] as $category => $entriesPerDate){
30             if(empty($category) || in_array($category, $this->skipSeries)) continue;
32             // Add results to our data set.
33             $dataSet->AddPoint($entriesPerDate, $category);
34             $dataSet->SetSerieName($this->getCategoryTranslation($category), $category);
35             $dataSet->AddSerie($category);
37             // Detect maximum value, to adjust the Y-Axis
38             $tmpMax = max($entriesPerDate);
39             if($tmpMax > $lineMax) $lineMax = $tmpMax;
40             $seriesCnt ++;
41         }
43         // Create a dataSet containing all series 
44         $allSeriesDataSet = clone $dataSet;
45         if(!in_array('errorsPerInterval', $this->skipSeries)){
46             $allSeriesDataSet->AddPoint($gData['errorsPerInterval'], 'Errors');
47             $allSeriesDataSet->SetSerieName(_('Error'), 'Errors');
48             $allSeriesDataSet->AddSerie('Errors');
49         }
51         // Add timeline
52         $dataSet->AddPoint($gData['dates'], 'date');
53         $dataSet->SetAbsciseLabelSerie('date');  
55         // Read graph from cache?
56         $pCache = new pCache('/var/spool/gosa/');
57         if($this->enableCaching && $pCache->IsInCache(get_class(),$allSeriesDataSet->GetData())){
58             $filename =  $pCache->GetHash(get_class(),$allSeriesDataSet->GetData());
59             $filename = '/var/spool/gosa/'.$filename;    
60             if(file_exists($filename) && is_readable($filename)){
61                 $this->graphID = preg_replace("/[^0-9]/","",microtime(TRUE)).rand(0,99999);
62                 session::set('statistics::graphFile'.$this->graphID,$filename);
63                 return;
64             }
65         }
67         $chart = new pChart(900,230);  
68         $chart->setFixedScale(0.000,$lineMax);
69         $chart->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
70         $chart->setGraphArea(50,8,830,200);  
71         $chart->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);  
72         $chart->drawRoundedRectangle(5,5,695,225,5,230,230,230);  
73         $chart->drawGraphArea(255,255,255,TRUE);  
74         $chart->drawGrid(4,TRUE,200,200,200,50);  
75         $chart->drawTreshold(0,143,55,72,TRUE,TRUE);  
76         $chart->drawTitle(50,22,_($this->title),50,50,50,585);  
77         $chart->drawScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2, TRUE);     
79         // Only draw this graph if we've at least one series to draw! 
80         if($seriesCnt){
81             $chart->drawFilledLineGraph($dataSet->GetData(),$dataSet->GetDataDescription(),50,TRUE);
82         }
84         // Do we've to add the errors series?
85         // If we have to, then add the error-data-series.
86         //  and set the color for the new error-series to red.
87         if(!in_array('errorsPerInterval', $this->skipSeries)){
89             // Set the color for the error Series to 'red'. 
90             // This has to be done before drawing the legend.
91             $chart->setColorPalette($seriesCnt,255,0,0);   
93             $dataSet->AddPoint($gData['errorsPerInterval'], 'Errors');
94             $dataSet->SetSerieName(_('Error'), 'Errors');
95             $dataSet->AddSerie('Errors');
96         }
98         $chart->drawLegend(750,30,$dataSet->GetDataDescription(),255,255,255);
100         // Draw the error graph on top of the other graphs now.
101         // But remove the category-graph before. 
102         if(!in_array('errorsPerInterval', $this->skipSeries)){
104             // Remove all graph series and add the error-series, then draw the new graph.
105             // (It is not relevant if it was really added before, so we simply remove all series!)
106             foreach($gData['actionsPerInterval'] as $category => $data){
107                 $dataSet->RemoveSerie($category);
108             }
109             $chart->setFixedScale(0,$errorMax);
110             $chart->drawRightScale($dataSet->GetData(),$dataSet->GetDataDescription(),SCALE_NORMAL,120,150,150,TRUE,0,2, TRUE);
111             $chart->drawBarGraph($dataSet->GetData(),$dataSet->GetDataDescription());
112         }
114         // Generate new and unique graph id
115         $this->graphID = preg_replace("/[^0-9]/","",microtime(TRUE));
116         $file = '/tmp/graph_'.$this->graphID;
117         $chart->Render($file);
118         session::set('statistics::graphFile'.$this->graphID,$file);
119         $pCache->WriteToCache(get_class(),$allSeriesDataSet->GetData(),$chart);   
121         // Keep a list of all selecteable data-series, to allow the user to disable
122         //  or enable series on demand.
123         $this->seriesList = array();
124         foreach($gData['actionsPerInterval'] as $key => $data){
125             $this->seriesList[$key] = $this->getCategoryTranslation($key); 
126         }
127         $this->seriesList['errorsPerInterval'] = _("Error");
129         return;
130     }
133 ?>