StatsDAOPGSQL.php 1.8 KB

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