Explorar o código

Refactor statistics

I made a new controller to handle statistics. The old statistics have been moved in that controller and a new action has been added to display idle feeds. I also added a menu in the left panel to navigate between the statistics pages.
See #90
Alexis Degrugillier %!s(int64=11) %!d(string=hai) anos
pai
achega
181fcd9889

+ 0 - 19
app/Controllers/indexController.php

@@ -198,25 +198,6 @@ class FreshRSS_index_Controller extends Minz_ActionController {
 		}
 	}
 	
-	public function statsAction () {
-		if (!$this->view->loginOk) {
-			Minz_Error::error (
-				403,
-				array ('error' => array (Minz_Translate::t ('access_denied')))
-			);
-		}
-
-		Minz_View::prependTitle (Minz_Translate::t ('stats') . ' · ');
-
-		$statsDAO = new FreshRSS_StatsDAO ();
-		Minz_View::appendScript (Minz_Url::display ('/scripts/flotr2.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/flotr2.min.js')));
-		$this->view->repartition = $statsDAO->calculateEntryRepartition();
-		$this->view->count = ($statsDAO->calculateEntryCount());
-		$this->view->feedByCategory = $statsDAO->calculateFeedByCategory();
-		$this->view->entryByCategory = $statsDAO->calculateEntryByCategory();
-		$this->view->topFeed = $statsDAO->calculateTopFeed();
-	}
-
 	public function aboutAction () {
 		Minz_View::prependTitle (Minz_Translate::t ('about') . ' · ');
 	}

+ 73 - 0
app/Controllers/statsController.php

@@ -0,0 +1,73 @@
+<?php
+
+class FreshRSS_stats_Controller extends Minz_ActionController {
+
+        public function mainAction() {
+                if (!$this->view->loginOk) {
+                        Minz_Error::error(
+                            403, array('error' => array(Minz_Translate::t('access_denied')))
+                        );
+                }
+
+                Minz_View::prependTitle(Minz_Translate::t('stats') . ' · ');
+                
+                $statsDAO = new FreshRSS_StatsDAO ();
+		Minz_View::appendScript (Minz_Url::display ('/scripts/flotr2.min.js?' . @filemtime(PUBLIC_PATH . '/scripts/flotr2.min.js')));
+		$this->view->repartition = $statsDAO->calculateEntryRepartition();
+		$this->view->count = ($statsDAO->calculateEntryCount());
+		$this->view->feedByCategory = $statsDAO->calculateFeedByCategory();
+		$this->view->entryByCategory = $statsDAO->calculateEntryByCategory();
+		$this->view->topFeed = $statsDAO->calculateTopFeed();
+        }
+
+        public function idleAction() {
+                if (!$this->view->loginOk) {
+                        Minz_Error::error(
+                            403, array('error' => array(Minz_Translate::t('access_denied')))
+                        );
+                }
+
+                Minz_View::prependTitle(Minz_Translate::t('stats') . ' · ');
+
+                $statsDAO = new FreshRSS_StatsDAO ();
+                $feeds = $statsDAO->calculateFeedLastDate();
+                $idleFeeds = array();
+                $now = new \DateTime();
+                $feedDate = clone $now;
+                $lastWeek = clone $now;
+                $lastWeek->modify('-1 week');
+                $lastMonth = clone $now;
+                $lastMonth->modify('-1 month');
+                $last3Month = clone $now;
+                $last3Month->modify('-3 month');
+                $last6Month = clone $now;
+                $last6Month->modify('-6 month');
+                $lastYear = clone $now;
+                $lastYear->modify('-1 year');
+
+                foreach ($feeds as $feed) {
+                        $feedDate->setTimestamp($feed['last_date']);
+                        if ($feedDate >= $lastWeek) {
+                                continue;
+                        }
+                        if ($feedDate < $lastWeek) {
+                                $idleFeeds['lastWeek'][] = $feed['name'];
+                        }
+                        if ($feedDate < $lastMonth) {
+                                $idleFeeds['lastMonth'][] = $feed['name'];
+                        }
+                        if ($feedDate < $last3Month) {
+                                $idleFeeds['last3Month'][] = $feed['name'];
+                        }
+                        if ($feedDate < $last6Month) {
+                                $idleFeeds['last6Month'][] = $feed['name'];
+                        }
+                        if ($feedDate < $lastYear) {
+                                $idleFeeds['lastYear'][] = $feed['name'];
+                        }
+                }
+
+                $this->view->idleFeeds = $idleFeeds;
+        }
+
+}

