StatsDAO.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. class FreshRSS_StatsDAO extends Minz_ModelPdo {
  3. const ENTRY_COUNT_PERIOD = 30;
  4. protected function sqlFloor(string $s): string {
  5. return "FLOOR($s)";
  6. }
  7. /**
  8. * Calculates entry repartition for all feeds and for main stream.
  9. *
  10. * @return array{'main_stream':array{'total':int,'count_unreads':int,'count_reads':int,'count_favorites':int},'all_feeds':array{'total':int,'count_unreads':int,'count_reads':int,'count_favorites':int}}
  11. */
  12. public function calculateEntryRepartition() {
  13. return array(
  14. 'main_stream' => $this->calculateEntryRepartitionPerFeed(null, true),
  15. 'all_feeds' => $this->calculateEntryRepartitionPerFeed(null, false),
  16. );
  17. }
  18. /**
  19. * Calculates entry repartition for the selection.
  20. * The repartition includes:
  21. * - total entries
  22. * - read entries
  23. * - unread entries
  24. * - favorite entries
  25. *
  26. * @return array{'total':int,'count_unreads':int,'count_reads':int,'count_favorites':int}
  27. */
  28. public function calculateEntryRepartitionPerFeed(?int $feed = null, bool $only_main = false): array {
  29. $filter = '';
  30. if ($only_main) {
  31. $filter .= 'AND f.priority = 10';
  32. }
  33. if (!is_null($feed)) {
  34. $filter .= "AND e.id_feed = {$feed}";
  35. }
  36. $sql = <<<SQL
  37. SELECT COUNT(1) AS total,
  38. COUNT(1) - SUM(e.is_read) AS count_unreads,
  39. SUM(e.is_read) AS count_reads,
  40. SUM(e.is_favorite) AS count_favorites
  41. FROM `_entry` AS e, `_feed` AS f
  42. WHERE e.id_feed = f.id
  43. {$filter}
  44. SQL;
  45. $stm = $this->pdo->query($sql);
  46. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  47. return $res[0];
  48. }
  49. /**
  50. * Calculates entry count per day on a 30 days period.
  51. * @return array<int,int>
  52. */
  53. public function calculateEntryCount() {
  54. $count = $this->initEntryCountArray();
  55. $midnight = mktime(0, 0, 0);
  56. $oldest = $midnight - (self::ENTRY_COUNT_PERIOD * 86400);
  57. // Get stats per day for the last 30 days
  58. $sqlDay = $this->sqlFloor("(date - $midnight) / 86400");
  59. $sql = <<<SQL
  60. SELECT {$sqlDay} AS day,
  61. COUNT(*) as count
  62. FROM `_entry`
  63. WHERE date >= {$oldest} AND date < {$midnight}
  64. GROUP BY day
  65. ORDER BY day ASC
  66. SQL;
  67. $stm = $this->pdo->query($sql);
  68. /** @var array<array{'day':int,'count':int}> */
  69. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  70. foreach ($res as $value) {
  71. $count[(int)($value['day'])] = (int) $value['count'];
  72. }
  73. return $count;
  74. }
  75. /**
  76. * Initialize an array for the entry count.
  77. * @return array<int,int>
  78. */
  79. protected function initEntryCountArray() {
  80. return $this->initStatsArray(-self::ENTRY_COUNT_PERIOD, -1);
  81. }
  82. /**
  83. * Calculates the number of article per hour of the day per feed
  84. * @return array<int,int>
  85. */
  86. public function calculateEntryRepartitionPerFeedPerHour(?int $feed = null): array {
  87. return $this->calculateEntryRepartitionPerFeedPerPeriod('%H', $feed);
  88. }
  89. /**
  90. * Calculates the number of article per day of week per feed
  91. * @return array<int,int>
  92. */
  93. public function calculateEntryRepartitionPerFeedPerDayOfWeek(?int $feed = null): array {
  94. return $this->calculateEntryRepartitionPerFeedPerPeriod('%w', $feed);
  95. }
  96. /**
  97. * Calculates the number of article per month per feed
  98. * @return array<int,int>
  99. */
  100. public function calculateEntryRepartitionPerFeedPerMonth(?int $feed = null): array {
  101. $monthRepartition = $this->calculateEntryRepartitionPerFeedPerPeriod('%m', $feed);
  102. // cut out the 0th month (Jan=1, Dec=12)
  103. \array_splice($monthRepartition, 0, 1);
  104. return $monthRepartition;
  105. }
  106. /**
  107. * Calculates the number of article per period per feed
  108. * @param string $period format string to use for grouping
  109. * @return array<int,int>
  110. */
  111. protected function calculateEntryRepartitionPerFeedPerPeriod(string $period, ?int $feed = null): array {
  112. $restrict = '';
  113. if ($feed) {
  114. $restrict = "WHERE e.id_feed = {$feed}";
  115. }
  116. $sql = <<<SQL
  117. SELECT DATE_FORMAT(FROM_UNIXTIME(e.date), '{$period}') AS period
  118. , COUNT(1) AS count
  119. FROM `_entry` AS e
  120. {$restrict}
  121. GROUP BY period
  122. ORDER BY period ASC
  123. SQL;
  124. $stm = $this->pdo->query($sql);
  125. $res = $stm->fetchAll(PDO::FETCH_NAMED);
  126. switch ($period) {
  127. case '%H':
  128. $periodMax = 24;
  129. break;
  130. case '%w':
  131. $periodMax = 7;
  132. break;
  133. case '%m':
  134. $periodMax = 12;
  135. break;
  136. default:
  137. $periodMax = 30;
  138. }
  139. $repartition = array_fill(0, $periodMax, 0);
  140. foreach ($res as $value) {
  141. $repartition[(int) $value['period']] = (int) $value['count'];
  142. }
  143. return $repartition;
  144. }
  145. /**
  146. * Calculates the average number of article per hour per feed
  147. */
  148. public function calculateEntryAveragePerFeedPerHour(?int $feed = null): float {
  149. return $this->calculateEntryAveragePerFeedPerPeriod(1 / 24, $feed);
  150. }
  151. /**
  152. * Calculates the average number of article per day of week per feed
  153. */
  154. public function calculateEntryAveragePerFeedPerDayOfWeek(?int $feed = null): float {
  155. return $this->calculateEntryAveragePerFeedPerPeriod(7, $feed);
  156. }
  157. /**
  158. * Calculates the average number of article per month per feed
  159. */
  160. public function calculateEntryAveragePerFeedPerMonth(?int $feed = null): float {
  161. return $this->calculateEntryAveragePerFeedPerPeriod(30, $feed);
  162. }
  163. /**
  164. * Calculates the average number of article per feed
  165. * @param float $period number used to divide the number of day in the period
  166. */
  167. protected function calculateEntryAveragePerFeedPerPeriod(float $period, ?int $feed = null): float {
  168. $restrict = '';
  169. if ($feed) {
  170. $restrict = "WHERE e.id_feed = {$feed}";
  171. }
  172. $sql = <<<SQL
  173. SELECT COUNT(1) AS count
  174. , MIN(date) AS date_min
  175. , MAX(date) AS date_max
  176. FROM `_entry` AS e
  177. {$restrict}
  178. SQL;
  179. $stm = $this->pdo->query($sql);
  180. $res = $stm->fetch(PDO::FETCH_NAMED);
  181. $date_min = new \DateTime();
  182. $date_min->setTimestamp($res['date_min']);
  183. $date_max = new \DateTime();
  184. $date_max->setTimestamp($res['date_max']);
  185. $interval = $date_max->diff($date_min, true);
  186. $interval_in_days = (float)($interval->format('%a'));
  187. if ($interval_in_days <= 0) {
  188. // Surely only one article.
  189. // We will return count / (period/period) == count.
  190. $interval_in_days = $period;
  191. }
  192. return intval($res['count']) / ($interval_in_days / $period);
  193. }
  194. /**
  195. * Initialize an array for statistics depending on a range
  196. * @return array<int,int>
  197. */
  198. protected function initStatsArray(int $min, int $max): array {
  199. return array_map(function () {
  200. return 0;
  201. }, array_flip(range($min, $max)));
  202. }
  203. /**
  204. * Calculates feed count per category.
  205. * @return array<array{'label':string,'data':int}>
  206. */
  207. public function calculateFeedByCategory(): array {
  208. $sql = <<<SQL
  209. SELECT c.name AS label
  210. , COUNT(f.id) AS data
  211. FROM `_category` AS c, `_feed` AS f
  212. WHERE c.id = f.category
  213. GROUP BY label
  214. ORDER BY data DESC
  215. SQL;
  216. $stm = $this->pdo->query($sql);
  217. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  218. return $res;
  219. }
  220. /**
  221. * Calculates entry count per category.
  222. * @return array<array{'label':string,'data':int}>
  223. */
  224. public function calculateEntryByCategory(): array {
  225. $sql = <<<SQL
  226. SELECT c.name AS label
  227. , COUNT(e.id) AS data
  228. FROM `_category` AS c, `_feed` AS f, `_entry` AS e
  229. WHERE c.id = f.category
  230. AND f.id = e.id_feed
  231. GROUP BY label
  232. ORDER BY data DESC
  233. SQL;
  234. $stm = $this->pdo->query($sql);
  235. $res = $stm->fetchAll(PDO::FETCH_ASSOC);
  236. return $res;
  237. }
  238. /**
  239. * Calculates the 10 top feeds based on their number of entries
  240. * @return array<array{'id':int,'name':string,'category':string,'count':int}>
  241. */
  242. public function calculateTopFeed(): array {
  243. $sql = <<<SQL
  244. SELECT f.id AS id
  245. , MAX(f.name) AS name
  246. , MAX(c.name) AS category
  247. , COUNT(e.id) AS count
  248. FROM `_category` AS c, `_feed` AS f, `_entry` AS e
  249. WHERE c.id = f.category
  250. AND f.id = e.id_feed
  251. GROUP BY f.id
  252. ORDER BY count DESC
  253. LIMIT 10
  254. SQL;
  255. $stm = $this->pdo->query($sql);
  256. return $stm->fetchAll(PDO::FETCH_ASSOC);
  257. }
  258. /**
  259. * Calculates the last publication date for each feed
  260. * @return array<array{'id':int,'name':string,'last_date':int,'nb_articles':int}>
  261. */
  262. public function calculateFeedLastDate(): array {
  263. $sql = <<<SQL
  264. SELECT MAX(f.id) as id
  265. , MAX(f.name) AS name
  266. , MAX(date) AS last_date
  267. , COUNT(*) AS nb_articles
  268. FROM `_feed` AS f, `_entry` AS e
  269. WHERE f.id = e.id_feed
  270. GROUP BY f.id
  271. ORDER BY name
  272. SQL;
  273. $stm = $this->pdo->query($sql);
  274. return $stm->fetchAll(PDO::FETCH_ASSOC);
  275. }
  276. /**
  277. * Gets days ready for graphs
  278. * @return array<string>
  279. */
  280. public function getDays(): array {
  281. return $this->convertToTranslatedJson(array(
  282. 'sun',
  283. 'mon',
  284. 'tue',
  285. 'wed',
  286. 'thu',
  287. 'fri',
  288. 'sat',
  289. ));
  290. }
  291. /**
  292. * Gets months ready for graphs
  293. * @return array<string>
  294. */
  295. public function getMonths(): array {
  296. return $this->convertToTranslatedJson(array(
  297. 'jan',
  298. 'feb',
  299. 'mar',
  300. 'apr',
  301. 'may_',
  302. 'jun',
  303. 'jul',
  304. 'aug',
  305. 'sep',
  306. 'oct',
  307. 'nov',
  308. 'dec',
  309. ));
  310. }
  311. /**
  312. * Translates array content
  313. * @param array<string> $data
  314. * @return array<string>
  315. */
  316. private function convertToTranslatedJson(array $data = array()) {
  317. $translated = array_map(function($a) {
  318. return _t('gen.date.' . $a);
  319. }, $data);
  320. return $translated;
  321. }
  322. }