From c095295d8d14c4f99f3307df7810de6410afca6f Mon Sep 17 00:00:00 2001 From: Mykola Buhaiov Date: Thu, 1 Feb 2024 17:31:18 +0200 Subject: [PATCH] refactor: refactor method for get psql connection --- .../AppHealthChecks.swift | 0 .../{Response => Models}/ComponentName.swift | 8 ++++++++ .../{Response => Models}/ComponentType.swift | 3 +++ .../HealthCheck+Equatable.swift | 0 .../{Response => Models}/HealthCheck.swift | 0 .../HealthCheckItem+Equatable.swift | 0 .../{Response => Models}/HealthCheckItem.swift | 0 .../{Response => Models}/HealthCheckStatus.swift | 0 .../{Response => Models}/MeasurementType.swift | 4 ++++ .../PsqlHealthChecks.swift | 16 ++++++++-------- .../PsqlHealthChecksProtocol.swift | 2 +- .../Mocks/PsqlHealthChecksMock.swift | 2 +- .../PsqlHealthChecksTests.swift | 2 +- 13 files changed, 26 insertions(+), 11 deletions(-) rename Sources/AppHealthChecks/{HealthChecks.ApplicationStatus => ApplicationHealthChecks}/AppHealthChecks.swift (100%) rename Sources/AppHealthChecks/{Response => Models}/ComponentName.swift (54%) rename Sources/AppHealthChecks/{Response => Models}/ComponentType.swift (81%) rename Sources/AppHealthChecks/{Response => Models}/HealthCheck+Equatable.swift (100%) rename Sources/AppHealthChecks/{Response => Models}/HealthCheck.swift (100%) rename Sources/AppHealthChecks/{Response => Models}/HealthCheckItem+Equatable.swift (100%) rename Sources/AppHealthChecks/{Response => Models}/HealthCheckItem.swift (100%) rename Sources/AppHealthChecks/{Response => Models}/HealthCheckStatus.swift (100%) rename Sources/AppHealthChecks/{Response => Models}/MeasurementType.swift (77%) rename Sources/AppHealthChecks/{HealthChecks.PostgreSQL => PostgreSQLHealthChecks}/PsqlHealthChecks.swift (85%) rename Sources/AppHealthChecks/{HealthChecks.PostgreSQL => PostgreSQLHealthChecks}/PsqlHealthChecksProtocol.swift (95%) diff --git a/Sources/AppHealthChecks/HealthChecks.ApplicationStatus/AppHealthChecks.swift b/Sources/AppHealthChecks/ApplicationHealthChecks/AppHealthChecks.swift similarity index 100% rename from Sources/AppHealthChecks/HealthChecks.ApplicationStatus/AppHealthChecks.swift rename to Sources/AppHealthChecks/ApplicationHealthChecks/AppHealthChecks.swift diff --git a/Sources/AppHealthChecks/Response/ComponentName.swift b/Sources/AppHealthChecks/Models/ComponentName.swift similarity index 54% rename from Sources/AppHealthChecks/Response/ComponentName.swift rename to Sources/AppHealthChecks/Models/ComponentName.swift index 1cf6b02..0319ecc 100644 --- a/Sources/AppHealthChecks/Response/ComponentName.swift +++ b/Sources/AppHealthChecks/Models/ComponentName.swift @@ -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 } diff --git a/Sources/AppHealthChecks/Response/ComponentType.swift b/Sources/AppHealthChecks/Models/ComponentType.swift similarity index 81% rename from Sources/AppHealthChecks/Response/ComponentType.swift rename to Sources/AppHealthChecks/Models/ComponentType.swift index d2486c8..3debadd 100644 --- a/Sources/AppHealthChecks/Response/ComponentType.swift +++ b/Sources/AppHealthChecks/Models/ComponentType.swift @@ -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 } diff --git a/Sources/AppHealthChecks/Response/HealthCheck+Equatable.swift b/Sources/AppHealthChecks/Models/HealthCheck+Equatable.swift similarity index 100% rename from Sources/AppHealthChecks/Response/HealthCheck+Equatable.swift rename to Sources/AppHealthChecks/Models/HealthCheck+Equatable.swift diff --git a/Sources/AppHealthChecks/Response/HealthCheck.swift b/Sources/AppHealthChecks/Models/HealthCheck.swift similarity index 100% rename from Sources/AppHealthChecks/Response/HealthCheck.swift rename to Sources/AppHealthChecks/Models/HealthCheck.swift diff --git a/Sources/AppHealthChecks/Response/HealthCheckItem+Equatable.swift b/Sources/AppHealthChecks/Models/HealthCheckItem+Equatable.swift similarity index 100% rename from Sources/AppHealthChecks/Response/HealthCheckItem+Equatable.swift rename to Sources/AppHealthChecks/Models/HealthCheckItem+Equatable.swift diff --git a/Sources/AppHealthChecks/Response/HealthCheckItem.swift b/Sources/AppHealthChecks/Models/HealthCheckItem.swift similarity index 100% rename from Sources/AppHealthChecks/Response/HealthCheckItem.swift rename to Sources/AppHealthChecks/Models/HealthCheckItem.swift diff --git a/Sources/AppHealthChecks/Response/HealthCheckStatus.swift b/Sources/AppHealthChecks/Models/HealthCheckStatus.swift similarity index 100% rename from Sources/AppHealthChecks/Response/HealthCheckStatus.swift rename to Sources/AppHealthChecks/Models/HealthCheckStatus.swift diff --git a/Sources/AppHealthChecks/Response/MeasurementType.swift b/Sources/AppHealthChecks/Models/MeasurementType.swift similarity index 77% rename from Sources/AppHealthChecks/Response/MeasurementType.swift rename to Sources/AppHealthChecks/Models/MeasurementType.swift index f4336c9..af0923e 100644 --- a/Sources/AppHealthChecks/Response/MeasurementType.swift +++ b/Sources/AppHealthChecks/Models/MeasurementType.swift @@ -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 } diff --git a/Sources/AppHealthChecks/HealthChecks.PostgreSQL/PsqlHealthChecks.swift b/Sources/AppHealthChecks/PostgreSQLHealthChecks/PsqlHealthChecks.swift similarity index 85% rename from Sources/AppHealthChecks/HealthChecks.PostgreSQL/PsqlHealthChecks.swift rename to Sources/AppHealthChecks/PostgreSQLHealthChecks/PsqlHealthChecks.swift index 5ec1d48..d9f3d91 100644 --- a/Sources/AppHealthChecks/HealthChecks.PostgreSQL/PsqlHealthChecks.swift +++ b/Sources/AppHealthChecks/PostgreSQLHealthChecks/PsqlHealthChecks.swift @@ -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 ) @@ -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() @@ -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 ) @@ -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 } } diff --git a/Sources/AppHealthChecks/HealthChecks.PostgreSQL/PsqlHealthChecksProtocol.swift b/Sources/AppHealthChecks/PostgreSQLHealthChecks/PsqlHealthChecksProtocol.swift similarity index 95% rename from Sources/AppHealthChecks/HealthChecks.PostgreSQL/PsqlHealthChecksProtocol.swift rename to Sources/AppHealthChecks/PostgreSQLHealthChecks/PsqlHealthChecksProtocol.swift index 803dddb..68e230a 100644 --- a/Sources/AppHealthChecks/HealthChecks.PostgreSQL/PsqlHealthChecksProtocol.swift +++ b/Sources/AppHealthChecks/PostgreSQLHealthChecks/PsqlHealthChecksProtocol.swift @@ -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 } diff --git a/Tests/AppHealthChecksTests/Mocks/PsqlHealthChecksMock.swift b/Tests/AppHealthChecksTests/Mocks/PsqlHealthChecksMock.swift index c1ea674..ff3e3a8 100644 --- a/Tests/AppHealthChecksTests/Mocks/PsqlHealthChecksMock.swift +++ b/Tests/AppHealthChecksTests/Mocks/PsqlHealthChecksMock.swift @@ -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 } } diff --git a/Tests/AppHealthChecksTests/PsqlHealthChecksTests.swift b/Tests/AppHealthChecksTests/PsqlHealthChecksTests.swift index d38b17b..6489872 100644 --- a/Tests/AppHealthChecksTests/PsqlHealthChecksTests.swift +++ b/Tests/AppHealthChecksTests/PsqlHealthChecksTests.swift @@ -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) } }