StatsDAO.php 9.6 KB

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