Blame view

php/classes/AmdaStats.php 9.22 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');
ebe18d0a   Elena.Budnik   new amda stat
11
	public $tasksAdd = array('ttoper', 'samp', 'upload', 'create', 'images');
a4da6a12   Elena.Budnik   temporary commit
12
	public $usersToExclude = array('bouchemit', 'impex');
ebe18d0a   Elena.Budnik   new amda stat
13
14
	public $success = true;
	private $user = null;
9846ec70   Benjamin Renard   WS getDataset wit...
15
	private $task = array('request'=>'plot','condition'=>'mining', 'download'=>'print','statistics'=>'statistics',
b9c5d84e   Benjamin Renard   Add test for getP...
16
				'getparameter'=>'ws_print', 'getdataset' => 'ws_print', 'getorbites' => 'ws_print', 'getplot' => 'ws_plot');
a4da6a12   Elena.Budnik   temporary commit
17
	
ebe18d0a   Elena.Budnik   new amda stat
18
19
20
21
22
23
24
	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
25
			$thisYear = date("Y");
ebe18d0a   Elena.Budnik   new amda stat
26
27
			if (!$user){  
			// general - to read
be67b122   Elena.Budnik   stats.xml par yesr
28
29
			// define('StatsXml',DATAPATH.'Statistics/Stats.xml');
				define("StatsXml",DATAPATH."Statistics/Stats$thisYear.xml");
ebe18d0a   Elena.Budnik   new amda stat
30
31
32
33
			// if (file_exists(StatsXml)) unlink(StatsXml);
			}
			else {
			// individual - to write
be67b122   Elena.Budnik   stats.xml par yesr
34
35
			// define("StatsXml", USERPATH."/".$user."/Stats.xml");
				define("StatsXml", USERPATH.$user."/Stats$thisYear.xml"); 
ebe18d0a   Elena.Budnik   new amda stat
36
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;
			}
		}         
16035364   Benjamin Renard   First commit
65
66
67
68
69
  }

