4
0

StatsDAOPGSQL.php 1.9 KB

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