StatsDAO.php 12 KB

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