statsController.php 8.0 KB

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