Blame view

php/classes/AmdaStats.php 11.5 KB
16035364   Benjamin Renard   First commit
1
2
3
<?php
/**
 * @class AmdaStats
aa94fd24   elena   Merge with last svn
4
 * @version  $Id: AmdaStats.php 2964 2015-06-26 07:53:48Z elena $
16035364   Benjamin Renard   First commit
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 * 
 */ 
 
class AmdaStats {
  
  public $statXml;
  public $tasks = array('mining', 'print', 'plot');
  public $tasksAdd = array('ttoper', 'samp', 'upload', 'create', 'images');
  public $usersToExclude = array( 'bouchemit', 'impex');
  public $success = true;
  public $paramInfo;

  public function __construct($user) {
     
        $this->statXml = new DomDocument('1.0','UTF-8');
        $this->statXml->preserveWhiteSpace = false;
        $this->statXml->formatOutput = true;
    
        if (!defined("StatsXml")){
         if (!$user){  
            // general - to read
                define('StatsXml',DATAPATH.'Statistics/Stats.xml');
aa94fd24   elena   Merge with last svn
27
              // if (file_exists(StatsXml)) unlink(StatsXml);
16035364   Benjamin Renard   First commit
28
29
30
31
32
33
34
35
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
65
66
67
68
69
70
71
            }
           else {
            // individual - to write
                define("StatsXml", USERPATH."/".$user."/Stats.xml"); 
            }
        }

        if (!file_exists(StatsXml)){
            if (!is_dir(DATAPATH.'Statistics')) {
                if (!mkdir(DATAPATH.'Statistics', 0775)) 
                                                    return -1;
                if (!chgrp(DATAPATH.'Statistics', APACHE_USER))
                                                    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;
            }
        }
         
  }

/*
*  Merge individual User Stats.xml into one generique Stats.xml
*/
  public function mergeXml() {
            
     $tags = array_merge($this->tasks, $this->tasksAdd);
   
     $doc2 = new DomDocument("1.0");
 
     $users=glob(USERPATH."*");
16035364   Benjamin Renard   First commit
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
     foreach ($users as $user) {        
        $name2 = $user."/Stats.xml";

        if (!file_Exists($name2)) continue;

        $doc2->load($name2);
        
        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);
                }
            }          
        }
    }
aa94fd24   elena   Merge with last svn
91
92
93
94
95
96

    // write task statistics as json
     $this->getModulesStat(null,null,true);
     // write data statistics as json
     $this->getDataStat(0,null,null,true);

16035364   Benjamin Renard   First commit
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    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);
  }

  public function getInfo($var) {

     $info = $this->paramInfo->GetParamInfo($var);
     $id = $info['ddinfos']['dataset']['id'];

     if (!$id) {
        $id = $info['codeinfos']['vi'];
        // check if from Remote base
        if (!$id) {
            $info = $this->paramInfo->getRemoteParamInfo($var);
            if (!$info)
                    return 'undefined';
            else 
                return  $info['base'].':'.$info['vi'];             
        }
        else 
           return $id;       
    }
    else  
        return $id;
    
 }

  public function addTask($task, $user, $vars){
 
   if (!in_array($user, $this->usersToExclude)) {

        $taskElement = $this->statXml->getElementsByTagName("$task")->item(0);
        if (is_object($taskElement)) {
            $newTask = $this->statXml->createElement('item');
            $newTask->setAttribute('date', date('Y-m-d'));
            $newTask->setAttribute('user', $user);
    
            if ($vars) { 
                $this->paramInfo = new ParamsInfoMgr();

                $ID = array();

                foreach ($vars as $var) {
                    if ((substr($var, 0, 7) == 'impex__') || (substr($var, 0, 13) == 'spase___IMPEX')){
                        $ID[] = 'impex';
                    }
                    elseif (substr($var, 0, 4) == 'wsd_') {
                        $ID[] = 'uploadedData';
                    } 
                    // if derived parameter parse it                                                               
                    elseif (substr($var, 0, 3) == 'ws_') {

                        if (!$parser) $parser = new Parser(); 
                        $realVar = $parser->replaceAll($var);         
                        $varArr = $parser->getVars($realVar);
                        foreach ($varArr as $var) {
                           $ID[] = $this->getInfo($var);
                        }
                    }
                    else {
                        $ID[] = $this->getInfo($var);
                    }                    
                }
                    
                $ID = array_unique($ID);

                    foreach ($ID as $id) {
                        $datasetElement = $this->statXml->createElement('dataset', $id);
                        $newTask->appendChild($datasetElement);  
                    }         
                }
            
            $taskElement->appendChild($newTask);
            $this->statXml->save(StatsXml);  
        }
        else 
            error_log('Check Stats.xml - no task element '.$task, 1, email);
      }     
  }
 
