StatsDAOPGSQL.php 1.8 KB

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