Skip to content

Commit

Permalink
refactor: refactor method for get psql connection
Browse files Browse the repository at this point in the history
  • Loading branch information
gulivero1773 committed Feb 1, 2024
1 parent b72b548 commit c095295
Show file tree
Hide file tree
Showing 13 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,21 @@ import Vapor

/// Human-readable name for the component
public enum ComponentName: String {
/// The Central Processing Unit (CPU) is the primary component of a computer that acts as its "control center."
case cpu
/// Memory, also known as random access memory (RAM), is a PC component that stores data while the computer runs
case memory
/// Redis is an open-source in-memory storage, used as a distributed, in-memory key–value database
case redis
/// PostgreSQL also known as Postgres, is a free and open-source relational database management system (RDBMS) emphasizing extensibility
case postgresql
/// MongoDB is a source-available, cross-platform, document-oriented database program.
case mongo
/// Distributed messaging system between server applications in real time
case kafka
/// Consul is a service networking solution to automate network configurations, discover services, and enable secure connectivity across any cloud or runtime.
case consul
/// gRPC is a modern open source high performance Remote Procedure Call (RPC) framework that can run in any environment.
case grpc
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import Vapor

/// Human-readable type for the component.
public enum ComponentType: String {
/// A part that combines with other parts to form something bigger
case component
/// A datastore is a repository for storing, managing and distributing data sets on an enterprise level
case datastore
/// A set of connected things or devices that operate together
case system
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ import Vapor

/// Name of the measurement type (a data point type) that the status is reported for
public enum MeasurementType: String {
/// An act or instance of making practical or profitable use of something
case utilization
/// The time lag between an electronic input and the output signal which depends upon the value of passive components used
case responseTime
/// The state of being connected
case connections
/// Uptime is a measure of system reliability, expressed as the percentage of time a machine
case uptime
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public struct PsqlHealthChecks: PsqlHealthChecksProtocol {
componentType: .datastore,
observedValue: Date().timeIntervalSinceReferenceDate - dateNow,
observedUnit: "s",
status: connectionDescription.isEmpty ? .pass : .fail,
status: connectionDescription.contains("ERROR:") ? .fail : .pass,
time: app.dateTimeISOFormat.string(from: Date()),
output: !connectionDescription.isEmpty ? connectionDescription : nil,
output: connectionDescription.contains("ERROR:") ? connectionDescription : nil,
links: nil,
node: nil
)
Expand All @@ -80,7 +80,7 @@ public struct PsqlHealthChecks: PsqlHealthChecksProtocol {
/// Get psql health using url connection
/// - Parameter url: `String`
/// - Returns: `HealthCheckItem`
public func checkConnection(url: String) async throws -> HealthCheckItem {
public func checkConnection(by url: String) async throws -> HealthCheckItem {
let dateNow = Date().timeIntervalSinceReferenceDate
try app.databases.use(.postgres(url: url), as: .psql)
let connectionDescription = await checkConnection()
Expand All @@ -89,9 +89,9 @@ public struct PsqlHealthChecks: PsqlHealthChecksProtocol {
componentType: .datastore,
observedValue: Date().timeIntervalSinceReferenceDate - dateNow,
observedUnit: "s",
status: connectionDescription.isEmpty ? .pass : .fail,
status: connectionDescription.contains("ERROR:") ? .fail : .pass,
time: app.dateTimeISOFormat.string(from: Date()),
output: !connectionDescription.isEmpty ? connectionDescription : nil,
output: connectionDescription.contains("ERROR:") ? connectionDescription : nil,
links: nil,
node: nil
)
Expand All @@ -103,9 +103,9 @@ public struct PsqlHealthChecks: PsqlHealthChecksProtocol {
private func checkConnection() async -> String {
let rows = try? await (app.db(.psql) as? PostgresDatabase)?.simpleQuery("SELECT version()").get()
let row = rows?.first?.makeRandomAccess()
guard (row?[data: "version"].string) != nil else {
return "No connect to Postgres database. Response: \(String(describing: row))"
guard let result = (row?[data: "version"].string) else {
return "ERROR: No connect to Postgres database. Response: \(String(describing: row))"
}
return ""
return result
}

Check warning on line 110 in Sources/AppHealthChecks/PostgreSQLHealthChecks/PsqlHealthChecks.swift

View check run for this annotation

Codecov / codecov/patch

Sources/AppHealthChecks/PostgreSQLHealthChecks/PsqlHealthChecks.swift#L103-L110

Added lines #L103 - L110 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ public protocol PsqlHealthChecksProtocol {
/// Get psql health using url connection
/// - Parameter url: `String`
/// - Returns: `HealthCheckItem`
func checkConnection(url: String) async throws -> HealthCheckItem
func checkConnection(by url: String) async throws -> HealthCheckItem
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public struct PsqlHealthChecksMock: PsqlHealthChecksProtocol {
return PsqlHealthChecksMock.healthCheckItem
}

public func checkConnection(url: String) async -> HealthCheckItem {
public func checkConnection(by url: String) async -> HealthCheckItem {
return PsqlHealthChecksMock.healthCheckItem
}
}
2 changes: 1 addition & 1 deletion Tests/AppHealthChecksTests/PsqlHealthChecksTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class PsqlHealthChecksTests: XCTestCase {
defer { app.shutdown() }
app.psqlHealthChecks = PsqlHealthChecksMock()
let url = "postgres://username:password@hostname:port/database?tlsmode=mode"
let result = try await app.psqlHealthChecks?.checkConnection(url: url)
let result = try await app.psqlHealthChecks?.checkConnection(by: url)
XCTAssertEqual(result, PsqlHealthChecksMock.healthCheckItem)
}
}

0 comments on commit c095295

Please sign in to comment.