StatsDAOSQLite.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 object.
  6. *
  7. * @return JSON object
  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. /**
  32. * Calculates entry average per day on a 30 days period.
  33. *
  34. * @return integer
  35. */
  36. public function calculateEntryAverage() {
  37. $period = self::ENTRY_COUNT_PERIOD;
  38. // Get stats per day for the last 30 days
  39. $sql = <<<SQL
  40. SELECT COUNT(1) / {$period} AS average
  41. FROM {$this->prefix}entry AS e
  42. WHERE strftime('%Y%m%d', e.date, 'unixepoch')
  43. BETWEEN strftime('%Y%m%d', 'now', '-{$period} days')
  44. AND strftime('%Y%m%d', 'now', '-1 day')
  45. SQL;
  46. $stm = $this->bd->prepare($sql);
  47. $stm->execute();
  48. $res = $stm->fetch(PDO::FETCH_NAMED);
  49. return round($res['average'], 2);
  50. }
  51. protected function calculateEntryRepartitionPerFeedPerPeriod($period, $feed = null) {
  52. if ($feed) {
  53. $restrict = "WHERE e.id_feed = {$feed}";
  54. } else {
  55. $restrict = '';
  56. }
  57. $sql = <<<SQL
  58. SELECT strftime('{$period}', e.date, 'unixepoch') AS period
  59. , COUNT(1) AS count
  60. FROM {$this->prefix}entry AS e
  61. {$restrict}
  62. GROUP BY period
  63. ORDER BY period ASC
  64. SQL;
  65. $stm = $this->bd->prepare($sql);
  66. $stm->execute();
  67. $res = $stm->fetchAll(PDO::FETCH_NAMED);
  68. $repartition = array();
  69. foreach ($res as $value) {
  70. $repartition[(int) $value['period']] = (int) $value['count'];
  71. }
  72. return $this->convertToSerie($repartition);
  73. }
  74. }