Commit ea08d0956761b5ad63686dda40f1693d16a2e281

Authored by Benjamin Renard
1 parent b1115ad8

Add WS requests in statistics

Showing 1 changed file with 68 additions and 31 deletions   Show diff stats
php/classes/AmdaStats.php
... ... @@ -8,6 +8,7 @@ class AmdaStats {
8 8  
9 9 public $statXml;
10 10 public $tasks = array('plot', 'mining', 'print', 'statistics');
  11 + public $tasksWs = array('ws_print', 'ws_plot');
11 12 public $tasksAdd = array('ttoper', 'samp', 'upload', 'create', 'images');
12 13 public $usersToExclude = array('bouchemit', 'impex');
13 14 public $success = true;
... ... @@ -61,8 +62,24 @@ class AmdaStats {
61 62 if (!$status)
62 63 $this->success = false;
63 64 }
  65 + else {
  66 + $allTasks = array_merge($this->tasks, $this->tasksAdd, $this->tasksWs);
  67 + $newTask = FALSE;
  68 + foreach ($allTasks as $task) {
  69 + $items = $this->statXml->getElementsByTagName($task);
  70 + if ($items->length == 0) {
  71 + //add missing task
  72 + $element = $this->statXml->createElement("$task");
  73 + $this->statXml->documentElement->appendChild($element);
  74 + $newTask = TRUE;
  75 + }
  76 + }
  77 + if ($newTask) {
  78 + $this->statXml->save(StatsXml);
  79 + }
  80 + }
64 81 }
65   - }
  82 + }
