Skip to content

Commit

Permalink
Add event count to HealthAnalysisEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
rcrahul43 committed Apr 22, 2024
1 parent 0747439 commit 6452563
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ extension DefaultEventWarehouser {
#endif
#if TRACKER_ENABLED
let healthEvent = HealthAnalysisEvent(eventName: .ClickstreamEventCached,
eventGUID: event.guid)
eventGUID: event.guid,
eventCount: 1)
if event.type != Constants.EventType.instant.rawValue {
Tracker.sharedInstance?.record(event: healthEvent)
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/Clickstream/NetworkManager/Core/NetworkBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ extension DefaultNetworkBuilder {

let healthEvent = HealthAnalysisEvent(eventName: .ClickstreamBatchSent,
events: eventGUIDsString,
eventBatchGUID: eventBatch.uuid)
eventBatchGUID: eventBatch.uuid,
eventCount: eventBatch.events.count)
Tracker.sharedInstance?.record(event: healthEvent)
#endif
}
Expand Down
15 changes: 10 additions & 5 deletions Sources/Clickstream/NetworkManager/Core/RetryMechanism.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ extension DefaultRetryMechanism {
var healthEvent: HealthAnalysisEvent!
healthEvent = HealthAnalysisEvent(eventName: .ClickstreamWriteToSocketFailed,
eventBatchGUID: eventRequest.guid,
reason: FailureReason.ParsingException.rawValue)
reason: FailureReason.ParsingException.rawValue,
eventCount: eventRequest.eventCount)
Tracker.sharedInstance?.record(event: healthEvent)
#if ETE_TEST_SUITE_ENABLED
Clickstream.ackEvent = AckEventDetails(guid: eventRequest.guid, status: "Bad Request")
Expand All @@ -269,7 +270,8 @@ extension DefaultRetryMechanism {
#if TRACKER_ENABLED
let healthEvent = HealthAnalysisEvent(eventName: .ClickstreamEventBatchErrorResponse,
eventBatchGUID: eventRequest.guid, // eventRequest.guid is the batch GUID
reason: error.localizedDescription)
reason: error.localizedDescription,
eventCount: eventRequest.eventCount)
Tracker.sharedInstance?.record(event: healthEvent)
#if ETE_TEST_SUITE_ENABLED
Clickstream.ackEvent = AckEventDetails(guid: eventRequest.guid, status: "\(error)")
Expand Down Expand Up @@ -382,7 +384,8 @@ extension DefaultRetryMechanism {
#if TRACKER_ENABLED
if Tracker.debugMode {
let healthEvent = HealthAnalysisEvent(eventName: .ClickstreamEventBatchTimeout,
eventBatchGUID: fetchedEventRequest.guid)
eventBatchGUID: fetchedEventRequest.guid,
eventCount: eventRequest.eventCount)
Tracker.sharedInstance?.record(event: healthEvent)
}
#endif
Expand Down Expand Up @@ -464,7 +467,8 @@ extension DefaultRetryMechanism {
guard eventRequest.eventType != Constants.EventType.instant else { return }

let healthEvent = HealthAnalysisEvent(eventName: .ClickstreamEventBatchSuccessAck,
eventBatchGUID: eventRequest.guid)
eventBatchGUID: eventRequest.guid,
eventCount: eventRequest.eventCount)
Tracker.sharedInstance?.record(event: healthEvent)

}
Expand All @@ -480,7 +484,8 @@ extension DefaultRetryMechanism {
guard eventRequest.eventType != Constants.EventType.instant else { return }

let healthEvent = HealthAnalysisEvent(eventName: .ClickstreamEventBatchSuccessAck,
eventBatchGUID: eventRequest.guid)
eventBatchGUID: eventRequest.guid,
eventCount: eventRequest.eventCount)
Tracker.sharedInstance?.record(event: healthEvent)

}
Expand Down
17 changes: 14 additions & 3 deletions Sources/Tracker/Health/HealthAnalysisEvent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,15 @@ struct HealthAnalysisEvent: Codable, Equatable, AnalysisEvent {

private(set) var timeToConnection: String?

private(set) var eventCount: Int

init?(eventName: HealthEvents,
events: String? = nil,
eventGUID: String? = nil,
eventBatchGUID: String? = nil,
reason: String? = nil,
timeToConnection: String? = nil) {
timeToConnection: String? = nil,
eventCount: Int = 0) {

// Don't initialize if debugMode is off
guard Tracker.debugMode else {
Expand All @@ -68,12 +71,13 @@ struct HealthAnalysisEvent: Codable, Equatable, AnalysisEvent {
self.guid = UUID().uuidString
self.sessionID = Tracker.sharedInstance?.commonProperties?.session.sessionId
self.timeToConnection = timeToConnection
self.eventCount = eventCount

self.trackedVia = Tracker.healthTrackingConfigs.trackedVia.rawValue
}

private enum CodingKeys : String, CodingKey {
case guid,eventName,eventType,timestamp,reason,eventGUID,eventBatchGUID,events,sessionID,trackedVia, timeToConnection
case guid,eventName,eventType,timestamp,reason,eventGUID,eventBatchGUID,events,sessionID,trackedVia, timeToConnection, eventCount
}

static func == (lhs: HealthAnalysisEvent, rhs: HealthAnalysisEvent) -> Bool {
Expand Down Expand Up @@ -110,6 +114,8 @@ extension HealthAnalysisEvent: Notifiable {
properties[TrackerConstant.clickstream_error_reason] = reason
}

properties[TrackerConstant.clickstream_event_count] = eventCount

let dict: [String : Any] = [TrackerConstant.eventName: eventName.rawValue,
TrackerConstant.eventProperties: properties]
NotificationCenter.default.post(name: TrackerConstant.DebugEventsNotification, object: dict)
Expand All @@ -130,6 +136,7 @@ extension HealthAnalysisEvent: DatabasePersistable {
t.column("eventBatchGUID", .text)
t.column("events", .text)
t.column("sessionID", .text)
t.column("eventCount", .integer)
}
}
}
Expand All @@ -153,6 +160,10 @@ extension HealthAnalysisEvent: DatabasePersistable {
t.add(column: "timeToConnection", .text)
}

return [("addsTrackedViaToHealthEvent", addsTrackedVia), ("addsTimeToConnectionToHealthEvent", addsTimeToConnection)]
let addsEventCount: (TableAlteration) -> Void = { t in
t.add(column: "eventCount", .integer).defaults(to: 0)
}

return [("addsTrackedViaToHealthEvent", addsTrackedVia), ("addsTimeToConnectionToHealthEvent", addsTimeToConnection), ("addsEventCountToHealthEvent", addsEventCount)]
}
}
13 changes: 11 additions & 2 deletions Sources/Tracker/Tracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,20 @@ public final class Tracker {
}
}
}

var eventCount = 0
for e in eventNameBasedAggregation {
if e.eventCount > 0 {
eventCount += e.eventCount
} else {
eventCount = 0
break /// break the loop and reset event count. If any one of the event has eventCount -1, final eventCount in aggregated event will be wrong and will be of no use
}
}

let eventBatchGuids = eventNameBasedAggregation.compactMap { $0.eventBatchGUID }

var healthEvent = Gojek_Clickstream_Internal_Health.with {
$0.numberOfEvents = Int64(eventGuids.count)
$0.numberOfEvents = eventCount > 0 ? Int64(eventCount) : Int64(eventGuids.count)
$0.numberOfBatches = Int64(eventBatchGuids.count)
$0.healthMeta = metaData
$0.healthMeta.eventGuid = eventGuid
Expand Down

0 comments on commit 6452563

Please sign in to comment.