StatsDAO.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 `unread`,
  38. SUM(e.is_read) AS `read`,
  39. SUM(e.is_favorite) AS `favorite`
  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. foreach ($res as $value) {
  153. $repartition[(int) $value['period']] = (int) $value['count'];
  154. }
  155. return $this->convertToSerie($repartition);
  156. }
  157. /**
  158. * Calculates the average number of article per hour per feed
  159. *
  160. * @param integer $feed id
  161. * @return integer
  162. */
  163. public function calculateEntryAveragePerFeedPerHour($feed = null) {
  164. return $this->calculateEntryAveragePerFeedPerPeriod(1 / 24, $feed);
  165. }
  166. /**
  167. * Calculates the average number of article per day of week per feed
  168. *
  169. * @param integer $feed id
  170. * @return integer
  171. */
  172. public function calculateEntryAveragePerFeedPerDayOfWeek($feed = null) {
  173. return $this->calculateEntryAveragePerFeedPerPeriod(7, $feed);
  174. }
  175. /**
  176. * Calculates the average number of article per month per feed
  177. *
  178. * @param integer $feed id
  179. * @return integer
  180. */
  181. public function calculateEntryAveragePerFeedPerMonth($feed = null) {
  182. return $this->calculateEntryAveragePerFeedPerPeriod(30, $feed);
  183. }
  184. /**
  185. * Calculates the average number of article per feed
  186. *
  187. * @param float $period number used to divide the number of day in the period
  188. * @param integer $feed id
  189. * @return integer
  190. */
  191. protected function calculateEntryAveragePerFeedPerPeriod($period, $feed = null) {
  192. $restrict = '';
  193. if ($feed) {
  194. $restrict = "WHERE e.id_feed = {$feed}";
  195. }
  196. $sql = <<<SQL
  197. SELECT COUNT(1) AS count
  198. , MIN(date) AS date_min
  199. , MAX(date) AS date_max
  200. FROM {$this->prefix}entry AS e
  201. {$restrict}
  202. SQL;
  203. $stm = $this->bd->prepare($sql);
  204. $stm->execute();
  205. $res = $stm->fetch(PDO::FETCH_NAMED);
  206. $date_min = new \DateTime();
  207. $date_min->setTimestamp($res['date_min']);
  208. $date_max = new \DateTime();
  209. $date_max->setTimestamp($res['date_max']);
  210. $interval = $date_max->diff($date_min, true);
  211. $interval_in_days = $interval->format('%a');
  212. if ($interval_in_days <= 0) {
  213. // Surely only one article.
  214. // We will return count / (period/period) == count.
  215. $interval_in_days = $period;
  216. }
  217. return $res['count'] / ($interval_in_days / $period);
  218. }
  219. /**
  220. * Initialize an array for statistics depending on a range
  221. *
  222. * @param integer $min
  223. * @param integer $max
  224. * @return array
  225. */
  226. protected function initStatsArray($min, $max) {
  227. return array_map(function () {
  228. return 0;
  229. }, array_flip(range($min, $max)));
  230. }
  231. /**
  232. * Calculates feed count per category.
  233. * Returns the result as a JSON object.
  234. *
  235. * @return JSON object
  236. */
  237. public function calculateFeedByCategory() {
  238. $sql = <<<SQL
  239. SELECT c.name AS label
  240. , COUNT(f.id) AS data
  241. FROM {$this->prefix}category AS c,
  242. {$this->prefix}feed AS f
  243. WHERE c.id = f.category
  244. GROUP BY label
  245. ORDER BY data DESC
  246. SQL;
  247. $stm = $this->bd->prepare($sql);
  248. $stm->execute();
  249. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  250. return $this->convertToPieSerie($res);
  251. }
  252. /**
  253. * Calculates entry count per category.
  254. * Returns the result as a JSON string.
  255. *
  256. * @return JSON object
  257. */
  258. public function calculateEntryByCategory() {
  259. $sql = <<<SQL
  260. SELECT c.name AS label
  261. , COUNT(e.id) AS data
  262. FROM {$this->prefix}category AS c,
  263. {$this->prefix}feed AS f,
  264. {$this->prefix}entry AS e
  265. WHERE c.id = f.category
  266. AND f.id = e.id_feed
  267. GROUP BY label
  268. ORDER BY data DESC
  269. SQL;
  270. $stm = $this->bd->prepare($sql);
  271. $stm->execute();
  272. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  273. return $this->convertToPieSerie($res);
  274. }
  275. /**
  276. * Calculates the 10 top feeds based on their number of entries
  277. *
  278. * @return array
  279. */
  280. public function calculateTopFeed() {
  281. $sql = <<<SQL
  282. SELECT f.id AS id
  283. , MAX(f.name) AS name
  284. , MAX(c.name) AS category
  285. , COUNT(e.id) AS count
  286. FROM {$this->prefix}category AS c,
  287. {$this->prefix}feed AS f,
  288. {$this->prefix}entry AS e
  289. WHERE c.id = f.category
  290. AND f.id = e.id_feed
  291. GROUP BY f.id
  292. ORDER BY count DESC
  293. LIMIT 10
  294. SQL;
  295. $stm = $this->bd->prepare($sql);
  296. $stm->execute();
  297. return $stm->fetchAll(PDO::FETCH_ASSOC);
  298. }
  299. /**
  300. * Calculates the last publication date for each feed
  301. *
  302. * @return array
  303. */
  304. public function calculateFeedLastDate() {
  305. $sql = <<<SQL
  306. SELECT MAX(f.id) as id
  307. , MAX(f.name) AS name
  308. , MAX(date) AS last_date
  309. , COUNT(*) AS nb_articles
  310. FROM {$this->prefix}feed AS f,
  311. {$this->prefix}entry AS e
  312. WHERE f.id = e.id_feed
  313. GROUP BY f.id
  314. ORDER BY name
  315. SQL;
  316. $stm = $this->bd->prepare($sql);
  317. $stm->execute();
  318. return $stm->fetchAll(PDO::FETCH_ASSOC);
  319. }
  320. protected function convertToSerie($data) {
  321. $serie = array();
  322. foreach ($data as $key => $value) {
  323. $serie[] = array($key, $value);
  324. }
  325. return $serie;
  326. }
  327. protected function convertToPieSerie($data) {
  328. $serie = array();
  329. foreach ($data as $value) {
  330. $value['data'] = array(array(0, (int) $value['data']));
  331. $serie[] = $value;
  332. }
  333. return $serie;
  334. }
  335. /**
  336. * Gets days ready for graphs
  337. *
  338. * @return string
  339. */
  340. public function getDays() {
  341. return $this->convertToTranslatedJson(array(
  342. 'sun',
  343. 'mon',
  344. 'tue',
  345. 'wed',
  346. 'thu',
  347. 'fri',
  348. 'sat',
  349. ));
  350. }
  351. /**
  352. * Gets months ready for graphs
  353. *
  354. * @return string
  355. */
  356. public function getMonths() {
  357. return $this->convertToTranslatedJson(array(
  358. 'jan',
  359. 'feb',
  360. 'mar',
  361. 'apr',
  362. 'may',
  363. 'jun',
  364. 'jul',
  365. 'aug',
  366. 'sep',
  367. 'oct',
  368. 'nov',
  369. 'dec',
  370. ));
  371. }
  372. /**
  373. * Translates array content
  374. *
  375. * @param array $data
  376. * @return JSON object
  377. */
  378. private function convertToTranslatedJson($data = array()) {
  379. $translated = array_map(function($a) {
  380. return _t('gen.date.' . $a);
  381. }, $data);
  382. return $translated;
  383. }
  384. }