Blame view

php/classes/AmdaStats.php 10.3 KB
16035364   Benjamin Renard   First commit
1
2
3
<?php
/**
 * @class AmdaStats
ebe18d0a   Elena.Budnik   new amda stat
4
 * @version  $Id: AmdaStats.php 2964 2015-06-26 07:53:48Z elena $ 
16035364   Benjamin Renard   First commit
5
6
7
 */ 
 
class AmdaStats {
ebe18d0a   Elena.Budnik   new amda stat
8
9

	public $statXml;
a4da6a12   Elena.Budnik   temporary commit
10
	public $tasks = array('plot', 'mining', 'print', 'statistics');
ea08d095   Benjamin Renard   Add WS requests i...
11
	public $tasksWs = array('ws_print', 'ws_plot');
ebe18d0a   Elena.Budnik   new amda stat
12
	public $tasksAdd = array('ttoper', 'samp', 'upload', 'create', 'images');
ddeb2c40   Benjamin Renard   Do not exlude imp...
13
	public $usersToExclude = array('bouchemit');
ebe18d0a   Elena.Budnik   new amda stat
14
15
	public $success = true;
	private $user = null;
9846ec70   Benjamin Renard   WS getDataset wit...
16
	private $task = array('request'=>'plot','condition'=>'mining', 'download'=>'print','statistics'=>'statistics',
b9c5d84e   Benjamin Renard   Add test for getP...
17
				'getparameter'=>'ws_print', 'getdataset' => 'ws_print', 'getorbites' => 'ws_print', 'getplot' => 'ws_plot');
a4da6a12   Elena.Budnik   temporary commit
18
	
ebe18d0a   Elena.Budnik   new amda stat
19
20
21
22
23
24
25
	public function __construct($user) {
		
		$this->statXml = new DomDocument('1.0','UTF-8');
		$this->statXml->preserveWhiteSpace = false;
		$this->statXml->formatOutput = true;
	
		if (!defined("StatsXml")){
be67b122   Elena.Budnik   stats.xml par yesr
26
			$thisYear = date("Y");
ebe18d0a   Elena.Budnik   new amda stat
27
28
			if (!$user){  
			// general - to read
be67b122   Elena.Budnik   stats.xml par yesr
29
30
			// define('StatsXml',DATAPATH.'Statistics/Stats.xml');
				define("StatsXml",DATAPATH."Statistics/Stats$thisYear.xml");
ebe18d0a   Elena.Budnik   new amda stat
31
32
33
34
			// if (file_exists(StatsXml)) unlink(StatsXml);
			}
			else {
			// individual - to write
be67b122   Elena.Budnik   stats.xml par yesr
35
36
			// define("StatsXml", USERPATH."/".$user."/Stats.xml");
				define("StatsXml", USERPATH.$user."/Stats$thisYear.xml"); 
ebe18d0a   Elena.Budnik   new amda stat
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
				$this->user = $user;
			}
		}

		if (!file_exists(StatsXml)) {
			if (!is_dir(DATAPATH.'Statistics')) {
					if (!mkdir(DATAPATH.'Statistics', 0775)) $this->success = false; //return -1;
					if (!chgrp(DATAPATH.'Statistics', APACHE_USER)) $this->success = false; // return -1;                 
			}

			$status = $this->generateXml(); 
			if (!$status) {
				error_log('Cannot create Stats.xml: Fatal Error '.$user,1,email); 
				$this->success = false;
			}
		}
		else {
			$status = $this->statXml->load(StatsXml); 
			if (!$status) {
				$status = $this->generateXml(); 
				$msg = $status ? 'Cannot load Stats.xml. New Stats.xml was created' : 
						'Cannot load Stats.xml. Cannot create Stats.xml: Fatal Error ';
						
				error_log($msg.$user,1,email);
				
				if (!$status) 
					$this->success = false;
			}
ea08d095   Benjamin Renard   Add WS requests i...
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
			else {
				$allTasks = array_merge($this->tasks, $this->tasksAdd, $this->tasksWs);
				$newTask = FALSE;
				foreach ($allTasks as $task) {
					$items = $this->statXml->getElementsByTagName($task);
					if ($items->length == 0) {
						//add missing task
						$element = $this->statXml->createElement("$task");
						$this->statXml->documentElement->appendChild($element);
						$newTask = TRUE;
					}
				}
               			if ($newTask) {
					$this->statXml->save(StatsXml);
				}
			}
ebe18d0a   Elena.Budnik   new amda stat
81
		}         
ea08d095   Benjamin Renard   Add WS requests i...
82
	}
16035364   Benjamin Renard   First commit
83
84
85
86

/*
*  Merge individual User Stats.xml into one generique Stats.xml
*/
be67b122   Elena.Budnik   stats.xml par yesr
87
88
89
90
	public function mergeXml($year) {
		// long procedure
		ini_set('max_execution_time', 600); 
		
ea08d095   Benjamin Renard   Add WS requests i...
91
		$allTasks = array_merge($this->tasks, $this->tasksAdd, $this->tasksWs);
ebe18d0a   Elena.Budnik   new amda stat
92

ea08d095   Benjamin Renard   Add WS requests i...
93
		$userDoc = new DomDocument("1.0");
be67b122   Elena.Budnik   stats.xml par yesr
94
		if ($year == null) $year = date("Y");
d3ee926a   Elena.Budnik   users in dataset ...
95

ebe18d0a   Elena.Budnik   new amda stat
96
97
		$users=glob(USERPATH."*");
		foreach ($users as $user) {        
ea08d095   Benjamin Renard   Add WS requests i...
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
			 $userXmlPath = $user."/Stats$year.xml";

			if (!file_exists($userXmlPath)) continue;

			$userDoc->load($userXmlPath);

			foreach ($allTasks as $task) {
				$globalTaskItems = $this->statXml->getElementsByTagName($task);
				if ($globalTaskItems->length == 0)
					continue;
				$globalTaskItem = $globalTaskItems->item(0);
				$userTaskItems = $userDoc->getElementsByTagName($task);
				if ($userTaskItems->length == 0)
					continue;
				$userTaskItem = $userTaskItems->item(0);
				$userItems = $userTaskItem->getElementsByTagName("item");
				if ($userItems->length > 0) {
					foreach ($userItems as $userItem) {
						$globalItem = $this->statXml->importNode($userItem, true);
						$globalTaskItem->appendChild($globalItem);
ebe18d0a   Elena.Budnik   new amda stat
118
119
120
121
122
123
124
125
126
					}
				}          
			}
		}

		// write task statistics as json
		$this->getModulesStat(null,null,true);
		// write data statistics as json
		$this->getDataStat(0,null,null,true);
d3ee926a   Elena.Budnik   users in dataset ...
127
	
ebe18d0a   Elena.Budnik   new amda stat
128
129
130
131
132
133
134
		return  $this->statXml->save(StatsXml);
	}

