statsController.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * Controller to handle application statistics.
  5. */
  6. class FreshRSS_stats_Controller extends FreshRSS_ActionController {
  7. /**
  8. * @var FreshRSS_ViewStats
  9. * @phpstan-ignore property.phpDocType
  10. */
  11. protected $view;
  12. public function __construct() {
  13. parent::__construct(FreshRSS_ViewStats::class);
  14. }
  15. /**
  16. * This action is called before every other action in that class. It is
  17. * the common boilerplate for every action. It is triggered by the
  18. * underlying framework.
  19. */
  20. #[\Override]
  21. public function firstAction(): void {
  22. if (!FreshRSS_Auth::hasAccess()) {
  23. Minz_Error::error(403);
  24. }
  25. $this->_csp([
  26. 'default-src' => "'self'",
  27. 'frame-ancestors' => "'none'",
  28. 'img-src' => '* data: blob:',
  29. ]);
  30. $catDAO = FreshRSS_Factory::createCategoryDao();
  31. $catDAO->checkDefault();
  32. $this->view->categories = $catDAO->listSortedCategories(prePopulateFeeds: false);
  33. FreshRSS_View::prependTitle(_t('admin.stats.title') . ' · ');
  34. }
  35. /**
  36. * This action handles the statistic main page.
  37. *
  38. * It displays the statistic main page.
  39. * The values computed to display the page are:
  40. * - repartition of read/unread/favorite/not favorite (repartition)
  41. * - number of article per day (entryCount)
  42. * - number of feed by category (feedByCategory)
  43. * - number of article by category (entryByCategory)
  44. * - list of most prolific feed (topFeed)
  45. */
  46. public function indexAction(): void {
  47. $statsDAO = FreshRSS_Factory::createStatsDAO();
  48. FreshRSS_View::appendScript(Minz_Url::display('/scripts/vendor/chart.umd.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/vendor/chart.umd.min.js')));
  49. $this->view->repartitions = $statsDAO->calculateEntryRepartition();
  50. $entryCount = $statsDAO->calculateEntryCount();
  51. if (count($entryCount) > 0) {
  52. $this->view->entryCount = $entryCount;
  53. $this->view->average = round(array_sum(array_values($entryCount)) / count($entryCount), 2);
  54. } else {
  55. $this->view->entryCount = [];
  56. $this->view->average = -1.0;
  57. }
  58. $feedByCategory = [];
  59. $feedByCategory_calculated = $statsDAO->calculateFeedByCategory();
  60. for ($i = 0; $i < count($feedByCategory_calculated); $i++) {
  61. $feedByCategory['label'][$i] = $feedByCategory_calculated[$i]['label'];
  62. $feedByCategory['data'][$i] = $feedByCategory_calculated[$i]['data'];
  63. }
  64. $this->view->feedByCategory = $feedByCategory;
  65. $entryByCategory = [];
  66. $entryByCategory_calculated = $statsDAO->calculateEntryByCategory();
  67. for ($i = 0; $i < count($entryByCategory_calculated); $i++) {
  68. $entryByCategory['label'][$i] = $entryByCategory_calculated[$i]['label'];
  69. $entryByCategory['data'][$i] = $entryByCategory_calculated[$i]['data'];
  70. }
  71. $this->view->entryByCategory = $entryByCategory;
  72. $this->view->topFeed = $statsDAO->calculateTopFeed();
  73. $last30DaysLabels = [];
  74. for ($i = 0; $i < 31; $i++) {
  75. $last30DaysLabels[$i] = date('d.m.Y', strtotime((-30 + $i) . ' days') ?: null);
  76. }
  77. $this->view->last30DaysLabels = $last30DaysLabels;
  78. }
  79. /**
  80. * This action handles the feed action on the idle statistic page.
  81. * set the 'from' parameter to remember that it had a redirection coming from stats controller,
  82. * to use the subscription controller to save it,
  83. * but shows the stats idle page
  84. */
  85. public function feedAction(): void {
  86. $id = Minz_Request::paramInt('id');
  87. $ajax = Minz_Request::paramBoolean('ajax');
  88. if ($ajax) {
  89. $url_redirect = ['c' => 'subscription', 'a' => 'feed', 'params' => ['id' => (string)$id, 'from' => 'stats', 'ajax' => (string)$ajax]];
  90. } else {
  91. $url_redirect = ['c' => 'subscription', 'a' => 'feed', 'params' => ['id' => (string)$id, 'from' => 'stats']];
  92. }
  93. Minz_Request::forward($url_redirect, true);
  94. }
  95. /**
  96. * This action handles the idle feed statistic page.
  97. *
  98. * It displays the list of idle feed for different period. The supported
  99. * periods are:
  100. * - last 5 years
  101. * - last 3 years
  102. * - last 2 years
  103. * - last year
  104. * - last 6 months
  105. * - last 3 months
  106. * - last month
  107. * - last week
  108. */
  109. public function idleAction(): void {
  110. FreshRSS_View::appendScript(Minz_Url::display('/scripts/feed.js?' . @filemtime(PUBLIC_PATH . '/scripts/feed.js')));
  111. $feed_dao = FreshRSS_Factory::createFeedDao();
  112. $statsDAO = FreshRSS_Factory::createStatsDAO();
  113. $feeds = $statsDAO->calculateFeedLastDate();
  114. $idleFeeds = [
  115. 'last_5_year' => [],
  116. 'last_3_year' => [],
  117. 'last_2_year' => [],
  118. 'last_year' => [],
  119. 'last_6_month' => [],
  120. 'last_3_month' => [],
  121. 'last_month' => [],
  122. 'last_week' => [],
  123. ];
  124. $now = new \DateTime();
  125. $feedDate = clone $now;
  126. $lastWeek = clone $now;
  127. $lastWeek->modify('-1 week');
  128. $lastMonth = clone $now;
  129. $lastMonth->modify('-1 month');
  130. $last3Month = clone $now;
  131. $last3Month->modify('-3 month');
  132. $last6Month = clone $now;
  133. $last6Month->modify('-6 month');
  134. $lastYear = clone $now;
  135. $lastYear->modify('-1 year');
  136. $last2Year = clone $now;
  137. $last2Year->modify('-2 year');
  138. $last3Year = clone $now;
  139. $last3Year->modify('-3 year');
  140. $last5Year = clone $now;
  141. $last5Year->modify('-5 year');
  142. foreach ($feeds as $feed) {
  143. $feedDAO = FreshRSS_Factory::createFeedDao();
  144. $feedObject = $feedDAO->searchById($feed['id']);
  145. if ($feedObject !== null) {
  146. $feed['favicon'] = $feedObject->favicon();
  147. }
  148. $feedDate->setTimestamp($feed['last_date']);
  149. if ($feedDate >= $lastWeek) {
  150. continue;
  151. }
  152. if ($feedDate < $last5Year) {
  153. $idleFeeds['last_5_year'][] = $feed;
  154. } elseif ($feedDate < $last3Year) {
  155. $idleFeeds['last_3_year'][] = $feed;
  156. } elseif ($feedDate < $last2Year) {
  157. $idleFeeds['last_2_year'][] = $feed;
  158. } elseif ($feedDate < $lastYear) {
  159. $idleFeeds['last_year'][] = $feed;
  160. } elseif ($feedDate < $last6Month) {
  161. $idleFeeds['last_6_month'][] = $feed;
  162. } elseif ($feedDate < $last3Month) {
  163. $idleFeeds['last_3_month'][] = $feed;
  164. } elseif ($feedDate < $lastMonth) {
  165. $idleFeeds['last_month'][] = $feed;
  166. } elseif ($feedDate < $lastWeek) {
  167. $idleFeeds['last_week'][] = $feed;
  168. }
  169. }
  170. $this->view->idleFeeds = $idleFeeds;
  171. $this->view->feeds = $feed_dao->listFeeds();
  172. $id = Minz_Request::paramInt('id');
  173. $this->view->displaySlider = false;
  174. if ($id !== 0) {
  175. $this->view->displaySlider = true;
  176. $feedDAO = FreshRSS_Factory::createFeedDao();
  177. $this->view->feed = $feedDAO->searchById($id) ?? FreshRSS_Feed::default();
  178. }
  179. }
  180. /**
  181. * This action handles the article repartition statistic page.
  182. *
  183. * It displays the number of article and the average of article for the
  184. * following periods:
  185. * - hour of the day
  186. * - day of the week
  187. * - month
  188. *
  189. * @todo verify that the metrics used here make some sense. Especially
  190. * for the average.
  191. */
  192. public function repartitionAction(): void {
  193. $statsDAO = FreshRSS_Factory::createStatsDAO();
  194. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  195. $feedDAO = FreshRSS_Factory::createFeedDao();
  196. FreshRSS_View::appendScript(Minz_Url::display('/scripts/vendor/chart.umd.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/vendor/chart.umd.min.js')));
  197. $id = Minz_Request::paramInt('id');
  198. if ($id === 0) {
  199. $id = null;
  200. }
  201. $this->view->categories = $categoryDAO->listCategories(prePopulateFeeds: true);
  202. $this->view->feed = $id === null ? FreshRSS_Feed::default() : ($feedDAO->searchById($id) ?? FreshRSS_Feed::default());
  203. $this->view->days = $statsDAO->getDays();
  204. $this->view->months = $statsDAO->getMonths();
  205. $this->view->repartition = $statsDAO->calculateEntryRepartitionPerFeed($id);
  206. $this->view->repartitionHour = $statsDAO->calculateEntryRepartitionPerFeedPerHour($id);
  207. $this->view->averageHour = $statsDAO->calculateEntryAveragePerFeedPerHour($id);
  208. $this->view->repartitionDayOfWeek = $statsDAO->calculateEntryRepartitionPerFeedPerDayOfWeek($id);
  209. $this->view->averageDayOfWeek = $statsDAO->calculateEntryAveragePerFeedPerDayOfWeek($id);
  210. $this->view->repartitionMonth = $statsDAO->calculateEntryRepartitionPerFeedPerMonth($id);
  211. $this->view->averageMonth = $statsDAO->calculateEntryAveragePerFeedPerMonth($id);
  212. $hours24Labels = [];
  213. for ($i = 0; $i < 24; $i++) {
  214. $hours24Labels[$i] = $i . ':xx';
  215. }
  216. $this->view->hours24Labels = $hours24Labels;
  217. }
  218. }