/*
*  Merge individual User Stats.xml into one generique Stats.xml
*/
be67b122   Elena.Budnik   stats.xml par yesr
70
71
72
73
	public function mergeXml($year) {
		// long procedure
		ini_set('max_execution_time', 600); 
		
ebe18d0a   Elena.Budnik   new amda stat
74
75
76
		$tags = array_merge($this->tasks, $this->tasksAdd);

		$doc2 = new DomDocument("1.0");
be67b122   Elena.Budnik   stats.xml par yesr
77
		if ($year == null) $year = date("Y");
d3ee926a   Elena.Budnik   users in dataset ...
78

ebe18d0a   Elena.Budnik   new amda stat
79
80
		$users=glob(USERPATH."*");
		foreach ($users as $user) {        
be67b122   Elena.Budnik   stats.xml par yesr
81
			 $name2 = $user."/Stats$year.xml";
ebe18d0a   Elena.Budnik   new amda stat
82

0ff6e9db   Elena.Budnik   command name case...
83
			if (!file_exists($name2)) continue;
ebe18d0a   Elena.Budnik   new amda stat
84
85

			$doc2->load($name2);
d3ee926a   Elena.Budnik   users in dataset ...
86

ebe18d0a   Elena.Budnik   new amda stat
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
			foreach ($tags as $tag) {    
				$tag1 = $this->statXml->getElementsByTagName($tag)->item(0);
				$tag2 = $doc2->getElementsByTagName($tag)->item(0);
				$items2 = $tag2->getElementsByTagName("item");
				if ($items2->length > 0) {
					foreach ($items2 as $item2) {
						$item1 = $this->statXml->importNode($item2, true);
						$tag1->appendChild($item1);
					}
				}          
			}
		}

		// 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 ...
104
	
ebe18d0a   Elena.Budnik   new amda stat
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
		return  $this->statXml->save(StatsXml);
	}

	private function generateXml() {

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

		$allTasks = array_merge($this->tasks, $this->tasksAdd);
		
		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
123
 
a4da6a12   Elena.Budnik   temporary commit
124
	public function addTask($user, $task, $vars) {
ebe18d0a   Elena.Budnik   new amda stat
125
126
127
128
129
	
// 		if (!$this->user) {
// 			error_log('User is null', 1, email);
// 			return;
// 		}
a4da6a12   Elena.Budnik   temporary commit
130
131
132
133
134
135
136
		if ($task == 'killplot')
			return;
			
		if ($vars)
			$realTask = $this->task[$task];
		else
			$realTask = $task;
fcfa8fa8   Elena.Budnik   donot create task...
137

ebe18d0a   Elena.Budnik   new amda stat
138
		if (!in_array($user, $this->usersToExclude)) {
a4da6a12   Elena.Budnik   temporary commit
139
			$taskElementNode = $this->statXml->getElementsByTagName("$realTask");
fcfa8fa8   Elena.Budnik   donot create task...
140
141
142
143

			if ($taskElementNode->length < 1) 
					return; 
					
a4da6a12   Elena.Budnik   temporary commit
144
			$taskElement = $taskElementNode->item(0);
ebe18d0a   Elena.Budnik   new amda stat
145
146
147
148
			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
149
           
ebe18d0a   Elena.Budnik   new amda stat
150
				if ($vars) { 
a4da6a12   Elena.Budnik   temporary commit
151
152
153
					$ID = array();

					foreach ($vars as $var) {
fcfa8fa8   Elena.Budnik   donot create task...
154
						$ID[] = $var;
ebe18d0a   Elena.Budnik   new amda stat
155
156
157
158
159
160
161
162
163
					}
						
					$ID = array_unique($ID);

					foreach ($ID as $id) {
						$datasetElement = $this->statXml->createElement('dataset', $id);
						$newTask->appendChild($datasetElement);  
					}         
				}
a4da6a12   Elena.Budnik   temporary commit
164
				
ebe18d0a   Elena.Budnik   new amda stat
165
166
167
168
169
				$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
170
		}
ebe18d0a   Elena.Budnik   new amda stat
171
	}
16035364   Benjamin Renard   First commit
172
173
174
175
 
/*
*     Show Statistics
*/
ebe18d0a   Elena.Budnik   new amda stat
176
	public function getModulesStat($start, $stop, $update){
16035364   Benjamin Renard   First commit
177

ebe18d0a   Elena.Budnik   new amda stat
178
179
180
181
182
		if (!$update && file_exists(DATAPATH.'Statistics/tasks.json')) {
					return  file_get_contents(DATAPATH.'Statistics/tasks.json');
		}
		
		$taskArray = array();
16035364   Benjamin Renard   First commit
183

ebe18d0a   Elena.Budnik   new amda stat
184
185
186
187
		foreach (array_merge($this->tasks,$this->tasksAdd) as $task) {
			$theTask = $this->statXml->getElementsByTagName($task)->item(0);
			$items = $theTask->getElementsByTagName('item');
			$hints = $items->length;
16035364   Benjamin Renard   First commit
188

ebe18d0a   Elena.Budnik   new amda stat
189
190
191
192
			$startStop = $this->getStartStop($items, $start, $stop);
			$taskArray[] = array('task' => $task, 'number' => $hints, 
										'start' => $startStop[0], 'stop' => $startStop[1]); 
		}
aa94fd24   elena   Merge with last svn
193

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

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

ebe18d0a   Elena.Budnik   new amda stat
198
199
		return $objToReturn;
	}
aa94fd24   elena   Merge with last svn
200

ebe18d0a   Elena.Budnik   new amda stat
201
202
203
204
205
/*
*     Show Statistics
*/
	public function getDataStat($index, $start, $stop, $update){

ebe18d0a   Elena.Budnik   new amda stat
206
207
208
209
210
211
212
213
		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 ...
214
			$usersArray = array();
ebe18d0a   Elena.Budnik   new amda stat
215
216
217
218
219
220
221
222
223

			foreach ($this->tasks as $task) {
				$theTask = $this->statXml->getElementsByTagName($task)->item(0);
				$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 ...
224
225
					$user = $item->getAttribute('user');
					
ebe18d0a   Elena.Budnik   new amda stat
226
227
228
					foreach ($VIs as $VI) {
						$id = $VI->nodeValue;
						if ($id) {
d3ee926a   Elena.Budnik   users in dataset ...
229
230
231

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

ebe18d0a   Elena.Budnik   new amda stat
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
							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 ...
260

ebe18d0a   Elena.Budnik   new amda stat
261
262
			$GENERALarray = array();
			arsort($TOTALarray);
e566b28b   Elena.Budnik   add percent
263
264
265
			
			$Ntotal = 0; 
           
ebe18d0a   Elena.Budnik   new amda stat
266
267
268
269
270
271
			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
272
				$stat = $VIarray['statistics'][$key];
d3ee926a   Elena.Budnik   users in dataset ...
273
274
				$uniqueUsers = count($usersArray[$key]);
				
ebe18d0a   Elena.Budnik   new amda stat
275
276
				if ($key != 'undefined') 
				{
e566b28b   Elena.Budnik   add percent
277
					$GENERALarray[] = array('id' => $key, 'number' => $value, 'percent' => $value,
ebe18d0a   Elena.Budnik   new amda stat
278
												'plot' => $plot, 'mining' => $mining, 
e566b28b   Elena.Budnik   add percent
279
												'print' => $print,'statistics' => $stat, 
d3ee926a   Elena.Budnik   users in dataset ...
280
												'start' => $viStart, 'stop' => $viStop, 'unique' => $uniqueUsers);
e566b28b   Elena.Budnik   add percent
281
282
												
					$Ntotal += $value;							
ebe18d0a   Elena.Budnik   new amda stat
283
				}
e566b28b   Elena.Budnik   add percent
284
285
286
			}
			
			$Ntotal /= 100;
ebe18d0a   Elena.Budnik   new amda stat
287

e566b28b   Elena.Budnik   add percent
288
289
290
291
292
293
			foreach ($GENERALarray as &$elem) {
						$elem['percent'] = round($elem['percent'] / $Ntotal, 2);
			}
			
		}
 
ebe18d0a   Elena.Budnik   new amda stat
294
		$Nmax = count($GENERALarray);
16035364   Benjamin Renard   First commit
295
 
ebe18d0a   Elena.Budnik   new amda stat
296
297
298
299
		$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
300
		
ebe18d0a   Elena.Budnik   new amda stat
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
		//  $objToReturn = array('stats' => $GENERALarray);
		return $objToReturn;
	}

	public function getStartStop($items, $start, $stop) {
		if (!$start) $start = 0;
		if (!$stop) $stop = 100000000000;
		
		$date = array();
		
		foreach ($items as $item) {
			$newDate = strtotime($item->getAttribute('date'));

			if (($newDate > $start) && ($newDate < $stop))
							$date[] =  $newDate;        
		}

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

	public function mergeStats($inXml) {

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

		$tags = array_merge($this->tasks,$this->tasksAdd);

		$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
348
349
}
?>