433e33833440fb7cfb4c2de8ab73a272294c605e
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';
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 var $unsbmittedFiles = array();
22 function __construct($config)
23 {
24 plugin::plugin($config, NULL);
26 // Init start and stop times for graph 1
27 $this->graph1DatePicker1 = date('d.m.Y', time() - 7 * 24 * 60 *60);
28 $this->graph1DatePicker2 = date('d.m.Y', time());
30 // First try to retrieve values via RPC
31 $this->rpcConfigured = FALSE;
32 if ($this->config->get_cfg_value("core","gosaRpcServer") != ""){
33 $this->rpcConfigured = TRUE;
34 $this->rpcHandle = $this->config->getRpcHandle(
35 "http://10.3.64.59:4000",
36 "65717fe6-9e3e-11df-b010-5452005f1250",
37 "WyukwauWoid2",
38 TRUE);
39 }
41 // Get list of unsubmitted files.
42 $this->unsbmittedFiles = $this->getUnsubmittedStatistics();
43 }
45 /*! \brief Returns a list local stored statistic files
46 @param Array A list of filenames and dates.
47 */
48 function getLocalStatisticsFiles()
49 {
51 $res = stats::getLocalStatFiles();
52 $tmp = array();
53 if(count($res)){
54 foreach($res as $file){
55 $date = strtotime($file);
56 if($date){
57 $tmp[$file] = $date;
58 }
59 }
60 }
61 return($tmp);
62 }
65 /*! \brief Returns a list of not transmitted stat files (except files for the current day)
66 * @return Array A list of unsubmitted statistic files.
67 */
68 function getUnsubmittedStatistics()
69 {
70 $available = $this->getLocalStatisticsFiles();
71 $alreadyTransmitted = $this->getStatisticsDatesFromServer();
72 $unsubmitted = array_intersect($available,$alreadyTransmitted);
74 // Exclude statistic collection from today, they are still active and cannot be submitted.
75 $curDate = date('Y-m-d');
76 if(isset($unsubmitted)) unset($unsubmitted[$curDate]);
78 return($unsubmitted);
79 }
82 /*! \brief Request a list of dates for which the server can return statistics.
83 @param Array A list of dates $ret=[iso-str] = timestamp
84 */
85 function getStatisticsDatesFromServer()
86 {
87 $res = $this->rpcHandle->getInstanceStatDates();
88 $dates = array();
89 if(!$this->rpcHandle->success()){
90 msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
91 }else{
92 foreach($res as $date){
93 $dates[$date] = strtotime($date);
94 }
95 }
96 return($dates);
97 }
100 function execute()
101 {
102 if(isset($_POST['graph1DatePicker1'])) $this->graph1DatePicker1 = get_post('graph1DatePicker1');
103 if(isset($_POST['graph1DatePicker2'])) $this->graph1DatePicker2 = get_post('graph1DatePicker2');
104 if(isset($_POST['graph1Interval'])) $this->graph1Interval = get_post('graph1Interval');
106 if(!$this->graph1Interval) $this->graph1Interval = 1;
108 $smarty = get_smarty();
109 $smarty->assign('graph1DatePicker1', $this->graph1DatePicker1);
110 $smarty->assign('graph1DatePicker2', $this->graph1DatePicker2);
111 $smarty->assign('graph1Interval', $this->graph1Interval);
112 $smarty->assign('intervalValues', array('1','2','5','7','30'));
114 // Do not render anything if we are not prepared to send and receive data via rpc.
115 $smarty->assign("rpcConfigured", $this->rpcConfigured);
116 $smarty->assign("validRpcHandle", TRUE);
117 if(!$this->rpcConfigured || !$this->rpcHandle){
118 $smarty->assign("validRpcHandle", FALSE);
119 return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
120 }
122 // Send stats
123 if(isset($_POST['transmitStatistics'])){
124 $this->unsbmittedFiles = $this->getUnsubmittedStatistics();
125 foreach($this->unsbmittedFiles as $filename => $date){
126 $tmp = stats::dumpTables($filename);
127 $dump = array();
128 foreach($tmp as $entry){
129 $dump[] = array_values($entry);
130 }
131 $res = $this->rpcHandle->updateInstanceStatus($dump);
132 if(!$this->rpcHandle->success()){
133 msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
134 }else{
135 stats::removeStatsFile($filename);
136 echo "Inserted ".$res." entries for date ".date('d.m.Y', $date)."<br>";
137 }
138 }
139 $this->unsbmittedFiles = $this->getUnsubmittedStatistics();
140 }
142 // Transmit daily statistics to GOsa-Server
143 if(isset($_POST['receiveStatistics'])){
145 // First try to retrieve values via RPC
146 if ($this->config->get_cfg_value("core","gosaRpcServer") != ""){
148 $start = strtotime($this->graph1DatePicker1);
149 $stop = strtotime($this->graph1DatePicker2);
151 // Request statistics now
152 $res = $this->rpcHandle->getInstanceStats($start,$stop);
153 if(!$this->rpcHandle->success()){
154 msg_dialog::display(_("Error"),msgPool::rpcError($this->rpcHandle->get_error()),ERROR_DIALOG);
155 }
157 if($res){
159 // Include pChart
160 new pChartInclude();
162 // Get most used categories, but only eight at once.
163 if(count($res['actionsPerCategory'])){
165 // --------
166 // Generate PIE chart of most used categories
167 // --------
169 arsort($res['actionsPerCategory']);
170 $mostUsedCategories = array_slice($res['actionsPerCategory'],0,8);
172 // Dataset definition
173 $DataSet = new pData;
174 $this->graphID_1 = preg_replace("/[^0-9]/","",microtime(TRUE));
176 $values = array_values($mostUsedCategories);
177 $keys = array_keys($mostUsedCategories);
179 $DataSet->AddPoint($values,"Serie1");
180 $DataSet->AddAllSeries();
181 $DataSet->AddPoint($keys,"Serie2");
182 $DataSet->SetAbsciseLabelSerie("Serie2");
184 // Initialise the graph
185 $Test = new pChart(400,200);
186 $Test->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);
189 $Test->drawPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),150,90,110,PIE_PERCENTAGE,TRUE,50,20,5);
190 $Test->drawPieLegend(310,15,$DataSet->GetData(),$DataSet->GetDataDescription(),200,255,200);
192 $file = '/tmp/graph_'.$this->graphID_1;
193 $Test->Render($file);
194 session::set('statistics::graphFile'.$this->graphID_1,$file);
195 }
197 // --------
198 // Generate combined line and car chart of plugin usage, ldap execution time and errors
199 // --------
201 // Generate new and unique graph id
202 $this->graphID_2 = preg_replace("/[^0-9]/","",microtime(TRUE));
204 // Prepare transmitted data, sort it by date and collect
205 // transmitted timestamps to be able to print the x-Axis labels.
206 $dataArray = array();
207 $dates = array();
208 $DataSet2 = new pData;
209 $max = 1;
210 $seriesNumber = 0;
211 foreach($res['actionsPerInterval'] as $category => $entriesPerDate){
213 // Collect data per category and store used timestamps
214 foreach($entriesPerDate as $dateStr => $count){
215 $date = strtotime($dateStr);
216 $dates[$date]=$date;
218 // Do not append empty data
219 if(empty($category)) continue;
220 if($count) $count = ($count);
221 $dataArray[$category][$date] = $count;
222 }
225 // Do not append empty data
226 if(empty($category)) continue;
228 // Sort results.
229 ksort($dataArray[$category]);
231 // Add results to our data set.
232 $DataSet2->AddPoint($dataArray[$category], $category);
233 $DataSet2->SetSerieName(_($category), $category);
234 $Test->setLabel($DataSet->GetData(),$DataSet->GetDataDescription(),"Serie1",0,"Daily incomes");
235 $DataSet2->AddSerie($category);
236 $seriesNumber++;
238 // Detect maximum value, to adjust the Y-Axis
239 $tmpMax = max($dataArray[$category]);
240 if($tmpMax > $max) $max = $tmpMax;
241 }
242 ksort($dates);
244 // Prepare date strings for X-Axis, only print a given number of
245 // of labels to keep the axis readable.
246 $Xam = 5; // Number of labels
247 $cnt = 0;
248 $tmp = array();
249 foreach($dates as $stamp){
250 if((count($dates) <= $Xam) || ($cnt % (floor(count($dates) / $Xam )) == 0)){
251 $tmp[$stamp] = date('d.m.Y',$stamp);
252 }else{
253 $tmp[$stamp] = ' ';
254 }
255 $cnt ++;
256 }
258 $DataSet2->AddPoint($tmp, 'date');
259 $DataSet2->SetAbsciseLabelSerie('date');
261 $Test2 = new pChart(800,230);
262 $Test2->setFixedScale(0.0001,($max*1.1));
263 $Test2->setFontProperties("./themes/default/fonts/LiberationSans-Regular.ttf",10);
264 $Test2->setGraphArea(50,30,585,200);
265 $Test2->drawFilledRoundedRectangle(7,7,693,223,5,240,240,240);
266 $Test2->drawRoundedRectangle(5,5,695,225,5,230,230,230);
267 $Test2->drawGraphArea(255,255,255,TRUE);
268 $Test2->drawGrid(4,TRUE,200,200,200,50);
269 $Test2->drawTreshold(0,143,55,72,TRUE,TRUE);
270 $Test2->drawTitle(50,22,"Plugin usage over time",50,50,50,585);
272 if(count($dates)){
273 $Test2->drawScale($DataSet2->GetData(),$DataSet2->GetDataDescription(),SCALE_NORMAL,150,150,150,TRUE,0,2);
274 }
276 // Draw the cubic curve graph
277 if(count($dataArray)){
278 $Test2->drawFilledLineGraph($DataSet2->GetData(),$DataSet2->GetDataDescription(),50,TRUE);
279 }
281 // Add error series
282 $errors = array();
283 foreach($res['errorsPerInterval'] as $dateStr => $count){
284 $date = strtotime($dateStr);
285 if($count !=0) $count = ($count);
286 $errors[$date] = $count;
287 }
289 $DataSet2->AddPoint($errors, 'Errors');
290 $DataSet2->SetSerieName('Errors', 'Errors');
291 $DataSet2->AddSerie('Errors');
292 $seriesNumber ++;
294 $Test2->setColorPalette($seriesNumber-1,255,0,0);
296 // Draw legend
297 $Test2->drawLegend(650,30,$DataSet2->GetDataDescription(),255,255,255);
299 // Remove plugin usage from data series, just keep error series.
300 foreach($dataArray as $categoryName => $list){
301 $DataSet2->RemoveSerie($categoryName);
302 }
304 // Draw right scale (Errors per day)
305 $Test2->setFixedScale(0.0001,(max($errors) +1) *1.1);
306 $Test2->drawRightScale($DataSet2->GetData(),$DataSet2->GetDataDescription(),SCALE_NORMAL,120,150,150,TRUE,0,2);
307 $Test2->drawBarGraph($DataSet2->GetData(),$DataSet2->GetDataDescription());
309 $file = '/tmp/graph_'.$this->graphID_2;
310 $Test2->Render($file);
311 session::set('statistics::graphFile'.$this->graphID_2,$file);
312 }
313 }
314 }
316 $smarty->assign('graphID_1', $this->graphID_1);
317 $smarty->assign('graphID_2', $this->graphID_2);
318 $smarty->assign('unsbmittedFiles', count($this->unsbmittedFiles));
319 $smarty->assign('unsbmittedFilesMsg',
320 sprintf(
321 _("You have currently %s unsubmitted statistic collection, do you want to transmit them now?"),
322 count($this->unsbmittedFiles)));
323 return($smarty->fetch(get_template_path('statistics.tpl', TRUE)));
324 }
326 function check()
327 {
328 $messages = plugin::check();
329 return($messages);
330 }
332 function save_object()
333 {
334 plugin::save_object();
335 }
336 }
337 ?>