#603: Show QoS informations
[platal.git] / modules / stats.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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'
43 => $this->make_hook('graph_evo', AUTH_COOKIE),
44 'stats/promos' => $this->make_hook('promos', AUTH_COOKIE),
45
46 'stats/coupures' => $this->make_hook('coupures', AUTH_PUBLIC),
47 );
48 }
49
50 function handler_stats(&$page)
51 {
52 $page->changeTpl('stats/index.tpl');
53 }
54
55 function handler_evolution(&$page, $jours = 365)
56 {
57 $page->changeTpl('stats/evolution_inscrits.tpl');
58 $page->assign('jours', $jours);
59 }
60
61 function handler_graph_evo(&$page, $jours = 365)
62 {
63 define('DUREEJOUR',24*3600);
64
65 //recupere le nombre d'inscriptions par jour sur la plage concernée
66 $res = XDB::iterRow(
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
71 FROM auth_user_md5
72 WHERE perms IN ('admin','user')
73 GROUP BY jour");
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
82 list($numjour, $nb) = $res->next();
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
95 //Genere le graphique à la volée avec GNUPLOT
96 header( "Content-type: image/png");
97
98 $delt = ($total - $init_nb)/10;
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 = 1920;
129
130 //recupere le nombre d'inscriptions par jour sur la plage concernée
131 $res = XDB::iterRow(
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
140 // la première ligne contient le total des inscrits avant la date de départ
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
154 //Genere le graphique à la volée avec GNUPLOT
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
167 set title "Nombre d'inscrits par promotion depuis $depart."
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);
178 $res = XDB::query("SELECT min(TO_DAYS(date_ins)-TO_DAYS(now()))
179 FROM auth_user_md5
180 WHERE promo = {?}
181 AND perms IN ('admin', 'user')",
182 $promo);
183 $jours = -$res->fetchOneCell();
184
185 //recupere le nombre d'inscriptions par jour sur la plage concernée
186 $res = XDB::iterRow(
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
191 FROM auth_user_md5
192 WHERE promo = {?} AND perms IN ('admin','user')
193 GROUP BY jour", $promo);
194
195 //genere des donnees compatibles avec GNUPLOT
196 $inscrits='';
197
198 // la première ligne contient le total des inscrits avant la date de départ (J - $jours)
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
215 //Genere le graphique à la volée avec GNUPLOT
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 {
247 $page->changeTpl('stats/nb_by_promo.tpl');
248
249 $res = XDB::iterRow(
250 "SELECT promo,COUNT(*)
251 FROM auth_user_md5
252 WHERE promo > 1900 AND perms IN ('admin','user')
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);
269 }
270
271 function handler_coupures(&$page, $cp_id = null)
272 {
273 $page->changeTpl('stats/coupure.tpl');
274
275 if (!is_null($cp_id)) {
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);
281 $cp = $res->fetchOneAssoc();
282 }
283
284 if(@$cp) {
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";
289 $sql = "SELECT id, debut, resume, services
290 FROM coupures where debut > '$beginning_date' order by debut desc";
291 $page->assign('coupures', XDB::iterator($sql));
292 $res = XDB::iterator("SELECT host, text
293 FROM mx_watch
294 WHERE state != 'ok'");
295 $page->assign('mxs', $res);
296 }
297 }
298 }
299
300 ?>