Project

General

Profile

1 38716 eri.katsar
<?php
2
require_once('./js/log4php/Logger.php');
3
require_once('./paths.php');
4
require_once('./MYDB.php');
5
6
/* TOOD Eri:  Class for recomputing stats numbers  present in the portal.
7
Queries are saved both here and the portal plugin.
8
*/
9
10
class ComputeStatsNumbers {
11
	private $log;
12
	private $db;
13
	private $cache = null; //predis
14
15
public function __construct(){
16
17
		global $redis_host;
18
		global $redis_port;
19
		global $redis_scheme;
20
		global $db_name;
21
         	GLOBAL $username;
22
		global $password;
23
        	global $host;
24
25
		$this->log = Logger::getLogger(__CLASS__);
26
                Logger::configure('./js/log4php/log4php.xml');
27
28
if(class_exists("Predis\Client")){
29
30
                        try {
31
//todo changed timeout from 0 to -1
32
                        $this->cache = new Predis\Client(array("scheme" => $redis_scheme,"host" => $redis_host,"port" => $redis_port,"read_write_timeout" => -1));
33
                                $this->log->info("redis host: ".$redis_host." and redis port: ".$redis_port);
34
                        } catch(Exception $e) {
35
                                $this->log->error("Error connecting to Redis server: ".$e->getMessage());
36
                                $this->cache = null;
37
                        }
38
                }
39
        else{
40
              $this->log->info("cache does not exist"); //predis
41
               exit;
42
        }
43
//NOW GET DB
44
		try {
45
	        $str = 'pgsql:host='.$host.";port=5432;dbname=".$db_name.";user=".$username.";password=".$password;
46
47
 $this->db = new PDO($str);
48
49
//TODO set search path here
50
                   } catch(Exception $e){
51
			$this->log->error('Could not connect to database: ' . $e->getMessage());
52
			exit;
53
		}
54
	}
55
56
57
	private function doQuery($query){
58
		$stmt = $this->db->query($query);
59
		if(!$stmt){
60
        	$arr = $this->db->errorInfo();
61
        	$this->log->error("Error executing query: ".$query." ".$arr[2]);
62
            return "-";
63
		}
64
        $t = $stmt->fetch();
65
66
        return number_format($t[0]);
67
	}
68
//TODO  here store Queries in cache
69
private function storeQuery($key, $query) {
70
$res=$this->doQuery($query);
71
72
//$this->cache = new Predis\Client(array("scheme" => $redis_scheme, "host" => $redis_host, "port" => $redis_port));
73
$this->cache->connect(); //predis
74
$this->cache->hset('SHADOW_STATS_NUMBERS',$key,$res);
75
$this->log->info("Stored query : ".$key."  ".$this->cache->hget('SHADOW_STATS_NUMBERS',$key));
76
$this->cache->quit();
77
78
}
79
80
function computeStats() {
81
82
 global $redis_host;
83
                /*OVERALL*/
84
85
		/*1*/
86
		$qpubs = "SELECT count(*) FROM result where type='publication'";
87
88
		/*2*/
89
		$qoapubs = "SELECT count(*) FROM result WHERE bestlicense='Open Access' and type='publication'";
90
91
		/*3*/
92
		$qnoapubs = "SELECT count(*) FROM result WHERE bestlicense='Closed Access' and type='publication'";
93
94
		/*4*/
95
		$qfpubs = "SELECT count(distinct result_projects.id) FROM result, result_projects where result.result_projects = result_projects.id and type='publication'";
96
97
		/*5*/
98
		$qproj = "SELECT count(*) FROM project";
99
100
101
102
103
104
/*DATASOURCES*/
105
106
                /*6*/
107
                $qdatasrc = "SELECT count(*) FROM datasource";
108
109
                /*7*/
110
                $qdtsrcpubs = "select count(*) from datasource where compatibility != 'not available' and compatibility != 'under validation'";
111
112
                /*8*/
113
                $qdtsrcpubreps = "SELECT count(*) FROM datasource WHERE type='Publication Repository'";
114
115
                /*9*/
116
                $qdtsrcoaj = "SELECT count(*) FROM datasource WHERE type='Journal Platform'";
117
118
                /*10*/
119
                $qdtsrcpubaggr = "SELECT count(*) FROM datasource WHERE type like 'Aggreg%'";
120
121
/*10*/
122
123
                $qdtsrcdatarepo = "SELECT count(*) FROM datasource WHERE type = 'Data Repository'";
124
125
/*FUNDER*/
126
                /*11*/
127
                $qfunders = "SELECT count(distinct funding_lvl0) FROM project WHERE funding_lvl0 !=''";
128
129
130
		/*FP7*/
131
132
		/*12: total number of fp7 pubs */
133
		$qfp7pubstotal = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects = result_projects.id and type='publication' and result_projects.project = project.id and funding_lvl0 = 'FP7'";
134
135
        /*13: number of fp7 projects with publications*/
136
		$qfp7projpubs = "SELECT count(distinct project.id) FROM result, result_projects, project WHERE result.result_projects = result_projects.id and type='publication' and result_projects.project = project.id and funding_lvl0='FP7'";
137
138
		/*14: total number of fp7 projects*/
139
		$qfp7projtotal = "SELECT count(id) FROM project WHERE funding_lvl0 = 'FP7'";
140
141
		/*15: fp7 open access pubs*/
142
		$qfp7oapubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result_projects = result_projects.id AND result_projects.project = project.id and type='publication' and funding_lvl0 = 'FP7' and bestlicense='Open Access'";
143
144
        /*16: fp7 restricted pubs*/
145
		$qfp7respubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects=result_projects.id AND result_projects.project = project.id and funding_lvl0 = 'FP7' and bestlicense='Restricted' and type='publication'";
146
147
        /*17: fp7 pubs in embargo*/
148
		$qfp7embpubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects = result_projects.id AND result_projects.project = project.id and funding_lvl0 = 'FP7' and bestlicense='Embargo' and type='publication'";
149
150
        /*18: total number of fp7 pubs with sc39*/
151
		$qsc39fp7pubstotal = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result_projects.project = project.id and funding_lvl0 = 'FP7' and sc39='yes' and result.result_projects = result_projects.id and type='publication'";
152
153
		/*19: fp7 projs with sc39 and pubs*/
154
		$qsc39fp7projpubs = "SELECT count(distinct project.id) FROM result, result_projects, project WHERE result_projects.project=project.id and funding_lvl0 = 'FP7' and sc39='yes' and result.result_projects = result_projects.id and type='publication'";
155
156
		/*20: total number of fp7 proj with sc39*/
157
		$qsc39fp7projtotal = "SELECT count(number) from project where funding_lvl0='FP7' and sc39='yes'";
158
159
		/*21: open access fp7 pubs with sc39*/
160
		$qsc39fp7oapubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects = result_projects.id AND result_projects.project = project.id and funding_lvl0 = 'FP7' and bestlicense='Open Access' and sc39='yes' and type='publication'";
161
162
163
		/*ERC*/
164
165
		/*22: total number of erc pubs*/
166
		$qercpubstotal = "SELECT count( distinct result.id) FROM result, project, result_projects where result_projects.project = project.id and project.funding_lvl2='ERC' and result.result_projects = result_projects.id and result.type='publication'";
167
168
169
        /*23: erc projects with pubs*/
170
		$qercprojpubs = "SELECT count(distinct project.id) FROM result, project, result_projects where result_projects.project = project.id and project.funding_lvl2='ERC' and result.result_projects = result_projects.id and type='publication'";
171
172
	    /*24: total erc projects*/
173
		$qercprojtotal = "SELECT count(id) FROM project WHERE funding_lvl2 = 'ERC'";
174
175
		/*25: erc open access pubs*/
176
		$qercoapubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects= result_projects.id AND result_projects.project = project.id and funding_lvl2 = 'ERC' and bestlicense='Open Access' and type='publication'";
177
178
        /*26: erc restricted pubs*/
179
		$qercrespubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects = result_projects.id AND result_projects.project = project.id and funding_lvl2 = 'ERC' and bestlicense='Restricted' and type='publication'";
180
181
        /*27: erc embargo pubs*/
182
		$qercembpubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects=result_projects.id AND result_projects.project = project.id and funding_lvl2 = 'ERC' and bestlicense='Embargo' and type='publication'";
183
184
185
		/*Wellcome Trust*/
186
187
		/*28: total number of wt pubs*/
188
189
		$qwtpubstotal = "SELECT count(distinct result_projects.id)    FROM result,result_projects, project WHERE result.result_projects= result_projects.id  AND result_projects.project = project.id and funder ='Wellcome Trust'  and type='publication'";
190
191
192
/*29: wt projects with pubs*/
193
		$qwtprojpubs = "SELECT count(distinct project.id) FROM result, project, result_projects where result_projects.project = project.id and project.funder='Wellcome Trust' and result.result_projects = result_projects.id and type='publication'";
194
		/*30: total wt projects*/
195
		$qwtprojtotal = "SELECT count(id) FROM project WHERE funder ='Wellcome Trust'";
196
197
		/*31: wt open access pubs*/
198
		$qwtoapubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects= result_projects.id AND result_projects.project = project.id and funder ='Wellcome Trust' and bestlicense='Open Access' and type='publication'";
199
        /*32: wt restricted pubs*/
200
		$qwtrespubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects = result_projects.id AND result_projects.project = project.id and funder='Wellcome Trust' and bestlicense='Restricted' and type='publication'";
201
        /*33: wt embargo pubs*/
202
		$qwtembpubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects=result_projects.id AND result_projects.project = project.id and funder ='Wellcome Trust' and bestlicense='Embargo' and type='publication'";
203
204
205
/*FET */
206
207
$fetpubs ="select count(distinct rc.id) as field0 from result_concepts rc join concept c on c.id=rc.concept join category cat on cat.id=c.category where cat.context like 'fet%'";
208
209
$fetoapubs ="select count(distinct rc.id) as field0 from result_concepts rc join result r on r.id=rc.id join concept c on c.id=rc.concept join category cat on cat.id=c.category where cat.context like 'fet%' and r. bestlicense='Open Access'";
210
/*
211
$fet_noa_pubs ="select count(distinct rc.id) as field0 from result_concepts rc join result r on r.id=rc.id join concept c on c.id=rc.concept join category cat on cat.id=c.category where cat.context ='fet' and r. bestlicense='Closed Access'";*/
212
213
$fetembpubs ="select count(distinct rc.id) as field0 from result_concepts rc join result r on r.id=rc.id join concept c on c.id=rc.concept join category cat on cat.id=c.category where cat.context like 'fet%' and r. bestlicense='Embargo'";
214
$fetrespubs ="select count(distinct rc.id) as field0 from result_concepts rc join result r on r.id=rc.id join concept c on c.id=rc.concept join category cat on cat.id=c.category where cat.context like 'fet%' and r. bestlicense='Restricted'";
215
216
217
218
219
	/*Natalia's extra stuff */
220
221
222
		/*35: organisations with publication results */
223
		$org_withpubs = "select count(*) from (select dor.organization as organization from datasource_organizations dor join result_datasources rd on rd.datasource=dor.id join result r on r.id=rd.id where r.type='publication' union select por.organization as organization from project_organizations por join result_projects rp on rp.project=por.id join result r on r.id=rp.id where r.type='publication') as foo";
224
225
		/*36: number of datasets */
226
		$data_total = "select count(distinct id) from result where type='dataset'";
227
228
229
               # $datasrc_withpubs = "SELECT count(distinct rd.datasource) from result_datasources rd join result r on r.id=rd.id where r.type='publication'";
230
231
                $datasrc_withpubs = "SELECT count(distinct rd.datasource) from result_datasources rd ";
232
233
234
/*DATASETS */
235
236
 $datasetpubs= "select count(distinct r.id) from result r join result_results rr on rr.id=r.id join result rp on rp.id=rr.result  where r.type='dataset' and rp.type='publication'" ;
237
238
//# of datasets linked to projects
239
240
$datasetproj="select count(distinct r.id) from result r  join result_projects rp on rp.id=r.id join project  p on p.id=rp.project  where r.type='dataset' ";
241
242
//# of publications linked to datasets
243
244
//$pubsdatasets ="select count(distinct r.id) from result r join result_results rr on rr.id=r.id join result rp on rp.id=rr.result  where r.type='publication' and rp.type='dataset'";
245
246
247
$pubsdatasets ="select count(distinct rp.id) from result  rp where  rp.type='dataset'";
248
/*EGI PAGE NUMS*/
249
250
251
$egivo="select count(distinct c.name) as field0 from result_concepts rc join concept c on c.id=rc.concept join category cat on cat.id=c.category where cat.id='egi::virtual'";
252
253
$egiproj="select count(distinct c.name) as field0 from result_concepts rc join concept c on c.id=rc.concept join category cat on cat.id=c.category where cat.id='egi::projects' ";
254
255
/*total of egi projects */
256
$egiprojtotal="select count (distinct c.name) as field0 from concept c  join category cat on cat.id=c.category where cat.id='egi::projects'";
257
258
259
$egipubs= " select count( distinct rc.id) as field0 from result r,result_concepts rc, concept con, category cat where rc.id=r.id  and  con.id=rc.concept and con.category= cat.id  and cat.context ='egi'";
260
261
$egioa="  select count( distinct rc.id) as field0 from result r,result_concepts rc, concept con, category cat where rc.id=r.id  and  con.id=rc.concept and con.category= cat.id  and cat.context ='egi' and  r.bestlicense='Open Access'";
262
//$eginoa="select  count(distinct r.id) as field0 from result r join result_concepts rc on r.id=rc.id join concept c on c.id=rc.concept where bestlicense='Closed Access'";
263
$egiemb=" select count( distinct rc.id) as field0 from result r,result_concepts rc, concept con, category cat where rc.id=r.id  and  con.id=rc.concept and con.category= cat.id  and cat.context ='egi' and  bestlicense='Embargo'";
264
$egires=" select count( distinct rc.id) as field0 from result r,result_concepts rc, concept con, category cat where rc.id=r.id  and  con.id=rc.concept and con.category= cat.id  and cat.context ='egi' and   bestlicense='Restricted'";
265
266
/*FCT*/
267
268
                /*22: total number of FCT pubs*/
269
                $qfctpubstotal = "SELECT count( distinct result.id) FROM result, project, result_projects where result_projects.project = project.id and project.funder='FCT' and result.result_projects = result_projects.id and result.type='publication'";
270
271
        /*23: FCT projects with pubs*/
272
                $qfctprojpubs = "SELECT count(distinct project.id) FROM result, project, result_projects where result_projects.project = project.id and project.funder='FCT' and result.result_projects = result_projects.id and type='publication'";            /*24: total FCT projects*/
273
                $qfctprojtotal = "SELECT count(id) FROM project WHERE funder ='FCT'";                /*25: FCT open access pubs*/
274
                $qfctoapubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects= result_projects.id AND result_projects.project = project.id and funder ='FCT' and bestlicense='Open Access' and type='publication'";        /*26: FCT restricted pubs*/
275
                $qfctrespubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects = result_projects.id AND result_projects.project = project.id and funder='FCT' and bestlicense='Restricted' and type='publication'";
276
        /*27: FCT embargo pubs*/
277
                $qfctembpubs = "SELECT count(distinct result_projects.id) FROM result, result_projects, project WHERE result.result_projects=result_projects.id AND result_projects.project = project.id and funder ='FCT' and bestlicense='Embargo' and type='publication'";
278
279
280
281
$qres=' select count  (distinct id) from result';
282
$qdtsrcdatarepo = "SELECT count(*) FROM datasource WHERE type = 'Data Repository'";
283
/*total of egi projects */
284
$egiprojtotal="select count (distinct c.name) as field0 from concept c  join category cat on cat.id=c.category where cat.id='egi::projects'";
285
286
287
288
289
290
291
292
293
294
295
296
/*1*/ 	$this->storeQuery('pubs', $qpubs);
297
	        /*2*/ 	$this->storeQuery('oapubs', $qoapubs);
298
	        /*3*/ 	$this->storeQuery('noapubs', $qnoapubs);
299
	    	/*4*/ 	$this->storeQuery('fpubs', $qfpubs);
300
	    	/*5*/ 	$this->storeQuery('proj', $qproj);
301
	    	/*6*/ 	$this->storeQuery('datasrc', $qdatasrc);
302
	    	/*7*/ 	$this->storeQuery('dtsrcpubs', $qdtsrcpubs);
303
	        /*8*/ 	$this->storeQuery('dtsrcpubreps', $qdtsrcpubreps);
304
	        /*9*/ 	$this->storeQuery('dtsrcoaj', $qdtsrcoaj);
305
	        /*10*/ 	$this->storeQuery('dtsrcpubaggr', $qdtsrcpubaggr);
306
	        /*11*/ 	$this->storeQuery('funders', $qfunders);
307
308
		/*FP7*/
309
310
        	/*12*/ 	$this->storeQuery('fp7pubstotal', $qfp7pubstotal);
311
        	/*13*/ 	$this->storeQuery('fp7projpubs', $qfp7projpubs);
312
        	/*14*/ 	$this->storeQuery('fp7projtotal', $qfp7projtotal);
313
        	/*15*/ 	$this->storeQuery('fp7oapubs', $qfp7oapubs);
314
        	/*16*/ 	$this->storeQuery('fp7respubs', $qfp7respubs);
315
        	/*17*/ 	$this->storeQuery('fp7embpubs', $qfp7embpubs);
316
        	/*18*/ 	$this->storeQuery('sc39fp7pubstotal', $qsc39fp7pubstotal);
317
		/*19*/ 	$this->storeQuery('sc39fp7projpubs', $qsc39fp7projpubs);
318
        	/*20*/ 	$this->storeQuery('sc39fp7projtotal', $qsc39fp7projtotal);
319
		/*21*/ 	$this->storeQuery('sc39fp7oapubs', $qsc39fp7oapubs);
320
321
		/*ERC*/
322
323
		/*22*/ 	$this->storeQuery('ercpubstotal', $qercpubstotal);
324
		/*23*/ 	$this->storeQuery('ercprojpubs', $qercprojpubs);
325
        /*24*/ 	$this->storeQuery('ercprojtotal', $qercprojtotal);
326
		/*25*/ 	$this->storeQuery('ercoapubs', $qercoapubs);
327
        /*26*/ 	$this->storeQuery('ercrespubs', $qercrespubs);
328
        /*27*/ 	$this->storeQuery('ercembpubs', $qercembpubs);
329
330
		/*Wellcome Trust*/
331
332
		/*28*/ 	$this->storeQuery('wtpubstotal', $qwtpubstotal);
333
		/*29*/ 	$this->storeQuery('wtprojpubs', $qwtprojpubs);
334
        /*30*/ 	$this->storeQuery('wtprojtotal', $qwtprojtotal);
335
		/*31*/ 	$this->storeQuery('wtoapubs', $qwtoapubs);
336
        /*32*/ 	$this->storeQuery('wtrespubs', $qwtrespubs);
337
        /*33*/ 	$this->storeQuery('wtembpubs', $qwtembpubs);
338
        /*34*/  $this->storeQuery('datasrc_withpubs', $datasrc_withpubs)      ;
339
 //todo commented those for duffy
340
	/*35*/$this->storeQuery('org_withpubs', $org_withpubs);
341
/*36*/	$this->storeQuery('data_total', $data_total);
342
343
344
//FET NUMS
345
346
347
//FET NUMS
348
$this->storeQuery('fetpubs', $fetpubs);
349
$this->storeQuery('fetoapubs', $fetoapubs);
350
$this->storeQuery('fetrespubs', $fetrespubs);
351
$this->storeQuery('fetembpubs', $fetembpubs);
352
353
//DATASET  NUMS
354
$this->storeQuery('datasetpubs', $datasetpubs);
355
$this->storeQuery('datasetproj', $datasetproj);
356
$this->storeQuery('pubsdatasets', $pubsdatasets);
357
358
/*EGI nums*/
359
$this->storeQuery('egipubs', $egipubs);
360
$this->storeQuery('egivo', $egivo);
361
$this->storeQuery('egiproj', $egiproj);
362
363
364
$this->storeQuery('egioa', $egioa);
365
$this->storeQuery('egiemb', $egiemb);
366
$this->storeQuery('egires', $egires);
367
368
//FCT NUMS
369
370
/*22*/  $this->storeQuery('fctpubstotal', $qfctpubstotal);
371
                /*23*/  $this->storeQuery('fctprojpubs', $qfctprojpubs);
372
        /*24*/  $this->storeQuery('fctprojtotal', $qfctprojtotal);
373
                /*25*/  $this->storeQuery('fctoapubs', $qfctoapubs);
374
        /*26*/  $this->storeQuery('fctrespubs', $qfctrespubs);
375
        /*27*/  $this->storeQuery('fctembpubs', $qfctembpubs);
376
377
	 $this->storeQuery('dtsrcdatarepo', $qdtsrcdatarepo);
378
         $this->storeQuery('res', $qres);
379
380
$this->StoreQuery('egiprojtotal',$egiprojtotal);
381
382
383
}
384
}
385
386
?>