StatsDAOPGSQL.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. class FreshRSS_StatsDAOPGSQL extends FreshRSS_StatsDAO {
  3. /**
  4. * Calculates the number of article per hour of the day per feed
  5. *
  6. * @param integer $feed id
  7. * @return string
  8. */
  9. public function calculateEntryRepartitionPerFeedPerHour($feed = null) {
  10. return $this->calculateEntryRepartitionPerFeedPerPeriod('hour', $feed);
  11. }
  12. /**
  13. * Calculates the number of article per day of week per feed
  14. *
  15. * @param integer $feed id
  16. * @return string
  17. */
  18. public function calculateEntryRepartitionPerFeedPerDayOfWeek($feed = null) {
  19. return $this->calculateEntryRepartitionPerFeedPerPeriod('day', $feed);
  20. }
  21. /**
  22. * Calculates the number of article per month per feed
  23. *
  24. * @param integer $feed
  25. * @return string
  26. */
  27. public function calculateEntryRepartitionPerFeedPerMonth($feed = null) {
  28. return $this->calculateEntryRepartitionPerFeedPerPeriod('month', $feed);
  29. }
  30. /**
  31. * Calculates the number of article per period per feed
  32. *
  33. * @param string $period format string to use for grouping
  34. * @param integer $feed id
  35. * @return string
  36. */
  37. protected function calculateEntryRepartitionPerFeedPerPeriod($period, $feed = null) {
  38. $restrict = '';
  39. if ($feed) {
  40. $restrict = "WHERE e.id_feed = {$feed}";
  41. }
  42. $sql = <<<SQL
  43. SELECT extract( {$period} from to_timestamp(e.date)) AS period
  44. , COUNT(1) AS count
  45. FROM `_entry` AS e
  46. {$restrict}
  47. GROUP BY period
  48. ORDER BY period ASC
  49. SQL;
  50. $stm = $this->pdo->query($sql);
  51. $res = $stm->fetchAll(PDO::FETCH_NAMED);
  52. foreach ($res as $value) {
  53. $repartition[(int) $value['period']] = (int) $value['count'];
  54. }
  55. return $repartition;
  56. }
  57. }