statsController.php 4.9 KB

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