4
0

StatsDAOSQLite.php 956 B

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