1 | <?php |
---|
2 | $USE_CACHE = true; |
---|
3 | class Cache{ |
---|
4 | private $cache_file; |
---|
5 | private $cache_dir; |
---|
6 | private $data_file; |
---|
7 | private $data; |
---|
8 | private $cache_timeout=60*60; |
---|
9 | |
---|
10 | function Cache(){ |
---|
11 | $this->cache_dir = sys_get_temp_dir(); |
---|
12 | $this->cache_file = "cache_analytics.db.tmp"; |
---|
13 | $this->datafile=$this->cache_dir.'/'.$this->cache_file; |
---|
14 | $this->data = array(); |
---|
15 | } |
---|
16 | function readFile(){ |
---|
17 | if (is_file($this->datafile) and is_writable($this->datafile)){ |
---|
18 | $this->filecontents = file_get_contents($this->datafile); |
---|
19 | }else{ |
---|
20 | // do nothing or throw error |
---|
21 | return false; |
---|
22 | } |
---|
23 | return true; |
---|
24 | } |
---|
25 | function newFile($data=array()){ |
---|
26 | // create new file |
---|
27 | $obj=$data; |
---|
28 | $json=''; |
---|
29 | try{ |
---|
30 | $json=json_encode($obj); |
---|
31 | }catch(Exception $e){ |
---|
32 | return false; |
---|
33 | } |
---|
34 | file_put_contents($this->datafile,$json); |
---|
35 | } |
---|
36 | function parseJson(){ |
---|
37 | try{ |
---|
38 | $json=json_decode($this->filecontents,true); |
---|
39 | $this->data=$json; |
---|
40 | }catch(Exception $e){ |
---|
41 | return false; |
---|
42 | } |
---|
43 | return true; |
---|
44 | } |
---|
45 | function process(){ |
---|
46 | $continue = false; |
---|
47 | if ($this->readFile()){ |
---|
48 | $continue = true; |
---|
49 | }else{ |
---|
50 | $this->newFile(); |
---|
51 | if ($this->readFile()){ |
---|
52 | $continue=true; |
---|
53 | } |
---|
54 | } |
---|
55 | if (! $continue){ |
---|
56 | return false; |
---|
57 | } |
---|
58 | if (! $this->parseJson()){ |
---|
59 | return false; |
---|
60 | } |
---|
61 | return true; |
---|
62 | } |
---|
63 | function get($key){ |
---|
64 | global $USE_CACHE; |
---|
65 | if (! $USE_CACHE){ |
---|
66 | return false; |
---|
67 | } |
---|
68 | if (! $this->process()){ |
---|
69 | return false; |
---|
70 | } |
---|
71 | try{ |
---|
72 | $data=$this->data; |
---|
73 | if ($data == null or ! is_array($data) or ! array_key_exists($key,$data)) |
---|
74 | return false; |
---|
75 | $data=$data[$key]; |
---|
76 | if (time() - (int)$data['timestamp'] > $this->cache_timeout){ |
---|
77 | return false; //'invalid'; |
---|
78 | }else{ |
---|
79 | return $data['value']; |
---|
80 | } |
---|
81 | }catch(Exception $e){ |
---|
82 | return false; |
---|
83 | } |
---|
84 | } |
---|
85 | function store($obj,$key){ |
---|
86 | if (! $this->process()){ |
---|
87 | return false; |
---|
88 | } |
---|
89 | try{ |
---|
90 | $this->data[$key]=['timestamp'=>time(),'value'=>$obj]; |
---|
91 | return $this->newFile($this->data); |
---|
92 | }catch(Exception $e){ |
---|
93 | return false; |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | class DB{ |
---|
99 | |
---|
100 | private $dbhost; |
---|
101 | private $dbname; |
---|
102 | private $dbuser; |
---|
103 | private $dbpass; |
---|
104 | private $ka_file='/var/run/analyticsd.keepalive'; |
---|
105 | private $alias; |
---|
106 | public $dbconn; |
---|
107 | |
---|
108 | function DB(){ |
---|
109 | require_once('config.php'); |
---|
110 | global $dbhost,$dbname,$dbpass,$dbuser,$distros; |
---|
111 | |
---|
112 | $this->dbhost=$dbhost; |
---|
113 | $this->dbname=$dbname; |
---|
114 | $this->dbpass=$dbpass; |
---|
115 | $this->dbuser=$dbuser; |
---|
116 | $this->alias=array(); |
---|
117 | $this->info_distro=json_decode($distros,true); |
---|
118 | if ($this->info_distro == NULL){ |
---|
119 | die('Error: Wrong json in Config.php'); |
---|
120 | } |
---|
121 | $this->init_dates(); |
---|
122 | $this->times=0; |
---|
123 | } |
---|
124 | function init_dates(){ |
---|
125 | $this->dates=array(); |
---|
126 | $this->dates['today']=date("Y-m-d"); |
---|
127 | $this->dates['first_current']=date("Y-m-").'01'; |
---|
128 | $this->dates['last_old']=date("Y-m-d",strtotime($this->dates['first_current']." -1 days")); |
---|
129 | $this->dates['first_old']=date("Y-m-",strtotime($this->dates['today']. "-1 months")).'01'; |
---|
130 | $this->dates['last_very_old']=date("Y-m-d",strtotime($this->dates['first_old']." -1 days")); |
---|
131 | $this->dates['first_very_old']=date("Y-m-",strtotime($this->dates['first_old']." -1 days")).'01'; |
---|
132 | $this->dates['date_current']="(date between '".$this->dates['first_current']."' and '".$this->dates['today']."')"; |
---|
133 | $this->dates['date_old']="(date between '".$this->dates['first_old']."' and '".$this->dates['last_old']."')"; |
---|
134 | $this->dates['date_very_old']="(date between '".$this->dates['first_very_old']."' and '".$this->dates['last_very_old']."')"; |
---|
135 | $this->dates['date_range_last_three_months']="(date between '".$this->dates['first_very_old']."' and '".$this->dates['today']."')"; |
---|
136 | } |
---|
137 | function connect(){ |
---|
138 | $this->dbconn=new mysqli($this->dbhost, $this->dbuser , $this->dbpass, $this->dbname); |
---|
139 | if ($this->dbconn->connect_error) { |
---|
140 | die('Connect Error:'. $this->dbconn->connect_error); |
---|
141 | } |
---|
142 | } |
---|
143 | function disconnect(){ |
---|
144 | $this->dbconn->close(); |
---|
145 | } |
---|
146 | function init_trans(){ |
---|
147 | $this->dbconn->autocommit(FALSE); |
---|
148 | $this->dbconn->begin_transaction(MYSQLI_TRANS_START_READ_WRITE); |
---|
149 | } |
---|
150 | |
---|
151 | function send_data($user,$version,$sabor,$apps,$specs,$date=''){ |
---|
152 | $spec_sql_names = ''; |
---|
153 | $spec_sql_values = ''; |
---|
154 | if ($specs != false){ |
---|
155 | try{ |
---|
156 | $arch = $specs['arch']; |
---|
157 | $mem = $specs['mem']; |
---|
158 | if (is_numeric($mem)){ |
---|
159 | $mem=(int)$mem; |
---|
160 | } |
---|
161 | $vga = substr($specs['vga'],0,80); |
---|
162 | $cpu = substr($specs['cpu']['model'],0,80); |
---|
163 | $ncpu = $specs['cpu']['ncpus']; |
---|
164 | if (is_numeric($ncpu)){ |
---|
165 | $ncpu=(int)$ncpu; |
---|
166 | } |
---|
167 | $spec_sql_names = ',arch,mem,vga,cpu,ncpu'; |
---|
168 | $spec_sql_values = ",'$arch',$mem,'$vga','$cpu',$ncpu"; |
---|
169 | }catch(Exception $e){ |
---|
170 | $spec_sql_names = ''; |
---|
171 | $spec_sql_values = ''; |
---|
172 | } |
---|
173 | } |
---|
174 | if ($date == ''){ |
---|
175 | $sql="INSERT INTO tmp_clients(user,version,sabor,status $spec_sql_names) values ('$user','$version','$sabor',0 $spec_sql_values)"; |
---|
176 | }else{ |
---|
177 | $sql="INSERT INTO tmp_clients(user,version,sabor,status,date $spec_sql_names) values ('$user','$version','$sabor',0,'$date' $spec_sql_values)"; |
---|
178 | } |
---|
179 | $retry=1; |
---|
180 | $done=false; |
---|
181 | $cli_id=false; |
---|
182 | while (! $done and $retry < 4){ |
---|
183 | $res=$this->dbconn->query($sql); |
---|
184 | if ($res){ |
---|
185 | $cli_id=$this->dbconn->insert_id; |
---|
186 | $done=true; |
---|
187 | }else{ |
---|
188 | $retry+=1; |
---|
189 | sleep($retry); |
---|
190 | } |
---|
191 | } |
---|
192 | if ($retry == 4 or $cli_id == false) |
---|
193 | throw new Exception('Error sending client data: '.$this->dbconn->error); |
---|
194 | $err_apps=false; |
---|
195 | $err_exception=false; |
---|
196 | if (count($apps) != 0){ |
---|
197 | if ($date == ''){ |
---|
198 | $sql="insert into tmp_packages(client,app,value) values"; |
---|
199 | }else{ |
---|
200 | $sql="insert into tmp_packages(client,app,value,date) values"; |
---|
201 | } |
---|
202 | $values=array(); |
---|
203 | // Prevent DoS attack |
---|
204 | $i = 1000; |
---|
205 | foreach ($apps as $app => $value){ |
---|
206 | // Max 1000 apps |
---|
207 | if ( $i > 0 ){ |
---|
208 | $i = $i - 1; |
---|
209 | }else{ |
---|
210 | throw new Exception('*** DoS detected, aborting more processing on this request ***'); |
---|
211 | } |
---|
212 | |
---|
213 | if (trim($app) == '' or trim($value) == ''){ |
---|
214 | $err_apps=true; |
---|
215 | $err_exception=new Exception('Wrong application values'); |
---|
216 | continue; |
---|
217 | } |
---|
218 | if ($date == ''){ |
---|
219 | $values[]="($cli_id,'$app',$value)"; |
---|
220 | }else{ |
---|
221 | $values[]="($cli_id,'$app',$value,'$date')"; |
---|
222 | } |
---|
223 | } |
---|
224 | if (count($values) > 0){ |
---|
225 | $sql.=implode(',',$values); |
---|
226 | $done=false; |
---|
227 | $retry=1; |
---|
228 | while (! $done and $retry < 4){ |
---|
229 | $res=$this->dbconn->query($sql); |
---|
230 | if ($res){ |
---|
231 | $done=true; |
---|
232 | }else{ |
---|
233 | $retry += 1; |
---|
234 | sleep($retry); |
---|
235 | } |
---|
236 | } |
---|
237 | if ($retry == 4 or ! $done){ |
---|
238 | $err_apps=true; |
---|
239 | $err_exception=new Exception('Error sending client app data: '.$this->dbconn->error.' QUERY='.$sql); |
---|
240 | } |
---|
241 | } |
---|
242 | } |
---|
243 | //End operations |
---|
244 | $sql = "Update tmp_clients set status=1 where id = $cli_id and status=0"; |
---|
245 | $retry=1; |
---|
246 | $done=false; |
---|
247 | while (! $done and $retry < 4){ |
---|
248 | $res=$this->dbconn->query($sql); |
---|
249 | if ($res){ |
---|
250 | $done=true; |
---|
251 | }else{ |
---|
252 | $retry+=1; |
---|
253 | sleep($retry); |
---|
254 | } |
---|
255 | } |
---|
256 | if ($retry == 4 or $cli_id == false){ |
---|
257 | throw new Exception('Error commiting client data: '.$this->dbconn->error); |
---|
258 | } |
---|
259 | if ($err_apps){ |
---|
260 | throw $err_exception; |
---|
261 | } |
---|
262 | } |
---|
263 | |
---|
264 | private function load_alias(){ |
---|
265 | $sql="SELECT name,alias from Alias"; |
---|
266 | $result=$this->dbconn->query($sql); |
---|
267 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
268 | $this->alias[$row['name']]=$row['alias']; |
---|
269 | } |
---|
270 | } |
---|
271 | function get_system_data(){ |
---|
272 | $sql="select * from Config"; |
---|
273 | $result=$this->dbconn->query($sql); |
---|
274 | if ($result){ |
---|
275 | $tmp=[]; |
---|
276 | foreach ($result->fetch_all(MYSQLI_ASSOC) as $value){ |
---|
277 | $tmp[$value['name']]=$value['value']; |
---|
278 | } |
---|
279 | return json_encode($tmp); |
---|
280 | } |
---|
281 | } |
---|
282 | function get_extended_data($app){ |
---|
283 | $today=date("Y-m-d"); |
---|
284 | $min_date=date("Y-m",strtotime($today." -1 year")).'-01'; |
---|
285 | $this->times=0; |
---|
286 | // CLIENTS DISTRIBUTION PER RELEASE/FLAVOUR |
---|
287 | $sql="select year(date) as year,month(date) as month,Releases_name,Flavours_name,count(*) as num_hosts from (select distinct Client_uid,date,Releases_name,Flavours_name from Client_Versions where date >= '$min_date' )t group by year,month,Releases_name,Flavours_name order by year Desc,month desc,Releases_name asc,num_hosts desc"; |
---|
288 | |
---|
289 | $cache = new Cache; |
---|
290 | $cache_key='extended_1'; |
---|
291 | $data = $cache->get($cache_key); |
---|
292 | $stime=microtime(true); |
---|
293 | if ($data != false){ |
---|
294 | $clients_month=$data; |
---|
295 | }else{ |
---|
296 | $result=$this->dbconn->query($sql); |
---|
297 | if ($result){ |
---|
298 | $this->times+=microtime(true)-$stime; |
---|
299 | $clients_month=[]; |
---|
300 | $tmp=[]; |
---|
301 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
302 | $date=$row['year'].'_'.$row['month']; |
---|
303 | $tmp[$date][$row['Releases_name']][$row['Flavours_name']]=intval($row['num_hosts']); |
---|
304 | } |
---|
305 | foreach ($tmp as $date){ |
---|
306 | $clients_month[]=$date; |
---|
307 | } |
---|
308 | $cache->store($clients_month,$cache_key); |
---|
309 | }else{ |
---|
310 | $clients_month=$this->dbconn->error; |
---|
311 | } |
---|
312 | } |
---|
313 | // architecture |
---|
314 | $sql="select year(date) as year, month(date) as month, Releases_name, count(arch) as count, arch from Client_Versions group by Releases_name,year,month,arch order by year desc,month desc,Releases_name asc,arch asc;"; |
---|
315 | $cache_key='extended_arch'; |
---|
316 | $data = $cache->get($cache_key); |
---|
317 | $stime=microtime(true); |
---|
318 | if ($data != false){ |
---|
319 | $clients_arch=$data; |
---|
320 | }else{ |
---|
321 | $result=$this->dbconn->query($sql); |
---|
322 | if ($result){ |
---|
323 | $this->times+=microtime(true)-$stime; |
---|
324 | $clients_arch=[]; |
---|
325 | $tmp=[]; |
---|
326 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
327 | $date=$row['year'].'_'.$row['month']; |
---|
328 | $tmp[$date][$row['Releases_name']][$row['arch']]=intval($row['count']); |
---|
329 | } |
---|
330 | foreach ($tmp as $date){ |
---|
331 | $clients_arch[]=$date; |
---|
332 | } |
---|
333 | $cache->store($clients_arch,$cache_key); |
---|
334 | }else{ |
---|
335 | $clients_arch=$this->dbconn->error; |
---|
336 | } |
---|
337 | } |
---|
338 | // memory |
---|
339 | //$sql_2G="select year(date) as year, month(date) as month, Releases_name, count(Client_uid) as count from Client_Versions where mem < 2048000 group by Releases_name, year, month order by year desc,month desc,Releases_name asc;"; |
---|
340 | $sql_2G = "select year(t1.date) as year, month(t1.date) as month, t1.Releases_name as rel, count(t2.uuid) as count from Client_Versions t1 left join (select * from Client_Versions where mem < 2048000 ) t2 on t1.uuid = t2.uuid group by year, month, rel order by year desc,month desc,rel asc;"; |
---|
341 | $cache_key='extended_memory_2G'; |
---|
342 | $data = $cache->get($cache_key); |
---|
343 | $stime=microtime(true); |
---|
344 | if ($data != false){ |
---|
345 | $clients_2G=$data; |
---|
346 | }else{ |
---|
347 | $result=$this->dbconn->query($sql_2G); |
---|
348 | if ($result){ |
---|
349 | $this->times+=microtime(true)-$stime; |
---|
350 | $clients_2G=[]; |
---|
351 | $tmp=[]; |
---|
352 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
353 | $date=$row['year'].'_'.$row['month']; |
---|
354 | $tmp[$date][$row['rel']]=intval($row['count']); |
---|
355 | } |
---|
356 | foreach ($tmp as $date){ |
---|
357 | $clients_2G[]=$date; |
---|
358 | } |
---|
359 | $cache->store($clients_2G,$cache_key); |
---|
360 | }else{ |
---|
361 | $clients_2G=$this->dbconn->error; |
---|
362 | } |
---|
363 | } |
---|
364 | //$sql_4G="select year(date) as year, month(date) as month, Releases_name, count(Client_uid) as count from Client_Versions where mem > 2048000 and mem < 4096000 group by Releases_name, year, month order by year desc,month desc,Releases_name asc;"; |
---|
365 | $sql_4G = "select year(t1.date) as year, month(t1.date) as month, t1.Releases_name as rel, count(t2.uuid) as count from Client_Versions t1 left join (select * from Client_Versions where mem > 2048000 and mem < 4096000 ) t2 on t1.uuid = t2.uuid group by year, month, rel order by year desc,month desc,rel asc;"; |
---|
366 | $cache_key='extended_memory_4G'; |
---|
367 | $data = $cache->get($cache_key); |
---|
368 | $stime=microtime(true); |
---|
369 | if ($data != false){ |
---|
370 | $clients_4G=$data; |
---|
371 | }else{ |
---|
372 | $result=$this->dbconn->query($sql_4G); |
---|
373 | if ($result){ |
---|
374 | $this->times+=microtime(true)-$stime; |
---|
375 | $clients_4G=[]; |
---|
376 | $tmp=[]; |
---|
377 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
378 | $date=$row['year'].'_'.$row['month']; |
---|
379 | $tmp[$date][$row['rel']]=intval($row['count']); |
---|
380 | } |
---|
381 | foreach ($tmp as $date){ |
---|
382 | $clients_4G[]=$date; |
---|
383 | } |
---|
384 | $cache->store($clients_4G,$cache_key); |
---|
385 | }else{ |
---|
386 | $clients_4G=$this->dbconn->error; |
---|
387 | } |
---|
388 | } |
---|
389 | //$sql_8G="select year(date) as year, month(date) as month, Releases_name, count(Client_uid) as count from Client_Versions where mem > 4096000 group by Releases_name, year, month order by year desc,month desc,Releases_name asc;"; |
---|
390 | $sql_8G = "select year(t1.date) as year, month(t1.date) as month, t1.Releases_name as rel, count(t2.uuid) as count from Client_Versions t1 left join (select * from Client_Versions where mem > 4096000 ) t2 on t1.uuid = t2.uuid group by year, month, rel order by year desc,month desc,rel asc;"; |
---|
391 | $cache_key='extended_memory_8G'; |
---|
392 | $data = $cache->get($cache_key); |
---|
393 | $stime=microtime(true); |
---|
394 | if ($data != false){ |
---|
395 | $clients_8G=$data; |
---|
396 | }else{ |
---|
397 | $result=$this->dbconn->query($sql_8G); |
---|
398 | if ($result){ |
---|
399 | $this->times+=microtime(true)-$stime; |
---|
400 | $clients_8G=[]; |
---|
401 | $tmp=[]; |
---|
402 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
403 | $date=$row['year'].'_'.$row['month']; |
---|
404 | $tmp[$date][$row['rel']]=intval($row['count']); |
---|
405 | } |
---|
406 | foreach ($tmp as $date){ |
---|
407 | $clients_8G[]=$date; |
---|
408 | } |
---|
409 | $cache->store($clients_8G,$cache_key); |
---|
410 | }else{ |
---|
411 | $clients_8G=$this->dbconn->error; |
---|
412 | } |
---|
413 | } |
---|
414 | // cpu |
---|
415 | //$sql_mono = "select year(date) as year, month(date) as month, Releases_name, count(Client_uid) as count from Client_Versions where ncpu < 2 group by Releases_name, year, month order by year desc,month desc,Releases_name asc;"; |
---|
416 | $sql_mono = "select year(t1.date) as year, month(t1.date) as month, t1.Releases_name as rel, count(t2.uuid) as count from Client_Versions t1 left join (select * from Client_Versions where ncpu = 1 ) t2 on t1.uuid = t2.uuid group by year, month, rel order by year desc,month desc,rel asc;"; |
---|
417 | $cache_key='extended_memory_cpu_mono'; |
---|
418 | $data = $cache->get($cache_key); |
---|
419 | $stime=microtime(true); |
---|
420 | if ($data != false){ |
---|
421 | $clients_mono=$data; |
---|
422 | }else{ |
---|
423 | $result=$this->dbconn->query($sql_mono); |
---|
424 | if ($result){ |
---|
425 | $this->times+=microtime(true)-$stime; |
---|
426 | $clients_mono=[]; |
---|
427 | $tmp=[]; |
---|
428 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
429 | $date=$row['year'].'_'.$row['month']; |
---|
430 | $tmp[$date][$row['rel']]=intval($row['count']); |
---|
431 | } |
---|
432 | foreach ($tmp as $date){ |
---|
433 | $clients_mono[]=$date; |
---|
434 | } |
---|
435 | $cache->store($clients_mono,$cache_key); |
---|
436 | }else{ |
---|
437 | $clients_mono=$this->dbconn->error; |
---|
438 | } |
---|
439 | } |
---|
440 | //$sql_dualquad = "select year(date) as year, month(date) as month, Releases_name, count(Client_uid) as count from Client_Versions where ncpu > 1 and ncpu < 5 group by Releases_name, year, month order by year desc,month desc,Releases_name asc;"; |
---|
441 | $sql_dualquad = "select year(t1.date) as year, month(t1.date) as month, t1.Releases_name as rel, count(t2.uuid) as count from Client_Versions t1 left join (select * from Client_Versions where ncpu > 1 and ncpu < 5 ) t2 on t1.uuid = t2.uuid group by year, month, rel order by year desc,month desc,rel asc;"; |
---|
442 | $cache_key='extended_memory_cpu_dualquad'; |
---|
443 | $data = $cache->get($cache_key); |
---|
444 | $stime=microtime(true); |
---|
445 | if ($data != false){ |
---|
446 | $clients_dualquad=$data; |
---|
447 | }else{ |
---|
448 | $result=$this->dbconn->query($sql_dualquad); |
---|
449 | if ($result){ |
---|
450 | $this->times+=microtime(true)-$stime; |
---|
451 | $clients_dualquad=[]; |
---|
452 | $tmp=[]; |
---|
453 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
454 | $date=$row['year'].'_'.$row['month']; |
---|
455 | $tmp[$date][$row['rel']]=intval($row['count']); |
---|
456 | } |
---|
457 | foreach ($tmp as $date){ |
---|
458 | $clients_dualquad[]=$date; |
---|
459 | } |
---|
460 | $cache->store($clients_dualquad,$cache_key); |
---|
461 | }else{ |
---|
462 | $clients_dualquad=$this->dbconn->error; |
---|
463 | } |
---|
464 | } |
---|
465 | $sql_more = "select year(t1.date) as year, month(t1.date) as month, t1.Releases_name as rel, count(t2.uuid) as count from Client_Versions t1 left join (select * from Client_Versions where ncpu > 4 ) t2 on t1.uuid = t2.uuid group by year, month, rel order by year desc,month desc,rel asc;"; |
---|
466 | $cache_key='extended_memory_cpu_other'; |
---|
467 | $data = $cache->get($cache_key); |
---|
468 | $stime=microtime(true); |
---|
469 | if ($data != false){ |
---|
470 | $clients_more=$data; |
---|
471 | }else{ |
---|
472 | $result=$this->dbconn->query($sql_more); |
---|
473 | if ($result){ |
---|
474 | $this->times+=microtime(true)-$stime; |
---|
475 | $clients_more=[]; |
---|
476 | $tmp=[]; |
---|
477 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
478 | $date=$row['year'].'_'.$row['month']; |
---|
479 | $tmp[$date][$row['rel']]=intval($row['count']); |
---|
480 | } |
---|
481 | foreach ($tmp as $date){ |
---|
482 | $clients_more[]=$date; |
---|
483 | } |
---|
484 | $cache->store($clients_more,$cache_key); |
---|
485 | }else{ |
---|
486 | $clients_more=$this->dbconn->error; |
---|
487 | } |
---|
488 | } |
---|
489 | // CLIENT UPDATES |
---|
490 | $sql="select year,month,count(*) as nclients,sum(cnt)-count(*) as nclients_updated from (select Client_uid,count(Client_uid) as cnt,year(date) as year,month(date) as month from Client_Versions where date >= '$min_date' GROUP by Client_uid,year,month having count(Client_uid) >= 1 ) t group by year,month order by year desc,month desc"; |
---|
491 | $sql="select year,month,count(*) as nclients,sum(cnt)-count(*) as nclients_updated,Releases_name as rel,Flavours_name as fla from (select Client_uid,count(Client_uid) as cnt,year(date) as year,month(date) as month,Releases_name,Flavours_name from Client_Versions where date >= '$min_date' GROUP by Client_uid,year,month,Releases_name,Flavours_name having count(Client_uid) >= 1 ) t group by year,month,Releases_name,Flavours_name order by year desc,month desc"; |
---|
492 | |
---|
493 | $cache_key='extended_2'; |
---|
494 | $data = $cache->get($cache_key); |
---|
495 | $stime=microtime(true); |
---|
496 | if ($data != false){ |
---|
497 | $num_updates_month=$data; |
---|
498 | }else{ |
---|
499 | $result=$this->dbconn->query($sql); |
---|
500 | |
---|
501 | if ($result){ |
---|
502 | $this->times+=microtime(true)-$stime; |
---|
503 | $i=0; |
---|
504 | $tmp=[]; |
---|
505 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
506 | $date=$row['year'].'_'.$row['month']; |
---|
507 | $tmp[$date][$row['rel']][$row['fla']]=intval($row['nclients_updated']); |
---|
508 | } |
---|
509 | foreach($tmp as $date){ |
---|
510 | $num_updates_month[]=$date; |
---|
511 | } |
---|
512 | $cache->store($num_updates_month,$cache_key); |
---|
513 | }else{ |
---|
514 | $num_updates_month=$this->dbconn->error; |
---|
515 | } |
---|
516 | } |
---|
517 | |
---|
518 | |
---|
519 | // CLIENT CHANGE RELEASE |
---|
520 | $sql="select year,month,count(*) as upgrades_en_mes from (select year,month,Client_uid as cliente_upgradeado from (select Client_uid,year(date) as year,month(date) as month from Client_Versions where date >= '$min_date' GROUP by Client_uid,Releases_name,year,month)t group by month,year,Client_uid having(count(*))>1)t group by year,month order by year desc,month desc limit 12"; |
---|
521 | |
---|
522 | |
---|
523 | $cache_key='extended_3'; |
---|
524 | $data = $cache->get($cache_key); |
---|
525 | $stime=microtime(true); |
---|
526 | if ($data != false){ |
---|
527 | $change_releases=$data; |
---|
528 | }else{ |
---|
529 | $result=$this->dbconn->query($sql); |
---|
530 | |
---|
531 | if ($result){ |
---|
532 | $this->times+=microtime(true)-$stime; |
---|
533 | $change_releases=[0,0,0,0,0,0,0,0,0,0,0,0]; |
---|
534 | $i=0; |
---|
535 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
536 | $change_releases[$i++]=intval($row['upgrades_en_mes']); |
---|
537 | } |
---|
538 | $cache->store($change_releases,$cache_key); |
---|
539 | }else{ |
---|
540 | $change_releases=$this->dbconn->error; |
---|
541 | } |
---|
542 | } |
---|
543 | |
---|
544 | |
---|
545 | // CLIENT CHANGE FLAVOUR |
---|
546 | $sql="select year,month,count(*) as cambio_sabor_en_mes from (select year,month,Client_uid as cliente_upgradeado from (select Client_uid,year(date) as year,month(date) as month from Client_Versions where date >= '$min_date' GROUP by Client_uid,Flavours_name,year,month)t group by month,year,Client_uid having(count(*))>1)t group by year,month order by year desc,month desc limit 12"; |
---|
547 | |
---|
548 | $cache_key='extended_4'; |
---|
549 | $data = $cache->get($cache_key); |
---|
550 | $stime=microtime(true); |
---|
551 | if ($data != false){ |
---|
552 | $change_flavour=$data; |
---|
553 | }else{ |
---|
554 | $result=$this->dbconn->query($sql); |
---|
555 | |
---|
556 | if ($result){ |
---|
557 | $this->times+=microtime(true)-$stime; |
---|
558 | $i=0; |
---|
559 | $change_flavour=[0,0,0,0,0,0,0,0,0,0,0,0]; |
---|
560 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
561 | $change_flavour[$i++]=intval($row['cambio_sabor_en_mes']); |
---|
562 | } |
---|
563 | $cache->store($change_flavour,$cache_key); |
---|
564 | }else{ |
---|
565 | $change_flavour=$this->dbconn->error; |
---|
566 | } |
---|
567 | } |
---|
568 | |
---|
569 | //sanitize input |
---|
570 | if ($app != NULL){ |
---|
571 | $app=preg_grep('/^[a-zA-Z0-9\-_]+$/',array($app)); |
---|
572 | if ($app != NULL and isset($app[0])){ |
---|
573 | $app=$this->dbconn->real_escape_string($app[0]); |
---|
574 | $stats['apps']=[]; |
---|
575 | $stats['apps']['app']=$app; |
---|
576 | $sql="select year(date) as year,month(date) as month,string,Releases_name as rel,Flavours_name as fla,sum(count) as count from RecvPackages where string='$app' and date >= '$min_date' group by year,month,Releases_name,Flavours_name order by year desc,month desc,sum(count) desc"; |
---|
577 | |
---|
578 | $cache_key="extended_app_$app"; |
---|
579 | $data = $cache->get($cache_key); |
---|
580 | $stime=microtime(true); |
---|
581 | if ($data != false){ |
---|
582 | $app_use=$data; |
---|
583 | $stats['apps']['app_use']=$app_use; |
---|
584 | }else{ |
---|
585 | $result=$this->dbconn->query($sql); |
---|
586 | if ($result){ |
---|
587 | $this->times+=microtime(true)-$stime; |
---|
588 | $tmp=[]; |
---|
589 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
590 | $date=$row['year'].'_'.$row['month']; |
---|
591 | $tmp[$date][$row['rel']][$row['fla']]=intval($row['count']); |
---|
592 | } |
---|
593 | foreach($tmp as $date){ |
---|
594 | $app_use[]=$date; |
---|
595 | } |
---|
596 | $cache->store($app_use,$cache_key); |
---|
597 | $stats['apps']['app_use']=$app_use; |
---|
598 | }else{ |
---|
599 | $app_use=$this->dbconn->error; |
---|
600 | } |
---|
601 | } |
---|
602 | |
---|
603 | } |
---|
604 | } |
---|
605 | // FINALIZATION & WRITE STRUCTURE |
---|
606 | if (isset($clients_month)){ |
---|
607 | $stats['clients']['clients_per_month']=$clients_month; |
---|
608 | } |
---|
609 | if (isset($num_updates_month)){ |
---|
610 | $stats['clients']['freq_updates_per_month']=$num_updates_month; |
---|
611 | } |
---|
612 | if (isset($change_releases)){ |
---|
613 | $stats['clients']['change_releases']=$change_releases; |
---|
614 | } |
---|
615 | if (isset($change_flavour)){ |
---|
616 | $stats['clients']['change_flavours']=$change_flavour; |
---|
617 | } |
---|
618 | if (isset($clients_arch)){ |
---|
619 | $stats['clients']['architectures']=$clients_arch; |
---|
620 | } |
---|
621 | if (isset($clients_2G)){ |
---|
622 | $stats['clients']['mem']['2G']=$clients_2G; |
---|
623 | } |
---|
624 | if (isset($clients_4G)){ |
---|
625 | $stats['clients']['mem']['4G']=$clients_4G; |
---|
626 | } |
---|
627 | if (isset($clients_8G)){ |
---|
628 | $stats['clients']['mem']['8G']=$clients_8G; |
---|
629 | } |
---|
630 | if (isset($clients_mono)){ |
---|
631 | $stats['clients']['cpu']['mono']=$clients_mono; |
---|
632 | } |
---|
633 | if (isset($clients_dualquad)){ |
---|
634 | $stats['clients']['cpu']['dualquad']=$clients_dualquad; |
---|
635 | } |
---|
636 | if (isset($clients_more)){ |
---|
637 | $stats['clients']['cpu']['more']=$clients_more; |
---|
638 | } |
---|
639 | $stats['debug_query_time']=strval(number_format($this->times,5)); |
---|
640 | if (file_exists($this->ka_file)){ |
---|
641 | $stats['debug_keep_alive']=date('Y-m-d H:i',file_get_contents($this->ka_file)); |
---|
642 | } |
---|
643 | return json_encode($stats); |
---|
644 | } |
---|
645 | |
---|
646 | function get_historic_data($typechart=''){ |
---|
647 | $this->load_alias(); |
---|
648 | $obj=[]; |
---|
649 | $this->times=0; |
---|
650 | foreach ($this->info_distro['distros'] as $distro){ |
---|
651 | $dname=$distro['name']; |
---|
652 | $dlike=$distro['like']; |
---|
653 | $obj[$dname]=array(); |
---|
654 | foreach ($distro['sabor'] as $sabor){ |
---|
655 | $sname=$sabor['name']; |
---|
656 | $slike=$sabor['like']; |
---|
657 | $obj[$dname][$sname][]=$this->get_chart($dlike,$slike,'current',$typechart); |
---|
658 | $obj[$dname][$sname][]=$this->get_chart($dlike,$slike,'old',$typechart); |
---|
659 | $obj[$dname][$sname][]=$this->get_chart($dlike,$slike,'very_old',$typechart); |
---|
660 | } |
---|
661 | } |
---|
662 | $obj['debug_query_time']=strval(number_format($this->times,3)); |
---|
663 | |
---|
664 | if (file_exists($this->ka_file)){ |
---|
665 | $obj['debug_keep_alive']=date('Y-m-d H:i',file_get_contents($this->ka_file)); |
---|
666 | } |
---|
667 | return json_encode($obj); |
---|
668 | } |
---|
669 | |
---|
670 | function get_chart($version='',$sabor='',$type='current',$typechart=''){ |
---|
671 | if ($typechart == ''){ |
---|
672 | $cache_key="$version"."_"."$sabor"."_"."$type"; |
---|
673 | $limit = "limit 10"; |
---|
674 | $sql_limit=10; |
---|
675 | $and = ""; |
---|
676 | }else{ |
---|
677 | $cache_key="$version"."_"."$sabor"."_"."$type"."_"."$typechart"; |
---|
678 | $limit = ""; |
---|
679 | $sql_limit=1000; |
---|
680 | $and = "and string in ( select `name` from `PackagesWhitelist` where status = '1' )"; |
---|
681 | } |
---|
682 | if ($version != ''){ |
---|
683 | $version = " and Releases_name = '$version' "; |
---|
684 | } |
---|
685 | if ($sabor != ''){ |
---|
686 | $sabor = " and Flavours_name = '$sabor' "; |
---|
687 | } |
---|
688 | $order=" order by count desc $limit "; |
---|
689 | $group=" group by app "; |
---|
690 | |
---|
691 | $where=$this->dates['date_'.$type]." $version $sabor "; |
---|
692 | $where_clients=$this->dates['date_'.$type]." $version $sabor "; |
---|
693 | |
---|
694 | $sql="SELECT string as app,sum(count) as count from RecvPackages where $where $and $group $order"; |
---|
695 | $sql_clients = "select count(distinct Client_uid) as count from Client_Versions where $where_clients $order"; |
---|
696 | $cache = new Cache; |
---|
697 | $data = $cache->get($cache_key); |
---|
698 | if ($data != false){ |
---|
699 | return $data; |
---|
700 | }else{ |
---|
701 | $data=array($this->get_result_from_sql($sql,$sql_limit),$this->get_clients_from_sql($sql_clients)); |
---|
702 | $cache->store($data,$cache_key); |
---|
703 | return $data; |
---|
704 | } |
---|
705 | } |
---|
706 | function get_clients_from_sql($sql){ |
---|
707 | $stime=microtime(true); |
---|
708 | if ($result=$this->dbconn->query($sql)){ |
---|
709 | $etime=microtime(true); |
---|
710 | $this->times+=($etime-$stime); |
---|
711 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
712 | if (isset($row['count'])){ |
---|
713 | return array('nclients'=>$row['count']); |
---|
714 | } |
---|
715 | } |
---|
716 | return array('nclients'=>'not available'); |
---|
717 | }else{ |
---|
718 | return array('nclients'=>$this->dbconn->error); |
---|
719 | } |
---|
720 | |
---|
721 | } |
---|
722 | function get_result_from_sql($sql,$limit){ |
---|
723 | $stime=microtime(true); |
---|
724 | if ($result=$this->dbconn->query($sql)){ |
---|
725 | $etime=microtime(true); |
---|
726 | $this->times+=($etime-$stime); |
---|
727 | $obj2=[]; |
---|
728 | $nobj=0; |
---|
729 | while($row=$result->fetch_array(MYSQLI_ASSOC)){ |
---|
730 | if (array_key_exists($row['app'],$this->alias)){ |
---|
731 | if (! empty($this->alias[$row['app']])){ |
---|
732 | if ($nobj < $limit) |
---|
733 | $obj2[$this->alias[$row['app']]]=$row['count']; |
---|
734 | $nobj++; |
---|
735 | } |
---|
736 | }else{ |
---|
737 | if ($nobj < $limit) |
---|
738 | $obj2[$row['app']]=$row['count']; |
---|
739 | $nobj++; |
---|
740 | } |
---|
741 | } |
---|
742 | return $obj2; |
---|
743 | }else{ |
---|
744 | return $this->dbconn->error; |
---|
745 | } |
---|
746 | } |
---|
747 | function get_whitelist_form(){ |
---|
748 | $sql = "select distinct `string` from `RecvPackages` order by `string` asc"; |
---|
749 | $sql2 = "select `name`,`status` from `PackagesWhitelist`"; |
---|
750 | $result=$this->dbconn->query($sql); |
---|
751 | $all_items=false; |
---|
752 | if ($result){ |
---|
753 | $all_items=$result->fetch_all(MYSQLI_ASSOC); |
---|
754 | } |
---|
755 | $whitelist=false; |
---|
756 | $result = $this->dbconn->query($sql2); |
---|
757 | if ($result){ |
---|
758 | $whitelist = $result->fetch_all(MYSQLI_ASSOC); |
---|
759 | } |
---|
760 | if ($all_items === false or $whitelist === false){ |
---|
761 | return false; |
---|
762 | } |
---|
763 | $classify = ['in'=> array(), 'out'=>array(), 'unk' => array()]; |
---|
764 | $clasified = array(); |
---|
765 | foreach ($whitelist as $tmp){ |
---|
766 | if ($tmp['status'] == '0'){ |
---|
767 | $classify['out'][]=$tmp['name']; |
---|
768 | $clasified[] = $tmp['name']; |
---|
769 | }else if($tmp['status'] == '1'){ |
---|
770 | $classify['in'][]=$tmp['name']; |
---|
771 | $clasified[] = $tmp['name']; |
---|
772 | } |
---|
773 | } |
---|
774 | foreach ($all_items as $tmp){ |
---|
775 | if (! in_array($tmp['string'],$clasified)){ |
---|
776 | $classify['unk'][] = $tmp['string']; |
---|
777 | } |
---|
778 | } |
---|
779 | return $classify; |
---|
780 | } |
---|
781 | } |
---|
782 | |
---|
783 | ?> |
---|