summaryrefslogtreecommitdiff
path: root/scripts/cron/refresh_stats.php
blob: 3b446ba4de606242f320441bcfa082f195af0a93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/php -q
<?php
/*
LibreSSL - CAcert web application
Copyright (C) 2004-2012  CAcert Inc.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

require_once(dirname(__FILE__).'/../../includes/mysql.php');

/**
 * Wrapper around mysql_query() to provide some error handling. Prints an error
 * message and dies if query fails
 *
 * @param string $sql
 * 		the SQL statement to execute
 * @return resource|boolean
 * 		the MySQL result set
 */
function sql_query($sql) {
	$res = mysql_query($sql);
	if (!$res) {
		fwrite(STDERR, "MySQL query failed:\n\"$sql\"\n".mysql_error());
		die(1);
	}

	return $res;
}

function tc($sql) {
	$row = mysql_fetch_assoc(sql_query($sql));
	return(intval($row['count']));
}

/**
* writes new data to cache, create cache or update existing cache, set current
* time stamp
* @return boolean
*/
function updateCache($stats) {
	$timestamp = time();
	$sql = "insert into `statscache` (`timestamp`, `cache`) values
	('$timestamp', '".mysql_real_escape_string(serialize($stats))."')";
	sql_query($sql);

	// Make sure the new statistic was inserted successfully
	$res = sql_query(
		"select 1 from `statscache` where `timestamp` = '$timestamp'");
	if (mysql_num_rows($res) !== 1) {
		fwrite(STDERR, "Error on inserting the new statistic");
		return false;
	}

	sql_query("delete from `statscache` where `timestamp` != '$timestamp'");
	return true;
}

