StatsDAOSQLite.php 1003 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. $stm = $this->pdo->query($sql);
  24. $res = $stm->fetchAll(PDO::FETCH_NAMED);
  25. switch ($period) {
  26. case '%H':
  27. $periodMax = 24;
  28. break;
  29. case '%w':
  30. $periodMax = 7;
  31. break;
  32. case '%m':
  33. $periodMax = 12;
  34. break;
  35. default:
  36. $periodMax = 30;
  37. }
  38. $repartition = array_fill(0, $periodMax, 0);
  39. foreach ($res as $value) {
  40. $repartition[(int) $value['period']] = (int) $value['count'];
  41. }
  42. return $repartition;
  43. }
  44. }