diff options
| author | 2014-08-24 15:29:09 +0200 | |
|---|---|---|
| committer | 2014-08-24 15:29:09 +0200 | |
| commit | 07444280d1a03daa7880c1539bde3a6e80945dab (patch) | |
| tree | c328a11f59e07d12e624576ed86b11ada1b9d198 /app/Models/StatsDAO.php | |
| parent | 1d2527b8bc2a125cc8b8508402417f60d314e330 (diff) | |
| parent | d1f79fee69a3667913f419cd9726ffb11f410bd0 (diff) | |
Merge branch 'dev' into beta
Conflicts:
README.md
Diffstat (limited to 'app/Models/StatsDAO.php')
| -rw-r--r-- | app/Models/StatsDAO.php | 132 |
1 files changed, 130 insertions, 2 deletions
diff --git a/app/Models/StatsDAO.php b/app/Models/StatsDAO.php index 66f5104b3..89be76a26 100644 --- a/app/Models/StatsDAO.php +++ b/app/Models/StatsDAO.php @@ -85,9 +85,83 @@ SQL; * @return array */ protected function initEntryCountArray() { + return $this->initStatsArray(-self::ENTRY_COUNT_PERIOD, -1); + } + + /** + * Calculates the number of article per hour of the day per feed + * + * @param integer $feed id + * @return string + */ + public function calculateEntryRepartitionPerFeedPerHour($feed = null) { + return $this->calculateEntryRepartitionPerFeedPerPeriod('%H', $feed); + } + + /** + * Calculates the number of article per day of week per feed + * + * @param integer $feed id + * @return string + */ + public function calculateEntryRepartitionPerFeedPerDayOfWeek($feed = null) { + return $this->calculateEntryRepartitionPerFeedPerPeriod('%w', $feed); + } + + /** + * Calculates the number of article per month per feed + * + * @param integer $feed + * @return string + */ + public function calculateEntryRepartitionPerFeedPerMonth($feed = null) { + return $this->calculateEntryRepartitionPerFeedPerPeriod('%m', $feed); + } + + /** + * Calculates the number of article per period per feed + * + * @param string $period format string to use for grouping + * @param integer $feed id + * @return string + */ + protected function calculateEntryRepartitionPerFeedPerPeriod($period, $feed = null) { + if ($feed) { + $restrict = "WHERE e.id_feed = {$feed}"; + } else { + $restrict = ''; + } + $sql = <<<SQL +SELECT DATE_FORMAT(FROM_UNIXTIME(e.date), '{$period}') AS period +, COUNT(1) AS count +FROM {$this->prefix}entry AS e +{$restrict} +GROUP BY period +ORDER BY period ASC +SQL; + + $stm = $this->bd->prepare($sql); + $stm->execute(); + $res = $stm->fetchAll(PDO::FETCH_NAMED); + + foreach ($res as $value) { + $repartition[(int) $value['period']] = (int) $value['count']; + } + + return $this->convertToSerie($repartition); + } + + /** + * Initialize an array for statistics depending on a range + * + * @param integer $min + * @param integer $max + * @return array + */ + protected function initStatsArray($min, $max) { return array_map(function () { return 0; - }, array_flip(range(-self::ENTRY_COUNT_PERIOD, -1))); + }, array_flip(range($min, $max))); } /** @@ -170,7 +244,8 @@ SQL; */ public function calculateFeedLastDate() { $sql = <<<SQL -SELECT MAX(f.name) AS name +SELECT MAX(f.id) as id +, MAX(f.name) AS name , MAX(date) AS last_date FROM {$this->prefix}feed AS f, {$this->prefix}entry AS e @@ -204,4 +279,57 @@ SQL; return json_encode($serie); } + /** + * Gets days ready for graphs + * + * @return string + */ + public function getDays() { + return $this->convertToTranslatedJson(array( + 'sun', + 'mon', + 'tue', + 'wed', + 'thu', + 'fri', + 'sat', + )); + } + + /** + * Gets months ready for graphs + * + * @return string + */ + public function getMonths() { + return $this->convertToTranslatedJson(array( + 'jan', + 'feb', + 'mar', + 'apr', + 'may', + 'jun', + 'jul', + 'aug', + 'sep', + 'oct', + 'nov', + 'dec', + )); + } + + /** + * Translates array content and encode it as JSON + * + * @param array $data + * @return string + */ + private function convertToTranslatedJson($data = array()) { + $translated = array_map(function ($a) { + return Minz_Translate::t($a); + }, $data); + + return json_encode($translated); + } + } |
