Skip to content

Commit 046c7aa

Browse files
authored
fix(Analytics): Fixing session duration being reported for non-stopped sessions. (#3403)
1 parent bdb1567 commit 046c7aa

File tree

4 files changed

+55
-3
lines changed

4 files changed

+55
-3
lines changed

AmplifyPlugins/Internal/Sources/InternalAWSPinpoint/Analytics/PinpointEvent+PinpointClientTypes.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ import AWSPluginsCore
1010
import Foundation
1111

1212
extension PinpointEvent {
13-
var clientTypeSession: PinpointClientTypes.Session {
13+
var clientTypeSession: PinpointClientTypes.Session? {
14+
#if os(watchOS)
15+
// If the session duration cannot be represented by Int, return a nil session instead.
16+
// This is extremely unlikely to happen since a session's stopTime is set when the app is closed
17+
if let duration = session.duration, duration > Int.max {
18+
return nil
19+
}
20+
#endif
1421
return PinpointClientTypes.Session(duration: Int(session.duration),
1522
id: session.sessionId,
1623
startTimestamp: session.startTime.asISO8601String,
@@ -48,3 +55,12 @@ extension Bundle {
4855
object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? ""
4956
}
5057
}
58+
59+
private extension Int {
60+
init?(_ value: Int64?) {
61+
guard let value = value else {
62+
return nil
63+
}
64+
self.init(value)
65+
}
66+
}

AmplifyPlugins/Internal/Sources/InternalAWSPinpoint/Session/PinpointSession.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ public class PinpointSession: Codable {
3535
return stopTime != nil
3636
}
3737

38-
var duration: Date.Millisecond {
39-
let endTime = stopTime ?? Date()
38+
var duration: Date.Millisecond? {
39+
/// According to Pinpoint's documentation, `duration` is only required if `stopTime` is not nil.
40+
guard let endTime = stopTime else {
41+
return nil
42+
}
4043
return endTime.millisecondsSince1970 - startTime.millisecondsSince1970
4144
}
4245

AmplifyPlugins/Internal/Tests/InternalAWSPinpointUnitTests/EventRecorderTests.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,37 @@ class EventRecorderTests: XCTestCase {
9494
XCTAssertEqual(storage.deleteEventCallCount, 2)
9595
}
9696

97+
/// - Given: a event recorder with events saved in the local storage with active and stopped sessions
98+
/// - When: submitAllEvents is invoked
99+
/// - Then: the input is generated accordingly by including duration only for the stopped session
100+
func testSubmitAllEvents_withActiveAndStoppedSessions_shouldGenerateRightInput() async throws {
101+
let activeSession = PinpointSession(
102+
sessionId: "active",
103+
startTime: Date(),
104+
stopTime: nil
105+
)
106+
let stoppedSession = PinpointSession(
107+
sessionId: "stopped",
108+
startTime: Date().addingTimeInterval(-10),
109+
stopTime: Date()
110+
)
111+
storage.events = [
112+
.init(id: "1", eventType: "eventType1", eventDate: Date(), session: activeSession),
113+
.init(id: "2", eventType: "eventType2", eventDate: Date(), session: stoppedSession)
114+
]
115+
116+
_ = try? await recorder.submitAllEvents()
117+
XCTAssertEqual(pinpointClient.putEventsCount, 1)
118+
let input = try XCTUnwrap(pinpointClient.putEventsLastInput)
119+
let batchItem = try XCTUnwrap(input.eventsRequest?.batchItem?.first?.value as? PinpointClientTypes.EventsBatch)
120+
let events = try XCTUnwrap(batchItem.events)
121+
XCTAssertEqual(events.count, 2)
122+
XCTAssertNotNil(events["1"]?.session, "Expected session for eventType1")
123+
XCTAssertNil(events["1"]?.session?.duration, "Expected nil session duration for eventType")
124+
XCTAssertNotNil(events["2"]?.session, "Expected session for eventType2")
125+
XCTAssertNotNil(events["2"]?.session?.duration, "Expected session duration for eventType2")
126+
}
127+
97128
/// - Given: a event recorder with events saved in the local storage
98129
/// - When: submitAllEvents is invoked and fails with a non-retryable error
99130
/// - Then: the events are marked as dirty

AmplifyPlugins/Internal/Tests/InternalAWSPinpointUnitTests/Mocks/MockPinpointClient.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,10 @@ class MockPinpointClient: PinpointClientProtocol {
367367

368368
var putEventsCount = 0
369369
var putEventsResult: Result<PutEventsOutput, Error> = .failure(CancellationError())
370+
var putEventsLastInput: PutEventsInput?
370371
func putEvents(input: PutEventsInput) async throws -> PutEventsOutput {
371372
putEventsCount += 1
373+
putEventsLastInput = input
372374
return try putEventsResult.get()
373375
}
374376

0 commit comments

Comments
 (0)