StatsDAO.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. class FreshRSS_StatsDAO extends Minz_ModelPdo {
  3. const ENTRY_COUNT_PERIOD = 30;
  4. /**
  5. * Calculates entry repartition for all feeds and for main stream.
  6. * The repartition includes:
  7. * - total entries
  8. * - read entries
  9. * - unread entries
  10. * - favorite entries
  11. *
  12. * @return type
  13. */
  14. public function calculateEntryRepartition() {
  15. $repartition = array();
  16. // Generates the repartition for the main stream of entry
  17. $sql = <<<SQL
  18. SELECT COUNT(1) AS `total`,
  19. COUNT(1) - SUM(e.is_read) AS `unread`,
  20. SUM(e.is_read) AS `read`,
  21. SUM(e.is_favorite) AS `favorite`
  22. FROM {$this->prefix}entry AS e
  23. , {$this->prefix}feed AS f
  24. WHERE e.id_feed = f.id
  25. AND f.priority = 10
  26. SQL;
  27. $stm = $this->bd->prepare($sql);
  28. $stm->execute();
  29. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  30. $repartition['main_stream'] = $res[0];
  31. // Generates the repartition for all entries
  32. $sql = <<<SQL
  33. SELECT COUNT(1) AS `total`,
  34. COUNT(1) - SUM(e.is_read) AS `unread`,
  35. SUM(e.is_read) AS `read`,
  36. SUM(e.is_favorite) AS `favorite`
  37. FROM {$this->prefix}entry AS e
  38. SQL;
  39. $stm = $this->bd->prepare($sql);
  40. $stm->execute();
  41. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  42. $repartition['all_feeds'] = $res[0];
  43. return $repartition;
  44. }
  45. /**
  46. * Calculates entry count per day on a 30 days period.
  47. * Returns the result as a JSON string.
  48. *
  49. * @return string
  50. */
  51. public function calculateEntryCount() {
  52. $count = $this->initEntryCountArray();
  53. $period = self::ENTRY_COUNT_PERIOD;
  54. // Get stats per day for the last 30 days
  55. $sql = <<<SQL
  56. SELECT DATEDIFF(FROM_UNIXTIME(e.date), NOW()) AS day,
  57. COUNT(1) AS count
  58. FROM {$this->prefix}entry AS e
  59. WHERE FROM_UNIXTIME(e.date, '%Y%m%d') BETWEEN DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -{$period} DAY), '%Y%m%d') AND DATE_FORMAT(DATE_ADD(NOW(), INTERVAL -1 DAY), '%Y%m%d')
  60. GROUP BY day
  61. ORDER BY day ASC
  62. SQL;
  63. $stm = $this->bd->prepare($sql);
  64. $stm->execute();
  65. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  66. foreach ($res as $value) {
  67. $count[$value['day']] = (int) $value['count'];
  68. }
  69. return $this->convertToSerie($count);
  70. }
  71. /**
  72. * Initialize an array for the entry count.
  73. *
  74. * @return array
  75. */
  76. protected function initEntryCountArray() {
  77. return array_map(function () {
  78. return 0;
  79. }, array_flip(range(-self::ENTRY_COUNT_PERIOD, -1)));
  80. }
  81. /**
  82. * Calculates feed count per category.
  83. * Returns the result as a JSON string.
  84. *
  85. * @return string
  86. */
  87. public function calculateFeedByCategory() {
  88. $sql = <<<SQL
  89. SELECT c.name AS label
  90. , COUNT(f.id) AS data
  91. FROM {$this->prefix}category AS c,
  92. {$this->prefix}feed AS f
  93. WHERE c.id = f.category
  94. GROUP BY label
  95. ORDER BY data DESC
  96. SQL;
  97. $stm = $this->bd->prepare($sql);
  98. $stm->execute();
  99. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  100. return $this->convertToPieSerie($res);
  101. }
  102. /**
  103. * Calculates entry count per category.
  104. * Returns the result as a JSON string.
  105. *
  106. * @return string
  107. */
  108. public function calculateEntryByCategory() {
  109. $sql = <<<SQL
  110. SELECT c.name AS label
  111. , COUNT(e.id) AS data
  112. FROM {$this->prefix}category AS c,
  113. {$this->prefix}feed AS f,
  114. {$this->prefix}entry AS e
  115. WHERE c.id = f.category
  116. AND f.id = e.id_feed
  117. GROUP BY label
  118. ORDER BY data DESC
  119. SQL;
  120. $stm = $this->bd->prepare($sql);
  121. $stm->execute();
  122. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  123. return $this->convertToPieSerie($res);
  124. }
  125. /**
  126. * Calculates the 10 top feeds based on their number of entries
  127. *
  128. * @return array
  129. */
  130. public function calculateTopFeed() {
  131. $sql = <<<SQL
  132. SELECT f.id AS id
  133. , MAX(f.name) AS name
  134. , MAX(c.name) AS category
  135. , COUNT(e.id) AS count
  136. FROM {$this->prefix}category AS c,
  137. {$this->prefix}feed AS f,
  138. {$this->prefix}entry AS e
  139. WHERE c.id = f.category
  140. AND f.id = e.id_feed
  141. GROUP BY f.id
  142. ORDER BY count DESC
  143. LIMIT 10
  144. SQL;
  145. $stm = $this->bd->prepare($sql);
  146. $stm->execute();
  147. return $stm->fetchAll(PDO::FETCH_ASSOC);
  148. }
  149. /**
  150. * Calculates the last publication date for each feed
  151. *
  152. * @return array
  153. */
  154. public function calculateFeedLastDate() {
  155. $sql = <<<SQL
  156. SELECT MAX(f.name) AS name
  157. , MAX(date) AS last_date
  158. FROM {$this->prefix}feed AS f,
  159. {$this->prefix}entry AS e
  160. WHERE f.id = e.id_feed
  161. GROUP BY f.id
  162. ORDER BY name
  163. SQL;
  164. $stm = $this->bd->prepare($sql);
  165. $stm->execute();
  166. return $stm->fetchAll(PDO::FETCH_ASSOC);
  167. }
  168. protected function convertToSerie($data) {
  169. $serie = array();
  170. foreach ($data as $key => $value) {
  171. $serie[] = array($key, $value);
  172. }
  173. return json_encode($serie);
  174. }
  175. protected function convertToPieSerie($data) {
  176. $serie = array();
  177. foreach ($data as $value) {
  178. $value['data'] = array(array(0, (int) $value['data']));
  179. $serie[] = $value;
  180. }
  181. return json_encode($serie);
  182. }
  183. }