	private function generateXml() {

		$rootElement = $this->statXml->createElement('stats');

ea08d095   Benjamin Renard   Add WS requests i...
135
		$allTasks = array_merge($this->tasks, $this->tasksAdd, $this->tasksWs);
ebe18d0a   Elena.Budnik   new amda stat
136
137
138
139
140
141
142
143
144
145
		
		foreach ($allTasks as $task) {            
			$element = $this->statXml->createElement("$task");        
			$rootElement->appendChild($element);        
		}

		$this->statXml->appendChild($rootElement);

		return $this->statXml->save(StatsXml);
	}
16035364   Benjamin Renard   First commit
146
 
a4da6a12   Elena.Budnik   temporary commit
147
	public function addTask($user, $task, $vars) {
ea08d095   Benjamin Renard   Add WS requests i...
148

ebe18d0a   Elena.Budnik   new amda stat
149
150
151
152
// 		if (!$this->user) {
// 			error_log('User is null', 1, email);
// 			return;
// 		}
a4da6a12   Elena.Budnik   temporary commit
153
154
155
156
157
158
159
		if ($task == 'killplot')
			return;
			
		if ($vars)
			$realTask = $this->task[$task];
		else
			$realTask = $task;
fcfa8fa8   Elena.Budnik   donot create task...
160

ebe18d0a   Elena.Budnik   new amda stat
161
		if (!in_array($user, $this->usersToExclude)) {
a4da6a12   Elena.Budnik   temporary commit
162
			$taskElementNode = $this->statXml->getElementsByTagName("$realTask");
fcfa8fa8   Elena.Budnik   donot create task...
163
164
165
166

			if ($taskElementNode->length < 1) 
					return; 
					
a4da6a12   Elena.Budnik   temporary commit
167
			$taskElement = $taskElementNode->item(0);
ebe18d0a   Elena.Budnik   new amda stat
168
169
170
171
			if (is_object($taskElement)) {
				$newTask = $this->statXml->createElement('item');
				$newTask->setAttribute('date', date('Y-m-d'));
				$newTask->setAttribute('user', $user);
a4da6a12   Elena.Budnik   temporary commit
172
           
ebe18d0a   Elena.Budnik   new amda stat
173
				if ($vars) { 
a4da6a12   Elena.Budnik   temporary commit
174
175
176
					$ID = array();

					foreach ($vars as $var) {
fcfa8fa8   Elena.Budnik   donot create task...
177
						$ID[] = $var;
ebe18d0a   Elena.Budnik   new amda stat
178
179
180
181
182
183
184
185
186
					}
						
					$ID = array_unique($ID);

					foreach ($ID as $id) {
						$datasetElement = $this->statXml->createElement('dataset', $id);
						$newTask->appendChild($datasetElement);  
					}         
				}
a4da6a12   Elena.Budnik   temporary commit
187
				
ebe18d0a   Elena.Budnik   new amda stat
188
189
190
191
192
				$taskElement->appendChild($newTask);
				$this->statXml->save(StatsXml);  
			}
			else 
				error_log('Check Stats.xml - no task element '.$task, 1, email);
ebe18d0a   Elena.Budnik   new amda stat
193
		}
ebe18d0a   Elena.Budnik   new amda stat
194
	}
16035364   Benjamin Renard   First commit
195
196
197
198
 
/*
*     Show Statistics
*/
ebe18d0a   Elena.Budnik   new amda stat
199
	public function getModulesStat($start, $stop, $update){
16035364   Benjamin Renard   First commit
200

ebe18d0a   Elena.Budnik   new amda stat
201
202
203
204
205
		if (!$update && file_exists(DATAPATH.'Statistics/tasks.json')) {
					return  file_get_contents(DATAPATH.'Statistics/tasks.json');
		}
		
		$taskArray = array();
16035364   Benjamin Renard   First commit
206

ea08d095   Benjamin Renard   Add WS requests i...
207
208
209
210
211
		foreach (array_merge($this->tasks,$this->tasksAdd, $this->tasksWs) as $task) {
			$taskItems = $this->statXml->getElementsByTagName($task);
			if ($taskItems->length < 1)
				return;
			$theTask = $taskItems->item(0);
ebe18d0a   Elena.Budnik   new amda stat
212
213
			$items = $theTask->getElementsByTagName('item');
			$hints = $items->length;
16035364   Benjamin Renard   First commit
214

ebe18d0a   Elena.Budnik   new amda stat
215
			$startStop = $this->getStartStop($items, $start, $stop);
ea08d095   Benjamin Renard   Add WS requests i...
216
217
218
			
			$taskArray[] = array('task' => $task, 'number' => $hints,
				'start' => $startStop[0], 'stop' => $startStop[1]); 
ebe18d0a   Elena.Budnik   new amda stat
219
		}
aa94fd24   elena   Merge with last svn
220

ebe18d0a   Elena.Budnik   new amda stat
221
		$objToReturn = json_encode(array('stats' => $taskArray));
aa94fd24   elena   Merge with last svn
222

ebe18d0a   Elena.Budnik   new amda stat
223
		file_put_contents(DATAPATH.'Statistics/tasks.json', $objToReturn);
aa94fd24   elena   Merge with last svn
224

ebe18d0a   Elena.Budnik   new amda stat
225
226
		return $objToReturn;
	}
aa94fd24   elena   Merge with last svn
227

ebe18d0a   Elena.Budnik   new amda stat
228
229
230
231
232
/*
*     Show Statistics
*/
	public function getDataStat($index, $start, $stop, $update){

ebe18d0a   Elena.Budnik   new amda stat
233
234
235
236
237
238
239
240
		if (!$update && file_exists(DATAPATH.'Statistics/data.json')) {
			$GENERALarray = json_decode(file_get_contents(DATAPATH.'Statistics/data.json'));
		}
		else {
			$VIarray = array();
			$TOTALarray = array();
			$STARTarray = array();
			$STOParray = array();
d3ee926a   Elena.Budnik   users in dataset ...
241
			$usersArray = array();
ebe18d0a   Elena.Budnik   new amda stat
242

ea08d095   Benjamin Renard   Add WS requests i...
243
244
245
246
247
248
249
			$dataTasks = array_merge($this->tasks, $this->tasksWs);

			foreach ($dataTasks as $task) {
				$taskItems = $this->statXml->getElementsByTagName($task);
				if ($taskItems->length < 1)
					continue;
				$theTask = $taskItems->item(0);
ebe18d0a   Elena.Budnik   new amda stat
250
251
252
253
254
255
				$items = $theTask->getElementsByTagName('item');
				$TASKarray = array();
		
				foreach ($items as $item){
					$VIs = $item->getElementsByTagName('dataset');
					$time = strtotime($item->getAttribute('date'));
d3ee926a   Elena.Budnik   users in dataset ...
256
257
					$user = $item->getAttribute('user');
					
ebe18d0a   Elena.Budnik   new amda stat
258
259
260
					foreach ($VIs as $VI) {
						$id = $VI->nodeValue;
						if ($id) {
d3ee926a   Elena.Budnik   users in dataset ...
261
262
263

							$usersArray[$id][$user]++;

ebe18d0a   Elena.Budnik   new amda stat
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
							if ($TASKarray[$id]) {
								$TASKarray[$id]++;
								$TOTALarray[$id]++;
								if ($STARTarray[$id] > $time) 
											$STARTarray[$id] = $time;
								if ($STOParray[$id] < $time) 
											$STOParray[$id] = $time;
							}
							else {
								if (!$TOTALarray[$id]) { 
									$STARTarray[$id] = $time;
									$STOParray[$id] = $time;
									$TOTALarray[$id] = 1;
								} 
								else {
									if ($STARTarray[$id] > $time) 
													$STARTarray[$id] = $time;
									if ($STOParray[$id] < $time) 
													$STOParray[$id] = $time;
									$TOTALarray[$id]++;
								}
								$TASKarray[$id] = 1;
							}            
						}                     
					}
				}
				$VIarray[$task] = $TASKarray;
			}
d3ee926a   Elena.Budnik   users in dataset ...
292

ebe18d0a   Elena.Budnik   new amda stat
293
294
			$GENERALarray = array();
			arsort($TOTALarray);
e566b28b   Elena.Budnik   add percent
295
296
297
			
			$Ntotal = 0; 
           
ebe18d0a   Elena.Budnik   new amda stat
298
299
300
301
302
303
			foreach ($TOTALarray as $key => $value) {
				$viStart = $STARTarray[$key];
				$viStop = $STOParray[$key];
				$plot = $VIarray['plot'][$key];
				$mining = $VIarray['mining'][$key];
				$print = $VIarray['print'][$key];
e566b28b   Elena.Budnik   add percent
304
				$stat = $VIarray['statistics'][$key];
d3ee926a   Elena.Budnik   users in dataset ...
305
306
				$uniqueUsers = count($usersArray[$key]);
				
ebe18d0a   Elena.Budnik   new amda stat
307
308
				if ($key != 'undefined') 
				{
e566b28b   Elena.Budnik   add percent
309
					$GENERALarray[] = array('id' => $key, 'number' => $value, 'percent' => $value,
ebe18d0a   Elena.Budnik   new amda stat
310
												'plot' => $plot, 'mining' => $mining, 
e566b28b   Elena.Budnik   add percent
311
												'print' => $print,'statistics' => $stat, 
d3ee926a   Elena.Budnik   users in dataset ...
312
												'start' => $viStart, 'stop' => $viStop, 'unique' => $uniqueUsers);
e566b28b   Elena.Budnik   add percent
313
314
												
					$Ntotal += $value;							
ebe18d0a   Elena.Budnik   new amda stat
315
				}
e566b28b   Elena.Budnik   add percent
316
317
318
			}
			
			$Ntotal /= 100;
ebe18d0a   Elena.Budnik   new amda stat
319

e566b28b   Elena.Budnik   add percent
320
321
322
323
324
325
			foreach ($GENERALarray as &$elem) {
						$elem['percent'] = round($elem['percent'] / $Ntotal, 2);
			}
			
		}
 
ebe18d0a   Elena.Budnik   new amda stat
326
		$Nmax = count($GENERALarray);
16035364   Benjamin Renard   First commit
327
 
ebe18d0a   Elena.Budnik   new amda stat
328
329
330
331
		$length = $index + 20 > $Nmax ? $Nmax - $index + 1 : 20;
		$objToReturn = array('stats' => array_reverse(array_slice($GENERALarray, $index, $length)));
		
		file_put_contents(DATAPATH.'Statistics/data.json',json_encode($GENERALarray)); 
e566b28b   Elena.Budnik   add percent
332
		
ebe18d0a   Elena.Budnik   new amda stat
333
334
335
336
337
338
339
340
341
		//  $objToReturn = array('stats' => $GENERALarray);
		return $objToReturn;
	}