66 83  
67 84 /*
68 85 * Merge individual User Stats.xml into one generique Stats.xml
... ... @@ -71,27 +88,33 @@ class AmdaStats {
71 88 // long procedure
72 89 ini_set('max_execution_time', 600);
73 90  
74   - $tags = array_merge($this->tasks, $this->tasksAdd);
  91 + $allTasks = array_merge($this->tasks, $this->tasksAdd, $this->tasksWs);
75 92  
76   - $doc2 = new DomDocument("1.0");
  93 + $userDoc = new DomDocument("1.0");
77 94 if ($year == null) $year = date("Y");
78 95  
79 96 $users=glob(USERPATH."*");
80 97 foreach ($users as $user) {
81   - $name2 = $user."/Stats$year.xml";
82   -
83   - if (!file_exists($name2)) continue;
84   -
85   - $doc2->load($name2);
86   -
87   - foreach ($tags as $tag) {
88   - $tag1 = $this->statXml->getElementsByTagName($tag)->item(0);
89   - $tag2 = $doc2->getElementsByTagName($tag)->item(0);
90   - $items2 = $tag2->getElementsByTagName("item");
91   - if ($items2->length > 0) {
92   - foreach ($items2 as $item2) {
93   - $item1 = $this->statXml->importNode($item2, true);
94   - $tag1->appendChild($item1);
  98 + $userXmlPath = $user."/Stats$year.xml";
  99 +
  100 + if (!file_exists($userXmlPath)) continue;
  101 +
  102 + $userDoc->load($userXmlPath);
  103 +
  104 + foreach ($allTasks as $task) {
  105 + $globalTaskItems = $this->statXml->getElementsByTagName($task);
  106 + if ($globalTaskItems->length == 0)
  107 + continue;
  108 + $globalTaskItem = $globalTaskItems->item(0);
  109 + $userTaskItems = $userDoc->getElementsByTagName($task);
  110 + if ($userTaskItems->length == 0)
  111 + continue;
  112 + $userTaskItem = $userTaskItems->item(0);
  113 + $userItems = $userTaskItem->getElementsByTagName("item");
  114 + if ($userItems->length > 0) {
  115 + foreach ($userItems as $userItem) {
  116 + $globalItem = $this->statXml->importNode($userItem, true);
  117 + $globalTaskItem->appendChild($globalItem);
95 118 }
96 119 }
97 120 }
... ... @@ -109,7 +132,7 @@ class AmdaStats {
109 132  
110 133 $rootElement = $this->statXml->createElement('stats');
111 134  
112   - $allTasks = array_merge($this->tasks, $this->tasksAdd);
  135 + $allTasks = array_merge($this->tasks, $this->tasksAdd, $this->tasksWs);
113 136  
114 137 foreach ($allTasks as $task) {
115 138 $element = $this->statXml->createElement("$task");
... ... @@ -122,7 +145,7 @@ class AmdaStats {
122 145 }
123 146  
124 147 public function addTask($user, $task, $vars) {
125   -
  148 +
126 149 // if (!$this->user) {
127 150 // error_log('User is null', 1, email);
128 151 // return;
... ... @@ -181,14 +204,18 @@ class AmdaStats {
181 204  
182 205 $taskArray = array();
183 206  
184   - foreach (array_merge($this->tasks,$this->tasksAdd) as $task) {
185   - $theTask = $this->statXml->getElementsByTagName($task)->item(0);
  207 + foreach (array_merge($this->tasks,$this->tasksAdd, $this->tasksWs) as $task) {
  208 + $taskItems = $this->statXml->getElementsByTagName($task);
  209 + if ($taskItems->length < 1)
  210 + return;
  211 + $theTask = $taskItems->item(0);
186 212 $items = $theTask->getElementsByTagName('item');
187 213 $hints = $items->length;
188 214  
189 215 $startStop = $this->getStartStop($items, $start, $stop);
190   - $taskArray[] = array('task' => $task, 'number' => $hints,
191   - 'start' => $startStop[0], 'stop' => $startStop[1]);
  216 +
  217 + $taskArray[] = array('task' => $task, 'number' => $hints,
  218 + 'start' => $startStop[0], 'stop' => $startStop[1]);
192 219 }
193 220  
194 221 $objToReturn = json_encode(array('stats' => $taskArray));
... ... @@ -213,8 +240,13 @@ class AmdaStats {
213 240 $STOParray = array();
214 241 $usersArray = array();
215 242  
216   - foreach ($this->tasks as $task) {
217   - $theTask = $this->statXml->getElementsByTagName($task)->item(0);
  243 + $dataTasks = array_merge($this->tasks, $this->tasksWs);
  244 +
  245 + foreach ($dataTasks as $task) {
  246 + $taskItems = $this->statXml->getElementsByTagName($task);
  247 + if ($taskItems->length < 1)
  248 + continue;
  249 + $theTask = $taskItems->item(0);
218 250 $items = $theTask->getElementsByTagName('item');
219 251 $TASKarray = array();
220 252  
... ... @@ -307,12 +339,17 @@ class AmdaStats {
307 339 if (!$stop) $stop = 100000000000;
308 340  
309 341 $date = array();
310   -
311   - foreach ($items as $item) {
312   - $newDate = strtotime($item->getAttribute('date'));
313 342  
314   - if (($newDate > $start) && ($newDate < $stop))
315   - $date[] = $newDate;
  343 + if ($items->length < 1) {
  344 + return array(0, 0);
  345 + }
  346 + else {
  347 + foreach ($items as $item) {
  348 + $newDate = strtotime($item->getAttribute('date'));
  349 +
  350 + if (($newDate > $start) && ($newDate < $stop))
  351 + $date[] = $newDate;
  352 + }
316 353 }
317 354  
318 355 return array(min($date), max($date));
... ... @@ -323,7 +360,7 @@ class AmdaStats {
323 360 if (!file_exists(StatsXml)) return 0;
324 361 if (!file_exists($inXml)) return 0;
325 362  
326   - $tags = array_merge($this->tasks,$this->tasksAdd);
  363 + $tags = array_merge($this->tasks,$this->tasksAdd, $this->tasksWs);
327 364  
328 365 $doc1 = new DomDocument("1.0");
329 366 $doc2 = new DomDocument("1.0");
... ...