StatsDAOSQLite.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. class FreshRSS_StatsDAOSQLite extends FreshRSS_StatsDAO {
  3. /**
  4. * Calculates entry count per day on a 30 days period.
  5. * Returns the result as a JSON string.
  6. *
  7. * @return string
  8. */
  9. public function calculateEntryCount() {
  10. $count = $this->initEntryCountArray();
  11. $period = parent::ENTRY_COUNT_PERIOD;
  12. // Get stats per day for the last 30 days
  13. $sql = <<<SQL
  14. SELECT round(julianday(e.date, 'unixepoch') - julianday('now')) AS day,
  15. COUNT(1) AS count
  16. FROM {$this->prefix}entry AS e
  17. WHERE strftime('%Y%m%d', e.date, 'unixepoch')
  18. BETWEEN strftime('%Y%m%d', 'now', '-{$period} days')
  19. AND strftime('%Y%m%d', 'now', '-1 day')
  20. GROUP BY day
  21. ORDER BY day ASC
  22. SQL;
  23. $stm = $this->bd->prepare($sql);
  24. $stm->execute();
  25. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  26. foreach ($res as $value) {
  27. $count[(int) $value['day']] = (int) $value['count'];
  28. }
  29. return $this->convertToSerie($count);
  30. }
  31. protected function calculateEntryRepartitionPerFeedPerPeriod($period, $feed = null) {
  32. if ($feed) {
  33. $restrict = "WHERE e.id_feed = {$feed}";
  34. } else {
  35. $restrict = '';
  36. }
  37. $sql = <<<SQL
  38. SELECT strftime('{$period}', e.date, 'unixepoch') AS period
  39. , COUNT(1) AS count
  40. FROM {$this->prefix}entry AS e
  41. {$restrict}
  42. GROUP BY period
  43. ORDER BY period ASC
  44. SQL;
  45. $stm = $this->bd->prepare($sql);
  46. $stm->execute();
  47. $res = $stm->fetchAll(PDO::FETCH_NAMED);
  48. foreach ($res as $value) {
  49. $repartition[(int) $value['period']] = (int) $value['count'];
  50. }
  51. return $this->convertToSerie($repartition);
  52. }
  53. }