Skip to content

Commit

Permalink
fix: change timeIntervalSinceReferenceDate to timeIntervalSince1970 a…
Browse files Browse the repository at this point in the history
…nd get response time in ms
  • Loading branch information
gulivero1773 committed Apr 20, 2024
1 parent 786125b commit 969d28f
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}

Expand Down
16 changes: 8 additions & 8 deletions Sources/HealthChecks/MongoHealthChecks/MongoHealthChecks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
12 changes: 6 additions & 6 deletions Sources/HealthChecks/RedisHealthChecks/RedisHealthChecks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit 969d28f

Please sign in to comment.