4a38a92063107698d111d3a947bd7a54bfb3cae0
[lhc/web/www.git] / www / plugins-dist / statistiques / inc / stats_visites_to_array.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2016 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13
14 if (!defined('_ECRIRE_INC_VERSION')) {
15 return;
16 }
17
18 include_spip('inc/statistiques');
19 // moyenne glissante sur 30 jours
20 define('MOYENNE_GLISSANTE_JOUR', 30);
21 // moyenne glissante sur 12 mois
22 define('MOYENNE_GLISSANTE_MOIS', 12);
23
24 function inc_stats_visites_to_array_dist($unite, $duree, $id_article, $options = array()) {
25 $now = time();
26
27 if (!in_array($unite, array('jour', 'mois'))) {
28 $unite = 'jour';
29 }
30 $serveur = '';
31
32 $table = "spip_visites";
33 $order = "date";
34 $where = array();
35 if ($duree) {
36 $where[] = sql_date_proche($order, -$duree, 'day', $serveur);
37 }
38
39 if ($id_article) {
40 $table = "spip_visites_articles";
41 $where[] = "id_article=" . intval($id_article);
42 }
43
44 $where = implode(" AND ", $where);
45 $format = ($unite == 'jour' ? '%Y-%m-%d' : '%Y-%m-01');
46
47 $res = sql_select("SUM(visites) AS v, DATE_FORMAT($order,'$format') AS d", $table, $where, "d", "d", "", '',
48 $serveur);
49
50 $format = str_replace('%', '', $format);
51 $periode = ($unite == 'jour' ? 24 * 3600 : 365 * 24 * 3600 / 12);
52 $step = intval(round($periode * 1.1, 0));
53 $glisse = constant('MOYENNE_GLISSANTE_' . strtoupper($unite));
54 moyenne_glissante();
55 $data = array();
56 $r = sql_fetch($res, $serveur);
57 if (!$r) {
58 $r = array('d' => date($format, $now), 'v' => 0);
59 }
60 do {
61 $data[$r['d']] = array('visites' => $r['v'], 'moyenne' => moyenne_glissante($r['v'], $glisse));
62 $last = $r['d'];
63
64 // donnee suivante
65 $r = sql_fetch($res, $serveur);
66 // si la derniere n'est pas la date courante, l'ajouter
67 if (!$r and $last != date($format, $now)) {
68 $r = array('d' => date($format, $now), 'v' => 0);
69 }
70
71 // completer les trous manquants si besoin
72 if ($r) {
73 $next = strtotime($last);
74 $current = strtotime($r['d']);
75 while (($next += $step) < $current and $d = date($format, $next)) {
76 if (!isset($data[$d])) {
77 $data[$d] = array('visites' => 0, 'moyenne' => moyenne_glissante(0, $glisse));
78 }
79 $last = $d;
80 $next = strtotime($last);
81 }
82 }
83 } while ($r);
84
85 // projection pour la derniere barre :
86 // mesure courante
87 // + moyenne au pro rata du temps qui reste
88 $moyenne = end($data);
89 $moyenne = prev($data);
90 $moyenne = ($moyenne and isset($moyenne['moyenne'])) ? $moyenne['moyenne'] : 0;
91 $data[$last]['moyenne'] = $moyenne;
92
93 // temps restant
94 $remaining = strtotime(date($format, strtotime(date($format, $now)) + $step)) - $now;
95
96 $prorata = $remaining / $periode;
97
98 // projection
99 $data[$last]['prevision'] = $data[$last]['visites'] + intval(round($moyenne * $prorata));
100 /*
101 * Compter les fichiers en attente de depouillement dans tmp/visites/
102 * pour affiner la prediction.
103 * A activer dans le mes_options si l'hebergement tient le coup en cas de gros pics de traffic
104 */
105 if (!$id_article and defined('_STATS_COMPTE_EN_ATTENTE') AND _STATS_COMPTE_EN_ATTENTE){
106 // eviter un depassement memoire en mesurant un echantillon pour commencer
107 $n = count(glob(_DIR_RACINE . "tmp/visites/0*"));
108 if ($n < 10000) {
109 $n = count(glob(_DIR_RACINE . "tmp/visites/*"));
110 } else {
111 $n += count(glob(_DIR_RACINE . "tmp/visites/4*"));
112 $n += count(glob(_DIR_RACINE . "tmp/visites/8*"));
113 $n += count(glob(_DIR_RACINE . "tmp/visites/c*"));
114 $n = 4 * $n;
115 }
116 $data[$last]['prevision'] += $n;
117 }
118
119 return $data;
120 }