+ 20 - 0
app/Models/StatsDAO.php

@@ -180,6 +180,26 @@ SQL;
 		$stm->execute();
 		return $stm->fetchAll(PDO::FETCH_ASSOC);
 	}
+        
+        /**
+         * Calculates the last publication date for each feed
+         * 
+         * @return array
+         */
+        public function calculateFeedLastDate() {
+                $sql = <<<SQL
+SELECT MAX(f.name) AS name
+, MAX(date) AS last_date
+FROM {$this->prefix}feed AS f,
+{$this->prefix}entry AS e
+WHERE f.id = e.id_feed
+GROUP BY f.id
+ORDER BY name
+SQL;
+                $stm = $this->bd->prepare($sql);
+                $stm->execute();
+                return $stm->fetchAll(PDO::FETCH_ASSOC);
+        }
 
 	private function convertToSerie($data) {
 		$serie = array();

+ 8 - 0
app/i18n/en.php

@@ -46,6 +46,14 @@ return array (
 	'no_query_filter'		=> 'No filter',
 	'about'				=> 'About',
 	'stats'				=> 'Statistics',
+        'stats_idle'                    => 'Idle feeds',
+        'stats_main'                    => 'Main statistics',
+    
+        'last_week'                     => 'Last week',
+        'last_month'                    => 'Last month',
+        'last_3_month'                  => 'Last three months',
+        'last_6_month'                  => 'Last six months',
+        'last_year'                     => 'Last year',
 
 	'your_rss_feeds'		=> 'Your RSS feeds',
 	'add_rss_feed'			=> 'Add a RSS feed',

+ 8 - 0
app/i18n/fr.php

@@ -46,6 +46,14 @@ return array (
 	'no_query_filter'		=> 'Aucun filtre appliqué',
 	'about'				=> 'À propos',
 	'stats'				=> 'Statistiques',
+        'stats_idle'                    => 'Flux inactifs',
+        'stats_main'                    => 'Statistiques principales',
+
+        'last_week'                     => 'La dernière semaine',
+        'last_month'                    => 'Le dernier mois',
+        'last_3_month'                  => 'Les derniers trois mois',
+        'last_6_month'                  => 'Les derniers six mois',
+        'last_year'                     => 'La dernière année',
 
 	'your_rss_feeds'		=> 'Vos flux RSS',
 	'add_rss_feed'			=> 'Ajouter un flux RSS',

+ 9 - 0
app/layout/aside_stats.phtml

@@ -0,0 +1,9 @@
+<ul class="nav nav-list aside">
+	<li class="nav-header"><?php echo Minz_Translate::t ('stats'); ?></li>
+	<li class="item<?php echo Minz_Request::actionName () == 'main' ? ' active' : ''; ?>">
+		<a href="<?php echo _url ('stats', 'main'); ?>"><?php echo Minz_Translate::t ('stats_main'); ?></a>
+	</li>
+	<li class="item<?php echo Minz_Request::actionName () == 'idle' ? ' active' : ''; ?>">
+		<a href="<?php echo _url ('stats', 'idle'); ?>"><?php echo Minz_Translate::t ('stats_idle'); ?></a>
+	</li>
+</ul>

+ 1 - 1
app/layout/header.phtml

@@ -76,7 +76,7 @@ if (Minz_Configuration::canLogIn()) {
 				<li class="separator"></li>
 				<li class="item"><a href="<?php echo _url ('configure', 'users'); ?>"><?php echo Minz_Translate::t ('users'); ?></a></li>
 				<li class="separator"></li>
-				<li class="item"><a href="<?php echo _url ('index', 'stats'); ?>"><?php echo Minz_Translate::t ('stats'); ?></a></li>
+				<li class="item"><a href="<?php echo _url ('stats', 'main'); ?>"><?php echo Minz_Translate::t ('stats'); ?></a></li>
 				<li class="item"><a href="<?php echo _url ('index', 'logs'); ?>"><?php echo Minz_Translate::t ('logs'); ?></a></li>
 				<li class="item"><a href="<?php echo _url ('index', 'about'); ?>"><?php echo Minz_Translate::t ('about'); ?></a></li>
 				<?php

+ 57 - 0
app/views/stats/idle.phtml

@@ -0,0 +1,57 @@
+<?php $this->partial('aside_stats'); ?>
+
+<div class="post content">
+        <a href="<?php echo _url ('index', 'index'); ?>"><?php echo Minz_Translate::t ('back_to_rss_feeds'); ?></a>
+        
+        <h1><?php echo Minz_Translate::t ('stats_idle'); ?></h1>
+        
+        <div class="stat">
+                <h2><?php echo Minz_Translate::t ('last_week'); ?></h2>
+
+                <ul>
+                        <?php foreach ($this->idleFeeds['lastWeek'] as $feed): ?>
+                                <li><?php echo $feed; ?></li>
+                        <?php endforeach; ?>
+                </ul>
+        </div>
+
+        <div class="stat">
+                <h2><?php echo Minz_Translate::t ('last_month'); ?></h2>
+
+                <ul>
+                        <?php foreach ($this->idleFeeds['lastMonth'] as $feed): ?>
+                                <li><?php echo $feed; ?></li>
+                        <?php endforeach; ?>
+                </ul>
+        </div>
+
+        <div class="stat">
+                <h2><?php echo Minz_Translate::t ('last_3_month'); ?></h2>
+
+                <ul>
+                        <?php foreach ($this->idleFeeds['last3Month'] as $feed): ?>
+                                <li><?php echo $feed; ?></li>
+                        <?php endforeach; ?>
+                </ul>
+        </div>
+
+        <div class="stat">
+                <h2><?php echo Minz_Translate::t ('last_6_month'); ?></h2>
+
+                <ul>
+                        <?php foreach ($this->idleFeeds['last6Month'] as $feed): ?>
+                                <li><?php echo $feed; ?></li>
+                        <?php endforeach; ?>
+                </ul>
+        </div>
+
+        <div class="stat">
+                <h2><?php echo Minz_Translate::t ('last_year'); ?></h2>
+
+                <ul>
+                        <?php foreach ($this->idleFeeds['lastYear'] as $feed): ?>
+                                <li><?php echo $feed; ?></li>
+                        <?php endforeach; ?>
+                </ul>
+        </div>
+</div>

+ 7 - 5
app/views/index/stats.phtml → app/views/stats/main.phtml

@@ -1,9 +1,11 @@
+<?php $this->partial('aside_stats'); ?>
+
 <div class="post content">
-	<a href="<?php echo _url ('index', 'index'); ?>"><?php echo Minz_Translate::t ('back_to_rss_feeds'); ?></a>
-	
-	<h1><?php echo Minz_Translate::t ('stats'); ?></h1>
-	
-	<div class="stat">
+        <a href="<?php echo _url ('index', 'index'); ?>"><?php echo Minz_Translate::t ('back_to_rss_feeds'); ?></a>
+        
+        <h1><?php echo Minz_Translate::t ('stats_main'); ?></h1>
+
+        <div class="stat">
 		<h2><?php echo Minz_Translate::t ('stats_entry_repartition'); ?></h2>
 		<table>
 			<thead>