Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix session start tracking #25

Merged
merged 2 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class AuthManagerTest {
val exp = Date(Date().time + 100 * 1000)
val cert = createMembershipCertificate(userId, groupId, true, adminUserId, Date(Date().time - 10 * 1000), exp, privateKey)

Assertions.assertEquals(authManager.membershipCertificateExpirationDate(cert, publicKey).time.toFloat(), exp.time.toFloat(), 1000.0.toFloat())
Assertions.assertEquals(authManager.membershipCertificateExpirationDate(cert, publicKey).time.toFloat(), exp.time.toFloat(), 2000.0.toFloat())
}

@Test
Expand Down
32 changes: 22 additions & 10 deletions app/src/main/java/tice/AppFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ class AppFlow constructor(val application: TICEApplication) : LifecycleObserver,

private lateinit var workManager: WorkManager

private var appInitialized = false

@OptIn(ExperimentalStdlibApi::class)
fun initApp(): Job {
val initJob = Job()
Expand Down Expand Up @@ -203,22 +205,32 @@ class AppFlow constructor(val application: TICEApplication) : LifecycleObserver,
}

val versionCode = BuildConfig.VERSION_CODE

tracker.get().setProperty(0, "android-$versionCode")
tracker.get().start()
trackSessionStart()

if (BuildConfig.APPLICATION_ID != "app.tice.TICE.development") {
tracker.get().start()
}
appInitialized = true

return initJob
}

private fun trackSessionStart() {
val lang =
ConfigurationCompat.getLocales(application.applicationContext.resources.configuration)[0].language
tracker.get().track(TrackerEvent.sessionStart(lang))
}

fun onStop() {
logger.debug("OnStop called")
val sessionDuration = Date().time - (sessionStart ?: Date()).time
tracker.get().track(TrackerEvent.sessionEnd(sessionDuration))

CoroutineScope(coroutineContextProvider.get().IO).launch {
tracker.get().dispatch()
if (appInitialized) {
val sessionDuration = Date().time - (sessionStart ?: Date()).time
tracker.get().track(TrackerEvent.sessionEnd(sessionDuration))

CoroutineScope(coroutineContextProvider.get().IO).launch {
tracker.get().dispatch()
}
}

webSocketReceiver.get().disconnect()
Expand All @@ -228,10 +240,10 @@ class AppFlow constructor(val application: TICEApplication) : LifecycleObserver,
fun onMoveToForeground() {
_isInForeground = AppStatusProvider.Status.FOREGROUND

val lang =
ConfigurationCompat.getLocales(application.applicationContext.resources.configuration)[0].language
tracker.get().track(TrackerEvent.sessionStart(lang))
sessionStart = Date()
if (appInitialized) {
trackSessionStart()
}
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
Expand Down
15 changes: 0 additions & 15 deletions app/src/test/java/tice/AppFlowTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -307,36 +307,21 @@ internal class AppFlowTest {

@Test
fun onStop() = runBlocking {
val slot = slot<TrackerEvent>()
every { mockTracker.track(capture(slot), any()) } answers { }

appFlow.onStop()

testDispatcher.advanceUntilIdle()

coVerify(exactly = 1) { mockTracker.dispatch() }
verify(exactly = 1) { mockTracker.track(any()) }
Assertions.assertEquals(slot.captured.name, "SessionEnd")
coVerify(exactly = 1) { mockWebSocketReceiver.disconnect() }
}

@Test
fun onMoveToForeground() = runBlocking {
val defaultStatus = appFlow.status

val slot = slot<TrackerEvent>()
every { mockTracker.track(capture(slot), any()) } answers { }

appFlow.onMoveToForeground()

val newStatus = appFlow.status

Assertions.assertEquals(defaultStatus, AppStatusProvider.Status.BACKGROUND)
Assertions.assertEquals(newStatus, AppStatusProvider.Status.FOREGROUND)

verify(exactly = 1) { mockTracker.track(any()) }
Assertions.assertEquals(slot.captured.name, "SessionStart")
Assertions.assertEquals(slot.captured.detail, testLanguage)
}

@Test
Expand Down