/*
*     Show Statistics
*/
aa94fd24   elena   Merge with last svn
196
197
198
199
200
201
  public function getModulesStat($start, $stop, $update){


    if (!$update && file_exists(DATAPATH.'Statistics/tasks.json')) {
             return  file_get_contents(DATAPATH.'Statistics/tasks.json');
    }
16035364   Benjamin Renard   First commit
202
203
204
205
206
207
208
209
210
    
    $taskArray = array();

    foreach (array_merge($this->tasks,$this->tasksAdd) as $task) {
        $theTask = $this->statXml->getElementsByTagName($task)->item(0);
        $items = $theTask->getElementsByTagName('item');
        $hints = $items->length;

        $startStop = $this->getStartStop($items, $start, $stop);
16035364   Benjamin Renard   First commit
211
        $taskArray[] = array('task' => $task, 'number' => $hints, 
aa94fd24   elena   Merge with last svn
212
                             'start' => $startStop[0], 'stop' => $startStop[1]); 
16035364   Benjamin Renard   First commit
213
    }
aa94fd24   elena   Merge with last svn
214
215
216
217
218
   
    $objToReturn = json_encode(array('stats' => $taskArray));
   
    file_put_contents(DATAPATH.'Statistics/tasks.json', $objToReturn);

16035364   Benjamin Renard   First commit
219
220
221
222
223
224
    return $objToReturn;
  }

/*
*     Show Statistics
*/
aa94fd24   elena   Merge with last svn
225
  public function getDataStat($index, $start, $stop, $update){
16035364   Benjamin Renard   First commit
226

16035364   Benjamin Renard   First commit
227

aa94fd24   elena   Merge with last svn
228
229
230
231
232
233
234
235
    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();
16035364   Benjamin Renard   First commit
236

aa94fd24   elena   Merge with last svn
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
            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'));

                    foreach ($VIs as $VI) {
                        $id = $VI->nodeValue;
                        if ($id) {
                            if ($TASKarray[$id]) {
                                $TASKarray[$id]++;
                                $TOTALarray[$id]++;
                                if ($STARTarray[$id] > $time) 
16035364   Benjamin Renard   First commit
255
                                        $STARTarray[$id] = $time;
aa94fd24   elena   Merge with last svn
256
                                if ($STOParray[$id] < $time) 
16035364   Benjamin Renard   First commit
257
                                        $STOParray[$id] = $time;
aa94fd24   elena   Merge with last svn
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
                            }
                            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;
16035364   Benjamin Renard   First commit
278
            }
aa94fd24   elena   Merge with last svn
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
        
        $GENERALarray = array();
        arsort($TOTALarray);
            
        foreach ($TOTALarray as $key => $value) {
                $viStart = $STARTarray[$key];
                $viStop = $STOParray[$key];
                $plot = $VIarray['plot'][$key];
                $mining = $VIarray['mining'][$key];
                $print = $VIarray['print'][$key];
                if ($key != 'undefined') 
                {
                    $GENERALarray[] = array('id' => $key, 'number' => $value,
                                        'plot' => $plot, 'mining' => $mining, 
                                        'print' => $print, 'start' => $viStart, 
                                        'stop' => $viStop);
                }
        } 
   }

16035364   Benjamin Renard   First commit
299
300
301
302
   $Nmax = count($GENERALarray);
 
   $length = $index + 20 > $Nmax ? $Nmax - $index + 1 : 20;
   $objToReturn = array('stats' => array_reverse(array_slice($GENERALarray, $index, $length)));
aa94fd24   elena   Merge with last svn
303
304
   
   file_put_contents(DATAPATH.'Statistics/data.json',json_encode($GENERALarray)); 
16035364   Benjamin Renard   First commit
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
 //  $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));
   }
aa94fd24   elena   Merge with last svn
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340

  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){
16035364   Benjamin Renard   First commit
341
 
aa94fd24   elena   Merge with last svn
342
343
344
345
346
347
348
349
350
351
352
353
354
355
        $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
356
357
}
?>