| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- <?php
- class FreshRSS_StatsDAO extends Minz_ModelPdo {
- const ENTRY_COUNT_PERIOD = 30;
- /**
- * Calculates entry repartition for all feeds and for main stream.
- * The repartition includes:
- * - total entries
- * - read entries
- * - unread entries
- * - favorite entries
- *
- * @return type
- */
- public function calculateEntryRepartition() {
- $repartition = array();
- // Generates the repartition for the main stream of entry
- $sql = <<<SQL
- SELECT COUNT(1) AS `total`,
- COUNT(1) - SUM(e.is_read) AS `unread`,
- SUM(e.is_read) AS `read`,
- SUM(e.is_favorite) AS `favorite`
- FROM {$this->prefix}entry AS e
- , {$this->prefix}feed AS f
- WHERE e.id_feed = f.id
- AND f.priority = 10
- SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
- $res = $stm->fetchAll(PDO::FETCH_ASSOC);
- $repartition['main_stream'] = $res[0];
- // Generates the repartition for all entries
- $sql = <<<SQL
- SELECT COUNT(1) AS `total`,
- COUNT(1) - SUM(e.is_read) AS `unread`,
- SUM(e.is_read) AS `read`,
- SUM(e.is_favorite) AS `favorite`
- FROM {$this->prefix}entry AS e
- SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
- $res = $stm->fetchAll(PDO::FETCH_ASSOC);
- $repartition['all_feeds'] = $res[0];
- return $repartition;
- }
- /**
- * Calculates entry count per day on a 30 days period.
- * Returns the result as a JSON string.
- *
- * @return string
- */
- public function calculateEntryCount() {
- $count = $this->initEntryCountArray();
- $period = self::ENTRY_COUNT_PERIOD;
- // Get stats per day for the last 30 days
- $sql = <<<SQL
- SELECT DATEDIFF(FROM_UNIXTIME(e.date), NOW()) AS day,
- COUNT(1) AS count
- FROM {$this->prefix}entry AS e
- WHERE FROM_UNIXTIME(e.date, '%Y%m%d') BETWEEN DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -{$period} DAY), '%Y%m%d') AND DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY), '%Y%m%d')
- GROUP BY day
- ORDER BY day ASC
- SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
- $res = $stm->fetchAll(PDO::FETCH_ASSOC);
- foreach ($res as $value) {
- $count[$value['day']] = (int) $value['count'];
- }
- return $this->convertToSerie($count);
- }
- /**
- * Calculates entry average per day on a 30 days period.
- *
- * @return integer
- */
- public function calculateEntryAverage() {
- $period = self::ENTRY_COUNT_PERIOD;
- // Get stats per day for the last 30 days
- $sql = <<<SQL
- SELECT COUNT(1) / {$period} AS average
- FROM {$this->prefix}entry AS e
- WHERE FROM_UNIXTIME(e.date, '%Y%m%d') BETWEEN DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -{$period} DAY), '%Y%m%d') AND DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY), '%Y%m%d')
- SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
- $res = $stm->fetch(PDO::FETCH_NAMED);
- return round($res['average'], 2);
- }
- /**
- * Initialize an array for the entry count.
- *
- * @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);
- }
- /**
- * Calculates the average number of article per hour per feed
- *
- * @param integer $feed id
- * @return integer
- */
- public function calculateEntryAveragePerFeedPerHour($feed = null) {
- return $this->calculateEntryAveragePerFeedPerPeriod(1/24, $feed);
- }
- /**
- * Calculates the average number of article per day of week per feed
- *
- * @param integer $feed id
- * @return integer
- */
- public function calculateEntryAveragePerFeedPerDayOfWeek($feed = null) {
- return $this->calculateEntryAveragePerFeedPerPeriod(7, $feed);
- }
- /**
- * Calculates the average number of article per month per feed
- *
- * @param integer $feed id
- * @return integer
- */
- public function calculateEntryAveragePerFeedPerMonth($feed = null) {
- return $this->calculateEntryAveragePerFeedPerPeriod(30, $feed);
- }
- /**
- * Calculates the average number of article per feed
- *
- * @param float $period number used to divide the number of day in the period
- * @param integer $feed id
- * @return integer
- */
- protected function calculateEntryAveragePerFeedPerPeriod($period, $feed = null) {
- if ($feed) {
- $restrict = "WHERE e.id_feed = {$feed}";
- } else {
- $restrict = '';
- }
- $sql = <<<SQL
- SELECT COUNT(1) AS count
- , MIN(date) AS date_min
- , MAX(date) AS date_max
- FROM {$this->prefix}entry AS e
- {$restrict}
- SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
- $res = $stm->fetch(PDO::FETCH_NAMED);
- $date_min = new \DateTime();
- $date_min->setTimestamp($res['date_min']);
- $date_max = new \DateTime();
- $date_max->setTimestamp($res['date_max']);
- $interval = $date_max->diff($date_min, true);
- $interval_in_days = $interval->format('%a');
- if ($interval_in_days <= 0) {
- // Surely only one article.
- // We will return count / (period/period) == count.
- $interval_in_days = $period;
- }
- return round($res['count'] / ($interval_in_days / $period), 2);
- }
- /**
- * 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($min, $max)));
- }
- /**
- * Calculates feed count per category.
- * Returns the result as a JSON string.
- *
- * @return string
- */
- public function calculateFeedByCategory() {
- $sql = <<<SQL
- SELECT c.name AS label
- , COUNT(f.id) AS data
- FROM {$this->prefix}category AS c,
- {$this->prefix}feed AS f
- WHERE c.id = f.category
- GROUP BY label
- ORDER BY data DESC
- SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
- $res = $stm->fetchAll(PDO::FETCH_ASSOC);
- return $this->convertToPieSerie($res);
- }
- /**
- * Calculates entry count per category.
- * Returns the result as a JSON string.
- *
- * @return string
- */
- public function calculateEntryByCategory() {
- $sql = <<<SQL
- SELECT c.name AS label
- , COUNT(e.id) AS data
- FROM {$this->prefix}category AS c,
- {$this->prefix}feed AS f,
- {$this->prefix}entry AS e
- WHERE c.id = f.category
- AND f.id = e.id_feed
- GROUP BY label
- ORDER BY data DESC
- SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
- $res = $stm->fetchAll(PDO::FETCH_ASSOC);
- return $this->convertToPieSerie($res);
- }
- /**
- * Calculates the 10 top feeds based on their number of entries
- *
- * @return array
- */
- public function calculateTopFeed() {
- $sql = <<<SQL
- SELECT f.id AS id
- , MAX(f.name) AS name
- , MAX(c.name) AS category
- , COUNT(e.id) AS count
- FROM {$this->prefix}category AS c,
- {$this->prefix}feed AS f,
- {$this->prefix}entry AS e
- WHERE c.id = f.category
- AND f.id = e.id_feed
- GROUP BY f.id
- ORDER BY count DESC
- LIMIT 10
- SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
- return $stm->fetchAll(PDO::FETCH_ASSOC);
- }
- /**
- * Calculates the last publication date for each feed
- *
- * @return array
- */
- public function calculateFeedLastDate() {
- $sql = <<<SQL
- SELECT MAX(f.id) as id
- , MAX(f.name) AS name
- , MAX(date) AS last_date
- , COUNT(*) AS nb_articles
- FROM {$this->prefix}feed AS f,
- {$this->prefix}entry AS e
- WHERE f.id = e.id_feed
- GROUP BY f.id
- ORDER BY name
- SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
- return $stm->fetchAll(PDO::FETCH_ASSOC);
- }
- protected function convertToSerie($data) {
- $serie = array();
- foreach ($data as $key => $value) {
- $serie[] = array($key, $value);
- }
- return json_encode($serie);
- }
- protected function convertToPieSerie($data) {
- $serie = array();
- foreach ($data as $value) {
- $value['data'] = array(array(0, (int) $value['data']));
- $serie[] = $value;
- }
- 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 _t($a);
- }, $data);
- return json_encode($translated);
- }
- }
|