statsController.php 5.0 KB

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