Code

9badf61ecd343d4e10d260c14944534133b425a2
[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                     // Build up DataSet. 
133                     $DataSet2 = new pData;  
134                     $intervalEntries = array();
135                     $max = 500;
136                     foreach($res['actionsPerInterval'] as $category => $dates){
137                         if(!isset($mostUsedCategories[$category])) continue;
138                         ksort($res['actionsPerInterval'][$category]);
139                         $DataSet2->AddPoint($res['actionsPerInterval'][$category], $category);
140                         $DataSet2->SetSerieName(_($category), $category);
141                     }
142                     $DataSet2->AddAllSeries();  
144                     $Xam = 5;
145                     $cnt =0;
146                     foreach($res['actionsPerIntervalDates'] as $key => $date){
147                         $date = strtotime($date);
148                         if((count($dates) <= $Xam) || 
149                             ($cnt % (floor(count($dates) / $Xam )) == 0)){
150                             $tmp[$date] = date('d.m.Y',$date);
151                         }else{
152                             $tmp[$date] = ' ';
153                         }
154                         $cnt ++;    
155                     }
156                
157  
158                     $DataSet2->AddPoint($tmp, 'date');
159                     $DataSet2->SetAbsciseLabelSerie('date');  
160                     $DataSet2->RemoveSerie('date');  
162                     $Test2 = new pChart(700,230);  
163                     $Test2->setFixedScale(0,$max);  
164                     $Test2->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
165                     $Test2->setGraphArea(50,30,585,200);  
166                     $Test2->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);  
167                     $Test2->drawRoundedRectangle(5,5,695,225,5,230,230,230);  
168                     $Test2->drawGraphArea(255,255,255,TRUE);  
169                     $Test2->drawScale($DataSet2->GetData(),$DataSet2->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2);     
170                     $Test2->drawGrid(4,TRUE,230,230,230,50);  
172                     // Draw the 0 line  
173                     $Test2->drawTreshold(0,143,55,72,TRUE,TRUE);  
175                     // Draw the cubic curve graph  
176                     #$Test2->drawBarGraph($DataSet2->GetData(),$DataSet2->GetDataDescription(),TRUE);  
177                     #$Test2->drawLineGraph($DataSet2->GetData(),$DataSet2->GetDataDescription(),TRUE); 
178                     $Test2->drawFilledLineGraph($DataSet2->GetData(),$DataSet2->GetDataDescription(),50,TRUE);  
180                     // Finish the graph  
181                     $DataSet2->RemoveSerie('date');  
182                     $Test2->drawLegend(600,30,$DataSet2->GetDataDescription(),255,255,255);  
183                     $Test2->drawTitle(50,22,"Plugin usage over time",50,50,50,585);  
185                     $file = '/tmp/graph_'.$this->graphID_2;
186                     $Test2->Render($file);
187                     session::set('statistics::graphFile'.$this->graphID_2,$file);
188                 }
189             }
190         }
192         $smarty->assign('graphID_1', $this->graphID_1);
193         $smarty->assign('graphID_2', $this->graphID_2);
194         return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
195     }
197     function check()
198     {
199         $messages = plugin::check();
200         return($messages);
201     }
203     function save_object()
204     {
205         plugin::save_object();
206     }
208 ?>