statsController.php 4.3 KB

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