From 637602ae8d3b0ebd9a0695d5b6467c442f3dbc04 Mon Sep 17 00:00:00 2001 From: Precious OSSAI Date: Tue, 19 Mar 2024 15:10:52 +0000 Subject: [PATCH 1/4] feat: add latency metrics to logs --- Sources/Risk/Logging/LoggerService.swift | 39 ++++++++++++++++--- Sources/Risk/Risk.swift | 8 ++-- Sources/Risk/Services/DeviceDataService.swift | 25 +++++++----- .../Risk/Services/FingerprintService.swift | 31 ++++++++++++--- Tests/Mocks/MockDeviceDataService.swift | 4 +- Tests/Mocks/MockFingerprintService.swift | 6 +-- Tests/Mocks/MockLoggerService.swift | 2 +- Tests/RiskTests/DeviceDataServiceTests.swift | 2 +- 8 files changed, 85 insertions(+), 32 deletions(-) diff --git a/Sources/Risk/Logging/LoggerService.swift b/Sources/Risk/Logging/LoggerService.swift index 2f529f7..cc474d2 100644 --- a/Sources/Risk/Logging/LoggerService.swift +++ b/Sources/Risk/Logging/LoggerService.swift @@ -17,6 +17,18 @@ enum RiskEvent: String, Codable { case loadFailure = "riskLoadFailure" } +struct Elapsed { + let block: Double? // time to retrieve configuration or loadFailure + let deviceDataPersist: Double? // time to persist data + let fpload: Double? // time to load fingerprint + let fppublish: Double? // time to publish fingerprint data + let total: Double? // total time + + private enum CodingKeys: String, CodingKey { + case block = "Block", deviceDataPersist = "DeviceDataPersist", fpload = "FpLoad", fppublish = "FpPublish", total = "Total" + } +} + struct RiskLogError { let reason: String // service method let message: String // description of error @@ -30,11 +42,11 @@ struct RiskLogError { protocol LoggerServiceProtocol { init(internalConfig: RiskSDKInternalConfig) - func log(riskEvent: RiskEvent, deviceSessionId: String?, requestId: String?, error: RiskLogError?) + func log(riskEvent: RiskEvent, blockTime: Double?, deviceDataPersistTime: Double?, fpLoadTime: Double?, fpPublishTime: Double?, deviceSessionId: String?, requestId: String?, error: RiskLogError?) } extension LoggerServiceProtocol { - func formatEvent(internalConfig: RiskSDKInternalConfig, riskEvent: RiskEvent, deviceSessionId: String?, requestId: String?, error: RiskLogError?) -> Event { + func formatEvent(internalConfig: RiskSDKInternalConfig, riskEvent: RiskEvent, deviceSessionId: String?, requestId: String?, error: RiskLogError?, latencyMetric: Elapsed) -> Event { let maskedPublicKey = getMaskedPublicKey(publicKey: internalConfig.merchantPublicKey) let ddTags = getDDTags(environment: internalConfig.environment.rawValue) var monitoringLevel: MonitoringLevel @@ -56,17 +68,27 @@ extension LoggerServiceProtocol { switch riskEvent { case .published, .collected: properties = [ + "Block": AnyCodable(latencyMetric.block), + "DeviceDataPersist": AnyCodable(latencyMetric.deviceDataPersist), + "FpLoad": AnyCodable(latencyMetric.fpload), + "FpPublish": AnyCodable(latencyMetric.fppublish), + "Total": AnyCodable(latencyMetric.total), "EventType": AnyCodable(riskEvent.rawValue), "FramesMode": AnyCodable(internalConfig.framesMode), "MaskedPublicKey": AnyCodable(maskedPublicKey), "ddTags": AnyCodable(ddTags), "RiskSDKVersion": AnyCodable(Constants.riskSdkVersion), "Timezone": AnyCodable(TimeZone.current.identifier), - "RequestId": AnyCodable(requestId), + "FpRequestId": AnyCodable(requestId), "DeviceSessionId": AnyCodable(deviceSessionId), ] case .publishFailure, .loadFailure, .publishDisabled: properties = [ + "Block": AnyCodable(latencyMetric.block), + "DeviceDataPersist": AnyCodable(latencyMetric.deviceDataPersist), + "FpLoad": AnyCodable(latencyMetric.fpload), + "FpPublish": AnyCodable(latencyMetric.fppublish), + "Total": AnyCodable(latencyMetric.total), "EventType": AnyCodable(riskEvent.rawValue), "FramesMode": AnyCodable(internalConfig.framesMode), "MaskedPublicKey": AnyCodable(maskedPublicKey), @@ -144,11 +166,16 @@ struct LoggerService: LoggerServiceProtocol { } - func log(riskEvent: RiskEvent, deviceSessionId: String? = nil, requestId: String? = nil, error: RiskLogError? = nil) { - let event = formatEvent(internalConfig: internalConfig, riskEvent: riskEvent, deviceSessionId: deviceSessionId, requestId: requestId, error: error) + func log(riskEvent: RiskEvent, blockTime: Double? = nil, deviceDataPersistTime: Double? = nil, fpLoadTime: Double? = nil, fpPublishTime: Double? = nil, deviceSessionId: String? = nil, requestId: String? = nil, error: RiskLogError? = nil) { + + let totalLatency = (blockTime ?? 0.00) + (deviceDataPersistTime ?? 0.00) + (fpLoadTime ?? 0.00) + (fpPublishTime ?? 0.00) + + let latencyMetric = Elapsed(block: blockTime, deviceDataPersist: deviceDataPersistTime, fpload: fpLoadTime, fppublish: fpPublishTime, total: totalLatency) + + let event = formatEvent(internalConfig: internalConfig, riskEvent: riskEvent, deviceSessionId: deviceSessionId, requestId: requestId, error: error, latencyMetric: latencyMetric) logger.log(event: event) } - + private func getDeviceModel() -> String { #if targetEnvironment(simulator) if let identifier = ProcessInfo().environment["SIMULATOR_MODEL_IDENTIFIER"] { diff --git a/Sources/Risk/Risk.swift b/Sources/Risk/Risk.swift index 0c4900e..d6ed792 100644 --- a/Sources/Risk/Risk.swift +++ b/Sources/Risk/Risk.swift @@ -50,8 +50,8 @@ public final class Risk { guard let self = self else { return } switch fpResult { - case .success(let requestId): - self.persistFpData(cardToken: cardToken, fingerprintRequestId: requestId, completion: completion) + case .success(let response): + self.persistFpData(cardToken: cardToken, fingerprintRequestId: response.requestId, fpLoadTime: response.fpLoadTime, fpPublishTime: response.fpPublishTime, completion: completion) case .failure(let error): completion(.failure(error)) @@ -59,8 +59,8 @@ public final class Risk { } } - private func persistFpData(cardToken: String?, fingerprintRequestId: String, completion: @escaping (Result) -> Void) { - self.deviceDataService.persistFpData(fingerprintRequestId: fingerprintRequestId, cardToken: cardToken) { result in + private func persistFpData(cardToken: String?, fingerprintRequestId: String, fpLoadTime: Double, fpPublishTime: Double, completion: @escaping (Result) -> Void) { + self.deviceDataService.persistFpData(fingerprintRequestId: fingerprintRequestId, fpLoadTime: fpLoadTime, fpPublishTime: fpPublishTime, cardToken: cardToken) { result in switch result { case .success(let response): completion(.success(PublishRiskData(deviceSessionId: response.deviceSessionId))) diff --git a/Sources/Risk/Services/DeviceDataService.swift b/Sources/Risk/Services/DeviceDataService.swift index 9991ce8..49cf2c9 100644 --- a/Sources/Risk/Services/DeviceDataService.swift +++ b/Sources/Risk/Services/DeviceDataService.swift @@ -7,6 +7,7 @@ // import Foundation +import QuartzCore struct FingerprintIntegration: Decodable, Equatable { let enabled: Bool @@ -42,31 +43,35 @@ struct PersistDeviceDataResponse: Decodable, Equatable { protocol DeviceDataServiceProtocol { func getConfiguration(completion: @escaping (Result) -> Void) - func persistFpData(fingerprintRequestId: String, cardToken: String?, completion: @escaping (Result) -> Void) + func persistFpData(fingerprintRequestId: String, fpLoadTime: Double, fpPublishTime: Double, cardToken: String?, completion: @escaping (Result) -> Void) } -struct DeviceDataService: DeviceDataServiceProtocol { +final class DeviceDataService: DeviceDataServiceProtocol { let config: RiskSDKInternalConfig let apiService: APIServiceProtocol let loggerService: LoggerServiceProtocol + var blockTime: Double init(config: RiskSDKInternalConfig, apiService: APIServiceProtocol = APIService(), loggerService: LoggerServiceProtocol) { self.config = config self.apiService = apiService self.loggerService = loggerService + self.blockTime = 0.00 } func getConfiguration(completion: @escaping (Result) -> Void) { + let startBlockTime = CACurrentMediaTime() let endpoint = "\(config.deviceDataEndpoint)/configuration?integrationType=\(config.integrationType.rawValue)&riskSdkVersion=\(Constants.riskSdkVersion)&timezone=\(TimeZone.current.identifier)" let authToken = config.merchantPublicKey apiService.getJSONFromAPIWithAuthorization(endpoint: endpoint, authToken: authToken, responseType: DeviceDataConfiguration.self) { result in + let endBlockTime = CACurrentMediaTime() + self.blockTime = (endBlockTime - startBlockTime) * 1000 switch result { case .success(let configuration): - guard configuration.fingerprintIntegration.enabled, let fingerprintPublicKey = configuration.fingerprintIntegration.publicKey else { - loggerService.log(riskEvent: .publishDisabled, deviceSessionId: nil, requestId: nil, error: RiskLogError(reason: "getConfiguration", message: RiskError.Configuration.integrationDisabled.localizedDescription, status: nil, type: "Error")) + self.loggerService.log(riskEvent: .publishDisabled, blockTime: self.blockTime, deviceDataPersistTime: nil, fpLoadTime: nil, fpPublishTime: nil, deviceSessionId: nil, requestId: nil, error: RiskLogError(reason: "getConfiguration", message: RiskError.Configuration.integrationDisabled.localizedDescription, status: nil, type: "Error")) return completion(.failure(.integrationDisabled)) } @@ -74,14 +79,14 @@ struct DeviceDataService: DeviceDataServiceProtocol { completion(.success( FingerprintConfiguration.init(publicKey: fingerprintPublicKey))) case .failure(let error): - - loggerService.log(riskEvent: .loadFailure, deviceSessionId: nil, requestId: nil, error: RiskLogError(reason: "getConfiguration", message: error.localizedDescription, status: nil, type: "Error")) + self.loggerService.log(riskEvent: .loadFailure, blockTime: self.blockTime, deviceDataPersistTime: nil, fpLoadTime: nil, fpPublishTime: nil, deviceSessionId: nil, requestId: nil, error: RiskLogError(reason: "getConfiguration", message: error.localizedDescription, status: nil, type: "Error")) return completion(.failure(.couldNotRetrieveConfiguration)) } } } - func persistFpData(fingerprintRequestId: String, cardToken: String?, completion: @escaping (Result) -> Void) { + func persistFpData(fingerprintRequestId: String, fpLoadTime: Double, fpPublishTime: Double, cardToken: String?, completion: @escaping (Result) -> Void) { + let startPersistTime = CACurrentMediaTime() let endpoint = "\(config.deviceDataEndpoint)/fingerprint?riskSdkVersion=\(Constants.riskSdkVersion)" let authToken = config.merchantPublicKey let integrationType = config.integrationType @@ -93,14 +98,16 @@ struct DeviceDataService: DeviceDataServiceProtocol { ) apiService.putDataToAPIWithAuthorization(endpoint: endpoint, authToken: authToken, data: data, responseType: PersistDeviceDataResponse.self) { result in + let endPersistTime = CACurrentMediaTime() + let persistTime = (endPersistTime - startPersistTime) * 1000 switch result { case .success(let response): - loggerService.log(riskEvent: .published, deviceSessionId: response.deviceSessionId, requestId: fingerprintRequestId, error: nil) + self.loggerService.log(riskEvent: .published, blockTime: self.blockTime, deviceDataPersistTime: persistTime, fpLoadTime: fpLoadTime, fpPublishTime: fpPublishTime, deviceSessionId: response.deviceSessionId, requestId: fingerprintRequestId, error: nil) completion(.success(response)) case .failure(let error): - loggerService.log(riskEvent: .publishFailure, deviceSessionId: nil, requestId: nil, error: RiskLogError(reason: "persistFpData", message: error.localizedDescription, status: nil, type: "Error")) + self.loggerService.log(riskEvent: .publishFailure, blockTime: self.blockTime, deviceDataPersistTime: nil, fpLoadTime: fpLoadTime, fpPublishTime: fpPublishTime, deviceSessionId: nil, requestId: nil, error: RiskLogError(reason: "persistFpData", message: error.localizedDescription, status: nil, type: "Error")) completion(.failure(.couldNotPersisRiskData)) } diff --git a/Sources/Risk/Services/FingerprintService.swift b/Sources/Risk/Services/FingerprintService.swift index ea489be..cd57943 100644 --- a/Sources/Risk/Services/FingerprintService.swift +++ b/Sources/Risk/Services/FingerprintService.swift @@ -7,9 +7,10 @@ import FingerprintPro import Foundation +import QuartzCore protocol FingerprintServiceProtocol { - func publishData(completion: @escaping (Result) -> Void) + func publishData(completion: @escaping (Result) -> Void) } extension FingerprintServiceProtocol { @@ -22,40 +23,58 @@ extension FingerprintServiceProtocol { } } +struct FpPublishData { + let requestId: String + let fpLoadTime: Double + let fpPublishTime: Double +} + final class FingerprintService: FingerprintServiceProtocol { private var requestId: String? private let client: FingerprintClientProviding private let internalConfig: RiskSDKInternalConfig private let loggerService: LoggerServiceProtocol + private let fpLoadTime: Double + private var fpPublishTime: Double init(fingerprintPublicKey: String, internalConfig: RiskSDKInternalConfig, loggerService: LoggerServiceProtocol) { + + let startBlockTime = CACurrentMediaTime() + let customDomain: Region = .custom(domain: internalConfig.fingerprintEndpoint) let configuration = Configuration(apiKey: fingerprintPublicKey, region: customDomain) client = FingerprintProFactory.getInstance(configuration) + let endBlockTime = CACurrentMediaTime() + self.fpLoadTime = (endBlockTime - startBlockTime) * 1000 + self.fpPublishTime = 0.00 + self.internalConfig = internalConfig self.loggerService = loggerService } - func publishData(completion: @escaping (Result) -> Void) { + func publishData(completion: @escaping (Result) -> Void) { + let startFpPublishTime = CACurrentMediaTime() guard requestId == nil else { - return completion(.success(requestId!)) + return completion(.success(FpPublishData(requestId: requestId!, fpLoadTime: self.fpLoadTime, fpPublishTime: self.fpPublishTime))) } let metadata = createMetadata(sourceType: internalConfig.sourceType.rawValue) client.getVisitorIdResponse(metadata) { [weak self] result in + let endFpPublishTime = CACurrentMediaTime() + self?.fpPublishTime = (endFpPublishTime - startFpPublishTime) * 1000 switch result { case .failure(let error): - self?.loggerService.log(riskEvent: .publishFailure, deviceSessionId: nil, requestId: nil, error: RiskLogError(reason: "publishData", message: error.localizedDescription, status: nil, type: "Error")) + self?.loggerService.log(riskEvent: .publishFailure, blockTime: nil, deviceDataPersistTime: nil, fpLoadTime: self?.fpLoadTime, fpPublishTime: self?.fpPublishTime, deviceSessionId: nil, requestId: nil, error: RiskLogError(reason: "publishData", message: error.localizedDescription, status: nil, type: "Error")) return completion(.failure(.couldNotPublishRiskData)) case let .success(response): - self?.loggerService.log(riskEvent: .collected, deviceSessionId: nil, requestId: response.requestId, error: nil) + self?.loggerService.log(riskEvent: .collected, blockTime: nil, deviceDataPersistTime: nil, fpLoadTime: self?.fpLoadTime, fpPublishTime: self?.fpPublishTime, deviceSessionId: nil, requestId: response.requestId, error: nil) self?.requestId = response.requestId - completion(.success(response.requestId)) + completion(.success(FpPublishData(requestId: response.requestId, fpLoadTime: self?.fpLoadTime ?? 0.00, fpPublishTime: self?.fpPublishTime ?? 0.00))) } } } diff --git a/Tests/Mocks/MockDeviceDataService.swift b/Tests/Mocks/MockDeviceDataService.swift index a5580f3..63b8a03 100644 --- a/Tests/Mocks/MockDeviceDataService.swift +++ b/Tests/Mocks/MockDeviceDataService.swift @@ -20,8 +20,8 @@ class MockDeviceDataService: DeviceDataServiceProtocol { completion(.failure(.couldNotRetrieveConfiguration)) } } - - func persistFpData(fingerprintRequestId: String, cardToken: String?, completion: @escaping (Result) -> Void) { + + func persistFpData(fingerprintRequestId: String, fpLoadTime: Double, fpPublishTime: Double, cardToken: String?, completion: @escaping (Result) -> Void) { if shouldSucceedPersistFpData { let response = PersistDeviceDataResponse(deviceSessionId: "mocked_device_session_id") completion(.success(response)) diff --git a/Tests/Mocks/MockFingerprintService.swift b/Tests/Mocks/MockFingerprintService.swift index 3247d89..42a7cfb 100644 --- a/Tests/Mocks/MockFingerprintService.swift +++ b/Tests/Mocks/MockFingerprintService.swift @@ -12,13 +12,13 @@ class MockFingerprintService: FingerprintServiceProtocol { var shouldSucceed: Bool = true var requestId: String? - func publishData(completion: @escaping (Result) -> Void) { + func publishData(completion: @escaping (Result) -> Void) { if shouldSucceed { if let requestId = requestId { - completion(.success(requestId)) + completion(.success(FpPublishData(requestId: requestId, fpLoadTime: 123.00, fpPublishTime: 321.00))) } else { let fakeRequestId = "fakeRequestId" - completion(.success(fakeRequestId)) + completion(.success(FpPublishData(requestId: fakeRequestId, fpLoadTime: 123.00, fpPublishTime: 321.00))) } } else { completion(.failure(.couldNotPublishRiskData)) diff --git a/Tests/Mocks/MockLoggerService.swift b/Tests/Mocks/MockLoggerService.swift index 926892a..1da0187 100644 --- a/Tests/Mocks/MockLoggerService.swift +++ b/Tests/Mocks/MockLoggerService.swift @@ -8,5 +8,5 @@ struct MockLoggerService: LoggerServiceProtocol { init(internalConfig: RiskSDKInternalConfig) {} - func log(riskEvent: RiskEvent, deviceSessionId: String?, requestId: String?, error: RiskLogError?) {} + func log(riskEvent: RiskEvent, blockTime: Double?, deviceDataPersistTime: Double?, fpLoadTime: Double?, fpPublishTime: Double?, deviceSessionId: String?, requestId: String?, error: RiskLogError?) {} } diff --git a/Tests/RiskTests/DeviceDataServiceTests.swift b/Tests/RiskTests/DeviceDataServiceTests.swift index 217dd50..e4389b9 100644 --- a/Tests/RiskTests/DeviceDataServiceTests.swift +++ b/Tests/RiskTests/DeviceDataServiceTests.swift @@ -51,7 +51,7 @@ class DeviceDataServiceTests: XCTestCase { mockAPIService.expectedDeviceDataResult = .success(expectedResponse) - deviceDataService.persistFpData(fingerprintRequestId: "12345.ab0cd", cardToken: "") { result in + deviceDataService.persistFpData(fingerprintRequestId: "12345.ab0cd", fpLoadTime: 123.00, fpPublishTime: 321.00, cardToken: "") { result in XCTAssertEqual(result, .success(expectedResponse)) expectation.fulfill() From e410bb70080a74442e8300adebcb6928cc434850 Mon Sep 17 00:00:00 2001 From: Precious OSSAI Date: Wed, 20 Mar 2024 12:01:19 +0000 Subject: [PATCH 2/4] refactor: change location of certain latency metric logs --- Sources/Risk/Risk.swift | 3 ++- Sources/Risk/Services/DeviceDataService.swift | 13 +++++++------ Sources/Risk/Services/FingerprintService.swift | 13 +++++++------ 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/Sources/Risk/Risk.swift b/Sources/Risk/Risk.swift index d6ed792..9753da0 100644 --- a/Sources/Risk/Risk.swift +++ b/Sources/Risk/Risk.swift @@ -30,7 +30,8 @@ public final class Risk { self.fingerprintService = FingerprintService( fingerprintPublicKey: configuration.publicKey, internalConfig: self.internalConfig, - loggerService: self.loggerService + loggerService: self.loggerService, + blockTime: configuration.blockTime ) completion(.success(())) diff --git a/Sources/Risk/Services/DeviceDataService.swift b/Sources/Risk/Services/DeviceDataService.swift index 49cf2c9..cf58c30 100644 --- a/Sources/Risk/Services/DeviceDataService.swift +++ b/Sources/Risk/Services/DeviceDataService.swift @@ -16,6 +16,7 @@ struct FingerprintIntegration: Decodable, Equatable { struct FingerprintConfiguration: Equatable { let publicKey: String + let blockTime: Double } struct DeviceDataConfiguration: Decodable, Equatable { @@ -66,10 +67,10 @@ final class DeviceDataService: DeviceDataServiceProtocol { apiService.getJSONFromAPIWithAuthorization(endpoint: endpoint, authToken: authToken, responseType: DeviceDataConfiguration.self) { result in - let endBlockTime = CACurrentMediaTime() - self.blockTime = (endBlockTime - startBlockTime) * 1000 switch result { case .success(let configuration): + let endBlockTime = CACurrentMediaTime() + self.blockTime = (endBlockTime - startBlockTime) * 1000 guard configuration.fingerprintIntegration.enabled, let fingerprintPublicKey = configuration.fingerprintIntegration.publicKey else { self.loggerService.log(riskEvent: .publishDisabled, blockTime: self.blockTime, deviceDataPersistTime: nil, fpLoadTime: nil, fpPublishTime: nil, deviceSessionId: nil, requestId: nil, error: RiskLogError(reason: "getConfiguration", message: RiskError.Configuration.integrationDisabled.localizedDescription, status: nil, type: "Error")) @@ -77,9 +78,9 @@ final class DeviceDataService: DeviceDataServiceProtocol { } completion(.success( - FingerprintConfiguration.init(publicKey: fingerprintPublicKey))) + FingerprintConfiguration.init(publicKey: fingerprintPublicKey, blockTime: self.blockTime))) case .failure(let error): - self.loggerService.log(riskEvent: .loadFailure, blockTime: self.blockTime, deviceDataPersistTime: nil, fpLoadTime: nil, fpPublishTime: nil, deviceSessionId: nil, requestId: nil, error: RiskLogError(reason: "getConfiguration", message: error.localizedDescription, status: nil, type: "Error")) + self.loggerService.log(riskEvent: .loadFailure, blockTime: nil, deviceDataPersistTime: nil, fpLoadTime: nil, fpPublishTime: nil, deviceSessionId: nil, requestId: nil, error: RiskLogError(reason: "getConfiguration", message: error.localizedDescription, status: nil, type: "Error")) return completion(.failure(.couldNotRetrieveConfiguration)) } } @@ -98,11 +99,11 @@ final class DeviceDataService: DeviceDataServiceProtocol { ) apiService.putDataToAPIWithAuthorization(endpoint: endpoint, authToken: authToken, data: data, responseType: PersistDeviceDataResponse.self) { result in - let endPersistTime = CACurrentMediaTime() - let persistTime = (endPersistTime - startPersistTime) * 1000 switch result { case .success(let response): + let endPersistTime = CACurrentMediaTime() + let persistTime = (endPersistTime - startPersistTime) * 1000 self.loggerService.log(riskEvent: .published, blockTime: self.blockTime, deviceDataPersistTime: persistTime, fpLoadTime: fpLoadTime, fpPublishTime: fpPublishTime, deviceSessionId: response.deviceSessionId, requestId: fingerprintRequestId, error: nil) completion(.success(response)) diff --git a/Sources/Risk/Services/FingerprintService.swift b/Sources/Risk/Services/FingerprintService.swift index cd57943..a276b0a 100644 --- a/Sources/Risk/Services/FingerprintService.swift +++ b/Sources/Risk/Services/FingerprintService.swift @@ -36,8 +36,9 @@ final class FingerprintService: FingerprintServiceProtocol { private let loggerService: LoggerServiceProtocol private let fpLoadTime: Double private var fpPublishTime: Double + private let blockTime: Double - init(fingerprintPublicKey: String, internalConfig: RiskSDKInternalConfig, loggerService: LoggerServiceProtocol) { + init(fingerprintPublicKey: String, internalConfig: RiskSDKInternalConfig, loggerService: LoggerServiceProtocol, blockTime: Double) { let startBlockTime = CACurrentMediaTime() @@ -47,7 +48,7 @@ final class FingerprintService: FingerprintServiceProtocol { let endBlockTime = CACurrentMediaTime() self.fpLoadTime = (endBlockTime - startBlockTime) * 1000 self.fpPublishTime = 0.00 - + self.blockTime = blockTime self.internalConfig = internalConfig self.loggerService = loggerService } @@ -62,16 +63,16 @@ final class FingerprintService: FingerprintServiceProtocol { let metadata = createMetadata(sourceType: internalConfig.sourceType.rawValue) client.getVisitorIdResponse(metadata) { [weak self] result in - let endFpPublishTime = CACurrentMediaTime() - self?.fpPublishTime = (endFpPublishTime - startFpPublishTime) * 1000 switch result { case .failure(let error): - self?.loggerService.log(riskEvent: .publishFailure, blockTime: nil, deviceDataPersistTime: nil, fpLoadTime: self?.fpLoadTime, fpPublishTime: self?.fpPublishTime, deviceSessionId: nil, requestId: nil, error: RiskLogError(reason: "publishData", message: error.localizedDescription, status: nil, type: "Error")) + self?.loggerService.log(riskEvent: .publishFailure, blockTime: self?.blockTime, deviceDataPersistTime: nil, fpLoadTime: self?.fpLoadTime, fpPublishTime: nil, deviceSessionId: nil, requestId: nil, error: RiskLogError(reason: "publishData", message: error.localizedDescription, status: nil, type: "Error")) return completion(.failure(.couldNotPublishRiskData)) case let .success(response): - self?.loggerService.log(riskEvent: .collected, blockTime: nil, deviceDataPersistTime: nil, fpLoadTime: self?.fpLoadTime, fpPublishTime: self?.fpPublishTime, deviceSessionId: nil, requestId: response.requestId, error: nil) + let endFpPublishTime = CACurrentMediaTime() + self?.fpPublishTime = (endFpPublishTime - startFpPublishTime) * 1000 + self?.loggerService.log(riskEvent: .collected, blockTime: self?.blockTime, deviceDataPersistTime: nil, fpLoadTime: self?.fpLoadTime, fpPublishTime: self?.fpPublishTime, deviceSessionId: nil, requestId: response.requestId, error: nil) self?.requestId = response.requestId completion(.success(FpPublishData(requestId: response.requestId, fpLoadTime: self?.fpLoadTime ?? 0.00, fpPublishTime: self?.fpPublishTime ?? 0.00))) From 13a8726a1cbdf66452f6c4e7156266bd45430f34 Mon Sep 17 00:00:00 2001 From: Precious OSSAI Date: Wed, 20 Mar 2024 12:08:23 +0000 Subject: [PATCH 3/4] refactor: update tests --- Tests/Mocks/MockDeviceDataService.swift | 2 +- Tests/RiskTests/DeviceDataServiceTests.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/Mocks/MockDeviceDataService.swift b/Tests/Mocks/MockDeviceDataService.swift index 63b8a03..d0e03c1 100644 --- a/Tests/Mocks/MockDeviceDataService.swift +++ b/Tests/Mocks/MockDeviceDataService.swift @@ -14,7 +14,7 @@ class MockDeviceDataService: DeviceDataServiceProtocol { func getConfiguration(completion: @escaping (Result) -> Void) { if shouldReturnConfiguration { - let configuration = FingerprintConfiguration(publicKey: "mocked_public_key") + let configuration = FingerprintConfiguration(publicKey: "mocked_public_key", blockTime: 123.00) completion(.success(configuration)) } else { completion(.failure(.couldNotRetrieveConfiguration)) diff --git a/Tests/RiskTests/DeviceDataServiceTests.swift b/Tests/RiskTests/DeviceDataServiceTests.swift index e4389b9..d79b719 100644 --- a/Tests/RiskTests/DeviceDataServiceTests.swift +++ b/Tests/RiskTests/DeviceDataServiceTests.swift @@ -23,7 +23,7 @@ class DeviceDataServiceTests: XCTestCase { let expectation = self.expectation(description: "Configuration received") let expectedApiConfiguration = DeviceDataConfiguration(fingerprintIntegration: FingerprintIntegration(enabled: true, publicKey: "mockPublicKey")) - let expectedDeviceDataServiceConfiguration = FingerprintConfiguration(publicKey: "mockPublicKey") + let expectedDeviceDataServiceConfiguration = FingerprintConfiguration(publicKey: "mockPublicKey", blockTime: 123.00) mockAPIService.expectedResult = .success(expectedApiConfiguration) From 9c654ff5869f868cfabc444fb6850286cb5f836b Mon Sep 17 00:00:00 2001 From: Precious OSSAI Date: Wed, 20 Mar 2024 13:32:03 +0000 Subject: [PATCH 4/4] refactor: update tests --- Tests/RiskTests/DeviceDataServiceTests.swift | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Tests/RiskTests/DeviceDataServiceTests.swift b/Tests/RiskTests/DeviceDataServiceTests.swift index d79b719..df384b1 100644 --- a/Tests/RiskTests/DeviceDataServiceTests.swift +++ b/Tests/RiskTests/DeviceDataServiceTests.swift @@ -28,7 +28,12 @@ class DeviceDataServiceTests: XCTestCase { mockAPIService.expectedResult = .success(expectedApiConfiguration) deviceDataService.getConfiguration { configuration in - XCTAssertEqual(configuration, .success(expectedDeviceDataServiceConfiguration)) + switch configuration { + case .success(let result): + XCTAssertEqual(result.publicKey, expectedDeviceDataServiceConfiguration.publicKey) + case .failure: + return + } expectation.fulfill() }