Code

Prepared statistics to handle over-time data
[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") != ""){
63                 $res = $this->rpcHandle->getInstanceStats();
64                 if(!$this->rpcHandle->success()){
65                     msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
66                 }
68                 $all = array_sum($res['category_count']);
69                 $data = array();
70                 $cnt = 5;
71                 foreach($res['category_count'] as $category => $count){
72                     $tmp[$category] = $count;
73                 }
75                 asort($tmp);
76                 $tmp = array_slice($tmp,0,7);
78                 // Include pChart 
79                 new pChartInclude();
81                 // Dataset definition   
82                 $DataSet = new pData;  
83                 $this->graphID_1 = preg_replace("/[^0-9]/","",microtime(TRUE));
84                 $DataSet->AddPoint(array_values($tmp),"Serie1");  
85                 $DataSet->AddPoint(array_keys($tmp),"Serie2");  
86                 $DataSet->AddAllSeries();  
87                 $DataSet->SetAbsciseLabelSerie("Serie2");  
89                 // Initialise the graph  
90                 $Test = new pChart(400,200);  
91                 # $Test->drawFilledRoundedRectangle(7,7,373,193,5,255,200,200);  
92                 # $Test->drawRoundedRectangle(5,5,375,195,5,200,255,200);  
94                 // Draw the pie chart  
95                 $Test->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",8);  
96                 $Test->drawPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),150,90,110,PIE_PERCENTAGE,TRUE,50,20,5);  
97                 $Test->drawPieLegend(310,15,$DataSet->GetData(),$DataSet->GetDataDescription(),200,255,200);  
99                 $file = '/tmp/graph_'.$this->graphID_1;
100                 $Test->Render($file);
101                 session::set('statistics::graphFile'.$this->graphID_1,$file);
105                 ## Second graph, plugin usage over time
106                 $this->graphID_2 = preg_replace("/[^0-9]/","",microtime(TRUE));
108                 $tmp = array();
109                 $DataSet2 = new pData; 
110                 $cnt = 2;
111                 foreach($res['category_count'] as $category => $count){
112                     for($i = 0; $i < 10; $i++){
113                         $tmp[$category][time() - ($i*60*60*24)] = rand(0,16);
114                     }
115                     $DataSet2->AddPoint($tmp[$category], $category);
116                     $DataSet2->SetSerieName(_($category), $category);
117                     $cnt --;
118                     if(!$cnt) break;
119                 }
120                 $DataSet2->AddAllSeries();  
122                 // Build up X-Axis
123                 $steps = 10;
124                 $it = 24*60*60;
125                 $start = time() - ($steps * $it);
126                 $stop = time();
127                 $DataDescription = array();
128                 for($i = $start; $i < $stop; $i += $it){
129                     $seriesX[] =  date('d .m', $i);
130 #                    $seriesX[] =  $i;
131                 }
132 #                $DataSet2->SetXAxisFormat("date");
133                 $DataSet2->AddPoint($seriesX, 'date');
134                 $DataSet2->SetAbsciseLabelSerie('date');  
135                 $DataSet2->RemoveSerie('date');  
137                 #  // Initialise the graph  
138                 $Test2 = new pChart(700,230);  
139                 $Test2->setFixedScale(0,20);  
140                 $Test2->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",8);  
141                 $Test2->setGraphArea(50,30,585,200);  
142                 $Test2->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);  
143                 $Test2->drawRoundedRectangle(5,5,695,225,5,230,230,230);  
144                 $Test2->drawGraphArea(255,255,255,TRUE);  
145                 $Test2->drawScale($DataSet2->GetData(),$DataSet2->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2,TRUE);     
146                 $Test2->drawGrid(4,TRUE,230,230,230,50);  
148                 // Draw the 0 line  
149                 $Test2->drawTreshold(0,143,55,72,TRUE,TRUE);  
151                 // Draw the cubic curve graph  
152                 $Test2->drawBarGraph($DataSet2->GetData(),$DataSet2->GetDataDescription(),TRUE);  
153                 $Test2->drawCubicCurve($DataSet2->GetData(),$DataSet2->GetDataDescription());  
155                 // Finish the graph  
156                 $DataSet2->RemoveSerie('date');  
157                 $Test2->drawLegend(600,30,$DataSet2->GetDataDescription(),255,255,255);  
158                 $Test2->drawTitle(50,22,"Plugin usage over time",50,50,50,585);  
160                 $file = '/tmp/graph_'.$this->graphID_2;
161                 $Test2->Render($file);
162                 session::set('statistics::graphFile'.$this->graphID_2,$file);
163             }
164         }
166         $smarty->assign('graphID_1', $this->graphID_1);
167         $smarty->assign('graphID_2', $this->graphID_2);
168         return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
169     }
171     function check()
172     {
173         $messages = plugin::check();
174         return($messages);
175     }
177     function save_object()
178     {
179         plugin::save_object();
180     }
182 ?>