statsController.php 8.2 KB

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