statsController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Controller to handle application statistics.
  4. */
  5. class FreshRSS_stats_Controller extends Minz_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. Minz_View::prependTitle(_t('admin.stats.title') . ' · ');
  20. }
  21. private function convertToSerie($data) {
  22. $serie = array();
  23. foreach ($data as $key => $value) {
  24. $serie[] = array($key, $value);
  25. }
  26. return $serie;
  27. }
  28. private function convertToPieSerie($data) {
  29. $serie = array();
  30. foreach ($data as $value) {
  31. $value['data'] = array(array(0, (int) $value['data']));
  32. $serie[] = $value;
  33. }
  34. return $serie;
  35. }
  36. /**
  37. * This action handles the statistic main page.
  38. *
  39. * It displays the statistic main page.
  40. * The values computed to display the page are:
  41. * - repartition of read/unread/favorite/not favorite
  42. * - number of article per day
  43. * - number of feed by category
  44. * - number of article by category
  45. * - list of most prolific feed
  46. */
  47. public function indexAction() {
  48. $statsDAO = FreshRSS_Factory::createStatsDAO();
  49. Minz_View::appendScript(Minz_Url::display('/scripts/flotr2.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/flotr2.min.js')));
  50. $this->view->repartition = $statsDAO->calculateEntryRepartition();
  51. $entryCount = $statsDAO->calculateEntryCount();
  52. $this->view->count = $this->convertToSerie($entryCount);
  53. $this->view->average = round(array_sum(array_values($entryCount)) / count($entryCount), 2);
  54. $this->view->feedByCategory = $this->convertToPieSerie($statsDAO->calculateFeedByCategory());
  55. $this->view->entryByCategory = $this->convertToPieSerie($statsDAO->calculateEntryByCategory());
  56. $this->view->topFeed = $statsDAO->calculateTopFeed();
  57. }
  58. /**
  59. * This action handles the idle feed statistic page.
  60. *
  61. * It displays the list of idle feed for different period. The supported
  62. * periods are:
  63. * - last 5 years
  64. * - last 3 years
  65. * - last 2 years
  66. * - last year
  67. * - last 6 months
  68. * - last 3 months
  69. * - last month
  70. * - last week
  71. */
  72. public function idleAction() {
  73. $statsDAO = FreshRSS_Factory::createStatsDAO();
  74. $feeds = $statsDAO->calculateFeedLastDate();
  75. $idleFeeds = array(
  76. 'last_5_year' => array(),
  77. 'last_3_year' => array(),
  78. 'last_2_year' => array(),
  79. 'last_year' => array(),
  80. 'last_6_month' => array(),
  81. 'last_3_month' => array(),
  82. 'last_month' => array(),
  83. 'last_week' => array(),
  84. );
  85. $now = new \DateTime();
  86. $feedDate = clone $now;
  87. $lastWeek = clone $now;
  88. $lastWeek->modify('-1 week');
  89. $lastMonth = clone $now;
  90. $lastMonth->modify('-1 month');
  91. $last3Month = clone $now;
  92. $last3Month->modify('-3 month');
  93. $last6Month = clone $now;
  94. $last6Month->modify('-6 month');
  95. $lastYear = clone $now;
  96. $lastYear->modify('-1 year');
  97. $last2Year = clone $now;
  98. $last2Year->modify('-2 year');
  99. $last3Year = clone $now;
  100. $last3Year->modify('-3 year');
  101. $last5Year = clone $now;
  102. $last5Year->modify('-5 year');
  103. foreach ($feeds as $feed) {
  104. $feedDate->setTimestamp($feed['last_date']);
  105. if ($feedDate >= $lastWeek) {
  106. continue;
  107. }
  108. if ($feedDate < $last5Year) {
  109. $idleFeeds['last_5_year'][] = $feed;
  110. } elseif ($feedDate < $last3Year) {
  111. $idleFeeds['last_3_year'][] = $feed;
  112. } elseif ($feedDate < $last2Year) {
  113. $idleFeeds['last_2_year'][] = $feed;
  114. } elseif ($feedDate < $lastYear) {
  115. $idleFeeds['last_year'][] = $feed;
  116. } elseif ($feedDate < $last6Month) {
  117. $idleFeeds['last_6_month'][] = $feed;
  118. } elseif ($feedDate < $last3Month) {
  119. $idleFeeds['last_3_month'][] = $feed;
  120. } elseif ($feedDate < $lastMonth) {
  121. $idleFeeds['last_month'][] = $feed;
  122. } elseif ($feedDate < $lastWeek) {
  123. $idleFeeds['last_week'][] = $feed;
  124. }
  125. }
  126. $this->view->idleFeeds = $idleFeeds;
  127. }
  128. /**
  129. * This action handles the article repartition statistic page.
  130. *
  131. * It displays the number of article and the average of article for the
  132. * following periods:
  133. * - hour of the day
  134. * - day of the week
  135. * - month
  136. *
  137. * @todo verify that the metrics used here make some sense. Especially
  138. * for the average.
  139. */
  140. public function repartitionAction() {
  141. $statsDAO = FreshRSS_Factory::createStatsDAO();
  142. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  143. $feedDAO = FreshRSS_Factory::createFeedDao();
  144. Minz_View::appendScript(Minz_Url::display('/scripts/flotr2.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/flotr2.min.js')));
  145. $id = Minz_Request::param('id', null);
  146. $this->view->categories = $categoryDAO->listCategories();
  147. $this->view->feed = $feedDAO->searchById($id);
  148. $this->view->days = $statsDAO->getDays();
  149. $this->view->months = $statsDAO->getMonths();
  150. $this->view->repartition = $statsDAO->calculateEntryRepartitionPerFeed($id);
  151. $this->view->repartitionHour = $this->convertToSerie($statsDAO->calculateEntryRepartitionPerFeedPerHour($id));
  152. $this->view->averageHour = $statsDAO->calculateEntryAveragePerFeedPerHour($id);
  153. $this->view->repartitionDayOfWeek = $this->convertToSerie($statsDAO->calculateEntryRepartitionPerFeedPerDayOfWeek($id));
  154. $this->view->averageDayOfWeek = $statsDAO->calculateEntryAveragePerFeedPerDayOfWeek($id);
  155. $this->view->repartitionMonth = $this->convertToSerie($statsDAO->calculateEntryRepartitionPerFeedPerMonth($id));
  156. $this->view->averageMonth = $statsDAO->calculateEntryAveragePerFeedPerMonth($id);
  157. }
  158. }