StatsDAO.php 9.1 KB

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