StatsDAOSQLite.php 998 B

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