Commit | Line | Data |
---|---|---|
bf2692e3 | 1 | <?php |
2 | /*************************************************************************** | |
8d84c630 | 3 | * Copyright (C) 2003-2009 Polytechnique.org * |
bf2692e3 | 4 | * http://opensource.polytechnique.org/ * |
5 | * * | |
6 | * This program is free software; you can redistribute it and/or modify * | |
7 | * it under the terms of the GNU General Public License as published by * | |
8 | * the Free Software Foundation; either version 2 of the License, or * | |
9 | * (at your option) any later version. * | |
10 | * * | |
11 | * This program is distributed in the hope that it will be useful, * | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | |
14 | * GNU General Public License for more details. * | |
15 | * * | |
16 | * You should have received a copy of the GNU General Public License * | |
17 | * along with this program; if not, write to the Free Software * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
22 | function serv_to_str($params) { | |
23 | $flags = explode(',',$params); | |
24 | $trad = Array('web' => 'site web', 'mail'=> 'redirection mail', | |
a7de4ef7 | 25 | 'smtp' => 'serveur sécurisé d\'envoi de mails', |
bf2692e3 | 26 | 'nntp' => 'serveur des forums de discussion'); |
27 | $ret = Array(); | |
28 | foreach ($flags as $flag) { | |
29 | $ret[] = $trad[$flag]; | |
30 | } | |
31 | return implode(', ',$ret); | |
32 | } | |
33 | ||
34 | class StatsModule extends PLModule | |
35 | { | |
36 | function handlers() | |
37 | { | |
38 | return array( | |
eb5a266d SJ |
39 | 'stats' => $this->make_hook('stats', AUTH_COOKIE), |
40 | 'stats/evolution' => $this->make_hook('evolution', AUTH_COOKIE), | |
41 | 'stats/graph' => $this->make_hook('graph', AUTH_COOKIE), | |
42 | 'stats/graph/evolution' => $this->make_hook('graph_evo', AUTH_COOKIE), | |
43 | 'stats/promos' => $this->make_hook('promos', AUTH_COOKIE), | |
44 | 'stats/profile' => $this->make_hook('profile', AUTH_COOKIE), | |
bf2692e3 | 45 | |
eb5a266d | 46 | 'stats/coupures' => $this->make_hook('coupures', AUTH_PUBLIC), |
bf2692e3 | 47 | ); |
48 | } | |
49 | ||
50 | function handler_stats(&$page) | |
51 | { | |
52 | $page->changeTpl('stats/index.tpl'); | |
bf2692e3 | 53 | } |
54 | ||
55 | function handler_evolution(&$page, $jours = 365) | |
56 | { | |
57 | $page->changeTpl('stats/evolution_inscrits.tpl'); | |
58 | $page->assign('jours', $jours); | |
bf2692e3 | 59 | } |
60 | ||
61 | function handler_graph_evo(&$page, $jours = 365) | |
62 | { | |
bf2692e3 | 63 | define('DUREEJOUR',24*3600); |
64 | ||
a7de4ef7 | 65 | //recupere le nombre d'inscriptions par jour sur la plage concernée |
08cce2ff | 66 | $res = XDB::iterRow( |
bf2692e3 | 67 | "SELECT IF( date_ins>DATE_SUB(NOW(),INTERVAL $jours DAY), |
68 | TO_DAYS(date_ins)-TO_DAYS(NOW()), | |
69 | ".(-($jours+1)).") AS jour, | |
70 | COUNT(user_id) AS nb | |
eaf30d86 | 71 | FROM auth_user_md5 |
99592c92 | 72 | WHERE perms IN ('admin','user') AND deces = 0 |
bf2692e3 | 73 | GROUP BY jour"); |
74 | ||
75 | //genere des donnees compatibles avec GNUPLOT | |
76 | $inscrits=''; | |
77 | ||
a7de4ef7 | 78 | // la première ligne contient le total des inscrits avant la date de départ (J - $jours) |
bf2692e3 | 79 | list(,$init_nb) = $res->next(); |
0521940c | 80 | $total = $init_nb; |
81 | $numjour = - $jours - 1; | |
bf2692e3 | 82 | |
83 | for ($i = -$jours; $i<=0; $i++) { | |
84 | if ($numjour<$i) { | |
85 | if(!list($numjour, $nb) = $res->next()) { | |
86 | $numjour = 0; | |
87 | $nb = 0; | |
88 | } | |
89 | } | |
90 | if ($numjour==$i) $total+=$nb; | |
91 | $inscrits .= date('d/m/y',$i*DUREEJOUR+time())." ".$total."\n"; | |
92 | } | |
93 | ||
a7de4ef7 | 94 | //Genere le graphique à la volée avec GNUPLOT |
bf2692e3 | 95 | header( "Content-type: image/png"); |
96 | ||
97 | $delt = ($total - $init_nb)/10; | |
0521940c | 98 | $delt = $delt ? $delt : 5; |
bf2692e3 | 99 | $ymin = round($init_nb - $delt,0); |
100 | $ymax = round($total + $delt,0); | |
101 | ||
102 | $gnuplot = <<<EOF2 | |
103 | gnuplot <<EOF | |
104 | ||
105 | set term png small color | |
106 | set size 640/480 | |
107 | set xdata time | |
108 | set timefmt "%d/%m/%y" | |
109 | ||
110 | set format x "%m/%y" | |
111 | set yr [$ymin:$ymax] | |
112 | ||
113 | set title "Nombre d'inscrits" | |
114 | ||
115 | plot "-" using 1:2 title 'inscrits' with lines; | |
116 | {$inscrits} | |
117 | EOF | |
118 | EOF2; | |
119 | ||
120 | passthru($gnuplot); | |
121 | exit; | |
122 | } | |
123 | ||
124 | function handler_graph(&$page, $promo = null) | |
125 | { | |
bf2692e3 | 126 | if ($promo == 'all') { |
a7de4ef7 | 127 | // date de départ |
203bc766 | 128 | $depart = 1930; |
bf2692e3 | 129 | |
a7de4ef7 | 130 | //recupere le nombre d'inscriptions par jour sur la plage concernée |
08cce2ff | 131 | $res = XDB::iterRow( |
bf2692e3 | 132 | "SELECT promo, SUM(perms IN ('admin', 'user')) / COUNT(*) * 100 |
133 | FROM auth_user_md5 | |
134 | WHERE promo >= $depart AND deces = 0 | |
135 | GROUP BY promo"); | |
136 | ||
137 | //genere des donnees compatibles avec GNUPLOT | |
138 | $inscrits=''; | |
139 | ||
a7de4ef7 | 140 | // la première ligne contient le total des inscrits avant la date de départ |
bf2692e3 | 141 | list($annee, $nb) = $res->next(); |
142 | ||
143 | for ($i = $depart; $i <= date("Y"); $i++) { | |
144 | if ($annee < $i) { | |
145 | if(!list($annee, $nb) = $res->next()) { | |
146 | $annee = 0; | |
147 | $nb = 0; | |
148 | } | |
149 | } | |
150 | if ($nb > 0 || $i < date('Y')) | |
151 | $inscrits .= $i.' '.$nb."\n"; | |
152 | } | |
153 | ||
a7de4ef7 | 154 | //Genere le graphique à la volée avec GNUPLOT |
bf2692e3 | 155 | $fin = $i+2; |
156 | ||
157 | $gnuplot = <<<EOF2 | |
158 | gnuplot <<EOF | |
159 | ||
160 | set term png small color | |
161 | set size 640/480 | |
162 | set timefmt "%d/%m/%y" | |
163 | ||
164 | set xr [$depart:$fin] | |
165 | set yr [0:100] | |
166 | ||
203bc766 | 167 | set title "Proportion d'inscrits par promotion depuis $depart, en %." |
bf2692e3 | 168 | |
169 | plot "-" using 1:2 title 'inscrits' with boxes; | |
170 | {$inscrits} | |
171 | EOF | |
172 | EOF2; | |
173 | ||
174 | } else { | |
175 | //nombre de jours sur le graph | |
176 | $jours = 365; | |
177 | define('DUREEJOUR',24*3600); | |
99592c92 VZ |
178 | $res = XDB::query( |
179 | "SELECT min(TO_DAYS(date_ins)-TO_DAYS(now())) | |
180 | FROM auth_user_md5 | |
181 | WHERE promo = {?} AND perms IN ('admin', 'user') AND deces = 0", | |
182 | $promo); | |
bf2692e3 | 183 | $jours = -$res->fetchOneCell(); |
184 | ||
a7de4ef7 | 185 | //recupere le nombre d'inscriptions par jour sur la plage concernée |
08cce2ff | 186 | $res = XDB::iterRow( |
bf2692e3 | 187 | "SELECT IF( date_ins>DATE_SUB(NOW(),INTERVAL $jours DAY), |
188 | TO_DAYS(date_ins)-TO_DAYS(NOW()), | |
189 | ".(-($jours+1)).") AS jour, | |
190 | COUNT(user_id) AS nb | |
eaf30d86 | 191 | FROM auth_user_md5 |
99592c92 | 192 | WHERE promo = {?} AND perms IN ('admin','user') AND deces = 0 |
bf2692e3 | 193 | GROUP BY jour", $promo); |
194 | ||
195 | //genere des donnees compatibles avec GNUPLOT | |
196 | $inscrits=''; | |
197 | ||
a7de4ef7 | 198 | // la première ligne contient le total des inscrits avant la date de départ (J - $jours) |
bf2692e3 | 199 | list(,$init_nb) = $res->next(); |
200 | $total = $init_nb; | |
201 | ||
202 | list($numjour, $nb) = $res->next(); | |
203 | ||
204 | for ($i = -$jours;$i<=0;$i++) { | |
205 | if ($numjour<$i) { | |
206 | if(!list($numjour, $nb) = $res->next()) { | |
207 | $numjour = 0; | |
208 | $nb = 0; | |
209 | } | |
210 | } | |
211 | if ($numjour==$i) $total+=$nb; | |
212 | $inscrits .= date('d/m/y',$i*DUREEJOUR+time())." ".$total."\n"; | |
213 | } | |
214 | ||
a7de4ef7 | 215 | //Genere le graphique à la volée avec GNUPLOT |
bf2692e3 | 216 | $delt = ($total - $init_nb) / 10; |
217 | $delt += ($delt < 1); | |
218 | $ymin = round($init_nb - $delt,0); | |
219 | $ymax = round($total + $delt,0); | |
220 | ||
221 | $gnuplot = <<<EOF2 | |
222 | gnuplot <<EOF | |
223 | ||
224 | set term png small color | |
225 | set size 640/480 | |
226 | set xdata time | |
227 | set timefmt "%d/%m/%y" | |
228 | ||
229 | set format x "%m/%y" | |
230 | set yr [$ymin:$ymax] | |
231 | ||
232 | set title "Nombre d'inscrits de la promotion $promo." | |
233 | ||
234 | plot "-" using 1:2 title 'inscrits' with lines; | |
235 | {$inscrits}e | |
236 | EOF | |
237 | EOF2; | |
238 | } | |
239 | ||
240 | header('Content-type: image/png'); | |
241 | passthru($gnuplot); | |
242 | exit; | |
243 | } | |
244 | ||
245 | function handler_promos(&$page, $promo = null) | |
246 | { | |
bf2692e3 | 247 | $page->changeTpl('stats/nb_by_promo.tpl'); |
248 | ||
08cce2ff | 249 | $res = XDB::iterRow( |
bf2692e3 | 250 | "SELECT promo,COUNT(*) |
251 | FROM auth_user_md5 | |
99592c92 | 252 | WHERE promo > 1900 AND perms IN ('admin','user') AND deces = 0 |
bf2692e3 | 253 | GROUP BY promo |
254 | ORDER BY promo"); | |
255 | $max=0; $min=3000; | |
256 | ||
257 | while (list($p,$nb) = $res->next()) { | |
258 | $p = intval($p); | |
259 | if(!isset($nbpromo[$p/10])) { | |
260 | $nbpromo[$p/10] = Array('','','','','','','','','',''); // tableau de 10 cases vides | |
261 | } | |
262 | $nbpromo[$p/10][$p%10]=Array('promo' => $p, 'nb' => $nb); | |
263 | } | |
264 | ||
265 | $page->assign_by_ref('nbs', $nbpromo); | |
266 | $page->assign('min', $min-$min % 10); | |
267 | $page->assign('max', $max+10-$max%10); | |
268 | $page->assign('promo', $promo); | |
bf2692e3 | 269 | } |
270 | ||
271 | function handler_coupures(&$page, $cp_id = null) | |
272 | { | |
bf2692e3 | 273 | $page->changeTpl('stats/coupure.tpl'); |
274 | ||
275 | if (!is_null($cp_id)) { | |
da398501 | 276 | $res = XDB::query("SELECT debut, |
277 | TIME_FORMAT(duree,'%kh%i') AS duree, | |
278 | resume, description, services | |
279 | FROM coupures | |
280 | WHERE id = {?}", $cp_id); | |
bf2692e3 | 281 | $cp = $res->fetchOneAssoc(); |
282 | } | |
283 | ||
ccdbc270 | 284 | if(@$cp) { |
bf2692e3 | 285 | $cp['lg_services'] = serv_to_str($cp['services']); |
286 | $page->assign_by_ref('cp',$cp); | |
287 | } else { | |
288 | $beginning_date = date("Ymd", time() - 3600*24*21) . "000000"; | |
da398501 | 289 | $sql = "SELECT id, debut, resume, services |
bf2692e3 | 290 | FROM coupures where debut > '$beginning_date' order by debut desc"; |
08cce2ff | 291 | $page->assign('coupures', XDB::iterator($sql)); |
ccdbc270 | 292 | $res = XDB::iterator("SELECT host, text |
293 | FROM mx_watch | |
294 | WHERE state != 'ok'"); | |
295 | $page->assign('mxs', $res); | |
bf2692e3 | 296 | } |
bf2692e3 | 297 | } |
b0239324 FB |
298 | |
299 | function handler_profile(&$page, $period = 'overall') | |
300 | { | |
301 | $page->changeTpl('stats/profile.tpl'); | |
302 | ||
303 | $time = ''; | |
304 | switch ($period) { | |
305 | case 'week': case 'month': case 'year': | |
306 | $time = ' AND e.stamp > DATE_SUB(CURDATE(), INTERVAL 1 ' . strtoupper($period) . ')'; | |
307 | break; | |
308 | } | |
309 | $rows = XDB::iterator("SELECT IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom, | |
310 | u.prenom, u.promo, e.data AS forlife, COUNT(*) AS count | |
311 | FROM logger.events AS e | |
312 | INNER JOIN logger.actions AS act ON (e.action = act.id) | |
313 | INNER JOIN aliases AS a ON (a.alias = e.data) | |
314 | INNER JOIN auth_user_md5 AS u ON (u.user_id = a.id) | |
315 | WHERE act.text = 'view_profile' $time | |
316 | GROUP BY e.data | |
317 | ORDER BY count DESC | |
318 | LIMIT 10"); | |
319 | $page->assign('profiles', $rows); | |
320 | $page->assign('period', $period); | |
321 | } | |
bf2692e3 | 322 | } |
323 | ||
a7de4ef7 | 324 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
bf2692e3 | 325 | ?> |