Happy New Year!
[platal.git] / modules / stats.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
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',
25 'smtp' => 'serveur sécurisé d\'envoi de mails',
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' => $this->make_hook('graph_evo', AUTH_COOKIE),
43 'stats/promos' => $this->make_hook('promos', AUTH_COOKIE),
44
45 'stats/coupures' => $this->make_hook('coupures', AUTH_PUBLIC),
46 );
47 }
48
49 function handler_stats(&$page)
50 {
51 $page->changeTpl('stats/index.tpl');
52 }
53
54 function handler_evolution(&$page, $jours = 365)
55 {
56 $page->changeTpl('stats/evolution_inscrits.tpl');
57 $page->assign('jours', $jours);
58 }
59
60 function handler_graph_evo(&$page, $jours = 365)
61 {
62 define('DUREEJOUR', 24 * 3600);
63
64 //recupere le nombre d'inscriptions par jour sur la plage concernée
65 $res = XDB::iterRow('SELECT IF(registration_date > DATE_SUB(NOW(), INTERVAL {?} DAY),
66 TO_DAYS(registration_date) - TO_DAYS(NOW()),
67 -{?}) AS jour,
68 COUNT(a.uid) AS nb
69 FROM accounts AS a
70 LEFT JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
71 LEFT JOIN profiles AS p ON (ap.pid = p.pid)
72 WHERE state = \'active\' AND p.deathdate IS NULL
73 GROUP BY jour', (int)$jours, 1 + (int)$jours);
74
75 //genere des donnees compatibles avec GNUPLOT
76 $inscrits='';
77
78 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
79 list(,$init_nb) = $res->next();
80 $total = $init_nb;
81 $numjour = - $jours - 1;
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
94 //Genere le graphique à la volée avec GNUPLOT
95 pl_cached_dynamic_content_headers("image/png");
96
97 $delt = ($total - $init_nb)/10;
98 $delt = $delt ? $delt : 5;
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 {
126 if ($promo == 'all') {
127 // date de départ
128 $depart = 1930;
129
130 //recupere le nombre d'inscriptions par jour sur la plage concernée
131 $res = XDB::iterRow("SELECT pe.entry_year AS promo, SUM(state = 'active') / COUNT(*) * 100
132 FROM accounts AS a
133 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
134 INNER JOIN profiles AS p ON (p.pid = ap.pid)
135 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET('primary', pe.flags))
136 WHERE pe.entry_year >= {?} AND p.deathdate IS NULL
137 GROUP BY promo", $depart);
138
139 //genere des donnees compatibles avec GNUPLOT
140 $inscrits='';
141
142 // la première ligne contient le total des inscrits avant la date de départ
143 list($annee, $nb) = $res->next();
144
145 for ($i = $depart; $i <= date("Y"); $i++) {
146 if ($annee < $i) {
147 if(!list($annee, $nb) = $res->next()) {
148 $annee = 0;
149 $nb = 0;
150 }
151 }
152 if ($nb > 0 || $i < date('Y'))
153 $inscrits .= $i.' '.$nb."\n";
154 }
155
156 //Genere le graphique à la volée avec GNUPLOT
157 $fin = $i+2;
158
159 $gnuplot = <<<EOF2
160 gnuplot <<EOF
161
162 set term png small color
163 set size 640/480
164 set timefmt "%d/%m/%y"
165
166 set xr [$depart:$fin]
167 set yr [0:100]
168
169 set title "Proportion d'inscrits par promotion depuis $depart, en %."
170
171 plot "-" using 1:2 title 'inscrits' with boxes;
172 {$inscrits}
173 EOF
174 EOF2;
175
176 } else {
177 //nombre de jours sur le graph
178 $jours = 365;
179 define('DUREEJOUR', 24 * 3600);
180
181 $res = XDB::query("SELECT MIN(TO_DAYS(a.registration_date) - TO_DAYS(NOW()))
182 FROM accounts AS a
183 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
184 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET('primary', pe.flags))
185 WHERE pe.entry_year = {?} AND a.state = 'active'", (int)$promo);
186 $jours = -$res->fetchOneCell();
187
188 //recupere le nombre d'inscriptions par jour sur la plage concernée
189 $res = XDB::iterRow("SELECT IF(a.registration_date > DATE_SUB(NOW(), INTERVAL {?} DAY),
190 TO_DAYS(a.registration_date) - TO_DAYS(NOW()),
191 -{?}) AS jour,
192 COUNT(a.uid) AS nb
193 FROM accounts AS a
194 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET('owner', ap.perms))
195 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET('primary', pe.flags))
196 WHERE pe.entry_year = {?} AND a.state = 'active'
197 GROUP BY jour", (int)$jours, 1 + (int)$jours, (int)$promo);
198
199 //genere des donnees compatibles avec GNUPLOT
200 $inscrits='';
201
202 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
203 list(,$init_nb) = $res->next();
204 $total = $init_nb;
205
206 list($numjour, $nb) = $res->next();
207
208 for ($i = -$jours;$i<=0;$i++) {
209 if ($numjour<$i) {
210 if(!list($numjour, $nb) = $res->next()) {
211 $numjour = 0;
212 $nb = 0;
213 }
214 }
215 if ($numjour==$i) $total+=$nb;
216 $inscrits .= date('d/m/y',$i*DUREEJOUR+time())." ".$total."\n";
217 }
218
219 //Genere le graphique à la volée avec GNUPLOT
220 $delt = ($total - $init_nb) / 10;
221 $delt += ($delt < 1);
222 $ymin = round($init_nb - $delt,0);
223 $ymax = round($total + $delt,0);
224
225 $gnuplot = <<<EOF2
226 gnuplot <<EOF
227
228 set term png small color
229 set size 640/480
230 set xdata time
231 set timefmt "%d/%m/%y"
232
233 set format x "%m/%y"
234 set yr [$ymin:$ymax]
235
236 set title "Nombre d'inscrits de la promotion $promo."
237
238 plot "-" using 1:2 title 'inscrits' with lines;
239 {$inscrits}e
240 EOF
241 EOF2;
242 }
243
244 pl_cached_dynamic_content_headers("image/png");
245 passthru($gnuplot);
246 exit;
247 }
248
249 function handler_promos(&$page, $promo = null)
250 {
251 $page->changeTpl('stats/nb_by_promo.tpl');
252
253 $res = XDB::iterRow('SELECT pe.entry_year AS promo, COUNT(*)
254 FROM accounts AS a
255 INNER JOIN account_profiles AS ap ON (ap.uid = a.uid AND FIND_IN_SET(\'owner\', ap.perms))
256 INNER JOIN profile_education AS pe ON (pe.pid = ap.pid AND FIND_IN_SET(\'primary\', pe.flags))
257 WHERE pe.entry_year >= 1900 AND a.state = \'active\'
258 GROUP BY promo
259 ORDER BY promo');
260 $max=0; $min=3000;
261
262 while (list($p,$nb) = $res->next()) {
263 $p = intval($p);
264 if(!isset($nbpromo[$p/10])) {
265 $nbpromo[$p/10] = Array('','','','','','','','','',''); // tableau de 10 cases vides
266 }
267 $nbpromo[$p/10][$p%10]=Array('promo' => $p, 'nb' => $nb);
268 }
269
270 $page->assign_by_ref('nbs', $nbpromo);
271 $page->assign('min', $min-$min % 10);
272 $page->assign('max', $max+10-$max%10);
273 $page->assign('promo', $promo);
274 }
275
276 function handler_coupures(&$page, $cp_id = null)
277 {
278 $page->changeTpl('stats/coupure.tpl');
279
280 if (!is_null($cp_id)) {
281 $res = XDB::query("SELECT debut,
282 TIME_FORMAT(duree,'%kh%i') AS duree,
283 resume, description, services
284 FROM downtimes
285 WHERE id = {?}", $cp_id);
286 $cp = $res->fetchOneAssoc();
287 }
288
289 if(@$cp) {
290 $cp['lg_services'] = serv_to_str($cp['services']);
291 $page->assign_by_ref('cp',$cp);
292 } else {
293 $beginning_date = date("Ymd", time() - 3600*24*21) . "000000";
294 $sql = "SELECT id, debut, resume, services
295 FROM downtimes where debut > '$beginning_date' order by debut desc";
296 $page->assign('coupures', XDB::iterator($sql));
297 $res = XDB::iterator("SELECT host, text
298 FROM mx_watch
299 WHERE state != 'ok'");
300 $page->assign('mxs', $res);
301 }
302 }
303 }
304
305 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
306 ?>