Code

098292175d67ada5c06103ab3a00ed5f1cb7faab
[gosa.git] / gosa-core / plugins / generic / statistics / class_statistics.inc
1 <?php
3 class statistics extends plugin
4 {
5     var $plHeadline = 'Statistics';
6     var $plDescription = 'GOsa usage statistics';
7     var $plShortIcon = 'statistics.png';
8     var $plIcon = 'plugin.png';
9     
10     var $rpcHandle = NULL;
11     var $rpcConfigured = FALSE;
13     var $graphID_1 = 0;
14     var $graphID_2 = 0;
16     var $graph1Interval = 1;
17     var $graph1DatePicker1 = 0;
18     var $graph1DatePicker2 = 0;
20     function __construct($config)
21     {
22         plugin::plugin($config, NULL);
24         // Init start and stop times for graph 1
25         $this->graph1DatePicker1 = date('d.m.Y', time() - 7 * 24 * 60 *60);
26         $this->graph1DatePicker2 = date('d.m.Y', time());
28         // First try to retrieve values via RPC
29         $this->rpcConfigured = FALSE;
30         if ($this->config->get_cfg_value("core","gosaRpcServer") != ""){
31             $this->rpcConfigured = TRUE;
32             $this->rpcHandle = $this->config->getRpcHandle(
33                     "http://10.3.64.59:4000",
34                     "65717fe6-9e3e-11df-b010-5452005f1250",
35                     "WyukwauWoid2",
36                     TRUE);
37         }
38     }
40     function execute()
41     {
42         if(isset($_POST['graph1DatePicker1'])) $this->graph1DatePicker1 = get_post('graph1DatePicker1');
43         if(isset($_POST['graph1DatePicker2'])) $this->graph1DatePicker2 = get_post('graph1DatePicker2');
44         if(isset($_POST['graph1Interval'])) $this->graph1Interval = get_post('graph1Interval');
46         if(!$this->graph1Interval) $this->graph1Interval = 1;
48         $smarty = get_smarty();
49         $smarty->assign('graph1DatePicker1', $this->graph1DatePicker1);
50         $smarty->assign('graph1DatePicker2', $this->graph1DatePicker2);
51         $smarty->assign('graph1Interval', $this->graph1Interval);
52         $smarty->assign('intervalValues', array('1','2','5','7','30'));
54         // Do not render anything if we are not prepared to send and receive data via rpc.
55         $smarty->assign("rpcConfigured", $this->rpcConfigured);
56         $smarty->assign("validRpcHandle", TRUE);
57         if(!$this->rpcConfigured || !$this->rpcHandle){
58             $smarty->assign("validRpcHandle", FALSE);
59             return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
60         }
62         // Send stats 
63         if(isset($_POST['transmitStatistics'])){
64             $tmp = stats::dumpTables();
65             $dump = array();
66             foreach($tmp as $entry){
67                 $dump[] = array_values($entry);
68             }
69             $res = $this->rpcHandle->updateInstanceStatus($dump);
70             if(!$this->rpcHandle->success()){
71                 msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
72             }else{
73                 echo $res." Entries inserted";
74             }
75         }
77         // Transmit daily statistics to GOsa-Server
78         if(isset($_POST['receiveStatistics'])){
80             // First try to retrieve values via RPC
81             if ($this->config->get_cfg_value("core","gosaRpcServer") != ""){
83                 $interval = $this->graph1Interval;
84                 $start = strtotime($this->graph1DatePicker1);
85                 $stop  = strtotime($this->graph1DatePicker2);
87                 // Request statistics now
88                 $res = $this->rpcHandle->getInstanceStats($start,$stop,$interval);
89                 if(!$this->rpcHandle->success()){
90                     msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
91                 }
93                 if($res){
95                     // Include pChart 
96                     new pChartInclude();
98                     // --------
99                     // Generate PIE chart of most used categories
100                     // --------
102                     // Get most used categories, but only eight at once.
103                     arsort($res['actionsPerCategory']);
104                     $mostUsedCategories = array_slice($res['actionsPerCategory'],0,8);
106                     // Dataset definition   
107                     $DataSet = new pData;  
108                     $this->graphID_1 = preg_replace("/[^0-9]/","",microtime(TRUE));
109                     $DataSet->AddPoint(array_values($mostUsedCategories),"Serie1");  
110                     $DataSet->AddPoint(array_keys($mostUsedCategories),"Serie2");  
111                     $DataSet->AddAllSeries();  
112                     $DataSet->SetAbsciseLabelSerie("Serie2");  
114                     // Initialise the graph  
115                     $Test = new pChart(400,200);  
116                     $Test->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
117                     $Test->drawPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),150,90,110,PIE_PERCENTAGE,TRUE,50,20,5);  
118                     $Test->drawPieLegend(310,15,$DataSet->GetData(),$DataSet->GetDataDescription(),200,255,200);  
120                     $file = '/tmp/graph_'.$this->graphID_1;
121                     $Test->Render($file);
122                     session::set('statistics::graphFile'.$this->graphID_1,$file);
125                     // --------
126                     // Generate combined line and car chart of plugin usage, ldap execution time and errors 
127                     // --------
129                     // Generate new and unique graph id
130                     $this->graphID_2 = preg_replace("/[^0-9]/","",microtime(TRUE));
132                     // Prepare transmitted data, sort it by date and collect 
133                     //  transmitted timestamps to be able to print the x-Axis labels.
134                     $dataArray = array();
135                     $dates = array();
136                     $DataSet2 = new pData;  
137                     $max = 0;
138                     foreach($res['actionsPerInterval'] as $category => $entriesPerDate){
140                         // Only print the 8 most used categories, else we receive a lot of errors
141                         //  from the pChart classes, seems that it can only handle 8 data series at once.
142                         if(!isset($mostUsedCategories[$category])) continue;
144                         // Collect data per category and store used timestamps
145                         foreach($entriesPerDate as $dateStr => $count){
146                             $date = strtotime($dateStr);
147                             $dataArray[$category][$date] = log($count + 1);
148                             $dates[$date]=$date;
149                         }
150                 
151                         // Sort results. 
152                         ksort($dataArray[$category]);
153                         ksort($dates);
155                         // Add results to our data set.
156                         $DataSet2->AddPoint($dataArray[$category], $category);
157                         $DataSet2->SetSerieName(_($category), $category);
159                         // Detect maximum value, to adjust the Y-Axis
160                         $tmpMax = max($dataArray[$category]);
161                         if($tmpMax > $max) $max = $tmpMax;
162                     }
163                     $DataSet2->AddAllSeries();  
165                     // Prepare date strings for X-Axis, only print a given number of 
166                     //  of labels to keep the axis readable.
167                     $Xam = 5; // Number of labels
168                     $cnt = 0;
169                     $tmp = array();
170                     foreach($dates as $stamp){
171                         if((count($dates) <= $Xam) || ($cnt % (floor(count($dates) / $Xam )) == 0)){
172                             $tmp[$stamp] = date('d.m.Y',$stamp);
173                         }else{
174                             $tmp[$stamp] = ' ';
175                         }
176                         $cnt ++;    
177                     }
178               
179                     $DataSet2->AddPoint($tmp, 'date');
180                     $DataSet2->SetAbsciseLabelSerie('date');  
181                     $DataSet2->RemoveSerie('date');  
183                     $Test2 = new pChart(700,230);  
184                     $Test2->setFixedScale(0,$max);  
185                     $Test2->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
186                     $Test2->setGraphArea(50,30,585,200);  
187                     $Test2->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);  
188                     $Test2->drawRoundedRectangle(5,5,695,225,5,230,230,230);  
189                     $Test2->drawGraphArea(255,255,255,TRUE);  
190                     $Test2->drawScale($DataSet2->GetData(),$DataSet2->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2);     
191                     $Test2->drawGrid(4,TRUE,230,230,230,50);  
193                     // Draw the 0 line  
194                     $Test2->drawTreshold(0,143,55,72,TRUE,TRUE);  
196                     // Draw the cubic curve graph  
197                     #$Test2->drawBarGraph($DataSet2->GetData(),$DataSet2->GetDataDescription(),TRUE);  
198                     #$Test2->drawLineGraph($DataSet2->GetData(),$DataSet2->GetDataDescription(),TRUE); 
199                     #$Test2->drawFilledLineGraph($DataSet2->GetData(),$DataSet2->GetDataDescription(),50,TRUE);  
200                     $Test2->drawFilledCubicCurve($DataSet2->GetData(),$DataSet2->GetDataDescription(),.1,20);  
202                     // Finish the graph  
203                     $DataSet2->RemoveSerie('date');  
204                     $Test2->drawLegend(600,30,$DataSet2->GetDataDescription(),255,255,255);  
205                     $Test2->drawTitle(50,22,"Plugin usage over time",50,50,50,585);  
207                     $file = '/tmp/graph_'.$this->graphID_2;
208                     $Test2->Render($file);
209                     session::set('statistics::graphFile'.$this->graphID_2,$file);
210                 }
211             }
212         }
214         $smarty->assign('graphID_1', $this->graphID_1);
215         $smarty->assign('graphID_2', $this->graphID_2);
216         return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
217     }
219     function check()
220     {
221         $messages = plugin::check();
222         return($messages);
223     }
225     function save_object()
226     {
227         plugin::save_object();
228     }
230 ?>