	public function getStartStop($items, $start, $stop) {
		if (!$start) $start = 0;
		if (!$stop) $stop = 100000000000;
		
		$date = array();
ebe18d0a   Elena.Budnik   new amda stat
342

ea08d095   Benjamin Renard   Add WS requests i...
343
344
345
346
347
348
349
350
351
352
		if ($items->length < 1) {
			return array(0, 0);
		}
		else {
			foreach ($items as $item) {
				$newDate = strtotime($item->getAttribute('date'));

				if (($newDate > $start) && ($newDate < $stop))
					$date[] =  $newDate;        
			}
ebe18d0a   Elena.Budnik   new amda stat
353
354
355
356
357
358
359
360
361
362
		}

		return array(min($date), max($date));
	}

	public function mergeStats($inXml) {

		if (!file_exists(StatsXml)) return 0;
		if (!file_exists($inXml)) return 0;

ea08d095   Benjamin Renard   Add WS requests i...
363
		$tags = array_merge($this->tasks,$this->tasksAdd, $this->tasksWs);
ebe18d0a   Elena.Budnik   new amda stat
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384

		$doc1 = new DomDocument("1.0");
		$doc2 = new DomDocument("1.0");

		if (!$doc1->load(StatsXml)) return 0;
		if (!$doc2->load($inXml)) return 0;

		foreach ($tags as $tag){
			$tag1 = $doc1->getElementsByTagName($tag)->item(0);
			$tag2 = $doc2->getElementsByTagName($tag)->item(0);
			$items2 = $tag2->getElementsByTagName("item");
			if ($items2->length > 0) {
				foreach ($items2 as $item2) {
					$item1 = $doc1->importNode($item2, true);
					$tag1->appendChild($item1);
				}
			}
		}

		return $doc1->save(StatsXml);
	} 
16035364   Benjamin Renard   First commit
385
386
}
?>