Skip to content

Commit c33da0b

Browse files
committed
Added package collection query limit for keywords
1 parent f9cd199 commit c33da0b

File tree

7 files changed

+27
-7
lines changed

7 files changed

+27
-7
lines changed

Sources/App/Controllers/PackageCollectionController.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ enum PackageCollectionController {
3939
authorName: "Swift Package Index",
4040
collectionName: keyword,
4141
keywords: [keyword],
42-
overview: "A custom package collection generated by the Swift Package Index"
42+
overview: "A custom package collection generated by the Swift Package Index",
43+
limit: Constants.maxKeywordPackageCollectionCount
4344
)
4445
case let .custom(key):
4546
let collection = try await CustomCollection.find(on: req.db, key: key)

Sources/App/Core/Constants.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,7 @@ enum Constants {
4646
// build system settings
4747
static let trimBuildsGracePeriod: TimeInterval = .hours(4)
4848
static let branchVersionRefreshDelay: TimeInterval = .hours(24)
49+
50+
// package
51+
static let maxKeywordPackageCollectionCount = 300
4952
}

Sources/App/Core/PackageCollection+VersionResult.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extension PackageCollection.VersionResult {
4040
var targets: [App.Target] { version.targets }
4141
var version: App.Version { model.version }
4242

43-
static func query(on database: Database, filterBy filter: PackageCollection.Filter) async throws -> [Self] {
43+
static func query(on database: Database, filterBy filter: PackageCollection.Filter, limit maxResults: Int? = nil) async throws -> [Self] {
4444
let query = M
4545
.query(
4646
on: database,
@@ -66,6 +66,10 @@ extension PackageCollection.VersionResult {
6666
query.filter(App.Package.self, \.$url ~~ packageURLs)
6767
}
6868

69+
if let maxResults = maxResults {
70+
query.limit(maxResults)
71+
}
72+
6973
return try await query.all()
7074
.map(Self.init(model:))
7175
}

Sources/App/Core/PackageCollection+generate.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,9 @@ extension PackageCollection {
4848
collectionName: String? = nil,
4949
keywords: [String]? = nil,
5050
overview: String? = nil,
51-
revision: Int? = nil) async throws -> PackageCollection {
52-
let results = try await VersionResult.query(on: db, filterBy: filter)
51+
revision: Int? = nil,
52+
limit maxResults: Int? = nil) async throws -> PackageCollection {
53+
let results = try await VersionResult.query(on: db, filterBy: filter, limit: maxResults)
5354

5455
// Multiple versions can reference the same package, therefore
5556
// we need to group them so we don't create duplicate packages.

Sources/App/Core/PackageCollection+signing.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@ extension SignedCollection {
3131
collectionName: String? = nil,
3232
keywords: [String]? = nil,
3333
overview: String? = nil,
34-
revision: Int? = nil) async throws -> SignedCollection {
34+
revision: Int? = nil,
35+
limit maxResults: Int? = nil) async throws -> SignedCollection {
3536
let collection = try await PackageCollection.generate(db: db,
3637
filterBy: filter,
3738
authorName: authorName,
3839
collectionName: collectionName,
3940
keywords: keywords,
4041
overview: overview,
41-
revision: revision)
42+
revision: revision,
43+
limit: maxResults)
4244
return try await sign(collection: collection)
4345
}
4446

Sources/App/Views/Keyword/KeywordShow+View.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ enum KeywordShow {
5959
.copyableInputForm(buttonName: "Copy Package Collection URL",
6060
eventName: "Copy Package Collection URL Button",
6161
valueToCopy: SiteURL.packageCollectionKeyword(.value(model.keyword)).absoluteURL()),
62+
.p(
63+
.h6(
64+
.i("Note: Package collections are limited to a maximum of \(Constants.maxKeywordPackageCollectionCount) packages. If a keyword has more than \(Constants.maxKeywordPackageCollectionCount) packages, only the top \(Constants.maxKeywordPackageCollectionCount) packages will be included based on their package score.")
65+
)
66+
),
6267
.hr(.class("minor")),
6368
.ul(
6469
.id("package-list"),
@@ -79,7 +84,6 @@ enum KeywordShow {
7984
)
8085
}
8186
}
82-
8387
}
8488

8589

Tests/AppTests/__Snapshots__/WebpageSnapshotTests/test_KeywordShow.1.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ <h2 class="trimmed">Packages for keyword “networking”</h2>
8989
<form class="copyable-input">
9090
<input type="text" data-button-name="Copy Package Collection URL" data-event-name="Copy Package Collection URL Button" value="http://localhost:8080/keywords/networking/collection.json" readonly/>
9191
</form>
92+
<p>
93+
<h6>
94+
<i>Note: Package collections are limited to a maximum of 300 packages. If a keyword has more than 300 packages, only the top 300 packages will be included based on their package score.</i>
95+
</h6>
96+
</p>
9297
<hr class="minor"/>
9398
<ul id="package-list">
9499
<li>

0 commit comments

Comments
 (0)