Project

General

Profile

1
<?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
		/*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
		$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

    
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
		$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
		$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

    
197
/*FET */
198

    
199
$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

    
201
$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
/*
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
$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
	
208

    
209

    
210

    
211
	/*Natalia's extra stuff */
212
		
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
               # $datasrc_withpubs = "SELECT count(distinct rd.datasource) from result_datasources rd join result r on r.id=rd.id where r.type='publication'";
222

    
223
                $datasrc_withpubs = "SELECT count(distinct rd.datasource) from result_datasources rd ";
224

    
225
 
226
/*DATASETS */
227

    
228
 $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

    
230
//# of datasets linked to projects
231

    
232
$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
//$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
$pubsdatasets ="select count(distinct rp.id) from result  rp where  rp.type='dataset'";
240
/*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
/*FCT*/
255

    
256
                /*22: total number of FCT pubs*/
257
                $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

    
259
        /*23: FCT projects with pubs*/
260
                $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
            /*24: total FCT projects*/
262
                $qfctprojtotal = "SELECT count(id) FROM project WHERE funding_lvl0 = 'FCT'";
263
                /*25: FCT open access pubs*/
264
                $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
        /*26: FCT restricted pubs*/
266
                $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

    
268
        /*27: FCT embargo pubs*/
269
                $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
/*1*/ 	$this->storeQuery('pubs', $qpubs);
275
	        /*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
        /*34*/  $this->storeQuery('datasrc_withpubs', $datasrc_withpubs)      ;
317
 //todo commented those for duffy     	
318
	/*35*/$this->storeQuery('org_withpubs', $org_withpubs);
319
/*36*/	$this->storeQuery('data_total', $data_total);
320

    
321

    
322
//FET NUMS
323

    
324

    
325
//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
//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
}
358
}
359

    
360
?>
361

    
(23-23/28)