Move analytic watchers to :services:analytics
This commit is contained in:
committed by
Jorge Martin Espinosa
parent
27d376806c
commit
71bfffe58f
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Element Creations Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.services.analytics.api.watchers
|
||||
|
||||
/**
|
||||
* Adds a performance check transaction measuring the time between a cold start (or, after we read the user consent after a cold start)
|
||||
* until the cached room list is displayed. This check only takes place in a cold app start after the user is authenticated.
|
||||
*/
|
||||
interface AnalyticsColdStartWatcher {
|
||||
fun start()
|
||||
fun whenLoggingIn()
|
||||
fun onRoomListVisible()
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Element Creations Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.services.analytics.api.watchers
|
||||
|
||||
/**
|
||||
* This component is used to check how long it takes for the room list to be up to date after opening the app while it's on a 'warm' state:
|
||||
* the app was previously running and we just returned to it.
|
||||
*/
|
||||
interface AnalyticsRoomListStateWatcher {
|
||||
fun start()
|
||||
fun stop()
|
||||
}
|
||||
@@ -25,16 +25,20 @@ dependencies {
|
||||
implementation(projects.libraries.core)
|
||||
implementation(projects.libraries.architecture)
|
||||
implementation(projects.libraries.designsystem)
|
||||
implementation(projects.libraries.matrix.api)
|
||||
implementation(projects.libraries.preferences.api)
|
||||
implementation(projects.libraries.sessionStorage.api)
|
||||
implementation(projects.services.appnavstate.api)
|
||||
|
||||
api(projects.services.analyticsproviders.api)
|
||||
api(projects.services.analytics.api)
|
||||
implementation(libs.androidx.datastore.preferences)
|
||||
|
||||
testCommonDependencies(libs)
|
||||
testImplementation(projects.libraries.matrix.test)
|
||||
testImplementation(projects.libraries.sessionStorage.test)
|
||||
testImplementation(projects.services.analytics.test)
|
||||
testImplementation(projects.services.analyticsproviders.test)
|
||||
testImplementation(projects.services.appnavstate.test)
|
||||
testImplementation(projects.services.toolbox.test)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Element Creations Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.services.analytics.impl.watchers
|
||||
|
||||
import dev.zacsweers.metro.AppScope
|
||||
import dev.zacsweers.metro.ContributesBinding
|
||||
import dev.zacsweers.metro.SingleIn
|
||||
import io.element.android.libraries.di.annotations.AppCoroutineScope
|
||||
import io.element.android.services.analytics.api.AnalyticsLongRunningTransaction
|
||||
import io.element.android.services.analytics.api.AnalyticsService
|
||||
import io.element.android.services.analytics.api.watchers.AnalyticsColdStartWatcher
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import timber.log.Timber
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
@SingleIn(AppScope::class)
|
||||
@ContributesBinding(AppScope::class)
|
||||
class DefaultAnalyticsColdStartWatcher(
|
||||
private val analyticsService: AnalyticsService,
|
||||
@AppCoroutineScope private val appCoroutineScope: CoroutineScope,
|
||||
) : AnalyticsColdStartWatcher {
|
||||
private val isColdStart = AtomicBoolean(true)
|
||||
|
||||
override fun start() {
|
||||
analyticsService.userConsentFlow
|
||||
.onEach { hasConsent ->
|
||||
if (hasConsent) {
|
||||
if (isColdStart.get()) {
|
||||
Timber.d("Starting cold start check")
|
||||
analyticsService.startLongRunningTransaction(AnalyticsLongRunningTransaction.ColdStartUntilCachedRoomList)
|
||||
} else {
|
||||
error("The app is no longer in a cold start state")
|
||||
}
|
||||
}
|
||||
}
|
||||
.catch { Timber.w(it.message) }
|
||||
.launchIn(appCoroutineScope)
|
||||
}
|
||||
|
||||
override fun whenLoggingIn() {
|
||||
if (isColdStart.getAndSet(false)) {
|
||||
analyticsService.removeLongRunningTransaction(AnalyticsLongRunningTransaction.ColdStartUntilCachedRoomList)
|
||||
Timber.d("Canceled cold start check: user is logging in")
|
||||
}
|
||||
}
|
||||
|
||||
override fun onRoomListVisible() {
|
||||
if (isColdStart.getAndSet(false)) {
|
||||
Timber.d("Room list is visible, finishing cold start check")
|
||||
analyticsService.removeLongRunningTransaction(AnalyticsLongRunningTransaction.ColdStartUntilCachedRoomList)?.finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Element Creations Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.services.analytics.impl.watchers
|
||||
|
||||
import dev.zacsweers.metro.ContributesBinding
|
||||
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
|
||||
import io.element.android.libraries.core.coroutine.childScope
|
||||
import io.element.android.libraries.core.coroutine.withPreviousValue
|
||||
import io.element.android.libraries.di.SessionScope
|
||||
import io.element.android.libraries.di.annotations.SessionCoroutineScope
|
||||
import io.element.android.libraries.matrix.api.roomlist.RoomListService
|
||||
import io.element.android.services.analytics.api.AnalyticsLongRunningTransaction
|
||||
import io.element.android.services.analytics.api.AnalyticsService
|
||||
import io.element.android.services.analytics.api.watchers.AnalyticsRoomListStateWatcher
|
||||
import io.element.android.services.appnavstate.api.AppNavigationStateService
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.cancel
|
||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import timber.log.Timber
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
|
||||
@ContributesBinding(SessionScope::class)
|
||||
class DefaultAnalyticsRoomListStateWatcher(
|
||||
private val appNavigationStateService: AppNavigationStateService,
|
||||
private val roomListService: RoomListService,
|
||||
private val analyticsService: AnalyticsService,
|
||||
@SessionCoroutineScope private val sessionCoroutineScope: CoroutineScope,
|
||||
private val dispatchers: CoroutineDispatchers,
|
||||
) : AnalyticsRoomListStateWatcher {
|
||||
private var currentCoroutineScope: CoroutineScope? = null
|
||||
private val isWarmState = AtomicBoolean(false)
|
||||
|
||||
override fun start() {
|
||||
if (currentCoroutineScope != null) {
|
||||
Timber.w("Can't start RoomListStateWatcher, it's already running.")
|
||||
return
|
||||
}
|
||||
|
||||
val coroutineScope = sessionCoroutineScope.childScope(dispatchers.computation, "AnalyticsRoomListStateWatcher")
|
||||
appNavigationStateService.appNavigationState
|
||||
.map { it.isInForeground }
|
||||
.distinctUntilChanged()
|
||||
.withPreviousValue()
|
||||
.onEach { (wasInForeground, isInForeground) ->
|
||||
if (isInForeground && roomListService.state.value != RoomListService.State.Running) {
|
||||
analyticsService.startLongRunningTransaction(AnalyticsLongRunningTransaction.ResumeAppUntilNewRoomsReceived)
|
||||
}
|
||||
|
||||
if (wasInForeground == false && isInForeground) {
|
||||
isWarmState.set(true)
|
||||
}
|
||||
}
|
||||
.launchIn(coroutineScope)
|
||||
|
||||
roomListService.state
|
||||
.onEach { state ->
|
||||
if (state == RoomListService.State.Running && isWarmState.get()) {
|
||||
analyticsService.removeLongRunningTransaction(AnalyticsLongRunningTransaction.ResumeAppUntilNewRoomsReceived)?.finish()
|
||||
}
|
||||
}
|
||||
.launchIn(coroutineScope)
|
||||
|
||||
currentCoroutineScope = coroutineScope
|
||||
}
|
||||
|
||||
override fun stop() {
|
||||
currentCoroutineScope?.cancel()
|
||||
currentCoroutineScope = null
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Element Creations Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.services.analytics.impl.watchers
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import io.element.android.services.analytics.api.AnalyticsLongRunningTransaction.ColdStartUntilCachedRoomList
|
||||
import io.element.android.services.analytics.test.FakeAnalyticsService
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.TestScope
|
||||
import kotlinx.coroutines.test.runCurrent
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Test
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class DefaultAnalyticsColdStartWatcherTest {
|
||||
@Test
|
||||
fun `watch - until room list is visible`() = runTest {
|
||||
val analyticsService = FakeAnalyticsService()
|
||||
val watcher = createAnalyticsColdStartWatcher(analyticsService)
|
||||
|
||||
// Start watching
|
||||
watcher.start()
|
||||
|
||||
// The user has given analytics consent, we can start tracking the cold start
|
||||
analyticsService.setUserConsent(true)
|
||||
runCurrent()
|
||||
|
||||
// The transaction is running
|
||||
assertThat(analyticsService.getLongRunningTransaction(ColdStartUntilCachedRoomList)).isNotNull()
|
||||
|
||||
// As soon as the room list is visible
|
||||
watcher.onRoomListVisible()
|
||||
runCurrent()
|
||||
|
||||
// The transaction is now finished
|
||||
assertThat(analyticsService.getLongRunningTransaction(ColdStartUntilCachedRoomList)).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `watch - user is logging in, transaction is cancelled since it's not a cold start`() = runTest {
|
||||
val analyticsService = FakeAnalyticsService()
|
||||
val watcher = createAnalyticsColdStartWatcher(analyticsService)
|
||||
|
||||
// Start watching
|
||||
watcher.start()
|
||||
|
||||
// The user has given analytics consent, we can start tracking the cold start
|
||||
analyticsService.setUserConsent(true)
|
||||
runCurrent()
|
||||
|
||||
// The transaction is running
|
||||
assertThat(analyticsService.getLongRunningTransaction(ColdStartUntilCachedRoomList)).isNotNull()
|
||||
|
||||
// If the user starts a login flow
|
||||
watcher.whenLoggingIn()
|
||||
runCurrent()
|
||||
|
||||
// The transaction is gone
|
||||
assertThat(analyticsService.getLongRunningTransaction(ColdStartUntilCachedRoomList)).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `watch - user was logging in, transaction is never started since it's not a cold start`() = runTest {
|
||||
val analyticsService = FakeAnalyticsService()
|
||||
val watcher = createAnalyticsColdStartWatcher(analyticsService)
|
||||
|
||||
// Start watching
|
||||
watcher.start()
|
||||
|
||||
// If the user starts a login flow
|
||||
watcher.whenLoggingIn()
|
||||
|
||||
// The user has given analytics consent, we can start tracking the cold start
|
||||
analyticsService.setUserConsent(true)
|
||||
runCurrent()
|
||||
|
||||
// The transaction never starts
|
||||
assertThat(analyticsService.getLongRunningTransaction(ColdStartUntilCachedRoomList)).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `watch - never gets consent so it does nothing`() = runTest {
|
||||
val analyticsService = FakeAnalyticsService()
|
||||
val watcher = createAnalyticsColdStartWatcher(analyticsService)
|
||||
|
||||
// Start watching
|
||||
watcher.start()
|
||||
|
||||
// The user never gets the analytics consent, so we do nothing
|
||||
runCurrent()
|
||||
|
||||
// The transaction is not running in that case
|
||||
assertThat(analyticsService.getLongRunningTransaction(ColdStartUntilCachedRoomList)).isNull()
|
||||
}
|
||||
|
||||
private fun TestScope.createAnalyticsColdStartWatcher(
|
||||
analyticsService: FakeAnalyticsService = FakeAnalyticsService(),
|
||||
) = DefaultAnalyticsColdStartWatcher(
|
||||
analyticsService = analyticsService,
|
||||
appCoroutineScope = backgroundScope,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Element Creations Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.services.analytics.impl.watchers
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import io.element.android.libraries.matrix.api.roomlist.RoomListService
|
||||
import io.element.android.libraries.matrix.test.roomlist.FakeRoomListService
|
||||
import io.element.android.services.analytics.api.AnalyticsLongRunningTransaction.ResumeAppUntilNewRoomsReceived
|
||||
import io.element.android.services.analytics.test.FakeAnalyticsService
|
||||
import io.element.android.services.appnavstate.api.AppNavigationState
|
||||
import io.element.android.services.appnavstate.api.NavigationState
|
||||
import io.element.android.services.appnavstate.test.FakeAppNavigationStateService
|
||||
import io.element.android.tests.testutils.testCoroutineDispatchers
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.test.TestScope
|
||||
import kotlinx.coroutines.test.runCurrent
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Test
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
class DefaultAnalyticsRoomListStateWatcherTest {
|
||||
@Test
|
||||
fun `Opening the app in a warm state tracks the time until the room list is synced`() = runTest {
|
||||
val navigationStateService = FakeAppNavigationStateService()
|
||||
val roomListService = FakeRoomListService().apply {
|
||||
postState(RoomListService.State.Idle)
|
||||
}
|
||||
val analyticsService = FakeAnalyticsService()
|
||||
val watcher = createAnalyticsRoomListStateWatcher(
|
||||
appNavigationStateService = navigationStateService,
|
||||
roomListService = roomListService,
|
||||
analyticsService = analyticsService,
|
||||
)
|
||||
|
||||
watcher.start()
|
||||
|
||||
// Give some time to load the initial state
|
||||
runCurrent()
|
||||
|
||||
// Make sure it's warm by changing its internal state
|
||||
navigationStateService.appNavigationState.emit(AppNavigationState(navigationState = NavigationState.Root, isInForeground = false))
|
||||
runCurrent()
|
||||
navigationStateService.appNavigationState.emit(AppNavigationState(navigationState = NavigationState.Root, isInForeground = true))
|
||||
runCurrent()
|
||||
|
||||
// The transaction should be present now
|
||||
assertThat(analyticsService.getLongRunningTransaction(ResumeAppUntilNewRoomsReceived)).isNotNull()
|
||||
|
||||
// And now the room list service running
|
||||
roomListService.postState(RoomListService.State.Running)
|
||||
runCurrent()
|
||||
|
||||
// And the transaction should now be gone
|
||||
assertThat(analyticsService.getLongRunningTransaction(ResumeAppUntilNewRoomsReceived)).isNull()
|
||||
|
||||
watcher.stop()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Opening the app in a cold state does nothing`() = runTest {
|
||||
val navigationStateService = FakeAppNavigationStateService().apply {
|
||||
appNavigationState.emit(AppNavigationState(NavigationState.Root, false))
|
||||
}
|
||||
val roomListService = FakeRoomListService().apply {
|
||||
postState(RoomListService.State.Idle)
|
||||
}
|
||||
val analyticsService = FakeAnalyticsService()
|
||||
val watcher = createAnalyticsRoomListStateWatcher(
|
||||
appNavigationStateService = navigationStateService,
|
||||
roomListService = roomListService,
|
||||
analyticsService = analyticsService,
|
||||
)
|
||||
|
||||
watcher.start()
|
||||
|
||||
// Give some time to load the initial state
|
||||
runCurrent()
|
||||
|
||||
// The room list service running
|
||||
roomListService.postState(RoomListService.State.Running)
|
||||
runCurrent()
|
||||
|
||||
// The transaction was never present
|
||||
assertThat(analyticsService.getLongRunningTransaction(ResumeAppUntilNewRoomsReceived)).isNull()
|
||||
|
||||
watcher.stop()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `The transaction won't be finished until the room list is synchronised`() = runTest {
|
||||
val navigationStateService = FakeAppNavigationStateService()
|
||||
val roomListService = FakeRoomListService().apply {
|
||||
postState(RoomListService.State.Idle)
|
||||
}
|
||||
val analyticsService = FakeAnalyticsService()
|
||||
val watcher = createAnalyticsRoomListStateWatcher(
|
||||
appNavigationStateService = navigationStateService,
|
||||
roomListService = roomListService,
|
||||
analyticsService = analyticsService,
|
||||
)
|
||||
|
||||
watcher.start()
|
||||
|
||||
// Give some time to load the initial state
|
||||
runCurrent()
|
||||
|
||||
// Make sure it's warm by changing its internal state
|
||||
navigationStateService.appNavigationState.emit(AppNavigationState(navigationState = NavigationState.Root, isInForeground = false))
|
||||
runCurrent()
|
||||
navigationStateService.appNavigationState.emit(AppNavigationState(navigationState = NavigationState.Root, isInForeground = true))
|
||||
runCurrent()
|
||||
|
||||
// The transaction should be present now
|
||||
assertThat(analyticsService.getLongRunningTransaction(ResumeAppUntilNewRoomsReceived)).isNotNull()
|
||||
|
||||
runCurrent()
|
||||
|
||||
// But without the room list syncing, it never finishes
|
||||
assertThat(analyticsService.getLongRunningTransaction(ResumeAppUntilNewRoomsReceived)).isNotNull()
|
||||
|
||||
watcher.stop()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `Opening the app when the room list state was already Running does nothing`() = runTest {
|
||||
val navigationStateService = FakeAppNavigationStateService()
|
||||
val roomListService = FakeRoomListService().apply {
|
||||
postState(RoomListService.State.Running)
|
||||
}
|
||||
val analyticsService = FakeAnalyticsService()
|
||||
val watcher = createAnalyticsRoomListStateWatcher(
|
||||
appNavigationStateService = navigationStateService,
|
||||
roomListService = roomListService,
|
||||
analyticsService = analyticsService,
|
||||
)
|
||||
|
||||
watcher.start()
|
||||
|
||||
// Give some time to load the initial state
|
||||
runCurrent()
|
||||
|
||||
// Make sure it's warm by changing its internal state
|
||||
navigationStateService.appNavigationState.emit(AppNavigationState(navigationState = NavigationState.Root, isInForeground = false))
|
||||
runCurrent()
|
||||
navigationStateService.appNavigationState.emit(AppNavigationState(navigationState = NavigationState.Root, isInForeground = true))
|
||||
runCurrent()
|
||||
|
||||
// The transaction was never added
|
||||
assertThat(analyticsService.getLongRunningTransaction(ResumeAppUntilNewRoomsReceived)).isNull()
|
||||
|
||||
watcher.stop()
|
||||
}
|
||||
|
||||
private fun TestScope.createAnalyticsRoomListStateWatcher(
|
||||
appNavigationStateService: FakeAppNavigationStateService = FakeAppNavigationStateService(),
|
||||
roomListService: FakeRoomListService = FakeRoomListService(),
|
||||
analyticsService: FakeAnalyticsService = FakeAnalyticsService(),
|
||||
) = DefaultAnalyticsRoomListStateWatcher(
|
||||
appNavigationStateService = appNavigationStateService,
|
||||
roomListService = roomListService,
|
||||
analyticsService = analyticsService,
|
||||
sessionCoroutineScope = backgroundScope,
|
||||
dispatchers = testCoroutineDispatchers(),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Element Creations Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
package io.element.android.services.analytics.test.watchers
|
||||
|
||||
import io.element.android.services.analytics.api.watchers.AnalyticsColdStartWatcher
|
||||
|
||||
class FakeAnalyticsColdStartWatcher : AnalyticsColdStartWatcher {
|
||||
override fun start() {}
|
||||
override fun whenLoggingIn() {}
|
||||
override fun onRoomListVisible() {}
|
||||
}
|
||||
Reference in New Issue
Block a user