statsController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. 'style-src' => "'self' 'unsafe-inline'",
  18. ]);
  19. $catDAO = FreshRSS_Factory::createCategoryDao();
  20. $feedDAO = FreshRSS_Factory::createFeedDao();
  21. $catDAO->checkDefault();
  22. $feedDAO->updateTTL();
  23. $this->view->categories = $catDAO->listSortedCategories(false);
  24. $this->view->default_category = $catDAO->getDefault();
  25. FreshRSS_View::prependTitle(_t('admin.stats.title') . ' · ');
  26. }
  27. private function convertToSeries($data) {
  28. $series = array();
  29. foreach ($data as $key => $value) {
  30. $series[] = array($key, $value);
  31. }
  32. return $series;
  33. }
  34. private function convertToPieSeries($data) {
  35. $series = array();
  36. foreach ($data as $value) {
  37. $value['data'] = array(array(0, (int) $value['data']));
  38. $series[] = $value;
  39. }
  40. return $series;
  41. }
  42. /**
  43. * This action handles the statistic main page.
  44. *
  45. * It displays the statistic main page.
  46. * The values computed to display the page are:
  47. * - repartition of read/unread/favorite/not favorite (repartition)
  48. * - number of article per day (entryCount)
  49. * - number of feed by category (feedByCategory)
  50. * - number of article by category (entryByCategory)
  51. * - list of most prolific feed (topFeed)
  52. */
  53. public function indexAction() {
  54. $statsDAO = FreshRSS_Factory::createStatsDAO();
  55. FreshRSS_View::appendScript(Minz_Url::display('/scripts/vendor/chart.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/vendor/chart.min.js')));
  56. $this->view->repartition = $statsDAO->calculateEntryRepartition();
  57. $entryCount = $statsDAO->calculateEntryCount();
  58. $this->view->entryCount = $entryCount;
  59. $this->view->average = round(array_sum(array_values($entryCount)) / count($entryCount), 2);
  60. $feedByCategory_calculated = $statsDAO->calculateFeedByCategory();
  61. $feedByCategory = [];
  62. for ($i = 0; $i < count($feedByCategory_calculated); $i++) {
  63. $feedByCategory['label'][$i] = $feedByCategory_calculated[$i]['label'];
  64. $feedByCategory['data'][$i] = $feedByCategory_calculated[$i]['data'];
  65. }
  66. $this->view->feedByCategory = $feedByCategory;
  67. $entryByCategory_calculated = $statsDAO->calculateEntryByCategory();
  68. $entryByCategory = [];
  69. for ($i = 0; $i < count($entryByCategory_calculated); $i++) {
  70. $entryByCategory['label'][$i] = $entryByCategory_calculated[$i]['label'];
  71. $entryByCategory['data'][$i] = $entryByCategory_calculated[$i]['data'];
  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'));
  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() {
  88. $id = Minz_Request::param('id');
  89. $ajax = Minz_Request::param('ajax');
  90. if ($ajax) {
  91. $url_redirect = array('c' => 'subscription', 'a' => 'feed', 'params' => array('id' => $id, 'from' => 'stats', 'ajax' => $ajax));
  92. } else {
  93. $url_redirect = array('c' => 'subscription', 'a' => 'feed', 'params' => array('id' => $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() {
  112. $statsDAO = FreshRSS_Factory::createStatsDAO();
  113. $feeds = $statsDAO->calculateFeedLastDate();
  114. $idleFeeds = array(
  115. 'last_5_year' => array(),
  116. 'last_3_year' => array(),
  117. 'last_2_year' => array(),
  118. 'last_year' => array(),
  119. 'last_6_month' => array(),
  120. 'last_3_month' => array(),
  121. 'last_month' => array(),
  122. 'last_week' => array(),
  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. $feed['favicon'] = $feedDAO->searchById($feed['id'])->favicon();
  145. $feedDate->setTimestamp($feed['last_date']);
  146. if ($feedDate >= $lastWeek) {
  147. continue;
  148. }
  149. if ($feedDate < $last5Year) {
  150. $idleFeeds['last_5_year'][] = $feed;
  151. } elseif ($feedDate < $last3Year) {
  152. $idleFeeds['last_3_year'][] = $feed;
  153. } elseif ($feedDate < $last2Year) {
  154. $idleFeeds['last_2_year'][] = $feed;
  155. } elseif ($feedDate < $lastYear) {
  156. $idleFeeds['last_year'][] = $feed;
  157. } elseif ($feedDate < $last6Month) {
  158. $idleFeeds['last_6_month'][] = $feed;
  159. } elseif ($feedDate < $last3Month) {
  160. $idleFeeds['last_3_month'][] = $feed;
  161. } elseif ($feedDate < $lastMonth) {
  162. $idleFeeds['last_month'][] = $feed;
  163. } elseif ($feedDate < $lastWeek) {
  164. $idleFeeds['last_week'][] = $feed;
  165. }
  166. }
  167. $this->view->idleFeeds = $idleFeeds;
  168. $id = Minz_Request::param('id');
  169. $this->view->displaySlider = false;
  170. if (false !== $id) {
  171. $this->view->displaySlider = true;
  172. $feedDAO = FreshRSS_Factory::createFeedDao();
  173. $this->view->feed = $feedDAO->searchById($id);
  174. }
  175. }
  176. /**
  177. * This action handles the article repartition statistic page.
  178. *
  179. * It displays the number of article and the average of article for the
  180. * following periods:
  181. * - hour of the day
  182. * - day of the week
  183. * - month
  184. *
  185. * @todo verify that the metrics used here make some sense. Especially
  186. * for the average.
  187. */
  188. public function repartitionAction() {
  189. $statsDAO = FreshRSS_Factory::createStatsDAO();
  190. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  191. $feedDAO = FreshRSS_Factory::createFeedDao();
  192. FreshRSS_View::appendScript(Minz_Url::display('/scripts/vendor/chart.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/vendor/chart.min.js')));
  193. $id = Minz_Request::param('id', null);
  194. $this->view->categories = $categoryDAO->listCategories();
  195. $this->view->feed = $feedDAO->searchById($id);
  196. $this->view->days = $statsDAO->getDays();
  197. $this->view->months = $statsDAO->getMonths();
  198. $this->view->repartition = $statsDAO->calculateEntryRepartitionPerFeed($id);
  199. $this->view->repartitionHour = $statsDAO->calculateEntryRepartitionPerFeedPerHour($id);
  200. $this->view->averageHour = $statsDAO->calculateEntryAveragePerFeedPerHour($id);
  201. $this->view->repartitionDayOfWeek = $statsDAO->calculateEntryRepartitionPerFeedPerDayOfWeek($id);
  202. $this->view->averageDayOfWeek = $statsDAO->calculateEntryAveragePerFeedPerDayOfWeek($id);
  203. $this->view->repartitionMonth = $statsDAO->calculateEntryRepartitionPerFeedPerMonth($id);
  204. $this->view->averageMonth = $statsDAO->calculateEntryAveragePerFeedPerMonth($id);
  205. $hours24Labels = [];
  206. for ($i = 0; $i < 24; $i++) {
  207. $hours24Labels[$i] = $i . ':xx';
  208. }
  209. $this->view->hours24Labels = $hours24Labels;
  210. }
  211. }