Commit | Line | Data |
---|---|---|
bf2692e3 | 1 | <?php |
2 | /*************************************************************************** | |
179afa7f | 3 | * Copyright (C) 2003-2008 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( | |
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' | |
43 | => $this->make_hook('graph_evo', AUTH_COOKIE), | |
44 | 'stats/promos' => $this->make_hook('promos', AUTH_COOKIE), | |
b0239324 | 45 | 'stats/profile' => $this->make_hook('profile', AUTH_COOKIE), |
bf2692e3 | 46 | |
47 | 'stats/coupures' => $this->make_hook('coupures', AUTH_PUBLIC), | |
48 | ); | |
49 | } | |
50 | ||
51 | function handler_stats(&$page) | |
52 | { | |
53 | $page->changeTpl('stats/index.tpl'); | |
bf2692e3 | 54 | } |
55 | ||
56 | function handler_evolution(&$page, $jours = 365) | |
57 | { | |
58 | $page->changeTpl('stats/evolution_inscrits.tpl'); | |
59 | $page->assign('jours', $jours); | |
bf2692e3 | 60 | } |
61 | ||
62 | function handler_graph_evo(&$page, $jours = 365) | |
63 | { | |
bf2692e3 | 64 | define('DUREEJOUR',24*3600); |
65 | ||
a7de4ef7 | 66 | //recupere le nombre d'inscriptions par jour sur la plage concernée |
08cce2ff | 67 | $res = XDB::iterRow( |
bf2692e3 | 68 | "SELECT IF( date_ins>DATE_SUB(NOW(),INTERVAL $jours DAY), |
69 | TO_DAYS(date_ins)-TO_DAYS(NOW()), | |
70 | ".(-($jours+1)).") AS jour, | |
71 | COUNT(user_id) AS nb | |
eaf30d86 | 72 | FROM auth_user_md5 |
99592c92 | 73 | WHERE perms IN ('admin','user') AND deces = 0 |
bf2692e3 | 74 | GROUP BY jour"); |
75 | ||
76 | //genere des donnees compatibles avec GNUPLOT | |
77 | $inscrits=''; | |
78 | ||
a7de4ef7 | 79 | // la première ligne contient le total des inscrits avant la date de départ (J - $jours) |
bf2692e3 | 80 | list(,$init_nb) = $res->next(); |
0521940c | 81 | $total = $init_nb; |
82 | $numjour = - $jours - 1; | |
bf2692e3 | 83 | |
84 | for ($i = -$jours; $i<=0; $i++) { | |
85 | if ($numjour<$i) { | |
86 | if(!list($numjour, $nb) = $res->next()) { | |
87 | $numjour = 0; | |
88 | $nb = 0; | |
89 | } | |
90 | } | |
91 | if ($numjour==$i) $total+=$nb; | |
92 | $inscrits .= date('d/m/y',$i*DUREEJOUR+time())." ".$total."\n"; | |
93 | } | |
94 | ||
a7de4ef7 | 95 | //Genere le graphique à la volée avec GNUPLOT |
bf2692e3 | 96 | header( "Content-type: image/png"); |
97 | ||
98 | $delt = ($total - $init_nb)/10; | |
0521940c | 99 | $delt = $delt ? $delt : 5; |
bf2692e3 | 100 | $ymin = round($init_nb - $delt,0); |
101 | $ymax = round($total + $delt,0); | |
102 | ||
103 | $gnuplot = <<<EOF2 | |
104 | gnuplot <<EOF | |
105 | ||
106 | set term png small color | |
107 | set size 640/480 | |
108 | set xdata time | |
109 | set timefmt "%d/%m/%y" | |
110 | ||
111 | set format x "%m/%y" | |
112 | set yr [$ymin:$ymax] | |
113 | ||
114 | set title "Nombre d'inscrits" | |
115 | ||
116 | plot "-" using 1:2 title 'inscrits' with lines; | |
117 | {$inscrits} | |
118 | EOF | |
119 | EOF2; | |
120 | ||
121 | passthru($gnuplot); | |
122 | exit; | |
123 | } | |
124 | ||
125 | function handler_graph(&$page, $promo = null) | |
126 | { | |
bf2692e3 | 127 | if ($promo == 'all') { |
a7de4ef7 | 128 | // date de départ |
203bc766 | 129 | $depart = 1930; |
bf2692e3 | 130 | |
a7de4ef7 | 131 | //recupere le nombre d'inscriptions par jour sur la plage concernée |
08cce2ff | 132 | $res = XDB::iterRow( |
bf2692e3 | 133 | "SELECT promo, SUM(perms IN ('admin', 'user')) / COUNT(*) * 100 |
134 | FROM auth_user_md5 | |
135 | WHERE promo >= $depart AND deces = 0 | |
136 | GROUP BY promo"); | |
137 | ||
138 | //genere des donnees compatibles avec GNUPLOT | |
139 | $inscrits=''; | |
140 | ||
a7de4ef7 | 141 | // la première ligne contient le total des inscrits avant la date de départ |
bf2692e3 | 142 | list($annee, $nb) = $res->next(); |
143 | ||
144 | for ($i = $depart; $i <= date("Y"); $i++) { | |
145 | if ($annee < $i) { | |
146 | if(!list($annee, $nb) = $res->next()) { | |
147 | $annee = 0; | |
148 | $nb = 0; | |
149 | } | |
150 | } | |
151 | if ($nb > 0 || $i < date('Y')) | |
152 | $inscrits .= $i.' '.$nb."\n"; | |
153 | } | |
154 | ||
a7de4ef7 | 155 | //Genere le graphique à la volée avec GNUPLOT |
bf2692e3 | 156 | $fin = $i+2; |
157 | ||
158 | $gnuplot = <<<EOF2 | |
159 | gnuplot <<EOF | |
160 | ||
161 | set term png small color | |
162 | set size 640/480 | |
163 | set timefmt "%d/%m/%y" | |
164 | ||
165 | set xr [$depart:$fin] | |
166 | set yr [0:100] | |
167 | ||
203bc766 | 168 | set title "Proportion d'inscrits par promotion depuis $depart, en %." |
bf2692e3 | 169 | |
170 | plot "-" using 1:2 title 'inscrits' with boxes; | |
171 | {$inscrits} | |
172 | EOF | |
173 | EOF2; | |
174 | ||
175 | } else { | |
176 | //nombre de jours sur le graph | |
177 | $jours = 365; | |
178 | define('DUREEJOUR',24*3600); | |
99592c92 VZ |
179 | $res = XDB::query( |
180 | "SELECT min(TO_DAYS(date_ins)-TO_DAYS(now())) | |
181 | FROM auth_user_md5 | |
182 | WHERE promo = {?} AND perms IN ('admin', 'user') AND deces = 0", | |
183 | $promo); | |
bf2692e3 | 184 | $jours = -$res->fetchOneCell(); |
185 | ||
a7de4ef7 | 186 | //recupere le nombre d'inscriptions par jour sur la plage concernée |
08cce2ff | 187 | $res = XDB::iterRow( |
bf2692e3 | 188 | "SELECT IF( date_ins>DATE_SUB(NOW(),INTERVAL $jours DAY), |
189 | TO_DAYS(date_ins)-TO_DAYS(NOW()), | |
190 | ".(-($jours+1)).") AS jour, | |
191 | COUNT(user_id) AS nb | |
eaf30d86 | 192 | FROM auth_user_md5 |
99592c92 | 193 | WHERE promo = {?} AND perms IN ('admin','user') AND deces = 0 |
bf2692e3 | 194 | GROUP BY jour", $promo); |
195 | ||
196 | //genere des donnees compatibles avec GNUPLOT | |
197 | $inscrits=''; | |
198 | ||
a7de4ef7 | 199 | // la première ligne contient le total des inscrits avant la date de départ (J - $jours) |
bf2692e3 | 200 | list(,$init_nb) = $res->next(); |
201 | $total = $init_nb; | |
202 | ||
203 | list($numjour, $nb) = $res->next(); | |
204 | ||
205 | for ($i = -$jours;$i<=0;$i++) { | |
206 | if ($numjour<$i) { | |
207 | if(!list($numjour, $nb) = $res->next()) { | |
208 | $numjour = 0; | |
209 | $nb = 0; | |
210 | } | |
211 | } | |
212 | if ($numjour==$i) $total+=$nb; | |
213 | $inscrits .= date('d/m/y',$i*DUREEJOUR+time())." ".$total."\n"; | |
214 | } | |
215 | ||
a7de4ef7 | 216 | //Genere le graphique à la volée avec GNUPLOT |
bf2692e3 | 217 | $delt = ($total - $init_nb) / 10; |
218 | $delt += ($delt < 1); | |
219 | $ymin = round($init_nb - $delt,0); | |
220 | $ymax = round($total + $delt,0); | |
221 | ||
222 | $gnuplot = <<<EOF2 | |
223 | gnuplot <<EOF | |
224 | ||
225 | set term png small color | |
226 | set size 640/480 | |
227 | set xdata time | |
228 | set timefmt "%d/%m/%y" | |
229 | ||
230 | set format x "%m/%y" | |
231 | set yr [$ymin:$ymax] | |
232 | ||
233 | set title "Nombre d'inscrits de la promotion $promo." | |
234 | ||
235 | plot "-" using 1:2 title 'inscrits' with lines; | |
236 | {$inscrits}e | |
237 | EOF | |
238 | EOF2; | |
239 | } | |
240 | ||
241 | header('Content-type: image/png'); | |
242 | passthru($gnuplot); | |
243 | exit; | |
244 | } | |
245 | ||
246 | function handler_promos(&$page, $promo = null) | |
247 | { | |
bf2692e3 | 248 | $page->changeTpl('stats/nb_by_promo.tpl'); |
249 | ||
08cce2ff | 250 | $res = XDB::iterRow( |
bf2692e3 | 251 | "SELECT promo,COUNT(*) |
252 | FROM auth_user_md5 | |
99592c92 | 253 | WHERE promo > 1900 AND perms IN ('admin','user') AND deces = 0 |
bf2692e3 | 254 | GROUP BY promo |
255 | ORDER BY promo"); | |
256 | $max=0; $min=3000; | |
257 | ||
258 | while (list($p,$nb) = $res->next()) { | |
259 | $p = intval($p); | |
260 | if(!isset($nbpromo[$p/10])) { | |
261 | $nbpromo[$p/10] = Array('','','','','','','','','',''); // tableau de 10 cases vides | |
262 | } | |
263 | $nbpromo[$p/10][$p%10]=Array('promo' => $p, 'nb' => $nb); | |
264 | } | |
265 | ||
266 | $page->assign_by_ref('nbs', $nbpromo); | |
267 | $page->assign('min', $min-$min % 10); | |
268 | $page->assign('max', $max+10-$max%10); | |
269 | $page->assign('promo', $promo); | |
bf2692e3 | 270 | } |
271 | ||
272 | function handler_coupures(&$page, $cp_id = null) | |
273 | { | |
bf2692e3 | 274 | $page->changeTpl('stats/coupure.tpl'); |
275 | ||
276 | if (!is_null($cp_id)) { | |
da398501 | 277 | $res = XDB::query("SELECT debut, |
278 | TIME_FORMAT(duree,'%kh%i') AS duree, | |
279 | resume, description, services | |
280 | FROM coupures | |
281 | WHERE id = {?}", $cp_id); | |
bf2692e3 | 282 | $cp = $res->fetchOneAssoc(); |
283 | } | |
284 | ||
ccdbc270 | 285 | if(@$cp) { |
bf2692e3 | 286 | $cp['lg_services'] = serv_to_str($cp['services']); |
287 | $page->assign_by_ref('cp',$cp); | |
288 | } else { | |
289 | $beginning_date = date("Ymd", time() - 3600*24*21) . "000000"; | |
da398501 | 290 | $sql = "SELECT id, debut, resume, services |
bf2692e3 | 291 | FROM coupures where debut > '$beginning_date' order by debut desc"; |
08cce2ff | 292 | $page->assign('coupures', XDB::iterator($sql)); |
ccdbc270 | 293 | $res = XDB::iterator("SELECT host, text |
294 | FROM mx_watch | |
295 | WHERE state != 'ok'"); | |
296 | $page->assign('mxs', $res); | |
bf2692e3 | 297 | } |
bf2692e3 | 298 | } |
b0239324 FB |
299 | |
300 | function handler_profile(&$page, $period = 'overall') | |
301 | { | |
302 | $page->changeTpl('stats/profile.tpl'); | |
303 | ||
304 | $time = ''; | |
305 | switch ($period) { | |
306 | case 'week': case 'month': case 'year': | |
307 | $time = ' AND e.stamp > DATE_SUB(CURDATE(), INTERVAL 1 ' . strtoupper($period) . ')'; | |
308 | break; | |
309 | } | |
310 | $rows = XDB::iterator("SELECT IF(u.nom_usage != '', u.nom_usage, u.nom) AS nom, | |
311 | u.prenom, u.promo, e.data AS forlife, COUNT(*) AS count | |
312 | FROM logger.events AS e | |
313 | INNER JOIN logger.actions AS act ON (e.action = act.id) | |
314 | INNER JOIN aliases AS a ON (a.alias = e.data) | |
315 | INNER JOIN auth_user_md5 AS u ON (u.user_id = a.id) | |
316 | WHERE act.text = 'view_profile' $time | |
317 | GROUP BY e.data | |
318 | ORDER BY count DESC | |
319 | LIMIT 10"); | |
320 | $page->assign('profiles', $rows); | |
321 | $page->assign('period', $period); | |
322 | } | |
bf2692e3 | 323 | } |
324 | ||
a7de4ef7 | 325 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
bf2692e3 | 326 | ?> |