Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3c9a772
Convert JoinedQueryBuilder.filter and first test over to @Dependency
finestructure Sep 20, 2024
a8251d3
Fix MastodonTests
finestructure Sep 20, 2024
0ef7b65
Fix ErrorReportingTests
finestructure Sep 20, 2024
1b2549a
Convert IngestorTests
finestructure Sep 20, 2024
f3b2763
Fix PackageTests
finestructure Sep 20, 2024
72674a9
Fix PipelineTests
finestructure Sep 20, 2024
e31eb42
Move dependency declaration into function
finestructure Oct 3, 2024
a9d9a06
Drop existing dependency
finestructure Oct 2, 2024
8b13530
Fix test_history
finestructure Oct 3, 2024
f80e259
Start fixing test_analyze_refreshCheckout_failed
finestructure Oct 4, 2024
c6e60c4
Fix test_trimCheckouts
finestructure Oct 4, 2024
42f8662
fix test_SearchShow
finestructure Oct 4, 2024
5121ad3
Fix SitemapTests
finestructure Oct 4, 2024
278ec56
Fix ScoreTests
finestructure Oct 4, 2024
f84ebd3
Start fixing ReAnalyzeVersionsTests
finestructure Oct 4, 2024
09826f9
Start fixing PipelineTests
finestructure Oct 4, 2024
8e46b81
Move @Dependency location
finestructure Oct 4, 2024
c0eba90
Fix PackageTests
finestructure Oct 4, 2024
26eb5a0
Fix PackageCollectionTests
finestructure Oct 4, 2024
d461119
Fix HomeIndexModelTests
finestructure Oct 4, 2024
18b0166
Fix BuildMonitorControllerTests
finestructure Oct 4, 2024
d4872c3
Fix test_analyze_refreshCheckout_failed via withEscapedDependencies
finestructure Oct 5, 2024
7e565a4
Fix remaining AnalyzeErrorTests
finestructure Oct 5, 2024
5d690b1
Fix reAnalyzeVersions via withEscapedDependencies as well
finestructure Oct 5, 2024
4ff0acb
Fix remaining AnalyzerTests
finestructure Oct 5, 2024
f306c7b
Remove commented out code
finestructure Oct 5, 2024
e068750
Cleanup
finestructure Oct 5, 2024
9352c47
More cleanup
finestructure Oct 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ let package = Package(
.package(url: "https://github.com/swiftlang/swift-package-manager.git", branch: "release/5.10"),
.package(url: "https://github.com/dankinsoid/VaporToOpenAPI.git", from: "4.4.4"),
.package(url: "https://github.com/pointfreeco/swift-custom-dump.git", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-dependencies", from: "1.0.0"),
.package(url: "https://github.com/pointfreeco/swift-parsing.git", from: "0.12.0"),
.package(url: "https://github.com/pointfreeco/swift-snapshot-testing.git", from: "1.11.1"),
.package(url: "https://github.com/scinfu/SwiftSoup.git", from: "2.3.2"),
Expand All @@ -64,6 +65,7 @@ let package = Package(
.product(name: "Cache", package: "cache"),
.product(name: "CanonicalPackageURL", package: "CanonicalPackageURL"),
.product(name: "CustomDump", package: "swift-custom-dump"),
.product(name: "Dependencies", package: "swift-dependencies"),
.product(name: "DependencyResolution", package: "DependencyResolution"),
.product(name: "Fluent", package: "fluent"),
.product(name: "FluentPostgresDriver", package: "fluent-postgres-driver"),
Expand Down
6 changes: 4 additions & 2 deletions Sources/App/Commands/Alerting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Dependencies
import Fluent
import NIOCore
import SQLKit
import Vapor
import NIOCore


enum Alerting {
Expand Down Expand Up @@ -117,7 +118,8 @@ extension Alerting {
defer {
Current.logger().debug("fetchBuilds elapsed: \(Date.now.timeIntervalSince(start).rounded(decimalPlaces: 2))s")
}
let cutoff = Current.date().addingTimeInterval(-timePeriod.timeInterval)
@Dependency(\.date.now) var now
let cutoff = now.addingTimeInterval(-timePeriod.timeInterval)
let builds = try await Build.query(on: database)
.field(\.$createdAt)
.field(\.$updatedAt)
Expand Down
76 changes: 43 additions & 33 deletions Sources/App/Commands/Analyze.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Dependencies
import DependencyResolution
import Fluent
import SPIManifest
Expand Down Expand Up @@ -84,8 +85,8 @@ extension Analyze {
}
.forEach { pair in
guard let (path, mod) = pair else { return }
let cutoff = Current.date()
.addingTimeInterval(-Constants.gitCheckoutMaxAge)
@Dependency(\.date.now) var now
let cutoff = now.addingTimeInterval(-Constants.gitCheckoutMaxAge)
if mod < cutoff {
try Current.fileManager.removeItem(atPath: path)
AppMetrics.analyzeTrimCheckoutsCount?.inc()
Expand Down Expand Up @@ -169,45 +170,53 @@ extension Analyze {
package: Joined<Package, Repository>) async throws {
try await refreshCheckout(package: package)

try await database.transaction { tx in
try await updateRepository(on: tx, package: package)
// 2024-10-05 sas: We need to explicitly weave dependencies into the `transaction` closure, because escaping closures strip them.
// https://github.com/pointfreeco/swift-dependencies/discussions/283#discussioncomment-10846172
// This might not be needed in Vapor 5 / FluentKit 2
// TODO: verify this is still needed once we upgrade to Vapor 5 / FluentKit 2
try await withEscapedDependencies { dependencies in
try await database.transaction { tx in
try await dependencies.yield {
try await updateRepository(on: tx, package: package)

let versionDelta = try await diffVersions(client: client, transaction: tx,
package: package)
let netDeleteCount = versionDelta.toDelete.count - versionDelta.toAdd.count
if netDeleteCount > 1 {
Current.logger().warning("Suspicious loss of \(netDeleteCount) versions for package \(package.model.id)")
}
let versionDelta = try await diffVersions(client: client, transaction: tx,
package: package)
let netDeleteCount = versionDelta.toDelete.count - versionDelta.toAdd.count
if netDeleteCount > 1 {
Current.logger().warning("Suspicious loss of \(netDeleteCount) versions for package \(package.model.id)")
}

try await applyVersionDelta(on: tx, delta: versionDelta)
try await applyVersionDelta(on: tx, delta: versionDelta)

let newVersions = versionDelta.toAdd
let newVersions = versionDelta.toAdd

mergeReleaseInfo(package: package, into: newVersions)
mergeReleaseInfo(package: package, into: newVersions)

var versionsPkgInfo = [(Version, PackageInfo)]()
for version in newVersions {
if let pkgInfo = try? await getPackageInfo(package: package, version: version) {
versionsPkgInfo.append((version, pkgInfo))
}
}
if !newVersions.isEmpty && versionsPkgInfo.isEmpty {
throw AppError.noValidVersions(package.model.id, package.model.url)
}
var versionsPkgInfo = [(Version, PackageInfo)]()
for version in newVersions {
if let pkgInfo = try? await getPackageInfo(package: package, version: version) {
versionsPkgInfo.append((version, pkgInfo))
}
}
if !newVersions.isEmpty && versionsPkgInfo.isEmpty {
throw AppError.noValidVersions(package.model.id, package.model.url)
}

for (version, pkgInfo) in versionsPkgInfo {
try await updateVersion(on: tx, version: version, packageInfo: pkgInfo)
try await recreateProducts(on: tx, version: version, manifest: pkgInfo.packageManifest)
try await recreateTargets(on: tx, version: version, manifest: pkgInfo.packageManifest)
}
for (version, pkgInfo) in versionsPkgInfo {
try await updateVersion(on: tx, version: version, packageInfo: pkgInfo)
try await recreateProducts(on: tx, version: version, manifest: pkgInfo.packageManifest)
try await recreateTargets(on: tx, version: version, manifest: pkgInfo.packageManifest)
}

let versions = try await updateLatestVersions(on: tx, package: package)

let versions = try await updateLatestVersions(on: tx, package: package)

let targets = await fetchTargets(on: tx, package: package)
let targets = await fetchTargets(on: tx, package: package)

updateScore(package: package, versions: versions, targets: targets)
updateScore(package: package, versions: versions, targets: targets)

await onNewVersions(client: client, package: package, versions: newVersions)
await onNewVersions(client: client, package: package, versions: newVersions)
}
}
}
}

Expand Down Expand Up @@ -410,7 +419,8 @@ extension Analyze {
return incoming
}

let ageOfExistingVersion = Current.date().timeIntervalSinceReferenceDate - existingVersion.commitDate.timeIntervalSinceReferenceDate
@Dependency(\.date.now) var now
let ageOfExistingVersion = now.timeIntervalSinceReferenceDate - existingVersion.commitDate.timeIntervalSinceReferenceDate

let resultingBranchVersion: Version
if existingVersion.reference.branchName != incomingVersion.reference.branchName {
Expand Down
77 changes: 44 additions & 33 deletions Sources/App/Commands/ReAnalyzeVersions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Vapor
import Dependencies
import Fluent
import Vapor


enum ReAnalyzeVersions {
Expand Down Expand Up @@ -48,13 +49,14 @@ enum ReAnalyzeVersions {
let db = context.application.db
Current.setLogger(Logger(component: "re-analyze-versions"))

@Dependency(\.date.now) var now
if let id = signature.packageId {
Current.logger().info("Re-analyzing versions (id: \(id)) ...")
do {
try await reAnalyzeVersions(
client: client,
database: db,
versionsLastUpdatedBefore: Current.date(),
versionsLastUpdatedBefore: now,
refreshCheckouts: signature.refreshCheckouts,
packageId: id
)
Expand Down Expand Up @@ -174,38 +176,46 @@ enum ReAnalyzeVersions {
for pkg in packages {
Current.logger().info("Re-analyzing package \(pkg.model.url) ...")

try await database.transaction { tx in
guard let cacheDir = Current.fileManager.cacheDirectoryPath(for: pkg.model) else { return }
if !Current.fileManager.fileExists(atPath: cacheDir) || refreshCheckouts {
try await Analyze.refreshCheckout(package: pkg)
}

let versions = try await getExistingVersions(client: client,
transaction: tx,
package: pkg,
before: cutoffDate)
Current.logger().info("Updating \(versions.count) versions (id: \(pkg.model.id)) ...")

try await setUpdatedAt(on: tx, versions: versions)

Analyze.mergeReleaseInfo(package: pkg, into: versions)

for version in versions {
let pkgInfo: Analyze.PackageInfo
do {
pkgInfo = try await Analyze.getPackageInfo(package: pkg, version: version)
} catch {
Current.logger().report(error: error)
continue
// 2024-10-05 sas: We need to explicitly weave dependencies into the `transaction` closure, because escaping closures strip them.
// https://github.com/pointfreeco/swift-dependencies/discussions/283#discussioncomment-10846172
// This might not be needed in Vapor 5 / FluentKit 2
// TODO: verify this is still needed once we upgrade to Vapor 5 / FluentKit 2
try await withEscapedDependencies { dependencies in
try await database.transaction { tx in
try await dependencies.yield {
guard let cacheDir = Current.fileManager.cacheDirectoryPath(for: pkg.model) else { return }
if !Current.fileManager.fileExists(atPath: cacheDir) || refreshCheckouts {
try await Analyze.refreshCheckout(package: pkg)
}

let versions = try await getExistingVersions(client: client,
transaction: tx,
package: pkg,
before: cutoffDate)
Current.logger().info("Updating \(versions.count) versions (id: \(pkg.model.id)) ...")

try await setUpdatedAt(on: tx, versions: versions)

Analyze.mergeReleaseInfo(package: pkg, into: versions)

for version in versions {
let pkgInfo: Analyze.PackageInfo
do {
pkgInfo = try await Analyze.getPackageInfo(package: pkg, version: version)
} catch {
Current.logger().report(error: error)
continue
}

try await Analyze.updateVersion(on: tx, version: version, packageInfo: pkgInfo)
try await Analyze.recreateProducts(on: tx, version: version, manifest: pkgInfo.packageManifest)
try await Analyze.recreateTargets(on: tx, version: version, manifest: pkgInfo.packageManifest)
}

// No need to run `updateLatestVersions` because we're only operating on existing versions,
// not adding any new ones that could change the `latest` marker.
}

try await Analyze.updateVersion(on: tx, version: version, packageInfo: pkgInfo)
try await Analyze.recreateProducts(on: tx, version: version, manifest: pkgInfo.packageManifest)
try await Analyze.recreateTargets(on: tx, version: version, manifest: pkgInfo.packageManifest)
}

// No need to run `updateLatestVersions` because we're only operating on existing versions,
// not adding any new ones that could change the `latest` marker.
}
}
}
Expand All @@ -225,8 +235,9 @@ enum ReAnalyzeVersions {


static func setUpdatedAt(on database: Database, versions: [Version]) async throws {
@Dependency(\.date.now) var now
for version in versions {
version.updatedAt = Current.date()
version.updatedAt = now
}
try await versions.save(on: database)
}
Expand Down
10 changes: 6 additions & 4 deletions Sources/App/Controllers/SiteMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Vapor
import Dependencies
import Fluent
import SQLKit
import Plot
import SQLKit
import Vapor


enum SiteMapController {
Expand Down Expand Up @@ -67,10 +68,11 @@ enum SiteMapController {
/// - Parameter packages: list of packages
/// - Returns: `SiteMapIndex`
static func index(packages: [SiteMapController.Package]) -> SiteMapIndex {
SiteMapIndex(
@Dependency(\.date.now) var now
return SiteMapIndex(
.sitemap(
.loc(SiteURL.siteMapStaticPages.absoluteURL()),
.lastmod(Current.date(), timeZone: .utc) // The home page updates every day.
.lastmod(now, timeZone: .utc) // The home page updates every day.
),
.group(
packages.map { package -> Node<SiteMapIndex.SiteMapIndexContext> in
Expand Down
2 changes: 0 additions & 2 deletions Sources/App/Core/AppEnvironment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ struct AppEnvironment: Sendable {
var collectionSigningCertificateChain: @Sendable () -> [URL]
var collectionSigningPrivateKey: @Sendable () -> Data?
var currentReferenceCache: @Sendable () -> CurrentReferenceCache?
var date: @Sendable () -> Date
var dbId: @Sendable () -> String?
var environment: @Sendable () -> Environment
var fetchDocumentation: @Sendable (_ client: Client, _ url: URI) async throws -> ClientResponse
Expand Down Expand Up @@ -162,7 +161,6 @@ extension AppEnvironment {
.map { Data($0.utf8) }
},
currentReferenceCache: { .live },
date: { .init() },
dbId: { Environment.get("DATABASE_ID") },
environment: { (try? Environment.detect()) ?? .development },
fetchDocumentation: { client, url in try await client.get(url) },
Expand Down
Loading
Loading