Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
RingerJK committed Feb 27, 2023
1 parent 89acc70 commit cd2873a
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import com.mapbox.navigation.base.internal.metric.extractEventsNames
import com.mapbox.navigation.base.metrics.MetricEvent
import com.mapbox.navigation.base.metrics.MetricsObserver
import com.mapbox.navigation.base.metrics.MetricsReporter
import com.mapbox.navigation.metrics.events.EventsServiceInterfacesManager
import com.mapbox.navigation.metrics.events.TelemetryEventsProvider
import com.mapbox.navigation.metrics.internal.EventsServiceProvider
import com.mapbox.navigation.metrics.internal.TelemetryServiceProvider
import com.mapbox.navigation.metrics.internal.TelemetryUtilsDelegate
Expand All @@ -34,7 +36,7 @@ object MapboxMetricsReporter : MetricsReporter {
private const val LOG_CATEGORY = "MapboxMetricsReporter"

private val gson = Gson()
private lateinit var eventsService: EventsServiceInterface
private lateinit var eventsManager: EventsServiceInterfacesManager
private lateinit var telemetryService: TelemetryService

@Volatile
Expand Down Expand Up @@ -84,10 +86,10 @@ object MapboxMetricsReporter : MetricsReporter {
userAgent: String
) {
isTelemetryInitialized = true
eventsManager = TelemetryEventsProvider.getOrCreateTelemetryEventsManager(accessToken)
val eventsServerOptions = EventsServerOptions(accessToken, userAgent, null)
eventsService = EventsServiceProvider.provideEventsService(eventsServerOptions)
telemetryService = TelemetryServiceProvider.provideTelemetryService(eventsServerOptions)
eventsService.registerObserver(eventsServiceObserver)
eventsManager.nativeEventsServiceInterface.registerObserver(eventsServiceObserver)
}

/**
Expand All @@ -109,7 +111,7 @@ object MapboxMetricsReporter : MetricsReporter {
fun disable() {
isTelemetryInitialized = false
removeObserver()
eventsService.unregisterObserver(eventsServiceObserver)
eventsManager.nativeEventsServiceInterface.unregisterObserver(eventsServiceObserver)
ioJobController.job.cancelChildren()
}

Expand All @@ -125,7 +127,7 @@ object MapboxMetricsReporter : MetricsReporter {
)
return
}
eventsService.sendEvent(
eventsManager.nativeEventsServiceInterface.sendEvent(
Event(eventsPriority, metricEvent.toValue(), null)
) {
if (it != null) {
Expand All @@ -144,7 +146,7 @@ object MapboxMetricsReporter : MetricsReporter {
*/
override fun sendTurnstileEvent(turnstileEvent: TurnstileEvent) {
ifTelemetryIsRunning {
eventsService.sendTurnstileEvent(turnstileEvent) {
eventsManager.nativeEventsServiceInterface.sendTurnstileEvent(turnstileEvent) {
if (it != null) {
logE("Failed to send Turnstile event: $it", LOG_CATEGORY)
}
Expand All @@ -170,14 +172,14 @@ object MapboxMetricsReporter : MetricsReporter {
* Register [EventsServiceObserver]
*/
fun registerEventsServiceObserver(observer: EventsServiceObserver) {
eventsService.registerObserver(observer)
eventsManager.nativeEventsServiceInterface.registerObserver(observer)
}

/**
* Unregister [EventsServiceObserver]
*/
fun unregisterEventsServiceObserver(observer: EventsServiceObserver) {
eventsService.unregisterObserver(observer)
eventsManager.nativeEventsServiceInterface.unregisterObserver(observer)
}

private inline fun ifTelemetryIsRunning(func: () -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mapbox.navigation.metrics.events

import com.mapbox.bindgen.Value

fun interface EventsObserver {
fun onEvents(events: Value)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.mapbox.navigation.metrics.events

import com.mapbox.bindgen.Value
import com.mapbox.common.EventsServiceError
import com.mapbox.common.EventsServiceInterface
import com.mapbox.common.EventsServiceObserver

class EventsServiceInterfacesManager(
// platformEventsServiceInterface: EventsServiceInterface,
internal val nativeEventsServiceInterface: EventsServiceInterface,
) {
private val eventServices = listOf(
// platformEventsServiceInterface,
nativeEventsServiceInterface,
)

private val observers = mutableListOf<EventsObserver>()

private val eventsServiceObserver = object : EventsServiceObserver {
override fun didEncounterError(error: EventsServiceError, events: Value) {
// do nothing
}

override fun didSendEvents(events: Value) {
observers.forEach { it.onEvents(events) }
}

}

fun registerEventsObserver(eventsObserver: EventsObserver) {
checkAndRegisterInternal()
observers.add(eventsObserver)
}

fun unregisterEventsObserver(eventsObserver: EventsObserver) {
observers.remove(eventsObserver)
checkAndUnregisterInternal()
}

private fun checkAndRegisterInternal() {
if (observers.isEmpty()) {
eventServices.forEach { it.registerObserver(eventsServiceObserver) }
}
}

private fun checkAndUnregisterInternal() {
if (observers.isEmpty()) {
eventServices.forEach { it.unregisterObserver(eventsServiceObserver) }
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mapbox.navigation.metrics.events

import com.mapbox.common.EventsServerOptions
import com.mapbox.common.EventsService

object TelemetryEventsProvider {
fun getOrCreateTelemetryEventsManager(accessToken: String): EventsServiceInterfacesManager =
EventsServiceInterfacesManager(
EventsService.getOrCreate(
EventsServerOptions(accessToken, "MapboxNavigationNative", null)
)
)
}

0 comments on commit cd2873a

Please sign in to comment.