statsController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Controller to handle application statistics.
  4. */
  5. class FreshRSS_stats_Controller extends Minz_ActionController {
  6. /**
  7. * This action handles the statistic main page.
  8. *
  9. * It displays the statistic main page.
  10. * The values computed to display the page are:
  11. * - repartition of read/unread/favorite/not favorite
  12. * - number of article per day
  13. * - number of feed by category
  14. * - number of article by category
  15. * - list of most prolific feed
  16. */
  17. public function indexAction() {
  18. $statsDAO = FreshRSS_Factory::createStatsDAO();
  19. Minz_View::appendScript(Minz_Url::display('/scripts/flotr2.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/flotr2.min.js')));
  20. $this->view->repartition = $statsDAO->calculateEntryRepartition();
  21. $this->view->count = $statsDAO->calculateEntryCount();
  22. $this->view->average = $statsDAO->calculateEntryAverage();
  23. $this->view->feedByCategory = $statsDAO->calculateFeedByCategory();
  24. $this->view->entryByCategory = $statsDAO->calculateEntryByCategory();
  25. $this->view->topFeed = $statsDAO->calculateTopFeed();
  26. }
  27. /**
  28. * This action handles the idle feed statistic page.
  29. *
  30. * It displays the list of idle feed for different period. The supported
  31. * periods are:
  32. * - last year
  33. * - last 6 months
  34. * - last 3 months
  35. * - last month
  36. * - last week
  37. */
  38. public function idleAction() {
  39. $statsDAO = FreshRSS_Factory::createStatsDAO();
  40. $feeds = $statsDAO->calculateFeedLastDate();
  41. $idleFeeds = array(
  42. 'last_year' => array(),
  43. 'last_6_month' => array(),
  44. 'last_3_month' => array(),
  45. 'last_month' => array(),
  46. 'last_week' => array(),
  47. );
  48. $now = new \DateTime();
  49. $feedDate = clone $now;
  50. $lastWeek = clone $now;
  51. $lastWeek->modify('-1 week');
  52. $lastMonth = clone $now;
  53. $lastMonth->modify('-1 month');
  54. $last3Month = clone $now;
  55. $last3Month->modify('-3 month');
  56. $last6Month = clone $now;
  57. $last6Month->modify('-6 month');
  58. $lastYear = clone $now;
  59. $lastYear->modify('-1 year');
  60. foreach ($feeds as $feed) {
  61. $feedDate->setTimestamp($feed['last_date']);
  62. if ($feedDate >= $lastWeek) {
  63. continue;
  64. }
  65. if ($feedDate < $lastYear) {
  66. $idleFeeds['last_year'][] = $feed;
  67. } elseif ($feedDate < $last6Month) {
  68. $idleFeeds['last_6_month'][] = $feed;
  69. } elseif ($feedDate < $last3Month) {
  70. $idleFeeds['last_3_month'][] = $feed;
  71. } elseif ($feedDate < $lastMonth) {
  72. $idleFeeds['last_month'][] = $feed;
  73. } elseif ($feedDate < $lastWeek) {
  74. $idleFeeds['last_week'][] = $feed;
  75. }
  76. }
  77. $this->view->idleFeeds = $idleFeeds;
  78. }
  79. /**
  80. * This action handles the article repartition statistic page.
  81. *
  82. * It displays the number of article and the average of article for the
  83. * following periods:
  84. * - hour of the day
  85. * - day of the week
  86. * - month
  87. *
  88. * @todo verify that the metrics used here make some sense. Especially
  89. * for the average.
  90. */
  91. public function repartitionAction() {
  92. $statsDAO = FreshRSS_Factory::createStatsDAO();
  93. $categoryDAO = new FreshRSS_CategoryDAO();
  94. $feedDAO = FreshRSS_Factory::createFeedDao();
  95. Minz_View::appendScript(Minz_Url::display('/scripts/flotr2.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/flotr2.min.js')));
  96. $id = Minz_Request::param ('id', null);
  97. $this->view->categories = $categoryDAO->listCategories();
  98. $this->view->feed = $feedDAO->searchById($id);
  99. $this->view->days = $statsDAO->getDays();
  100. $this->view->months = $statsDAO->getMonths();
  101. $this->view->repartitionHour = $statsDAO->calculateEntryRepartitionPerFeedPerHour($id);
  102. $this->view->averageHour = $statsDAO->calculateEntryAveragePerFeedPerHour($id);
  103. $this->view->repartitionDayOfWeek = $statsDAO->calculateEntryRepartitionPerFeedPerDayOfWeek($id);
  104. $this->view->averageDayOfWeek = $statsDAO->calculateEntryAveragePerFeedPerDayOfWeek($id);
  105. $this->view->repartitionMonth = $statsDAO->calculateEntryRepartitionPerFeedPerMonth($id);
  106. $this->view->averageMonth = $statsDAO->calculateEntryAveragePerFeedPerMonth($id);
  107. }
  108. /**
  109. * This action is called before every other action in that class. It is
  110. * the common boiler plate for every action. It is triggered by the
  111. * underlying framework.
  112. */
  113. public function firstAction() {
  114. if (!$this->view->loginOk) {
  115. Minz_Error::error(
  116. 403, array('error' => array(_t('access_denied')))
  117. );
  118. }
  119. Minz_View::prependTitle(_t('stats') . ' · ');
  120. }
  121. }