Project

General

Profile

1 37810 eri.katsar
<?php
2
require_once('./js/log4php/Logger.php');
3
require_once('./paths.php');
4
require "./predis/autoload.php";
5
6
class ComputeStatsNumbers {
7
	private $log;
8
	private $db;
9
	private $cache = null; //predis
10
11
	public function __construct(){
12
13
		global $redis_host;
14
		global $redis_port;
15
		global $redis_scheme;
16
17
		global $host;
18
19
		$this->log = Logger::getLogger(__CLASS__);
20
		if(class_exists("Predis\Client")){
21
22
			try {
23
24
				Predis\Autoloader::register();
25
		//		$this->cache = new Predis\Client(array( //predis
26
		//			"scheme" => $redis_scheme,
27
        	//			"host" => $redis_host,
28
        	//			"port" => $redis_port,
29
		//			"read_write_timeout" => 600));
30
31
        	//		$this->cache->connect(); //predis
32
		//		$this->cache->quit();
33
34
35
				$this->log->info("redis host: ".$redis_host);
36
				$this->log->info("redis port: ".$redis_port);
37
38
			} catch(Exception $e) {
39
				$this->log->error("Error connecting to Redis server: ".$e->getMessage());
40
				$this->cache = null; //predis
41
			}
42
		}
43
    	else{
44
            $this->log->info("cache does not exist"); //predis
45
			exit;
46
    	}
47
48
		try {
49
			$str = 'pgsql:host='.$host.';port=5432;dbname=stats;user=dnet;password=dnetPwd';
50
			$this->db = new PDO($str);
51
		} catch(Exception $e){
52
			$this->log->error('Could not connect to database: ' . $e->getMessage());
53
			exit;
54
		}
55
	}
56
57
58
	private function doQuery($query){
59
		$stmt = $this->db->query($query);
60
		if(!$stmt){
61
        	$arr = $this->db->errorInfo();
62
        	$this->log->error("Error executing query: ".$query." ".$arr[2]);
63
            return "-";
64
		}
65
        $t = $stmt->fetch();
66
        return number_format($t[0]);
67
	}
68
69
	private function storeQuery($key, $query) {
70
		global $redis_host;
71
		global $redis_port;
72
		global $redis_scheme;
73
74
		$this->cache = new Predis\Client(array("scheme" => $redis_scheme, "host" => $redis_host, "port" => $redis_port));
75
76
		$this->cache->connect(); //predis
77
		$this->cache->hset('STATS_NUMBERS', $key, $this->doQuery($query));
78
79
		$this->cache->quit();
80
	}
81
82
	function computeStats() {
83
		/*OVERALL*/
84
85
		/*1*/
86
		$qpubs = "SELECT count(*) FROM result where type='publication'";
87
88
		/*2*/
89
		$qoapubs = "SELECT count(*) FROM result WHERE access_mode='Open Access' and type='publication'";
90
91
		/*3*/
92
		$qnoapubs = "SELECT count(*) FROM result WHERE access_mode='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!='non compatible' and compatibility!='unknown'";
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 access_mode='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 access_mode='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 access_mode='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 access_mode='Open Access' and sc39='yes' and type='publication'";
150
151
152
		/*ERC*/
153
154
		/*22: total number of erc pubs*/
155
		$qercpubstotal = "select count(r.id)  from result r natural join result_projects rp join project p on rp.project=p.id where type='publication'   and funding_lvl2='ERC'";
156
157
        /*23: erc projects with pubs*/
158
		$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'";
159
160
	    /*24: total erc projects*/
161
		$qercprojtotal = "SELECT count(id) FROM project WHERE funding_lvl2 = 'ERC'";
162
163
		/*25: erc open access pubs*/
164
		$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 access_mode='Open Access' and type='publication'";
165
166
        /*26: erc restricted pubs*/
167
		$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 access_mode='Restricted' and type='publication'";
168
169
        /*27: erc embargo pubs*/
170
		$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 access_mode='Embargo' and type='publication'";
171
172
173
		/*WT*/
174
175
		/*28: total number of wt pubs*/
176
		$qwtpubstotal = "select count(r.id)  from result r natural join result_projects rp join project p on rp.project=p.id where type='publication' and date <> ''  and funding_lvl0='WT'";
177
178
        /*29: wt projects with pubs*/
179
		$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'";
180
181
		/*30: total wt projects*/
182
		$qwtprojtotal = "SELECT count(id) FROM project WHERE funding_lvl0 = 'WT'";
183
184
		/*31: wt open access pubs*/
185
		$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 access_mode='Open Access' and type='publication'";
186
187
        /*32: wt restricted pubs*/
188
		$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 access_mode='Restricted' and type='publication'";
189
190
        /*33: wt embargo pubs*/
191
		$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 access_mode='Embargo' and type='publication'";
192
193
194
		/*Natalia's extra stuff */
195
196
		/*34: datasources with publication results */
197
		$datasrc_withpubs = "SELECT count(distinct rd.datasource) from result_datasources rd join result r on r.id=rd.id where r.type='publication'";
198
199
		/*35: organisations with publication results */
200
		$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";
201
202
		/*36: number of datasets */
203
		$data_total = "select count(id) from result where type='dataset';";
204
205
		/*1*/ 	$this->storeQuery('pubs', $qpubs);
206
	        /*2*/ 	$this->storeQuery('oapubs', $qoapubs);
207
	        /*3*/ 	$this->storeQuery('noapubs', $qnoapubs);
208
	    	/*4*/ 	$this->storeQuery('fpubs', $qfpubs);
209
	    	/*5*/ 	$this->storeQuery('proj', $qproj);
210
	    	/*6*/ 	$this->storeQuery('datasrc', $qdatasrc);
211
	    	/*7*/ 	$this->storeQuery('dtsrcpubs', $qdtsrcpubs);
212
	        /*8*/ 	$this->storeQuery('dtsrcpubreps', $qdtsrcpubreps);
213
	        /*9*/ 	$this->storeQuery('dtsrcoaj', $qdtsrcoaj);
214
	        /*10*/ 	$this->storeQuery('dtsrcpubaggr', $qdtsrcpubaggr);
215
	        /*11*/ 	$this->storeQuery('funders', $qfunders);
216
217
		/*FP7*/
218
219
        	/*12*/ 	$this->storeQuery('fp7pubstotal', $qfp7pubstotal);
220
        	/*13*/ 	$this->storeQuery('fp7projpubs', $qfp7projpubs);
221
        	/*14*/ 	$this->storeQuery('fp7projtotal', $qfp7projtotal);
222
        	/*15*/ 	$this->storeQuery('fp7oapubs', $qfp7oapubs);
223
        	/*16*/ 	$this->storeQuery('fp7respubs', $qfp7respubs);
224
        	/*17*/ 	$this->storeQuery('fp7embpubs', $qfp7embpubs);
225
        	/*18*/ 	$this->storeQuery('sc39fp7pubstotal', $qsc39fp7pubstotal);
226
		/*19*/ 	$this->storeQuery('sc39fp7projpubs', $qsc39fp7projpubs);
227
        	/*20*/ 	$this->storeQuery('sc39fp7projtotal', $qsc39fp7projtotal);
228
		/*21*/ 	$this->storeQuery('sc39fp7oapubs', $qsc39fp7oapubs);
229
230
		/*ERC*/
231
232
		/*22*/ 	$this->storeQuery('ercpubstotal', $qercpubstotal);
233
		/*23*/ 	$this->storeQuery('ercprojpubs', $qercprojpubs);
234
        /*24*/ 	$this->storeQuery('ercprojtotal', $qercprojtotal);
235
		/*25*/ 	$this->storeQuery('ercoapubs', $qercoapubs);
236
        /*26*/ 	$this->storeQuery('ercrespubs', $qercrespubs);
237
        /*27*/ 	$this->storeQuery('ercembpubs', $qercembpubs);
238
239
		/*WT*/
240
241
		/*28*/ 	$this->storeQuery('wtpubstotal', $qwtpubstotal);
242
		/*29*/ 	$this->storeQuery('wtprojpubs', $qwtprojpubs);
243
        /*30*/ 	$this->storeQuery('wtprojtotal', $qwtprojtotal);
244
		/*31*/ 	$this->storeQuery('wtoapubs', $qwtoapubs);
245
        /*32*/ 	$this->storeQuery('wtrespubs', $qwtrespubs);
246
        /*33*/ 	$this->storeQuery('wtembpubs', $qwtembpubs);
247
248
        /*34*/	$this->storeQuery('datasrc_withpubs', $datasrc_withpubs);
249
		/*35*/	$this->storeQuery('org_withpubs', $org_withpubs);
250
		/*36*/	$this->storeQuery('data_total', $data_total);
251
252
        //$this->log->info("statistics numbers: ".print_r($temp, TRUE));
253
	//$this->cache->connect();
254
//        $this->cache->hmset("STATS_NUMBERS", $temp);
255
256
		//$this->cache->save();
257
	}
258
}
259
260
$csn = new ComputeStatsNumbers();
261
$csn->computeStats();
262
263
?>