statsController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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::prependScript(Minz_Url::display('/scripts/jquery.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/jquery.min.js')));
  50. Minz_View::appendScript(Minz_Url::display('/scripts/flotr2.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/flotr2.min.js')));
  51. $this->view->repartition = $statsDAO->calculateEntryRepartition();
  52. $entryCount = $statsDAO->calculateEntryCount();
  53. $this->view->count = $this->convertToSerie($entryCount);
  54. $this->view->average = round(array_sum(array_values($entryCount)) / count($entryCount), 2);
  55. $this->view->feedByCategory = $this->convertToPieSerie($statsDAO->calculateFeedByCategory());
  56. $this->view->entryByCategory = $this->convertToPieSerie($statsDAO->calculateEntryByCategory());
  57. $this->view->topFeed = $statsDAO->calculateTopFeed();
  58. }
  59. /**
  60. * This action handles the idle feed statistic page.
  61. *
  62. * It displays the list of idle feed for different period. The supported
  63. * periods are:
  64. * - last 5 years
  65. * - last 3 years
  66. * - last 2 years
  67. * - last year
  68. * - last 6 months
  69. * - last 3 months
  70. * - last month
  71. * - last week
  72. */
  73. public function idleAction() {
  74. $statsDAO = FreshRSS_Factory::createStatsDAO();
  75. $feeds = $statsDAO->calculateFeedLastDate();
  76. $idleFeeds = array(
  77. 'last_5_year' => array(),
  78. 'last_3_year' => array(),
  79. 'last_2_year' => array(),
  80. 'last_year' => array(),
  81. 'last_6_month' => array(),
  82. 'last_3_month' => array(),
  83. 'last_month' => array(),
  84. 'last_week' => array(),
  85. );
  86. $now = new \DateTime();
  87. $feedDate = clone $now;
  88. $lastWeek = clone $now;
  89. $lastWeek->modify('-1 week');
  90. $lastMonth = clone $now;
  91. $lastMonth->modify('-1 month');
  92. $last3Month = clone $now;
  93. $last3Month->modify('-3 month');
  94. $last6Month = clone $now;
  95. $last6Month->modify('-6 month');
  96. $lastYear = clone $now;
  97. $lastYear->modify('-1 year');
  98. $last2Year = clone $now;
  99. $last2Year->modify('-2 year');
  100. $last3Year = clone $now;
  101. $last3Year->modify('-3 year');
  102. $last5Year = clone $now;
  103. $last5Year->modify('-5 year');
  104. foreach ($feeds as $feed) {
  105. $feedDate->setTimestamp($feed['last_date']);
  106. if ($feedDate >= $lastWeek) {
  107. continue;
  108. }
  109. if ($feedDate < $last5Year) {
  110. $idleFeeds['last_5_year'][] = $feed;
  111. } elseif ($feedDate < $last3Year) {
  112. $idleFeeds['last_3_year'][] = $feed;
  113. } elseif ($feedDate < $last2Year) {
  114. $idleFeeds['last_2_year'][] = $feed;
  115. } elseif ($feedDate < $lastYear) {
  116. $idleFeeds['last_year'][] = $feed;
  117. } elseif ($feedDate < $last6Month) {
  118. $idleFeeds['last_6_month'][] = $feed;
  119. } elseif ($feedDate < $last3Month) {
  120. $idleFeeds['last_3_month'][] = $feed;
  121. } elseif ($feedDate < $lastMonth) {
  122. $idleFeeds['last_month'][] = $feed;
  123. } elseif ($feedDate < $lastWeek) {
  124. $idleFeeds['last_week'][] = $feed;
  125. }
  126. }
  127. $this->view->idleFeeds = $idleFeeds;
  128. }
  129. /**
  130. * This action handles the article repartition statistic page.
  131. *
  132. * It displays the number of article and the average of article for the
  133. * following periods:
  134. * - hour of the day
  135. * - day of the week
  136. * - month
  137. *
  138. * @todo verify that the metrics used here make some sense. Especially
  139. * for the average.
  140. */
  141. public function repartitionAction() {
  142. $statsDAO = FreshRSS_Factory::createStatsDAO();
  143. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  144. $feedDAO = FreshRSS_Factory::createFeedDao();
  145. Minz_View::appendScript(Minz_Url::display('/scripts/flotr2.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/flotr2.min.js')));
  146. $id = Minz_Request::param('id', null);
  147. $this->view->categories = $categoryDAO->listCategories();
  148. $this->view->feed = $feedDAO->searchById($id);
  149. $this->view->days = $statsDAO->getDays();
  150. $this->view->months = $statsDAO->getMonths();
  151. $this->view->repartition = $statsDAO->calculateEntryRepartitionPerFeed($id);
  152. $this->view->repartitionHour = $this->convertToSerie($statsDAO->calculateEntryRepartitionPerFeedPerHour($id));
  153. $this->view->averageHour = $statsDAO->calculateEntryAveragePerFeedPerHour($id);
  154. $this->view->repartitionDayOfWeek = $this->convertToSerie($statsDAO->calculateEntryRepartitionPerFeedPerDayOfWeek($id));
  155. $this->view->averageDayOfWeek = $statsDAO->calculateEntryAveragePerFeedPerDayOfWeek($id);
  156. $this->view->repartitionMonth = $this->convertToSerie($statsDAO->calculateEntryRepartitionPerFeedPerMonth($id));
  157. $this->view->averageMonth = $statsDAO->calculateEntryAveragePerFeedPerMonth($id);
  158. }
  159. }