4
0

StatsDAOSQLite.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. declare(strict_types=1);
  3. class FreshRSS_StatsDAOSQLite extends FreshRSS_StatsDAO {
  4. #[\Override]
  5. protected function sqlDateToIsoGranularity(string $field, int $precision, string $granularity): string {
  6. $offset = $this->getTimezoneOffset();
  7. return match ($granularity) {
  8. 'day' => "strftime('%Y-%m-%d', ($field / $precision) + $offset, 'unixepoch')",
  9. 'month' => "strftime('%Y-%m', ($field / $precision) + $offset, 'unixepoch')",
  10. 'year' => "strftime('%Y', ($field / $precision) + $offset, 'unixepoch')",
  11. default => throw new InvalidArgumentException('Invalid date granularity'),
  12. };
  13. }
  14. #[\Override]
  15. protected function sqlFloor(string $s): string {
  16. return "CAST(($s) AS INT)";
  17. }
  18. /**
  19. * @return array<int,int>
  20. */
  21. #[\Override]
  22. protected function calculateEntryRepartitionPerFeedPerPeriod(string $period, ?int $feed = null): array {
  23. if ($feed) {
  24. $restrict = "WHERE e.id_feed = {$feed}";
  25. } else {
  26. $restrict = '';
  27. }
  28. $offset = $this->getTimezoneOffset();
  29. $sql = <<<SQL
  30. SELECT strftime('{$period}', e.date + {$offset}, 'unixepoch') AS period
  31. , COUNT(1) AS count
  32. FROM `_entry` AS e
  33. {$restrict}
  34. GROUP BY period
  35. ORDER BY period ASC
  36. SQL;
  37. $res = $this->fetchAssoc($sql);
  38. if ($res == null) {
  39. return [];
  40. }
  41. $periodMax = match ($period) {
  42. '%H' => 24,
  43. '%w' => 7,
  44. '%m' => 12,
  45. default => 30,
  46. };
  47. $repartition = array_fill(0, $periodMax, 0);
  48. foreach ($res as $value) {
  49. $repartition[(int)$value['period']] = (int)$value['count'];
  50. }
  51. return $repartition;
  52. }
  53. }