Project

General

Profile

1 33214 eri.katsar
<?php
2
require_once('./js/log4php/Logger.php');
3
require_once('./paths.php');
4
require_once('./MYDB.php');
5
6 37264 eri.katsar
/* TOOD Eri:  Class for recomputing stats numbers  present in the portal.
7
Queries are saved both here and the portal plugin.
8
*/
9
10 33214 eri.katsar
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 33689 eri.katsar
		global $db_name;
21
         	GLOBAL $username;
22
		global $password;
23
        	global $host;
24 33214 eri.katsar
25 33689 eri.katsar
		$this->log = Logger::getLogger(__CLASS__);
26
                Logger::configure('./js/log4php/log4php.xml');
27
28
if(class_exists("Predis\Client")){
29 33214 eri.katsar
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 33689 eri.katsar
              $this->log->info("cache does not exist"); //predis
41
               exit;
42 33214 eri.katsar
        }
43
//NOW GET DB
44
		try {
45 33689 eri.katsar
	        $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 33214 eri.katsar
			$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 33689 eri.katsar
//$this->cache = new Predis\Client(array("scheme" => $redis_scheme, "host" => $redis_host, "port" => $redis_port));
73 33214 eri.katsar
$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 33689 eri.katsar
78 33214 eri.katsar
}
79
80
function computeStats() {
81
82
 global $redis_host;
83 33689 eri.katsar
                /*OVERALL*/
84
85 33214 eri.katsar
		/*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
		/*6*/
101
		$qdatasrc = "SELECT count(*) FROM datasource";
102
103
		/*7*/
104
		$qdtsrcpubs = "select count(*) from datasource where compatibility != 'not available' and compatibility != 'under validation'";
105
106
		/*8*/
107
		$qdtsrcpubreps = "SELECT count(*) FROM datasource WHERE type='Repository'";
108
109
		/*9*/
110
		$qdtsrcoaj = "SELECT count(*) FROM datasource WHERE type='Journal'";
111
112
		/*10*/
113
		$qdtsrcpubaggr = "SELECT count(*) FROM datasource WHERE type='Aggregated Repository'";
114
115
		/*11*/
116
		$qfunders = "SELECT count(distinct funding_lvl0) FROM project WHERE funding_lvl0 !=''";
117
118
119
		/*FP7*/
120
121
		/*12: total number of fp7 pubs */
122
		$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'";
123
124
        /*13: number of fp7 projects with publications*/
125
		$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'";
126
127
		/*14: total number of fp7 projects*/
128
		$qfp7projtotal = "SELECT count(id) FROM project WHERE funding_lvl0 = 'FP7'";
129
130
		/*15: fp7 open access pubs*/
131
		$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'";
132
133
        /*16: fp7 restricted pubs*/
134
		$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'";
135
136
        /*17: fp7 pubs in embargo*/
137
		$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'";
138
139
        /*18: total number of fp7 pubs with sc39*/
140
		$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'";
141
142
		/*19: fp7 projs with sc39 and pubs*/
143
		$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'";
144
145
		/*20: total number of fp7 proj with sc39*/
146
		$qsc39fp7projtotal = "SELECT count(number) from project where funding_lvl0='FP7' and sc39='yes'";
147
148
		/*21: open access fp7 pubs with sc39*/
149
		$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'";
150
151
152
		/*ERC*/
153
154
		/*22: total number of erc pubs*/
155 34069 eri.katsar
		$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'";
156
157 33214 eri.katsar
158
        /*23: erc projects with pubs*/
159
		$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'";
160
161
	    /*24: total erc projects*/
162
		$qercprojtotal = "SELECT count(id) FROM project WHERE funding_lvl2 = 'ERC'";
163
164
		/*25: erc open access pubs*/
165
		$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'";
166
167
        /*26: erc restricted pubs*/
168
		$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'";
169
170
        /*27: erc embargo pubs*/
171
		$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'";
172
173
174
		/*WT*/
175
176
		/*28: total number of wt pubs*/
177
178 34069 eri.katsar
		$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 funding_lvl0 = 'WT'  and type='publication'";
179
180
181
/*29: wt projects with pubs*/
182 33214 eri.katsar
		$qwtprojpubs = "SELECT count(distinct project.id) FROM result, project, result_projects where result_projects.project = project.id and project.funding_lvl0='WT' and result.result_projects = result_projects.id and type='publication'";
183
184
		/*30: total wt projects*/
185
		$qwtprojtotal = "SELECT count(id) FROM project WHERE funding_lvl0 = 'WT'";
186
187
		/*31: wt open access pubs*/
188
		$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 funding_lvl0 = 'WT' and bestlicense='Open Access' and type='publication'";
189
190
        /*32: wt restricted pubs*/
191
		$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 funding_lvl0 = 'WT' and bestlicense='Restricted' and type='publication'";
192
193
        /*33: wt embargo pubs*/
194
		$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 funding_lvl0 = 'WT' and bestlicense='Embargo' and type='publication'";
195
196 34069 eri.katsar
197
/*FET */
198
199 34321 eri.katsar
$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%'";
200 34069 eri.katsar
201 34321 eri.katsar
$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'";
202 34069 eri.katsar
/*
203
$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'";*/
204
205 34321 eri.katsar
$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'";
206
$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'";
207 34069 eri.katsar
208
209
210
211
	/*Natalia's extra stuff */
212 33214 eri.katsar
213
214
		/*35: organisations with publication results */
215
		$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";
216
217
		/*36: number of datasets */
218
		$data_total = "select count(distinct id) from result where type='dataset'";
219
220
221 33668 eri.katsar
               # $datasrc_withpubs = "SELECT count(distinct rd.datasource) from result_datasources rd join result r on r.id=rd.id where r.type='publication'";
222 33214 eri.katsar
223 33668 eri.katsar
                $datasrc_withpubs = "SELECT count(distinct rd.datasource) from result_datasources rd ";
224
225 34321 eri.katsar
226 34471 eri.katsar
/*DATASETS */
227 33930 eri.katsar
228 34069 eri.katsar
 $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'" ;
229 33930 eri.katsar
230 34069 eri.katsar
//# of datasets linked to projects
231 33930 eri.katsar
232 34069 eri.katsar
$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' ";
233
234
//# of publications linked to datasets
235
236 34471 eri.katsar
//$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'";
237
238
239 34321 eri.katsar
$pubsdatasets ="select count(distinct rp.id) from result  rp where  rp.type='dataset'";
240 34069 eri.katsar
/*EGI PAGE NUMS*/
241
242
243
$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'";
244
245
$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' ";
246
247
$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'";
248
249
$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'";
250
//$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'";
251
$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'";
252
$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'";
253
254 34321 eri.katsar
/*FCT*/
255 34069 eri.katsar
256 34471 eri.katsar
                /*22: total number of FCT pubs*/
257 34321 eri.katsar
                $qfctpubstotal = "SELECT count( distinct result.id) FROM result, project, result_projects where result_projects.project = project.id and project.funding_lvl0='FCT' and result.result_projects = result_projects.id and result.type='publication'";
258 34069 eri.katsar
259 34471 eri.katsar
        /*23: FCT projects with pubs*/
260 34321 eri.katsar
                $qfctprojpubs = "SELECT count(distinct project.id) FROM result, project, result_projects where result_projects.project = project.id and project.funding_lvl0='FCT' and result.result_projects = result_projects.id and type='publication'";
261 34471 eri.katsar
            /*24: total FCT projects*/
262 34321 eri.katsar
                $qfctprojtotal = "SELECT count(id) FROM project WHERE funding_lvl0 = 'FCT'";
263 34471 eri.katsar
                /*25: FCT open access pubs*/
264 34321 eri.katsar
                $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 funding_lvl0 = 'FCT' and bestlicense='Open Access' and type='publication'";
265 34471 eri.katsar
        /*26: FCT restricted pubs*/
266 34321 eri.katsar
                $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 funding_lvl0 = 'FCT' and bestlicense='Restricted' and type='publication'";
267 34069 eri.katsar
268 34471 eri.katsar
        /*27: FCT embargo pubs*/
269 34321 eri.katsar
                $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 funding_lvl0 = 'FCT' and bestlicense='Embargo' and type='publication'";
270
271
272
273
274 34069 eri.katsar
/*1*/ 	$this->storeQuery('pubs', $qpubs);
275 33214 eri.katsar
	        /*2*/ 	$this->storeQuery('oapubs', $qoapubs);
276
	        /*3*/ 	$this->storeQuery('noapubs', $qnoapubs);
277
	    	/*4*/ 	$this->storeQuery('fpubs', $qfpubs);
278
	    	/*5*/ 	$this->storeQuery('proj', $qproj);
279
	    	/*6*/ 	$this->storeQuery('datasrc', $qdatasrc);
280
	    	/*7*/ 	$this->storeQuery('dtsrcpubs', $qdtsrcpubs);
281
	        /*8*/ 	$this->storeQuery('dtsrcpubreps', $qdtsrcpubreps);
282
	        /*9*/ 	$this->storeQuery('dtsrcoaj', $qdtsrcoaj);
283
	        /*10*/ 	$this->storeQuery('dtsrcpubaggr', $qdtsrcpubaggr);
284
	        /*11*/ 	$this->storeQuery('funders', $qfunders);
285
286
		/*FP7*/
287
288
        	/*12*/ 	$this->storeQuery('fp7pubstotal', $qfp7pubstotal);
289
        	/*13*/ 	$this->storeQuery('fp7projpubs', $qfp7projpubs);
290
        	/*14*/ 	$this->storeQuery('fp7projtotal', $qfp7projtotal);
291
        	/*15*/ 	$this->storeQuery('fp7oapubs', $qfp7oapubs);
292
        	/*16*/ 	$this->storeQuery('fp7respubs', $qfp7respubs);
293
        	/*17*/ 	$this->storeQuery('fp7embpubs', $qfp7embpubs);
294
        	/*18*/ 	$this->storeQuery('sc39fp7pubstotal', $qsc39fp7pubstotal);
295
		/*19*/ 	$this->storeQuery('sc39fp7projpubs', $qsc39fp7projpubs);
296
        	/*20*/ 	$this->storeQuery('sc39fp7projtotal', $qsc39fp7projtotal);
297
		/*21*/ 	$this->storeQuery('sc39fp7oapubs', $qsc39fp7oapubs);
298
299
		/*ERC*/
300
301
		/*22*/ 	$this->storeQuery('ercpubstotal', $qercpubstotal);
302
		/*23*/ 	$this->storeQuery('ercprojpubs', $qercprojpubs);
303
        /*24*/ 	$this->storeQuery('ercprojtotal', $qercprojtotal);
304
		/*25*/ 	$this->storeQuery('ercoapubs', $qercoapubs);
305
        /*26*/ 	$this->storeQuery('ercrespubs', $qercrespubs);
306
        /*27*/ 	$this->storeQuery('ercembpubs', $qercembpubs);
307
308
		/*WT*/
309
310
		/*28*/ 	$this->storeQuery('wtpubstotal', $qwtpubstotal);
311
		/*29*/ 	$this->storeQuery('wtprojpubs', $qwtprojpubs);
312
        /*30*/ 	$this->storeQuery('wtprojtotal', $qwtprojtotal);
313
		/*31*/ 	$this->storeQuery('wtoapubs', $qwtoapubs);
314
        /*32*/ 	$this->storeQuery('wtrespubs', $qwtrespubs);
315
        /*33*/ 	$this->storeQuery('wtembpubs', $qwtembpubs);
316 33668 eri.katsar
        /*34*/  $this->storeQuery('datasrc_withpubs', $datasrc_withpubs)      ;
317 33214 eri.katsar
 //todo commented those for duffy
318 33668 eri.katsar
	/*35*/$this->storeQuery('org_withpubs', $org_withpubs);
319 33214 eri.katsar
/*36*/	$this->storeQuery('data_total', $data_total);
320
321 33930 eri.katsar
322
//FET NUMS
323
324
325 34069 eri.katsar
//FET NUMS
326
$this->storeQuery('fetpubs', $fetpubs);
327
$this->storeQuery('fetoapubs', $fetoapubs);
328
$this->storeQuery('fetrespubs', $fetrespubs);
329
$this->storeQuery('fetembpubs', $fetembpubs);
330
331
//DATASET  NUMS
332
$this->storeQuery('datasetpubs', $datasetpubs);
333
$this->storeQuery('datasetproj', $datasetproj);
334
$this->storeQuery('pubsdatasets', $pubsdatasets);
335
336
/*EGI nums*/
337
$this->storeQuery('egipubs', $egipubs);
338
$this->storeQuery('egivo', $egivo);
339
$this->storeQuery('egiproj', $egiproj);
340
341
342
$this->storeQuery('egioa', $egioa);
343
$this->storeQuery('egiemb', $egiemb);
344
$this->storeQuery('egires', $egires);
345
346 34321 eri.katsar
//FCT NUMS
347
348
/*22*/  $this->storeQuery('fctpubstotal', $qfctpubstotal);
349
                /*23*/  $this->storeQuery('fctprojpubs', $qfctprojpubs);
350
        /*24*/  $this->storeQuery('fctprojtotal', $qfctprojtotal);
351
                /*25*/  $this->storeQuery('fctoapubs', $qfctoapubs);
352
        /*26*/  $this->storeQuery('fctrespubs', $qfctrespubs);
353
        /*27*/  $this->storeQuery('fctembpubs', $qfctembpubs);
354
355
356
357 33214 eri.katsar
}
358 34069 eri.katsar
}
359 33214 eri.katsar
360
?>