StatsDAO.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 string.
  53. *
  54. * @return string
  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. if ($feed) {
  138. $restrict = "WHERE e.id_feed = {$feed}";
  139. } else {
  140. $restrict = '';
  141. }
  142. $sql = <<<SQL
  143. SELECT DATE_FORMAT(FROM_UNIXTIME(e.date), '{$period}') AS period
  144. , COUNT(1) AS count
  145. FROM {$this->prefix}entry AS e
  146. {$restrict}
  147. GROUP BY period
  148. ORDER BY period ASC
  149. SQL;
  150. $stm = $this->bd->prepare($sql);
  151. $stm->execute();
  152. $res = $stm->fetchAll(PDO::FETCH_NAMED);
  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. if ($feed) {
  194. $restrict = "WHERE e.id_feed = {$feed}";
  195. } else {
  196. $restrict = '';
  197. }
  198. $sql = <<<SQL
  199. SELECT COUNT(1) AS count
  200. , MIN(date) AS date_min
  201. , MAX(date) AS date_max
  202. FROM {$this->prefix}entry AS e
  203. {$restrict}
  204. SQL;
  205. $stm = $this->bd->prepare($sql);
  206. $stm->execute();
  207. $res = $stm->fetch(PDO::FETCH_NAMED);
  208. $date_min = new \DateTime();
  209. $date_min->setTimestamp($res['date_min']);
  210. $date_max = new \DateTime();
  211. $date_max->setTimestamp($res['date_max']);
  212. $interval = $date_max->diff($date_min, true);
  213. $interval_in_days = $interval->format('%a');
  214. if ($interval_in_days <= 0) {
  215. // Surely only one article.
  216. // We will return count / (period/period) == count.
  217. $interval_in_days = $period;
  218. }
  219. return $res['count'] / ($interval_in_days / $period);
  220. }
  221. /**
  222. * Initialize an array for statistics depending on a range
  223. *
  224. * @param integer $min
  225. * @param integer $max
  226. * @return array
  227. */
  228. protected function initStatsArray($min, $max) {
  229. return array_map(function () {
  230. return 0;
  231. }, array_flip(range($min, $max)));
  232. }
  233. /**
  234. * Calculates feed count per category.
  235. * Returns the result as a JSON string.
  236. *
  237. * @return string
  238. */
  239. public function calculateFeedByCategory() {
  240. $sql = <<<SQL
  241. SELECT c.name AS label
  242. , COUNT(f.id) AS data
  243. FROM {$this->prefix}category AS c,
  244. {$this->prefix}feed AS f
  245. WHERE c.id = f.category
  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 entry count per category.
  256. * Returns the result as a JSON string.
  257. *
  258. * @return string
  259. */
  260. public function calculateEntryByCategory() {
  261. $sql = <<<SQL
  262. SELECT c.name AS label
  263. , COUNT(e.id) AS data
  264. FROM {$this->prefix}category AS c,
  265. {$this->prefix}feed AS f,
  266. {$this->prefix}entry AS e
  267. WHERE c.id = f.category
  268. AND f.id = e.id_feed
  269. GROUP BY label
  270. ORDER BY data DESC
  271. SQL;
  272. $stm = $this->bd->prepare($sql);
  273. $stm->execute();
  274. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  275. return $this->convertToPieSerie($res);
  276. }
  277. /**
  278. * Calculates the 10 top feeds based on their number of entries
  279. *
  280. * @return array
  281. */
  282. public function calculateTopFeed() {
  283. $sql = <<<SQL
  284. SELECT f.id AS id
  285. , MAX(f.name) AS name
  286. , MAX(c.name) AS category
  287. , COUNT(e.id) AS count
  288. FROM {$this->prefix}category AS c,
  289. {$this->prefix}feed AS f,
  290. {$this->prefix}entry AS e
  291. WHERE c.id = f.category
  292. AND f.id = e.id_feed
  293. GROUP BY f.id
  294. ORDER BY count DESC
  295. LIMIT 10
  296. SQL;
  297. $stm = $this->bd->prepare($sql);
  298. $stm->execute();
  299. return $stm->fetchAll(PDO::FETCH_ASSOC);
  300. }
  301. /**
  302. * Calculates the last publication date for each feed
  303. *
  304. * @return array
  305. */
  306. public function calculateFeedLastDate() {
  307. $sql = <<<SQL
  308. SELECT MAX(f.id) as id
  309. , MAX(f.name) AS name
  310. , MAX(date) AS last_date
  311. , COUNT(*) AS nb_articles
  312. FROM {$this->prefix}feed AS f,
  313. {$this->prefix}entry AS e
  314. WHERE f.id = e.id_feed
  315. GROUP BY f.id
  316. ORDER BY name
  317. SQL;
  318. $stm = $this->bd->prepare($sql);
  319. $stm->execute();
  320. return $stm->fetchAll(PDO::FETCH_ASSOC);
  321. }
  322. protected function convertToSerie($data) {
  323. $serie = array();
  324. foreach ($data as $key => $value) {
  325. $serie[] = array($key, $value);
  326. }
  327. return json_encode($serie);
  328. }
  329. protected function convertToPieSerie($data) {
  330. $serie = array();
  331. foreach ($data as $value) {
  332. $value['data'] = array(array(0, (int) $value['data']));
  333. $serie[] = $value;
  334. }
  335. return json_encode($serie);
  336. }
  337. /**
  338. * Gets days ready for graphs
  339. *
  340. * @return string
  341. */
  342. public function getDays() {
  343. return $this->convertToTranslatedJson(array(
  344. 'sun',
  345. 'mon',
  346. 'tue',
  347. 'wed',
  348. 'thu',
  349. 'fri',
  350. 'sat',
  351. ));
  352. }
  353. /**
  354. * Gets months ready for graphs
  355. *
  356. * @return string
  357. */
  358. public function getMonths() {
  359. return $this->convertToTranslatedJson(array(
  360. 'jan',
  361. 'feb',
  362. 'mar',
  363. 'apr',
  364. 'may',
  365. 'jun',
  366. 'jul',
  367. 'aug',
  368. 'sep',
  369. 'oct',
  370. 'nov',
  371. 'dec',
  372. ));
  373. }
  374. /**
  375. * Translates array content and encode it as JSON
  376. *
  377. * @param array $data
  378. * @return string
  379. */
  380. private function convertToTranslatedJson($data = array()) {
  381. $translated = array_map(function($a) {
  382. return _t($a);
  383. }, $data);
  384. return json_encode($translated);
  385. }
  386. }