Skip to content

Commit 4238201

Browse files
committed
Clickstream Health refactor
1 parent 4dc002e commit 4238201

File tree

3 files changed

+41
-14
lines changed

3 files changed

+41
-14
lines changed

clickstream-health-metrics-api/src/main/kotlin/clickstream/health/intermediate/CSHealthEventProcessor.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ import kotlinx.coroutines.flow.Flow
1212
* 2) Returning stream of health events for processing.
1313
* 3) Sending health event data to upstream listener.
1414
*
15+
* **Sequence Diagram**
16+
* ```
17+
* App Clickstream
18+
* +---+---+---+---+---+---+ +---+---+---+---+---+---+
19+
* | Sending Events | --------> | Received the Events |
20+
* +---+---+---+---+---+---+ +---+---+---+---+---+---+
21+
* |
22+
* |
23+
* | +---+---+---+---+---+---+---+---+----+
24+
* if app on active state ---------> | - run the ticker with 10s delay |
25+
* | | - collect events from db |
26+
* | | - transform and send to backend |
27+
* | +---+---+---+---+---+---+---+---+----+
28+
* |
29+
* | +---+---+---+---+---+---+---+---+---+---+----+
30+
* else if app on inactive state --> | - run flushEvents and flushHealthMetrics |
31+
* | - transform and send to backend |
32+
* +---+---+---+---+---+---+---+---+---+----+---+
33+
*```
1534
* */
1635
public interface CSHealthEventProcessor {
1736

clickstream-health-metrics/src/main/kotlin/clickstream/health/internal/processor/CSHealthEventProcessorImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ internal open class CSHealthEventProcessorImpl(
158158
}
159159

160160
val data = events[0].copy(
161-
eventGuid = eventId, eventBatchGuid = batchId, count = events.size
161+
eventGuid = eventId, eventBatchGuid = batchId, count = list.size
162162
).eventData()
163163

164164
pushEventToUpstream(events[0].eventName, data)

clickstream/src/main/kotlin/clickstream/internal/eventscheduler/CSBackgroundScheduler.kt

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import clickstream.lifecycle.CSAppLifeCycle
2020
import clickstream.listener.CSEventListener
2121
import clickstream.logger.CSLogger
2222
import clickstream.report.CSReportDataTracker
23+
import com.gojek.clickstream.internal.Health
2324
import kotlinx.coroutines.CoroutineDispatcher
2425
import kotlinx.coroutines.CoroutineScope
2526
import kotlinx.coroutines.ExperimentalCoroutinesApi
@@ -102,11 +103,14 @@ internal open class CSBackgroundScheduler(
102103
logger.debug { "$tag#sendEvents" }
103104
csSocketConnectionManager.connect()
104105
if (!waitForNetwork()) {
106+
doWithHealth {
107+
logger.debug { "$tag#No connection, trying to send health logs to upstream if enabled" }
108+
}
105109
return false
106110
}
107111
flushAllEvents()
108112
waitForAck()
109-
flushHealthEvents()
113+
doWithHealth { flushHealthEvents(it) }
110114
return eventRepository.getEventCount() == 0
111115
}
112116

@@ -154,20 +158,24 @@ internal open class CSBackgroundScheduler(
154158
}
155159
}
156160

157-
private suspend fun flushHealthEvents() {
161+
private suspend inline fun doWithHealth(crossinline exec: suspend (List<Health>) -> Unit) {
158162
logger.debug { "$tag#flushHealthEvents" }
159163
healthProcessor?.getHealthEventFlow(CSEventTypesConstant.AGGREGATE)?.collect { healthList ->
160-
val healthMappedEvent = healthList.map { health ->
161-
CSEvent(
162-
guid = health.healthMeta.eventGuid,
163-
timestamp = health.eventTimestamp,
164-
message = health
165-
)
166-
}.map { CSEventData.create(it) }
167-
logger.debug { "$tag#flushHealthEvents - healthEvents size ${healthMappedEvent.size}" }
168-
if (healthMappedEvent.isNotEmpty()) {
169-
forwardEvents(healthMappedEvent, forFlushing = true)
170-
}
164+
exec(healthList)
165+
}
166+
}
167+
168+
private suspend fun flushHealthEvents(healthList: List<Health>) {
169+
val healthMappedEvent = healthList.map { health ->
170+
CSEvent(
171+
guid = health.healthMeta.eventGuid,
172+
timestamp = health.eventTimestamp,
173+
message = health
174+
)
175+
}.map { CSEventData.create(it) }
176+
logger.debug { "$tag#flushHealthEvents - healthEvents size ${healthMappedEvent.size}" }
177+
if (healthMappedEvent.isNotEmpty()) {
178+
forwardEvents(healthMappedEvent, forFlushing = true)
171179
}
172180
}
173181

0 commit comments

Comments
 (0)