Project

General

Profile

1
<?php
2
require_once('./js/log4php/Logger.php');
3
require_once('./paths.php');
4
require_once('./MYDB.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
		global $host;
17

    
18
                $this->log = Logger::getLogger(__CLASS__);
19
                  Logger::configure('./js/log4php/log4php.xml');
20
              if(class_exists("Predis\Client")){
21

    
22
                        try {
23
//todo changed timeout from 0 to -1 
24
                        $this->cache = new Predis\Client(array("scheme" => $redis_scheme,"host" => $redis_host,"port" => $redis_port,"read_write_timeout" => -1));
25
                                $this->log->info("redis host: ".$redis_host." and redis port: ".$redis_port);
26
                        } catch(Exception $e) {
27
                                $this->log->error("Error connecting to Redis server: ".$e->getMessage());
28
                                $this->cache = null;
29
                        }
30
                }
31
        else{
32
            $this->log->info("cache does not exist"); //predis
33
                        exit;
34
        }
35
//NOW GET DB
36
		try {
37
	         $str = 'pgsql:host='.$host.';port=5432;dbname=stats;user=sqoop;password=sqoop';
38
      	$this->db = new PDO($str);
39
		} catch(Exception $e){
40
			$this->log->error('Could not connect to database: ' . $e->getMessage());
41
			exit;
42
		}
43
	}
44

    
45

    
46
	private function doQuery($query){
47
		$stmt = $this->db->query($query);
48
		if(!$stmt){
49
        	$arr = $this->db->errorInfo();
50
        	$this->log->error("Error executing query: ".$query." ".$arr[2]);
51
            return "-";
52
		}
53
        $t = $stmt->fetch();
54
          
55
        return number_format($t[0]);
56
	}
57
//TODO  here store Queries in cache
58
private function storeQuery($key, $query) {
59
		global $redis_host;
60
		global $redis_port;
61
		global $redis_scheme;
62

    
63
$res=$this->doQuery($query);
64

    
65
$this->cache = new Predis\Client(array("scheme" => $redis_scheme, "host" => $redis_host, "port" => $redis_port));
66
$this->cache->connect(); //predis
67
$this->cache->hset('SHADOW_STATS_NUMBERS',$key,$res);
68

    
69

    
70
//TODO  hset name is STATS_NUMBERS; field name = each key; query=field value
71

    
72
$this->log->info("Stored query : ".$key."  ".$this->cache->hget('SHADOW_STATS_NUMBERS',$key));
73
$this->cache->quit();
74
}
75

    
76
function computeStats() {
77
		
78
 global $redis_host;
79
                global $redis_port;
80
                global $redis_scheme;
81
/*OVERALL*/
82

    
83
		/*1*/ 
84
		$qpubs = "SELECT count(*) FROM result where type='publication'";
85

    
86
		/*2*/ 
87
		$qoapubs = "SELECT count(*) FROM result WHERE bestlicense='Open Access' and type='publication'";
88

    
89
		/*3*/ 
90
		$qnoapubs = "SELECT count(*) FROM result WHERE bestlicense='Closed Access' and type='publication'";
91

    
92
		/*4*/ 
93
		$qfpubs = "SELECT count(distinct result_projects.id) FROM result, result_projects where result.result_projects = result_projects.id and type='publication'";
94

    
95
		/*5*/ 
96
		$qproj = "SELECT count(*) FROM project";
97

    
98
		/*6*/ 
99
		$qdatasrc = "SELECT count(*) FROM datasource";
100

    
101
		/*7*/ 
102
		$qdtsrcpubs = "select count(*) from datasource where compatibility != 'not available' and compatibility != 'under validation'";
103

    
104
		/*8*/ 
105
		$qdtsrcpubreps = "SELECT count(*) FROM datasource WHERE type='Repository'";
106

    
107
		/*9*/ 
108
		$qdtsrcoaj = "SELECT count(*) FROM datasource WHERE type='Journal'";
109

    
110
		/*10*/
111
		$qdtsrcpubaggr = "SELECT count(*) FROM datasource WHERE type='Aggregated Repository'";
112

    
113
		/*11*/
114
		$qfunders = "SELECT count(distinct funding_lvl0) FROM project WHERE funding_lvl0 !=''";
115

    
116

    
117
		/*FP7*/
118

    
119
		/*12: total number of fp7 pubs */
120
		$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'";
121

    
122
        /*13: number of fp7 projects with publications*/
123
		$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'";
124

    
125
		/*14: total number of fp7 projects*/
126
		$qfp7projtotal = "SELECT count(id) FROM project WHERE funding_lvl0 = 'FP7'";
127

    
128
		/*15: fp7 open access pubs*/
129
		$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'";
130

    
131
        /*16: fp7 restricted pubs*/
132
		$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'";
133

    
134
        /*17: fp7 pubs in embargo*/
135
		$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'";
136

    
137
        /*18: total number of fp7 pubs with sc39*/
138
		$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'";
139

    
140
		/*19: fp7 projs with sc39 and pubs*/
141
		$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'";
142
		
143
		/*20: total number of fp7 proj with sc39*/
144
		$qsc39fp7projtotal = "SELECT count(number) from project where funding_lvl0='FP7' and sc39='yes'";
145

    
146
		/*21: open access fp7 pubs with sc39*/
147
		$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'";
148

    
149

    
150
		/*ERC*/
151

    
152
		/*22: total number of erc pubs*/
153
		$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'";
154

    
155
        /*23: erc projects with pubs*/
156
		$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'";
157

    
158
	    /*24: total erc projects*/
159
		$qercprojtotal = "SELECT count(id) FROM project WHERE funding_lvl2 = 'ERC'";
160

    
161
		/*25: erc open access pubs*/
162
		$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'";
163

    
164
        /*26: erc restricted pubs*/
165
		$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'";
166

    
167
        /*27: erc embargo pubs*/
168
		$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'";
169

    
170

    
171
		/*WT*/
172

    
173
		/*28: total number of wt pubs*/
174
		$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'";
175

    
176
        /*29: wt projects with pubs*/
177
		$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'";
178

    
179
		/*30: total wt projects*/
180
		$qwtprojtotal = "SELECT count(id) FROM project WHERE funding_lvl0 = 'WT'";
181

    
182
		/*31: wt open access pubs*/
183
		$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'";
184

    
185
        /*32: wt restricted pubs*/
186
		$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'";
187

    
188
        /*33: wt embargo pubs*/
189
		$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'";
190
		
191
		
192
		/*Natalia's extra stuff */
193
		
194

    
195
		
196
		/*35: organisations with publication results */
197
		$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";
198
		
199
		/*36: number of datasets */
200
		$data_total = "select count(distinct id) from result where type='dataset'";
201

    
202

    
203
               $datasrc_withpubs = "SELECT count(distinct rd.datasource) from result_datasources rd join result r on r.id=rd.id where r.type='publication'";
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

    
249

    
250
 //todo commented those for duffy     	
251
//	/*35*/	$this->storeQuery('org_withpubs', $org_withpubs);
252

    
253
/*36*/	$this->storeQuery('data_total', $data_total);
254

    
255
/*34*/  $this->storeQuery('datasrc_withpubs', $datasrc_withpubs);
256

    
257
	}
258
}
259

    
260
?>
(22-22/29)