Code

7bd54700896561720b2c7f0a58aa6ff72e07d5c5
[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                 $start = strtotime($this->graph1DatePicker1);
84                 $stop  = strtotime($this->graph1DatePicker2);
86                 // Request statistics now
87                 $res = $this->rpcHandle->getInstanceStats($start,$stop);
88                 if(!$this->rpcHandle->success()){
89                     msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
90                 }
92                 if($res){
95                     // Include pChart 
96                     new pChartInclude();
98                     // Get most used categories, but only eight at once.
99                     if(count($res['actionsPerCategory']) && 0){
101                         // --------
102                         // Generate PIE chart of most used categories
103                         // --------
105                         arsort($res['actionsPerCategory']);
106                         $mostUsedCategories = array_slice($res['actionsPerCategory'],0,8);
108                         // Dataset definition   
109                         $DataSet = new pData;  
110                         $this->graphID_1 = preg_replace("/[^0-9]/","",microtime(TRUE));
112                         $DataSet->AddPoint(array_values($mostUsedCategories),"Serie1");  
113                         $DataSet->AddPoint(array_keys($mostUsedCategories),"Serie2");  
114                         $DataSet->AddAllSeries();  
115                         $DataSet->SetAbsciseLabelSerie("Serie2");  
117                         // Initialise the graph  
118                         $Test = new pChart(400,200);  
119                         $Test->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
122                         $Test->drawPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),150,90,110,PIE_PERCENTAGE,TRUE,50,20,5);  
123                         $Test->drawPieLegend(310,15,$DataSet->GetData(),$DataSet->GetDataDescription(),200,255,200);  
125                         $file = '/tmp/graph_'.$this->graphID_1;
126                         $Test->Render($file);
127                         session::set('statistics::graphFile'.$this->graphID_1,$file);
128                     }
130                     // --------
131                     // Generate combined line and car chart of plugin usage, ldap execution time and errors 
132                     // --------
134                     // Generate new and unique graph id
135                     $this->graphID_2 = preg_replace("/[^0-9]/","",microtime(TRUE));
137                     // Prepare transmitted data, sort it by date and collect 
138                     //  transmitted timestamps to be able to print the x-Axis labels.
139                     $dataArray = array();
140                     $dates = array();
141                     $DataSet2 = new pData;  
142                     $max = 1;
143                     foreach($res['actionsPerInterval'] as $category => $entriesPerDate){
145                         // Collect data per category and store used timestamps
146                         foreach($entriesPerDate as $dateStr => $count){
147                             $date = strtotime($dateStr);
148                             $dates[$date]=$date;
150                             // Do not append empty data                            
151                             if(empty($category)) continue;
152                             $dataArray[$category][$date] = log($count + 1);
153                         }
154                        
155                         // Do not append empty data 
156                         if(empty($category)) continue;
158                         // Sort results. 
159                         ksort($dataArray[$category]);
161                         // Add results to our data set.
162                         $DataSet2->AddPoint($dataArray[$category], $category);
163                         $DataSet2->SetSerieName(_($category), $category);
165                         // Detect maximum value, to adjust the Y-Axis
166                         $tmpMax = max($dataArray[$category]);
167                         if($tmpMax > $max) $max = $tmpMax;
168                     }
169                     ksort($dates);
171                     $DataSet2->AddAllSeries();  
173                     // Prepare date strings for X-Axis, only print a given number of 
174                     //  of labels to keep the axis readable.
175                     $Xam = 5; // Number of labels
176                     $cnt = 0;
177                     $tmp = array();
178                     foreach($dates as $stamp){
179                         if((count($dates) <= $Xam) || ($cnt % (floor(count($dates) / $Xam )) == 0)){
180                             $tmp[$stamp] = date('d.m.Y',$stamp);
181                         }else{
182                             $tmp[$stamp] = ' ';
183                         }
184                         $cnt ++;    
185                     }
187                     $DataSet2->AddPoint($tmp, 'date');
188                     $DataSet2->SetAbsciseLabelSerie('date');  
189                     $DataSet2->RemoveSerie('date');  
191                     $Test2 = new pChart(700,230);  
192                     $Test2->setFixedScale(0,$max);  
193                     $Test2->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
194                     $Test2->setGraphArea(50,30,585,200);  
195                     $Test2->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);  
196                     $Test2->drawRoundedRectangle(5,5,695,225,5,230,230,230);  
197                     $Test2->drawGraphArea(255,255,255,TRUE);  
198                     $Test2->drawGrid(4,TRUE,230,230,230,50);  
199                     $Test2->drawTreshold(0,143,55,72,TRUE,TRUE);  
200                     $Test2->drawTitle(50,22,"Plugin usage over time",50,50,50,585);  
202                     if(count($dates)){
203                         $Test2->drawScale($DataSet2->GetData(),$DataSet2->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2);     
204                     }
206                     // Draw the cubic curve graph  
207                     if(count($dataArray)){
208                         $Test2->drawFilledCubicCurve($DataSet2->GetData(),$DataSet2->GetDataDescription(),.1,20);  
209                         $Test2->drawLegend(600,30,$DataSet2->GetDataDescription(),255,255,255);  
210                     }
212                     $file = '/tmp/graph_'.$this->graphID_2;
213                     $Test2->Render($file);
214                     session::set('statistics::graphFile'.$this->graphID_2,$file);
215                 }
216             }
217         }
219         $smarty->assign('graphID_1', $this->graphID_1);
220         $smarty->assign('graphID_2', $this->graphID_2);
221         return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
222     }
224     function check()
225     {
226         $messages = plugin::check();
227         return($messages);
228     }
230     function save_object()
231     {
232         plugin::save_object();
233     }
235 ?>