-
Notifications
You must be signed in to change notification settings - Fork 319
/
DiagnosticsTracker.swift
109 lines (91 loc) · 3.99 KB
/
DiagnosticsTracker.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//
// Copyright RevenueCat Inc. All Rights Reserved.
//
// Licensed under the MIT License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://opensource.org/licenses/MIT
//
// DiagnosticsTracker.swift
//
// Created by Cesar de la Vega on 4/4/24.
import Foundation
protocol DiagnosticsTrackerType {
@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *)
func track(_ event: DiagnosticsEvent) async
@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *)
func trackCustomerInfoVerificationResultIfNeeded(_ customerInfo: CustomerInfo) async
@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *)
// swiftlint:disable:next function_parameter_count
func trackHttpRequestPerformed(endpointName: String,
responseTime: TimeInterval,
wasSuccessful: Bool,
responseCode: Int,
resultOrigin: HTTPResponseOrigin?,
verificationResult: VerificationResult) async
}
@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *)
final class DiagnosticsTracker: DiagnosticsTrackerType {
private let diagnosticsFileHandler: DiagnosticsFileHandlerType
private let dateProvider: DateProvider
init(diagnosticsFileHandler: DiagnosticsFileHandlerType,
dateProvider: DateProvider = DateProvider()) {
self.diagnosticsFileHandler = diagnosticsFileHandler
self.dateProvider = dateProvider
}
func track(_ event: DiagnosticsEvent) async {
await self.clearDiagnosticsFileIfTooBig()
await self.diagnosticsFileHandler.appendEvent(diagnosticsEvent: event)
}
func trackCustomerInfoVerificationResultIfNeeded(
_ customerInfo: CustomerInfo
) async {
let verificationResult = customerInfo.entitlements.verification
if verificationResult == .notRequested {
return
}
let event = DiagnosticsEvent(
eventType: .customerInfoVerificationResult,
properties: [.verificationResultKey: AnyEncodable(verificationResult.name)],
timestamp: self.dateProvider.now()
)
await track(event)
}
// swiftlint:disable:next function_parameter_count
func trackHttpRequestPerformed(endpointName: String,
responseTime: TimeInterval,
wasSuccessful: Bool,
responseCode: Int,
resultOrigin: HTTPResponseOrigin?,
verificationResult: VerificationResult) async {
await track(
DiagnosticsEvent(
eventType: DiagnosticsEvent.EventType.httpRequestPerformed,
properties: [
.endpointNameKey: AnyEncodable(endpointName),
.responseTimeMillisKey: AnyEncodable(responseTime * 1000),
.successfulKey: AnyEncodable(wasSuccessful),
.responseCodeKey: AnyEncodable(responseCode),
.eTagHitKey: AnyEncodable(resultOrigin == .cache),
.verificationResultKey: AnyEncodable(verificationResult.name)
],
timestamp: self.dateProvider.now()
)
)
}
}
@available(iOS 15.0, tvOS 15.0, macOS 12.0, watchOS 8.0, *)
private extension DiagnosticsTracker {
func clearDiagnosticsFileIfTooBig() async {
if await self.diagnosticsFileHandler.isDiagnosticsFileTooBig() {
await self.diagnosticsFileHandler.emptyDiagnosticsFile()
await self.trackMaxEventsStoredLimitReached()
}
}
func trackMaxEventsStoredLimitReached() async {
await self.track(.init(eventType: .maxEventsStoredLimitReached,
properties: [:],
timestamp: self.dateProvider.now()))
}
}