StatsDAOPGSQL.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 int $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. $res = $this->fetchAssoc($sql);
  45. if ($res == null) {
  46. return [];
  47. }
  48. switch ($period) {
  49. case 'hour':
  50. $periodMax = 24;
  51. break;
  52. case 'day':
  53. $periodMax = 7;
  54. break;
  55. case 'month':
  56. $periodMax = 12;
  57. break;
  58. default:
  59. $periodMax = 30;
  60. }
  61. $repartition = array_fill(0, $periodMax, 0);
  62. foreach ($res as $value) {
  63. $repartition[(int)$value['period']] = (int)$value['count'];
  64. }
  65. return $repartition;
  66. }
  67. }