statsController.php 4.3 KB

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