Browse Source

Show 30days sum instead of current monthly total in download stats, fixes #406

Jordi Boggiano 10 years ago
parent
commit
da48ec02cf

+ 2 - 2
src/Packagist/WebBundle/Controller/WebController.php

@@ -1082,13 +1082,13 @@ class WebController extends Controller
             }
 
             try {
-                $downloads = $this->get('packagist.download_manager')->getDownloads($package);
+                $downloads = $this->get('packagist.download_manager')->getTotalDownloads($package);
             } catch (ConnectionException $e) {
                 return;
             }
 
             // more than 50 downloads = established package, do not allow deletion by maintainers
-            if ($downloads['total'] > 50) {
+            if ($downloads > 50) {
                 return;
             }
         }

+ 28 - 9
src/Packagist/WebBundle/Model/DownloadManager.php

@@ -41,17 +41,36 @@ class DownloadManager
             $package = $package->getId();
         }
 
-        $counts = $this->redis->mget(
-            'dl:' . $package,
-            'dl:' . $package.':'.date('Ym'),
-            'dl:' . $package.':'.date('Ymd')
-        );
+        $date = new \DateTime();
+        $keys = array('dl:'.$package);
+        for ($i = 0; $i < 30; $i++) {
+            $keys[] = 'dl:' . $package . ':' . $date->format('Ymd');
+            $date->modify('-1 day');
+        }
 
-        return array(
-            'total' => (int) $counts[0] ?: 0,
-            'monthly' => (int) $counts[1] ?: 0,
-            'daily' => (int)$counts[2] ?: 0,
+        $vals = $this->redis->mget($keys);
+        $result = array(
+            'total' => (int) array_shift($vals) ?: 0,
+            'monthly' => (int) array_sum($vals) ?: 0,
+            'daily' => (int) $vals[0] ?: 0,
         );
+
+        return $result;
+    }
+
+    /**
+     * Gets the total download count for a package.
+     *
+     * @param \Packagist\WebBundle\Entity\Package|int $package
+     * @return int
+     */
+    public function getTotalDownloads($package)
+    {
+        if ($package instanceof Package) {
+            $package = $package->getId();
+        }
+
+        return (int) $this->redis->get('dl:' . $package) ?: 0;
     }
 
     /**

+ 1 - 1
src/Packagist/WebBundle/Resources/views/Web/viewPackage.html.twig

@@ -64,7 +64,7 @@
 
             <p class="downloads">
                 <span>Overall:</span> {% if downloads.total is defined %}{{ downloads.total|number_format(0, '.', ' ') }} install{{ downloads.total == 1 ? '' : 's' }}{% else %}N/A{% endif %}<br />
-                <span>This month:</span> {% if downloads.monthly is defined %}{{ downloads.monthly|number_format(0, '.', ' ') }} install{{ downloads.monthly == 1 ? '' : 's' }}{% else %}N/A{% endif %}<br />
+                <span>30 days:</span> {% if downloads.monthly is defined %}{{ downloads.monthly|number_format(0, '.', ' ') }} install{{ downloads.monthly == 1 ? '' : 's' }}{% else %}N/A{% endif %}<br />
                 <span>Today:</span> {% if downloads.daily is defined %}{{ downloads.daily|number_format(0, '.', ' ') }} install{{ downloads.daily == 1 ? '' : 's' }}{% else %}N/A{% endif %}<br />
             </p>