Created
February 9, 2013 08:22
-
-
Save ajshort/4744580 to your computer and use it in GitHub Desktop.
Exposes Packagist download counts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -95,10 +95,36 @@ class ApiController extends Controller | |
{ | |
return $this->receivePost($request, '{^(?:https?://)?(?P<domain>bitbucket\.org)/(?P<repo>[\w.-]+/[\w.-]+?)/?$}'); | |
} | |
/** | |
+ * @Route("/downloads/{name}", name="get_downloads", requirements={"name"="[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+"}, defaults={"_format" = "json"}) | |
+ * @Method({"GET"}) | |
+ */ | |
+ public function getDownloadsAction(Request $request, $name) | |
+ { | |
+ $packages = $this->getPackageAndVersionIds($name); | |
+ | |
+ if (!$packages) { | |
+ return new JsonResponse(array('status' => 'error', 'message' => 'Package not found'), 404); | |
+ } | |
+ | |
+ $overall = $this->getDownloads($packages[0]['id']); | |
+ $versions = array(); | |
+ | |
+ foreach($packages as $package) { | |
+ $versions[$package['vnormalized']] = $this->getDownloads($package['id'], $package['vid']); | |
+ } | |
+ | |
+ return new JsonResponse(array( | |
+ 'status' => 'success', | |
+ 'overall' => $overall, | |
+ 'versions' => $versions | |
+ )); | |
+ } | |
+ | |
+ /** | |
* @Route("/downloads/{name}", name="track_download", requirements={"name"="[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+"}, defaults={"_format" = "json"}) | |
* @Method({"POST"}) | |
*/ | |
public function trackDownloadAction(Request $request, $name) | |
{ | |
@@ -152,10 +178,21 @@ class ApiController extends Controller | |
} | |
return new JsonResponse(array('status' => 'success'), 201); | |
} | |
+ protected function getPackageAndVersionIds($name) | |
+ { | |
+ return $this->get('doctrine.dbal.default_connection')->fetchAll( | |
+ 'SELECT p.id, v.id vid, v.normalizedVersion vnormalized | |
+ FROM package p | |
+ LEFT JOIN package_version v ON p.id = v.package_id | |
+ WHERE p.name = ?', | |
+ array($name) | |
+ ); | |
+ } | |
+ | |
protected function getPackageAndVersionId($name, $version) | |
{ | |
return $this->get('doctrine.dbal.default_connection')->fetchAssoc( | |
'SELECT p.id, v.id vid | |
FROM package p | |
@@ -165,10 +202,22 @@ class ApiController extends Controller | |
LIMIT 1', | |
array($name, $version) | |
); | |
} | |
+ protected function getDownloads($id, $vid = null) | |
+ { | |
+ $redis = $this->get('snc_redis.default'); | |
+ $key = $vid ? "dl:$id-$vid" : "dl:$id" ; | |
+ | |
+ return array( | |
+ 'overall' => (int) $redis->get($key), | |
+ 'month' => (int) $redis->get($key . ':' . date('Ym')), | |
+ 'day' => (int) $redis->get($key . ':' . date('Ym')) | |
+ ); | |
+ } | |
+ | |
protected function trackDownload($id, $vid, $ip) | |
{ | |
$redis = $this->get('snc_redis.default'); | |
$throttleKey = 'dl:'.$id.':'.$ip.':'.date('Ymd'); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment