Code

Updated stats plugin
[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;
15     function __construct($config)
16     {
17         plugin::plugin($config, NULL);
19         // First try to retrieve values via RPC
20         $this->rpcConfigured = FALSE;
21         if ($this->config->get_cfg_value("core","gosaRpcServer") != ""){
22             $this->rpcConfigured = TRUE;
23             $this->rpcHandle = $this->config->getRpcHandle(
24                     "http://10.3.64.59:4000",
25                     "65717fe6-9e3e-11df-b010-5452005f1250",
26                     "WyukwauWoid2",
27                     TRUE);
28         }
29     }
31     function execute()
32     {
33         $smarty = get_smarty();
35         // Do not render anything if we are not prepared to send and receive data via rpc.
36         $smarty->assign("rpcConfigured", $this->rpcConfigured);
37         $smarty->assign("validRpcHandle", TRUE);
38         if(!$this->rpcConfigured || !$this->rpcHandle){
39             $smarty->assign("validRpcHandle", FALSE);
40             return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
41         }
43         // Send stats 
44         if(isset($_POST['transmitStatistics'])){
45             $tmp = stats::dumpTables();
46             $dump = array();
47             foreach($tmp as $entry){
48                 $dump[] = array_values($entry);
49             }
50             $res = $this->rpcHandle->updateInstanceStatus($dump);
51             if(!$this->rpcHandle->success()){
52                 msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
53             }else{
54                 echo $res." Entries inserted";
55             }
56         }
58         // Transmit daily statistics to GOsa-Server
59         if(isset($_POST['receiveStatistics'])){
61             // First try to retrieve values via RPC
62             if ($this->config->get_cfg_value("core","gosaRpcServer") != ""){
65                 $days = 40;
66                 $interval = 1;
67                 $start = time() - ($days*86400);
68                 $stop  = time();
70                 $res = $this->rpcHandle->getInstanceStats($start,$stop,'day',$interval);
71                 if(!$this->rpcHandle->success()){
72                     msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
73                 }
75                 if($res){
77                     // Include pChart 
78                     new pChartInclude();
80                     // --------
81                     // Generate PIE chart of most used categories
82                     // --------
84                     // Get most used categories, but only eight at once.
85                     arsort($res['actionsPerCategory']);
86                     $mostUsedCategories = array_slice($res['actionsPerCategory'],0,8);
88                     // Dataset definition   
89                     $DataSet = new pData;  
90                     $this->graphID_1 = preg_replace("/[^0-9]/","",microtime(TRUE));
91                     $DataSet->AddPoint(array_values($mostUsedCategories),"Serie1");  
92                     $DataSet->AddPoint(array_keys($mostUsedCategories),"Serie2");  
93                     $DataSet->AddAllSeries();  
94                     $DataSet->SetAbsciseLabelSerie("Serie2");  
96                     // Initialise the graph  
97                     $Test = new pChart(400,200);  
98                     $Test->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
99                     $Test->drawPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),150,90,110,PIE_PERCENTAGE,TRUE,50,20,5);  
100                     $Test->drawPieLegend(310,15,$DataSet->GetData(),$DataSet->GetDataDescription(),200,255,200);  
102                     $file = '/tmp/graph_'.$this->graphID_1;
103                     $Test->Render($file);
104                     session::set('statistics::graphFile'.$this->graphID_1,$file);
107                     // --------
108                     // Generate combined line and car chart of plugin usage, ldap execution time and errors 
109                     // --------
111                     // Generate new and unique graph id
112                     $this->graphID_2 = preg_replace("/[^0-9]/","",microtime(TRUE));
114                     // Build up DataSet. 
115                     $tmp = array();
116                     $DataSet2 = new pData;  
117                     $dates = array();
118                     $max = 0;
120                     foreach($res['actionsPerInterval'] as $id => $data){
122                         $category = $data['category'];
123                         $date =  $data['date'];
125                         if(!isset($mostUsedCategories[$category])) continue;
127                         if($data['count'] > $max) $max = $data['count'];
128                         $tmp[$category][$date] = $data['count'];
129                         $dates[$date] = $date;
130                     }
131                     $dates = array_values($dates);
132                     sort($dates);
134                     foreach($tmp as $category => $data){
135                         ksort($tmp[$category]);
136                         $DataSet2->AddPoint($tmp[$category], $category);
137                         $DataSet2->SetSerieName(_($category), $category);
138                     }
139                     $DataSet2->AddAllSeries();  
141                     $tmp = array();
142                     foreach($dates as $date) $tmp[] = date('d.m', $date);
144                     $DataSet2->AddPoint($tmp, 'date');
145                     $DataSet2->SetAbsciseLabelSerie('date');  
146                     $DataSet2->RemoveSerie('date');  
148                     $Test2 = new pChart(700,230);  
149                     $Test2->setFixedScale(0,$max);  
150                     $Test2->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);  
151                     $Test2->setGraphArea(50,30,585,200);  
152                     $Test2->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);  
153                     $Test2->drawRoundedRectangle(5,5,695,225,5,230,230,230);  
154                     $Test2->drawGraphArea(255,255,255,TRUE);  
155                     $Test2->drawScale($DataSet2->GetData(),$DataSet2->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2);     
156                     $Test2->drawGrid(4,TRUE,230,230,230,50);  
158                     // Draw the 0 line  
159                     $Test2->drawTreshold(0,143,55,72,TRUE,TRUE);  
161                     // Draw the cubic curve graph  
162                     #$Test2->drawBarGraph($DataSet2->GetData(),$DataSet2->GetDataDescription(),TRUE);  
163                     #$Test2->drawLineGraph($DataSet2->GetData(),$DataSet2->GetDataDescription(),TRUE); 
164                     $Test2->drawFilledLineGraph($DataSet2->GetData(),$DataSet2->GetDataDescription(),50,TRUE);  
166                     // Finish the graph  
167                     $DataSet2->RemoveSerie('date');  
168                     $Test2->drawLegend(600,30,$DataSet2->GetDataDescription(),255,255,255);  
169                     $Test2->drawTitle(50,22,"Plugin usage over time",50,50,50,585);  
171                     $file = '/tmp/graph_'.$this->graphID_2;
172                     $Test2->Render($file);
173                     session::set('statistics::graphFile'.$this->graphID_2,$file);
174                 }
175             }
176         }
178         $smarty->assign('graphID_1', $this->graphID_1);
179         $smarty->assign('graphID_2', $this->graphID_2);
180         return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
181     }
183     function check()
184     {
185         $messages = plugin::check();
186         return($messages);
187     }
189     function save_object()
190     {
191         plugin::save_object();
192     }
194 ?>