StatsDAO.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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 $this->initStatsArray(-self::ENTRY_COUNT_PERIOD, -1);
  78. }
  79. /**
  80. * Calculates the number of article per hour of the day per feed
  81. *
  82. * @param integer $feed id
  83. * @return string
  84. */
  85. public function calculateEntryRepartitionPerFeedPerHour($feed = null) {
  86. return $this->calculateEntryRepartitionPerFeedPerPeriod('%H', $feed);
  87. }
  88. /**
  89. * Calculates the number of article per day of week per feed
  90. *
  91. * @param integer $feed id
  92. * @return string
  93. */
  94. public function calculateEntryRepartitionPerFeedPerDayOfWeek($feed = null) {
  95. return $this->calculateEntryRepartitionPerFeedPerPeriod('%w', $feed);
  96. }
  97. /**
  98. * Calculates the number of article per month per feed
  99. *
  100. * @param integer $feed
  101. * @return string
  102. */
  103. public function calculateEntryRepartitionPerFeedPerMonth($feed = null) {
  104. return $this->calculateEntryRepartitionPerFeedPerPeriod('%m', $feed);
  105. }
  106. /**
  107. * Calculates the number of article per period per feed
  108. *
  109. * @param string $period format string to use for grouping
  110. * @param integer $feed id
  111. * @return string
  112. */
  113. protected function calculateEntryRepartitionPerFeedPerPeriod($period, $feed = null) {
  114. if ($feed) {
  115. $restrict = "WHERE e.id_feed = {$feed}";
  116. } else {
  117. $restrict = '';
  118. }
  119. $sql = <<<SQL
  120. SELECT DATE_FORMAT(FROM_UNIXTIME(e.date), '{$period}') AS period
  121. , COUNT(1) AS count
  122. FROM {$this->prefix}entry AS e
  123. {$restrict}
  124. GROUP BY period
  125. ORDER BY period ASC
  126. SQL;
  127. $stm = $this->bd->prepare($sql);
  128. $stm->execute();
  129. $res = $stm->fetchAll(PDO::FETCH_NAMED);
  130. foreach ($res as $value) {
  131. $repartition[(int) $value['period']] = (int) $value['count'];
  132. }
  133. return $this->convertToSerie($repartition);
  134. }
  135. /**
  136. * Calculates the average number of article per hour per feed
  137. *
  138. * @param integer $feed id
  139. * @return integer
  140. */
  141. public function calculateEntryAveragePerFeedPerHour($feed = null) {
  142. return $this->calculateEntryAveragePerFeedPerPeriod(1/24, $feed);
  143. }
  144. /**
  145. * Calculates the average number of article per day of week per feed
  146. *
  147. * @param integer $feed id
  148. * @return integer
  149. */
  150. public function calculateEntryAveragePerFeedPerDayOfWeek($feed = null) {
  151. return $this->calculateEntryAveragePerFeedPerPeriod(7, $feed);
  152. }
  153. /**
  154. * Calculates the average number of article per month per feed
  155. *
  156. * @param integer $feed id
  157. * @return integer
  158. */
  159. public function calculateEntryAveragePerFeedPerMonth($feed = null) {
  160. return $this->calculateEntryAveragePerFeedPerPeriod(30, $feed);
  161. }
  162. /**
  163. * Calculates the average number of article per feed
  164. *
  165. * @param float $period number used to divide the number of day in the period
  166. * @param integer $feed id
  167. * @return integer
  168. */
  169. protected function calculateEntryAveragePerFeedPerPeriod($period, $feed = null) {
  170. if ($feed) {
  171. $restrict = "WHERE e.id_feed = {$feed}";
  172. } else {
  173. $restrict = '';
  174. }
  175. $sql = <<<SQL
  176. SELECT COUNT(1) AS count
  177. , MIN(date) AS date_min
  178. , MAX(date) AS date_max
  179. FROM {$this->prefix}entry AS e
  180. {$restrict}
  181. SQL;
  182. $stm = $this->bd->prepare($sql);
  183. $stm->execute();
  184. $res = $stm->fetch(PDO::FETCH_NAMED);
  185. $date_min = new \DateTime();
  186. $date_min->setTimestamp($res['date_min']);
  187. $date_max = new \DateTime();
  188. $date_max->setTimestamp($res['date_max']);
  189. $interval = $date_max->diff($date_min, true);
  190. $interval_in_days = $interval->format('%a');
  191. if ($interval_in_days <= 0) {
  192. // Surely only one article.
  193. // We will return count / (period/period) == count.
  194. $interval_in_days = $period;
  195. }
  196. return round($res['count'] / ($interval_in_days / $period), 2);
  197. }
  198. /**
  199. * Initialize an array for statistics depending on a range
  200. *
  201. * @param integer $min
  202. * @param integer $max
  203. * @return array
  204. */
  205. protected function initStatsArray($min, $max) {
  206. return array_map(function () {
  207. return 0;
  208. }, array_flip(range($min, $max)));
  209. }
  210. /**
  211. * Calculates feed count per category.
  212. * Returns the result as a JSON string.
  213. *
  214. * @return string
  215. */
  216. public function calculateFeedByCategory() {
  217. $sql = <<<SQL
  218. SELECT c.name AS label
  219. , COUNT(f.id) AS data
  220. FROM {$this->prefix}category AS c,
  221. {$this->prefix}feed AS f
  222. WHERE c.id = f.category
  223. GROUP BY label
  224. ORDER BY data DESC
  225. SQL;
  226. $stm = $this->bd->prepare($sql);
  227. $stm->execute();
  228. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  229. return $this->convertToPieSerie($res);
  230. }
  231. /**
  232. * Calculates entry count per category.
  233. * Returns the result as a JSON string.
  234. *
  235. * @return string
  236. */
  237. public function calculateEntryByCategory() {
  238. $sql = <<<SQL
  239. SELECT c.name AS label
  240. , COUNT(e.id) AS data
  241. FROM {$this->prefix}category AS c,
  242. {$this->prefix}feed AS f,
  243. {$this->prefix}entry AS e
  244. WHERE c.id = f.category
  245. AND f.id = e.id_feed
  246. GROUP BY label
  247. ORDER BY data DESC
  248. SQL;
  249. $stm = $this->bd->prepare($sql);
  250. $stm->execute();
  251. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  252. return $this->convertToPieSerie($res);
  253. }
  254. /**
  255. * Calculates the 10 top feeds based on their number of entries
  256. *
  257. * @return array
  258. */
  259. public function calculateTopFeed() {
  260. $sql = <<<SQL
  261. SELECT f.id AS id
  262. , MAX(f.name) AS name
  263. , MAX(c.name) AS category
  264. , COUNT(e.id) AS count
  265. FROM {$this->prefix}category AS c,
  266. {$this->prefix}feed AS f,
  267. {$this->prefix}entry AS e
  268. WHERE c.id = f.category
  269. AND f.id = e.id_feed
  270. GROUP BY f.id
  271. ORDER BY count DESC
  272. LIMIT 10
  273. SQL;
  274. $stm = $this->bd->prepare($sql);
  275. $stm->execute();
  276. return $stm->fetchAll(PDO::FETCH_ASSOC);
  277. }
  278. /**
  279. * Calculates the last publication date for each feed
  280. *
  281. * @return array
  282. */
  283. public function calculateFeedLastDate() {
  284. $sql = <<<SQL
  285. SELECT MAX(f.id) as id
  286. , MAX(f.name) AS name
  287. , MAX(date) AS last_date
  288. , COUNT(*) AS nb_articles
  289. FROM {$this->prefix}feed AS f,
  290. {$this->prefix}entry AS e
  291. WHERE f.id = e.id_feed
  292. GROUP BY f.id
  293. ORDER BY name
  294. SQL;
  295. $stm = $this->bd->prepare($sql);
  296. $stm->execute();
  297. return $stm->fetchAll(PDO::FETCH_ASSOC);
  298. }
  299. protected function convertToSerie($data) {
  300. $serie = array();
  301. foreach ($data as $key => $value) {
  302. $serie[] = array($key, $value);
  303. }
  304. return json_encode($serie);
  305. }
  306. protected function convertToPieSerie($data) {
  307. $serie = array();
  308. foreach ($data as $value) {
  309. $value['data'] = array(array(0, (int) $value['data']));
  310. $serie[] = $value;
  311. }
  312. return json_encode($serie);
  313. }
  314. /**
  315. * Gets days ready for graphs
  316. *
  317. * @return string
  318. */
  319. public function getDays() {
  320. return $this->convertToTranslatedJson(array(
  321. 'sun',
  322. 'mon',
  323. 'tue',
  324. 'wed',
  325. 'thu',
  326. 'fri',
  327. 'sat',
  328. ));
  329. }
  330. /**
  331. * Gets months ready for graphs
  332. *
  333. * @return string
  334. */
  335. public function getMonths() {
  336. return $this->convertToTranslatedJson(array(
  337. 'jan',
  338. 'feb',
  339. 'mar',
  340. 'apr',
  341. 'may',
  342. 'jun',
  343. 'jul',
  344. 'aug',
  345. 'sep',
  346. 'oct',
  347. 'nov',
  348. 'dec',
  349. ));
  350. }
  351. /**
  352. * Translates array content and encode it as JSON
  353. *
  354. * @param array $data
  355. * @return string
  356. */
  357. private function convertToTranslatedJson($data = array()) {
  358. $translated = array_map(function ($a) {
  359. return Minz_Translate::t($a);
  360. }, $data);
  361. return json_encode($translated);
  362. }
  363. }