Skip to content

Commit

Permalink
Got rid of init route event in copilot (#7622)
Browse files Browse the repository at this point in the history
  • Loading branch information
VysotskiVadim authored Nov 20, 2023
1 parent 88aff79 commit 50ef0b0
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 314 deletions.
1 change: 1 addition & 0 deletions changelog/unreleased/bugfixes/7622.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Optimised memory consumption of Copilot.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal object HistoryAttachmentsUtils {
"$PATH_SEPARATOR${copilotMetadata.driveMode}$PATH_SEPARATOR$HYPHEN" +
"$PATH_SEPARATOR${copilotMetadata.driveId}"

fun retrieveSpecVersion(): String = "1.1"
fun retrieveSpecVersion(): String = "1.2"

fun retrieveNavSdkVersion(): String = BuildConfig.MAPBOX_NAVIGATION_VERSION_NAME

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ sealed class HistoryEvent(internal val snakeCaseEventName: String, internal val

internal const val SEARCH_RESULTS_EVENT_NAME = "search_results"
internal const val SEARCH_RESULT_USED_EVENT_NAME = "search_result_used"
internal const val INIT_ROUTE_EVENT_NAME = "init_route"
internal const val DRIVE_ENDS_EVENT_NAME = "drive_ends"
internal const val GOING_TO_FOREGROUND_EVENT_NAME = "going_to_foreground"
internal const val GOING_TO_BACKGROUND_EVENT_NAME = "going_to_background"
Expand Down Expand Up @@ -42,10 +41,6 @@ data class SearchResultsEvent(val searchResults: SearchResults) :
data class SearchResultUsedEvent(val searchResultUsed: SearchResultUsed) :
HistoryEvent(SEARCH_RESULT_USED_EVENT_NAME, searchResultUsed)

@OptIn(ExperimentalPreviewMapboxNavigationAPI::class)
internal data class InitRouteEvent(val initRoute: InitRoute, val preSerializedInitRoute: String) :
HistoryEvent(INIT_ROUTE_EVENT_NAME, initRoute)

@OptIn(ExperimentalPreviewMapboxNavigationAPI::class)
internal data class DriveEndsEvent(val driveEnds: DriveEnds) :
HistoryEvent(DRIVE_ENDS_EVENT_NAME, driveEnds)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.mapbox.geojson.Point
import com.mapbox.geojson.PointAsCoordinatesTypeAdapter
import com.mapbox.navigation.base.ExperimentalPreviewMapboxNavigationAPI
import com.mapbox.navigation.base.options.DeviceType
import com.mapbox.navigation.base.route.NavigationRoute
import com.mapbox.navigation.base.trip.model.RouteLegProgress
import com.mapbox.navigation.base.trip.model.RouteProgress
import com.mapbox.navigation.copilot.HistoryAttachmentsUtils.copyToAndRemove
Expand All @@ -27,7 +26,6 @@ import com.mapbox.navigation.copilot.HistoryAttachmentsUtils.utcTimeNow
import com.mapbox.navigation.copilot.internal.CopilotMetadata
import com.mapbox.navigation.core.MapboxNavigation
import com.mapbox.navigation.core.arrival.ArrivalObserver
import com.mapbox.navigation.core.directions.session.RoutesObserver
import com.mapbox.navigation.core.internal.HistoryRecordingSessionState
import com.mapbox.navigation.core.internal.HistoryRecordingSessionState.ActiveGuidance
import com.mapbox.navigation.core.internal.HistoryRecordingSessionState.FreeDrive
Expand All @@ -43,12 +41,9 @@ import com.mapbox.navigation.core.internal.telemetry.registerUserFeedbackCallbac
import com.mapbox.navigation.core.internal.telemetry.unregisterUserFeedbackCallback
import com.mapbox.navigation.core.lifecycle.MapboxNavigationApp
import com.mapbox.navigation.utils.internal.InternalJobControlFactory
import com.mapbox.navigation.utils.internal.ThreadController
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.util.Locale

Expand All @@ -59,8 +54,7 @@ import java.util.Locale
*/
@OptIn(ExperimentalPreviewMapboxNavigationAPI::class)
internal class MapboxCopilotImpl(
private val mapboxNavigation: MapboxNavigation,
private val computationDispatcher: CoroutineDispatcher = ThreadController.DefaultDispatcher
private val mapboxNavigation: MapboxNavigation
) {

private val mainJobController by lazy { InternalJobControlFactory.createMainScopeJobControl() }
Expand Down Expand Up @@ -135,21 +129,6 @@ internal class MapboxCopilotImpl(
cancelRecordingHistory()
}
}
private var initRoute = false
private val routesObserver = RoutesObserver { routesResult ->
val navigationRoutes = routesResult.navigationRoutes
if (initialRoute(navigationRoutes)) {
initRouteSerializationJob = mainJobController.scope.launch {
val route = navigationRoutes.first().directionsRoute
val initRoute = InitRoute(route.requestUuid(), route)
val preSerializedInitRoute = withContext(computationDispatcher) {
toEventJson(initRoute)
}
push(InitRouteEvent(initRoute, preSerializedInitRoute))
}
initRoute = true
}
}
private var arrivedAtFinalDestination = false
private val arrivalObserver = object : ArrivalObserver {

Expand Down Expand Up @@ -204,14 +183,8 @@ internal class MapboxCopilotImpl(
*/
fun push(historyEvent: HistoryEvent) {
val eventType = historyEvent.snakeCaseEventName
val eventJson = when (historyEvent) {
is InitRouteEvent -> historyEvent.preSerializedInitRoute
else -> toEventJson(historyEvent.eventDTO)
}
val eventJson = toEventJson(historyEvent.eventDTO)
when (historyEvent) {
is InitRouteEvent -> {
addActiveGuidance(eventType, eventJson)
}
is DriveEndsEvent, GoingToBackgroundEvent, GoingToForegroundEvent -> {
pushHistoryJson(eventType, eventJson)
}
Expand Down Expand Up @@ -278,7 +251,6 @@ internal class MapboxCopilotImpl(
is FreeDrive -> "free-drive"
else -> throw IllegalArgumentException("Should not try and track idle state")
}
mapboxNavigation.registerRoutesObserver(routesObserver)
mapboxNavigation.registerArrivalObserver(arrivalObserver)
restartRecordingHistoryJob = mainJobController.scope.launch {
while (true) {
Expand All @@ -295,10 +267,6 @@ internal class MapboxCopilotImpl(
return utcTimeNow(format, locale)
}

private fun initialRoute(navigationRoutes: List<NavigationRoute>): Boolean =
currentHistoryRecordingSessionState is ActiveGuidance &&
navigationRoutes.isNotEmpty() && !initRoute

private fun toEventJson(event: EventDTO): String {
val eventJson = gson.toJson(event) ?: ""
check(eventJson != "null") {
Expand Down Expand Up @@ -470,8 +438,6 @@ internal class MapboxCopilotImpl(
historyFilePath ?: return@stopRecording
callback(historyFilePath)
}
mapboxNavigation.unregisterRoutesObserver(routesObserver)
initRoute = false
hasFeedback = false
mapboxNavigation.unregisterArrivalObserver(arrivalObserver)
arrivedAtFinalDestination = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class HistoryAttachmentsUtilsTest {

val sessionId = HistoryAttachmentsUtils.generateSessionId(navigationSession, "owner")

val expectedSessionId = "co-pilot/owner/1.1/mbx-debug/-/-/free-drive/" +
val expectedSessionId = "co-pilot/owner/1.2/mbx-debug/-/-/free-drive/" +
"-/3e48fd7a-fc82-42a8-9bae-baeb724f92ce"
assertEquals(expectedSessionId, sessionId)
}
Expand Down
Loading

0 comments on commit 50ef0b0

Please sign in to comment.