4
0

StatsDAOPGSQL.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=1);
  3. class FreshRSS_StatsDAOPGSQL extends FreshRSS_StatsDAO {
  4. #[\Override]
  5. protected function sqlDateToIsoGranularity(string $field, int $precision, string $granularity): string {
  6. if (!preg_match('/^[a-zA-Z0-9_.]+$/', $field)) {
  7. throw new InvalidArgumentException('Invalid date field!');
  8. }
  9. $offset = $this->getTimezoneOffset();
  10. return match ($granularity) {
  11. 'day' => "to_char(to_timestamp(($field / $precision) + $offset), 'YYYY-MM-DD')",
  12. 'month' => "to_char(to_timestamp(($field / $precision) + $offset), 'YYYY-MM')",
  13. 'year' => "to_char(to_timestamp(($field / $precision) + $offset), 'YYYY')",
  14. default => throw new InvalidArgumentException('Invalid date granularity'),
  15. };
  16. }
  17. /**
  18. * Calculates the number of article per hour of the day per feed
  19. *
  20. * @param int $feed id
  21. * @return array<int,int>
  22. */
  23. #[\Override]
  24. public function calculateEntryRepartitionPerFeedPerHour(?int $feed = null): array {
  25. return $this->calculateEntryRepartitionPerFeedPerPeriod('hour', $feed);
  26. }
  27. /**
  28. * Calculates the number of article per day of week per feed
  29. * @return array<int,int>
  30. */
  31. #[\Override]
  32. public function calculateEntryRepartitionPerFeedPerDayOfWeek(?int $feed = null): array {
  33. // PostgreSQL uses `dow` (0 = Sunday .. 6 = Saturday) for the day of week,
  34. // whereas `day` would extract the day of the month.
  35. return $this->calculateEntryRepartitionPerFeedPerPeriod('dow', $feed);
  36. }
  37. /**
  38. * Calculates the number of article per month per feed
  39. * @return array<int,int>
  40. */
  41. #[\Override]
  42. public function calculateEntryRepartitionPerFeedPerMonth(?int $feed = null): array {
  43. $monthRepartition = $this->calculateEntryRepartitionPerFeedPerPeriod('month', $feed);
  44. return $this->normalizeMonthRepartition($monthRepartition);
  45. }
  46. /**
  47. * Calculates the number of article per period per feed
  48. * @param string $period format string to use for grouping
  49. * @return array<int,int>
  50. */
  51. #[\Override]
  52. protected function calculateEntryRepartitionPerFeedPerPeriod(string $period, ?int $feed = null): array {
  53. $restrict = '';
  54. if ($feed) {
  55. $restrict = "WHERE e.id_feed = {$feed}";
  56. }
  57. $offset = $this->getTimezoneOffset();
  58. $sql = <<<SQL
  59. SELECT extract( {$period} from to_timestamp(e.date + {$offset})) AS period, COUNT(1) AS count
  60. FROM `_entry` AS e
  61. {$restrict}
  62. GROUP BY period
  63. ORDER BY period ASC
  64. SQL;
  65. $res = $this->fetchAssoc($sql);
  66. if ($res == null) {
  67. return [];
  68. }
  69. $periodMax = match ($period) {
  70. 'hour' => 24,
  71. 'dow' => 7,
  72. 'month' => 12,
  73. default => 30,
  74. };
  75. $repartition = array_fill(0, $periodMax, 0);
  76. foreach ($res as $value) {
  77. $repartition[(int)$value['period']] = (int)$value['count'];
  78. }
  79. return $repartition;
  80. }
  81. }