StatsDAO.php 11 KB

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