statsController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 year
  65. * - last 6 months
  66. * - last 3 months
  67. * - last month
  68. * - last week
  69. */
  70. public function idleAction() {
  71. $statsDAO = FreshRSS_Factory::createStatsDAO();
  72. $feeds = $statsDAO->calculateFeedLastDate();
  73. $idleFeeds = array(
  74. 'last_year' => array(),
  75. 'last_6_month' => array(),
  76. 'last_3_month' => array(),
  77. 'last_month' => array(),
  78. 'last_week' => array(),
  79. );
  80. $now = new \DateTime();
  81. $feedDate = clone $now;
  82. $lastWeek = clone $now;
  83. $lastWeek->modify('-1 week');
  84. $lastMonth = clone $now;
  85. $lastMonth->modify('-1 month');
  86. $last3Month = clone $now;
  87. $last3Month->modify('-3 month');
  88. $last6Month = clone $now;
  89. $last6Month->modify('-6 month');
  90. $lastYear = clone $now;
  91. $lastYear->modify('-1 year');
  92. foreach ($feeds as $feed) {
  93. $feedDate->setTimestamp($feed['last_date']);
  94. if ($feedDate >= $lastWeek) {
  95. continue;
  96. }
  97. if ($feedDate < $lastYear) {
  98. $idleFeeds['last_year'][] = $feed;
  99. } elseif ($feedDate < $last6Month) {
  100. $idleFeeds['last_6_month'][] = $feed;
  101. } elseif ($feedDate < $last3Month) {
  102. $idleFeeds['last_3_month'][] = $feed;
  103. } elseif ($feedDate < $lastMonth) {
  104. $idleFeeds['last_month'][] = $feed;
  105. } elseif ($feedDate < $lastWeek) {
  106. $idleFeeds['last_week'][] = $feed;
  107. }
  108. }
  109. $this->view->idleFeeds = $idleFeeds;
  110. }
  111. /**
  112. * This action handles the article repartition statistic page.
  113. *
  114. * It displays the number of article and the average of article for the
  115. * following periods:
  116. * - hour of the day
  117. * - day of the week
  118. * - month
  119. *
  120. * @todo verify that the metrics used here make some sense. Especially
  121. * for the average.
  122. */
  123. public function repartitionAction() {
  124. $statsDAO = FreshRSS_Factory::createStatsDAO();
  125. $categoryDAO = FreshRSS_Factory::createCategoryDao();
  126. $feedDAO = FreshRSS_Factory::createFeedDao();
  127. Minz_View::appendScript(Minz_Url::display('/scripts/flotr2.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/flotr2.min.js')));
  128. $id = Minz_Request::param('id', null);
  129. $this->view->categories = $categoryDAO->listCategories();
  130. $this->view->feed = $feedDAO->searchById($id);
  131. $this->view->days = $statsDAO->getDays();
  132. $this->view->months = $statsDAO->getMonths();
  133. $this->view->repartition = $statsDAO->calculateEntryRepartitionPerFeed($id);
  134. $this->view->repartitionHour = $this->convertToSerie($statsDAO->calculateEntryRepartitionPerFeedPerHour($id));
  135. $this->view->averageHour = $statsDAO->calculateEntryAveragePerFeedPerHour($id);
  136. $this->view->repartitionDayOfWeek = $this->convertToSerie($statsDAO->calculateEntryRepartitionPerFeedPerDayOfWeek($id));
  137. $this->view->averageDayOfWeek = $statsDAO->calculateEntryAveragePerFeedPerDayOfWeek($id);
  138. $this->view->repartitionMonth = $this->convertToSerie($statsDAO->calculateEntryRepartitionPerFeedPerMonth($id));
  139. $this->view->averageMonth = $statsDAO->calculateEntryAveragePerFeedPerMonth($id);
  140. }
  141. }