StatsDAO.php 9.6 KB

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