/**
* get statistics data from live tables, takes a long time so please try to use the
* cache
* @return array
*/
function getDataFromLive() {
	echo "Calculating current statistics\n";

	$stats = array();
	$stats['verified_users'] = number_format(tc(
		"select count(*) as `count` from `users`
			where `verified` = 1
			and `deleted` = 0
			and `locked` = 0"));

	$stats['verified_emails'] = number_format(tc(
		"select count(*) as `count` from `email`
			where `hash` = '' and `deleted` = 0"));

	$stats['verified_domains'] = number_format(tc(
		"select count(*) as `count` from `domains`
			where `hash` = '' and `deleted` = 0"));

	$certs = tc("select count(*) as `count` from `domaincerts`
			where `expire` != 0");
	$certs += tc("select count(*) as `count` from `emailcerts`
			where `expire` != 0");
	$certs += tc("select count(*) as `count` from `gpg`
			where `expire` != 0");
	$certs += tc("select count(*) as `count` from `orgdomaincerts`
			where `expire` != 0");
	$certs += tc("select count(*) as `count` from `orgemailcerts`
			where `expire` != 0");
	$stats['verified_certificates'] = number_format($certs);

	$certs = tc("select count(*) as `count` from `domaincerts`
		where `revoked` = 0 and `expire` > NOW()");
	$certs += tc("select count(*) as `count` from `emailcerts`
		where `revoked` = 0 and `expire` > NOW()");
	$certs += tc("select count(*) as `count` from `gpg`
		where `expire` > NOW()");
	$certs += tc("select count(*) as `count` from `orgdomaincerts`
		where `revoked` = 0 and `expire` > NOW()");
	$certs += tc("select count(*) as `count` from `orgemailcerts`
		where `revoked` = 0 and `expire` > NOW()");
	$stats['valid_certificates'] = number_format($certs);

	$stats['assurances_made'] = number_format(tc(
		"select count(*) as `count` from `notary`
			where (`method` = '' or `method` = 'Face to Face Meeting')
			and `deleted` = 0"));

	$stats['users_1to49'] = number_format(tc(
		"select count(*) as `count` from (
			select 1 from `notary`
				where `deleted` = 0
				group by `to`
				having sum(`points`) > 0 and sum(`points`) < 50
			) as `low_points`"));

	$stats['users_50to99'] = number_format(tc(
		"select count(*) as `count` from (
			select 1 from `notary`
				where `deleted` = 0
				group by `to`
				having sum(`points`) >= 50 and sum(`points`) < 100
			) as `high_points`"));

	$stats['assurer_candidates'] = number_format(tc(
		"select count(*) as `count` from `users`
			where (
				select sum(`points`) from `notary`
					where `to`=`users`.`id`
					and `deleted` = 0
				) >= 100
			and not exists(
				select 1 from `cats_passed` as `cp`, `cats_variant` as `cv`
					where `cp`.`user_id`=`users`.`id`
					and `cp`.`variant_id`=`cv`.`id`
					and `cv`.`type_id`=1
				)"
		));

	$stats['aussurers_with_test'] = number_format(tc(
		"select count(*) as `count` from `users`
			where (
				select sum(`points`) from `notary`
					where `to`=`users`.`id`
					and `deleted` = 0
				) >= 100
			and exists(
				select 1 from `cats_passed` as `cp`, `cats_variant` as `cv`
					where `cp`.`user_id`=`users`.`id`
					and `cp`.`variant_id`=`cv`.`id`
					and `cv`.`type_id`=1
				)"
		));

	$stats['points_issued'] = number_format(tc(
		"select sum(greatest(`points`, `awarded`)) as `count` from `notary`
			where `deleted` = 0
			and `method` = 'Face to Face Meeting'"));

	$totalusers=0;
	$totassurers=0;
	$totalcerts=0;
	for($i = 0; $i < 12; $i++) {
		$first_ts = mktime(0, 0, 0, date("m") - $i, 1, date("Y"));
		$next_month_ts =  mktime(0, 0, 0, date("m") - $i + 1, 1, date("Y"));
		$first = date("Y-m-d", $first_ts);
		$next_month = date("Y-m-d", $next_month_ts);

		echo "Calculating statistics for month $first\n";

		$totalusers += $users = tc(
			"select count(*) as `count` from `users`
				where `created` >= '$first' and `created` < '$next_month'
				and `verified` = 1
				and `deleted` = 0
				and `locked` = 0");

		$totassurers += $assurers = tc(
			"select count(*) as `count` from (
				select 1 from `notary`
					where `when` >= '$first' and `when` < '$next_month'
					and `method`!='Administrative Increase'
					and `deleted` = 0
					group by `to` having sum(`points`) >= 100
				) as `assurer_candidates`");

		$certs = tc(
			"select count(*) as `count` from `domaincerts`
				where `created` >= '$first' and `created` < '$next_month'
				and `expire` != 0");
		$certs += tc(
			"select count(*) as `count` from `emailcerts`
				where `created` >= '$first' and `created` < '$next_month'
				and `expire` != 0");
		$certs += tc(
			"select count(*) as `count` from `gpg`
				where `issued` >= '$first' and `issued` < '$next_month'
				and `expire` != 0");
		$certs += tc(
			"select count(*) as `count` from `orgdomaincerts`
				where `created` >= '$first' and `created` < '$next_month'
				and `expire` != 0");
		$certs += tc(
			"select count(*) as `count` from `orgemailcerts`
				where `created` >= '$first' and `created` < '$next_month'
				and `expire` != 0");
		$totalcerts += $certs;

		$tmp_arr = array();
		$tmp_arr['date'] = date("Y-m", $first_ts);
		$tmp_arr['new_users'] = number_format($users);
		$tmp_arr['new_assurers'] = number_format($assurers);
		$tmp_arr['new_certificates'] = number_format($certs);

		$stats['growth_last_12m'][] = $tmp_arr;
	}
	$stats['growth_last_12m_total'] = array(
			'new_users' => number_format($totalusers),
			'new_assurers' => number_format($totassurers),
			'new_certificates' => number_format($totalcerts),
		);

	$totalcerts = 0;
	$totalusers = 0;
	$totassurers = 0;
	for($i = date("Y"); $i >= 2002; $i--) {
		$first_ts = mktime(0, 0, 0, 1, 1, $i);
		$next_year_ts =  mktime(0, 0, 0, 1, 1, $i + 1);
		$first = date("Y-m-d", $first_ts);
		$next_year = date("Y-m-d", $next_year_ts);

		echo "Calculating statistics for year $i\n";

		$totalusers += $users = tc(
			"select count(*) as `count` from `users`
				where `created` >= '$first' and `created` < '$next_year'
				and `verified` = 1
				and `deleted` = 0
				and `locked` = 0");

		$totassurers += $assurers = tc(
			"select count(*) as `count` from (
				select 1 from `notary`
					where `when` >= '$first' and `when` < '$next_year'
					and `method`!='Administrative Increase'
					and `deleted` = 0
					group by `to` having sum(`points`) >= 100
				) as `assurer_candidates`");

		$certs = tc(
			"select count(*) as `count` from `domaincerts`
				where `created` >= '$first' and `created` < '$next_year'
				and `expire` != 0");
		$certs += tc(
			"select count(*) as `count` from `emailcerts`
				where `created` >= '$first' and `created` < '$next_year'
				and `expire` != 0");
		$certs += tc(
			"select count(*) as `count` from `gpg`
				where `issued` >= '$first' and `issued` < '$next_year'
				and `expire` != 0");
		$certs += tc(
			"select count(*) as `count` from `orgdomaincerts`
				where `created` >= '$first' and `created` < '$next_year'
				and `expire` != 0");
		$certs += tc(
			"select count(*) as `count` from `orgemailcerts`
				where `created` >= '$first' and `created` < '$next_year'
				and `expire` != 0");
		$totalcerts += $certs;

		$tmp_arr = array();
		$tmp_arr['date'] = $i;
		$tmp_arr['new_users'] = number_format($users);
		$tmp_arr['new_assurers'] = number_format($assurers);
		$tmp_arr['new_certificates'] = number_format($certs);

		$stats['growth_last_years'][] = $tmp_arr;
	}
	$stats['growth_last_years_total'] = array(
			'new_users' => number_format($totalusers),
			'new_assurers' => number_format($totassurers),
			'new_certificates' => number_format($totalcerts),
		);

	return $stats;
}


$stats = getDataFromLive();
if (! updateCache($stats) ) {
	fwrite(STDERR,
		"An error occured. The statistics were not successfully updated!");
	die(1);
}