StatsDAO.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. return round($res['count'] / ($interval->format('%a') / ($period)), 2);
  191. }
  192. /**
  193. * Initialize an array for statistics depending on a range
  194. *
  195. * @param integer $min
  196. * @param integer $max
  197. * @return array
  198. */
  199. protected function initStatsArray($min, $max) {
  200. return array_map(function () {
  201. return 0;
  202. }, array_flip(range($min, $max)));
  203. }
  204. /**
  205. * Calculates feed count per category.
  206. * Returns the result as a JSON string.
  207. *
  208. * @return string
  209. */
  210. public function calculateFeedByCategory() {
  211. $sql = <<<SQL
  212. SELECT c.name AS label
  213. , COUNT(f.id) AS data
  214. FROM {$this->prefix}category AS c,
  215. {$this->prefix}feed AS f
  216. WHERE c.id = f.category
  217. GROUP BY label
  218. ORDER BY data DESC
  219. SQL;
  220. $stm = $this->bd->prepare($sql);
  221. $stm->execute();
  222. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  223. return $this->convertToPieSerie($res);
  224. }
  225. /**
  226. * Calculates entry count per category.
  227. * Returns the result as a JSON string.
  228. *
  229. * @return string
  230. */
  231. public function calculateEntryByCategory() {
  232. $sql = <<<SQL
  233. SELECT c.name AS label
  234. , COUNT(e.id) AS data
  235. FROM {$this->prefix}category AS c,
  236. {$this->prefix}feed AS f,
  237. {$this->prefix}entry AS e
  238. WHERE c.id = f.category
  239. AND f.id = e.id_feed
  240. GROUP BY label
  241. ORDER BY data DESC
  242. SQL;
  243. $stm = $this->bd->prepare($sql);
  244. $stm->execute();
  245. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  246. return $this->convertToPieSerie($res);
  247. }
  248. /**
  249. * Calculates the 10 top feeds based on their number of entries
  250. *
  251. * @return array
  252. */
  253. public function calculateTopFeed() {
  254. $sql = <<<SQL
  255. SELECT f.id AS id
  256. , MAX(f.name) AS name
  257. , MAX(c.name) AS category
  258. , COUNT(e.id) AS count
  259. FROM {$this->prefix}category AS c,
  260. {$this->prefix}feed AS f,
  261. {$this->prefix}entry AS e
  262. WHERE c.id = f.category
  263. AND f.id = e.id_feed
  264. GROUP BY f.id
  265. ORDER BY count DESC
  266. LIMIT 10
  267. SQL;
  268. $stm = $this->bd->prepare($sql);
  269. $stm->execute();
  270. return $stm->fetchAll(PDO::FETCH_ASSOC);
  271. }
  272. /**
  273. * Calculates the last publication date for each feed
  274. *
  275. * @return array
  276. */
  277. public function calculateFeedLastDate() {
  278. $sql = <<<SQL
  279. SELECT MAX(f.id) as id
  280. , MAX(f.name) AS name
  281. , MAX(date) AS last_date
  282. FROM {$this->prefix}feed AS f,
  283. {$this->prefix}entry AS e
  284. WHERE f.id = e.id_feed
  285. GROUP BY f.id
  286. ORDER BY name
  287. SQL;
  288. $stm = $this->bd->prepare($sql);
  289. $stm->execute();
  290. return $stm->fetchAll(PDO::FETCH_ASSOC);
  291. }
  292. protected function convertToSerie($data) {
  293. $serie = array();
  294. foreach ($data as $key => $value) {
  295. $serie[] = array($key, $value);
  296. }
  297. return json_encode($serie);
  298. }
  299. protected function convertToPieSerie($data) {
  300. $serie = array();
  301. foreach ($data as $value) {
  302. $value['data'] = array(array(0, (int) $value['data']));
  303. $serie[] = $value;
  304. }
  305. return json_encode($serie);
  306. }
  307. /**
  308. * Gets days ready for graphs
  309. *
  310. * @return string
  311. */
  312. public function getDays() {
  313. return $this->convertToTranslatedJson(array(
  314. 'sun',
  315. 'mon',
  316. 'tue',
  317. 'wed',
  318. 'thu',
  319. 'fri',
  320. 'sat',
  321. ));
  322. }
  323. /**
  324. * Gets months ready for graphs
  325. *
  326. * @return string
  327. */
  328. public function getMonths() {
  329. return $this->convertToTranslatedJson(array(
  330. 'jan',
  331. 'feb',
  332. 'mar',
  333. 'apr',
  334. 'may',
  335. 'jun',
  336. 'jul',
  337. 'aug',
  338. 'sep',
  339. 'oct',
  340. 'nov',
  341. 'dec',
  342. ));
  343. }
  344. /**
  345. * Translates array content and encode it as JSON
  346. *
  347. * @param array $data
  348. * @return string
  349. */
  350. private function convertToTranslatedJson($data = array()) {
  351. $translated = array_map(function ($a) {
  352. return Minz_Translate::t($a);
  353. }, $data);
  354. return json_encode($translated);
  355. }
  356. }