Skip to content

Commit

Permalink
Improve Popular Package listing based on 'require'/'require-dev' count
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioheleno committed Mar 20, 2022
1 parent b79b844 commit 7ef99b6
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/Application/Action/Package/ViewPackageBadgeAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ static function (Dependency $dependency): bool {

case VersionStatusEnum::NoDeps:
$status = [
'text' => 'no deps',
'text' => 'none',
'color' => 'blue'
];
break;
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/Package/PackageRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function create(string $name): Package;

public function all(): PackageCollection;

public function findPopular(): PackageCollection;
public function findPopular(int $limit = 10): PackageCollection;

public function exists(string $name): bool;

Expand Down
2 changes: 2 additions & 0 deletions src/Domain/Stats/StatsRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function create(

public function all(): StatsCollection;

public function findPopular(): StatsCollection;

public function exists(string $packageName): bool;

/**
Expand Down
38 changes: 16 additions & 22 deletions src/Infrastructure/Persistence/Package/PdoPackageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ public function create(string $name): Package {
);
}

/**
* {@inheritdoc}
*/
public function all(): PackageCollection {
static $stmt = null;
if ($stmt === null) {
Expand All @@ -74,34 +71,37 @@ public function all(): PackageCollection {
return $packageCol;
}

/**
* {@inheritdoc}
*/
public function findPopular(): PackageCollection {
public function findPopular(int $limit = 10): PackageCollection {
static $stmt = null;
if ($stmt === null) {
$stmt = $this->pdo->query(
<<<SQL
SELECT "packages".*
SELECT "dependencies"."name", COUNT("dependencies".*) AS "total"
FROM "packages"
LEFT JOIN "stats" ON ("stats"."package_name" = "packages"."name")
ORDER BY "stats"."daily_downloads" DESC, "packages"."created_at" ASC
LIMIT 10
INNER JOIN "versions" ON (
"versions"."package_name" = "packages"."name" AND
"versions"."number" = "packages"."latest_version"
)
INNER JOIN "dependencies" ON ("dependencies"."version_id" = "versions"."id")
WHERE "packages"."latest_version" != '' AND "versions"."release" IS TRUE
GROUP BY "dependencies"."name"
ORDER BY "total" DESC
SQL
);
}

$packageCol = new PackageCollection();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$packageCol->add($this->hydrate($row));
while (($row = $stmt->fetch(PDO::FETCH_ASSOC)) && $packageCol->count() < $limit) {
if ($this->exists($row['name']) === false) {
continue;
}

$packageCol->add($this->get($row['name']));
}

return $packageCol;
}

/**
* {@inheritdoc}
*/
public function exists(string $name): bool {
static $stmt = null;
if ($stmt === null) {
Expand All @@ -119,9 +119,6 @@ public function exists(string $name): bool {
return $stmt->rowCount() === 1;
}

/**
* {@inheritdoc}
*/
public function get(string $name): Package {
static $stmt = null;
if ($stmt === null) {
Expand All @@ -144,9 +141,6 @@ public function get(string $name): Package {
return $this->hydrate($row);
}

/**
* {@inheritdoc}
*/
public function find(array $query): PackageCollection {
$where = [];
foreach (array_keys($query) as $col) {
Expand Down
34 changes: 22 additions & 12 deletions src/Infrastructure/Persistence/Stats/PdoStatsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,6 @@ public function create(
);
}

/**
* {@inheritdoc}
*/
public function all(): StatsCollection {
static $stmt = null;
if ($stmt === null) {
Expand All @@ -103,9 +100,28 @@ public function all(): StatsCollection {
return $statsCol;
}

/**
* {@inheritdoc}
*/
public function findPopular(): StatsCollection {
static $stmt = null;
if ($stmt === null) {
$stmt = $this->pdo->query(
<<<SQL
SELECT "stats".*
FROM "stats"
INNER JOIN "packages" ON ("packages"."name" = "stats"."package_name")
ORDER BY "stats"."daily_downloads" DESC, "packages"."created_at" ASC
LIMIT 10
SQL
);
}

$statsCol = new StatsCollection();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$statsCol->add($this->hydrate($row));
}

return $statsCol;
}

public function exists(string $packageName): bool {
static $stmt = null;
if ($stmt === null) {
Expand All @@ -124,9 +140,6 @@ public function exists(string $packageName): bool {
return $stmt->rowCount() === 1;
}

/**
* {@inheritdoc}
*/
public function get(string $packageName): Stats {
static $stmt = null;
if ($stmt === null) {
Expand All @@ -149,9 +162,6 @@ public function get(string $packageName): Stats {
return $this->hydrate($row);
}

/**
* {@inheritdoc}
*/
public function find(array $query): StatsCollection {
$where = [];
foreach (array_keys($query) as $col) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,10 @@ public function create(
);
}

/**
* {@inheritdoc}
*/
public function all(): VersionCollection {
return new VersionCollection();
}

/**
* {@inheritdoc}
*/
public function get(int $id): Version {
static $stmt = null;
if ($stmt === null) {
Expand All @@ -96,9 +90,6 @@ public function get(int $id): Version {
return $this->hydrate($row);
}

/**
* {@inheritdoc}
*/
public function find(array $query): VersionCollection {
$where = [];
$cols = array_keys($query);
Expand Down

0 comments on commit 7ef99b6

Please sign in to comment.