diff --git a/Sources/HealthChecks/ApplicationHealthChecks/ApplicationHealthChecks.swift b/Sources/HealthChecks/ApplicationHealthChecks/ApplicationHealthChecks.swift index a1b4237..8d993ab 100644 --- a/Sources/HealthChecks/ApplicationHealthChecks/ApplicationHealthChecks.swift +++ b/Sources/HealthChecks/ApplicationHealthChecks/ApplicationHealthChecks.swift @@ -40,7 +40,7 @@ public struct ApplicationHealthChecks: ApplicationHealthChecksProtocol { /// Get uptime of the system. /// - Returns: A `HealthCheckItem` representing the application's uptime. public func uptime() -> HealthCheckItem { - let uptime = Date().timeIntervalSinceReferenceDate - app.launchTime + let uptime = Date().timeIntervalSince1970 - app.launchTime return HealthCheckItem( componentType: .system, observedValue: uptime, diff --git a/Sources/HealthChecks/ConsulHealthChecks/ConsulHealthChecks.swift b/Sources/HealthChecks/ConsulHealthChecks/ConsulHealthChecks.swift index 4540355..7d24e30 100644 --- a/Sources/HealthChecks/ConsulHealthChecks/ConsulHealthChecks.swift +++ b/Sources/HealthChecks/ConsulHealthChecks/ConsulHealthChecks.swift @@ -46,7 +46,7 @@ public struct ConsulHealthChecks: ConsulHealthChecksProtocol { public func check(for options: [MeasurementType]) async -> [String: HealthCheckItem] { var result = ["": HealthCheckItem()] let measurementTypes = Array(Set(options)) - let dateNow = Date().timeIntervalSinceReferenceDate + let dateNow = Date().timeIntervalSince1970 let response = await getStatus() for type in measurementTypes { switch type { @@ -113,7 +113,7 @@ public struct ConsulHealthChecks: ConsulHealthChecksProtocol { return HealthCheckItem( componentId: app.consulConfig?.id, componentType: .component, - observedValue: response.status == .ok ? Date().timeIntervalSinceReferenceDate - start : 0, + observedValue: response.status == .ok ? Date().timeIntervalSince1970 - start : 0, observedUnit: "s", status: response.status == .ok ? .pass : .fail, time: response.status == .ok ? app.dateTimeISOFormat.string(from: Date()) : nil, diff --git a/Sources/HealthChecks/Extensions/Application+Extensions.swift b/Sources/HealthChecks/Extensions/Application+Extensions.swift index a514aba..e2a3a6a 100644 --- a/Sources/HealthChecks/Extensions/Application+Extensions.swift +++ b/Sources/HealthChecks/Extensions/Application+Extensions.swift @@ -71,7 +71,7 @@ extension Application { /// Setup `launchTimeKey` in application storage public var launchTime: Double { - get { storage[LaunchTimeKey.self] ?? Date().timeIntervalSinceReferenceDate } + get { storage[LaunchTimeKey.self] ?? Date().timeIntervalSince1970 } set { storage[LaunchTimeKey.self] = newValue } } diff --git a/Sources/HealthChecks/MongoHealthChecks/MongoHealthChecks.swift b/Sources/HealthChecks/MongoHealthChecks/MongoHealthChecks.swift index 8b7a88f..ca690f6 100644 --- a/Sources/HealthChecks/MongoHealthChecks/MongoHealthChecks.swift +++ b/Sources/HealthChecks/MongoHealthChecks/MongoHealthChecks.swift @@ -45,13 +45,13 @@ public struct MongoHealthChecks: MongoHealthChecksProtocol { /// Get mongo connection /// - Returns: `HealthCheckItem` public func connection() async -> HealthCheckItem { - let dateNow = Date().timeIntervalSinceReferenceDate + let dateNow = Date().timeIntervalSince1970 let connectionDescription = await getConnection() let result = HealthCheckItem( - componentId: app.psqlId, + componentId: app.mongoId, componentType: .datastore, - observedValue: Date().timeIntervalSinceReferenceDate - dateNow, - observedUnit: "s", + // TODO: need get active connection + // observedValue: "", status: connectionDescription.contains("connecting") ? .pass : .fail, time: app.dateTimeISOFormat.string(from: Date()), output: !connectionDescription.contains("connecting") ? connectionDescription : nil, @@ -64,13 +64,13 @@ public struct MongoHealthChecks: MongoHealthChecksProtocol { /// Get mongo response time /// - Returns: `HealthCheckItem` public func responseTime() async -> HealthCheckItem { - let dateNow = Date().timeIntervalSinceReferenceDate + let dateNow = Date().timeIntervalSince1970 let connectionDescription = await getConnection() let result = HealthCheckItem( - componentId: app.psqlId, + componentId: app.mongoId, componentType: .datastore, - observedValue: Date().timeIntervalSinceReferenceDate - dateNow, - observedUnit: "s", + observedValue: (Date().timeIntervalSince1970 - dateNow) * 1000, + observedUnit: "ms", status: connectionDescription.contains("connecting") ? .pass : .fail, time: app.dateTimeISOFormat.string(from: Date()), output: !connectionDescription.contains("connecting") ? connectionDescription : nil, diff --git a/Sources/HealthChecks/PostgresHealthChecks/PostgresHealthChecks.swift b/Sources/HealthChecks/PostgresHealthChecks/PostgresHealthChecks.swift index 3d834d6..66e49e2 100644 --- a/Sources/HealthChecks/PostgresHealthChecks/PostgresHealthChecks.swift +++ b/Sources/HealthChecks/PostgresHealthChecks/PostgresHealthChecks.swift @@ -40,13 +40,13 @@ public struct PostgresHealthChecks: PostgresHealthChecksProtocol { /// Get psql version /// - Returns: `HealthCheckItem` public func connection() async -> HealthCheckItem { - let dateNow = Date().timeIntervalSinceReferenceDate + let dateNow = Date().timeIntervalSince1970 let versionDescription = await getVersion() let result = HealthCheckItem( componentId: app.psqlId, componentType: .datastore, - observedValue: Date().timeIntervalSinceReferenceDate - dateNow, - observedUnit: "s", + // TODO: need get active connection + // observedValue: "", status: versionDescription.contains("PostgreSQL") ? .pass : .fail, time: app.dateTimeISOFormat.string(from: Date()), output: !versionDescription.contains("PostgreSQL") ? versionDescription : nil, @@ -59,13 +59,13 @@ public struct PostgresHealthChecks: PostgresHealthChecksProtocol { /// Get psql response time /// - Returns: `HealthCheckItem` public func responseTime() async -> HealthCheckItem { - let dateNow = Date().timeIntervalSinceReferenceDate + let dateNow = Date().timeIntervalSince1970 let versionDescription = await getVersion() let result = HealthCheckItem( componentId: app.psqlId, componentType: .datastore, - observedValue: Date().timeIntervalSinceReferenceDate - dateNow, - observedUnit: "s", + observedValue: (Date().timeIntervalSince1970 - dateNow) * 1000, + observedUnit: "ms", status: versionDescription.contains("PostgreSQL") ? .pass : .fail, time: app.dateTimeISOFormat.string(from: Date()), output: !versionDescription.contains("PostgreSQL") ? versionDescription : nil, diff --git a/Sources/HealthChecks/RedisHealthChecks/RedisHealthChecks.swift b/Sources/HealthChecks/RedisHealthChecks/RedisHealthChecks.swift index ef0b31c..6efcf0e 100644 --- a/Sources/HealthChecks/RedisHealthChecks/RedisHealthChecks.swift +++ b/Sources/HealthChecks/RedisHealthChecks/RedisHealthChecks.swift @@ -39,13 +39,13 @@ public struct RedisHealthChecks: RedisHealthChecksProtocol { /// Get redis connection /// - Returns: `HealthCheckItem` public func connection() async -> HealthCheckItem { - let dateNow = Date().timeIntervalSinceReferenceDate + let dateNow = Date().timeIntervalSince1970 let response = await ping() let result = HealthCheckItem( componentId: app.redisId, componentType: .datastore, - observedValue: Date().timeIntervalSinceReferenceDate - dateNow, - observedUnit: "s", + // TODO: need get active connection + // observedValue: "", status: response.lowercased().contains("pong") ? .pass : .fail, time: app.dateTimeISOFormat.string(from: Date()), output: !response.lowercased().contains("pong") ? response : nil, @@ -58,13 +58,13 @@ public struct RedisHealthChecks: RedisHealthChecksProtocol { /// Get response time from redis /// - Returns: `HealthCheckItem` public func responseTime() async -> HealthCheckItem { - let dateNow = Date().timeIntervalSinceReferenceDate + let dateNow = Date().timeIntervalSince1970 let response = await ping() let result = HealthCheckItem( componentId: app.redisId, componentType: .datastore, - observedValue: Date().timeIntervalSinceReferenceDate - dateNow, - observedUnit: "s", + observedValue: (Date().timeIntervalSince1970 - dateNow) * 1000, + observedUnit: "ms", status: response.lowercased().contains("pong") ? .pass : .fail, time: app.dateTimeISOFormat.string(from: Date()), output: !response.lowercased().contains("pong") ? response : nil, diff --git a/Tests/HealthChecksTests/ApplicationHealthChecksTests/ApplicationHealthChecksCheckTests.swift b/Tests/HealthChecksTests/ApplicationHealthChecksTests/ApplicationHealthChecksCheckTests.swift index d9cb823..bf9436e 100644 --- a/Tests/HealthChecksTests/ApplicationHealthChecksTests/ApplicationHealthChecksCheckTests.swift +++ b/Tests/HealthChecksTests/ApplicationHealthChecksTests/ApplicationHealthChecksCheckTests.swift @@ -31,7 +31,7 @@ final class ApplicationHealthChecksCheckTests: XCTestCase { func testCheck() async { let app = Application(.testing) defer { app.shutdown() } - app.launchTime = Date().timeIntervalSinceReferenceDate + app.launchTime = Date().timeIntervalSince1970 app.applicationHealthChecks = ApplicationHealthChecksMock() let result = await app.applicationHealthChecks?.check(for: [MeasurementType.uptime]) let uptime = result?[MeasurementType.uptime.rawValue] @@ -57,7 +57,7 @@ final class ApplicationHealthChecksCheckTests: XCTestCase { guard let observedValue = uptimeItem.observedValue else { return XCTFail("no have observed value") } - let expectedUptime = Date().timeIntervalSinceReferenceDate - app.launchTime + let expectedUptime = Date().timeIntervalSince1970 - app.launchTime XCTAssertTrue(abs(observedValue - expectedUptime) < 1.0) } diff --git a/Tests/HealthChecksTests/ApplicationHealthChecksTests/ApplicationHealthChecksUptimeTests.swift b/Tests/HealthChecksTests/ApplicationHealthChecksTests/ApplicationHealthChecksUptimeTests.swift index fc4711e..9760666 100644 --- a/Tests/HealthChecksTests/ApplicationHealthChecksTests/ApplicationHealthChecksUptimeTests.swift +++ b/Tests/HealthChecksTests/ApplicationHealthChecksTests/ApplicationHealthChecksUptimeTests.swift @@ -50,7 +50,7 @@ final class ApplicationHealthChecksUptimeTests: XCTestCase { XCTAssertEqual(item.status, .pass) // Assert time is within a reasonable range of actual uptime - let expectedUptime = Date().timeIntervalSinceReferenceDate - app.launchTime + let expectedUptime = Date().timeIntervalSince1970 - app.launchTime guard let value = item.observedValue else { return XCTFail("no have observed value") } diff --git a/Tests/HealthChecksTests/ConsulHealthChecksTests/ConsulHealthChecksResponseTimeTests.swift b/Tests/HealthChecksTests/ConsulHealthChecksTests/ConsulHealthChecksResponseTimeTests.swift index ae3339f..f9b08a8 100644 --- a/Tests/HealthChecksTests/ConsulHealthChecksTests/ConsulHealthChecksResponseTimeTests.swift +++ b/Tests/HealthChecksTests/ConsulHealthChecksTests/ConsulHealthChecksResponseTimeTests.swift @@ -42,7 +42,7 @@ final class ConsulHealthChecksResponseTimeTests: XCTestCase { let healthChecks = ConsulHealthChecks(app: app) let response = await healthChecks.getStatus() - let result = healthChecks.responseTime(from: response, Date().timeIntervalSinceReferenceDate) + let result = healthChecks.responseTime(from: response, Date().timeIntervalSince1970) XCTAssertEqual(result.status, .pass) guard let observedValue = result.observedValue else { return XCTFail("no have observed value") @@ -58,7 +58,7 @@ final class ConsulHealthChecksResponseTimeTests: XCTestCase { let clientResponse = ClientResponse(status: .badRequest) let healthChecks = ConsulHealthChecks(app: app) - let result = healthChecks.responseTime(from: clientResponse, Date().timeIntervalSinceReferenceDate) + let result = healthChecks.responseTime(from: clientResponse, Date().timeIntervalSince1970) XCTAssertEqual(result.status, .fail) guard let observedValue = result.observedValue else { return XCTFail("no have observed value")