statsController.php 8.0 KB

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