Skip to content

Commit 4cc494c

Browse files
committed
Remove serialization (this will be handled in encoders) and rename SessionState
1 parent 3c53a66 commit 4cc494c

File tree

6 files changed

+60
-180
lines changed

6 files changed

+60
-180
lines changed

firebase-sessions/firebase-sessions.gradle.kts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,11 @@ android {
4242
}
4343

4444
dependencies {
45-
implementation(project(":encoders:firebase-encoders"))
46-
implementation(project(":encoders:firebase-encoders-json"))
4745
implementation(project(":firebase-common"))
4846
implementation(project(":firebase-common:ktx"))
4947
implementation(project(":firebase-components"))
5048
implementation(libs.androidx.annotation)
5149

52-
testImplementation(libs.androidx.test.junit)
53-
testImplementation(libs.androidx.test.runner)
54-
testImplementation(libs.robolectric)
5550
testImplementation(libs.junit)
5651
testImplementation(libs.truth)
5752

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/FirebaseSessions.kt

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import androidx.annotation.Discouraged
2222
import com.google.firebase.FirebaseApp
2323
import com.google.firebase.ktx.Firebase
2424
import com.google.firebase.ktx.app
25+
import com.google.firebase.sessions.EventType.SESSION_START
2526

2627
class FirebaseSessions internal constructor(firebaseApp: FirebaseApp) {
27-
2828
private val sessionGenerator = SessionGenerator(collectEvents = true)
2929

3030
init {
@@ -41,10 +41,19 @@ class FirebaseSessions internal constructor(firebaseApp: FirebaseApp) {
4141
fun greeting(): String = "Matt says hi!"
4242

4343
private fun initiateSessionStart() {
44-
// TODO(mrober): Generate a session
45-
Log.i(TAG, "Initiate session start")
44+
val sessionState = sessionGenerator.generateNewSession()
45+
val sessionEvent =
46+
SessionEvent(
47+
eventType = SESSION_START,
48+
sessionData =
49+
SessionInfo(
50+
sessionState.sessionId,
51+
sessionState.firstSessionId,
52+
sessionState.sessionIndex
53+
),
54+
)
4655

47-
sessionGenerator.generateNewSession()
56+
Log.i(TAG, "Initiate session start: $sessionEvent")
4857
}
4958

5059
companion object {

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionEvent.kt

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,13 @@
1616

1717
package com.google.firebase.sessions
1818

19-
import com.google.firebase.encoders.ValueEncoder
20-
import com.google.firebase.encoders.ValueEncoderContext
21-
import com.google.firebase.encoders.annotations.Encodable
22-
2319
/**
2420
* Contains the relevant information around an App Quality Session.
2521
*
2622
* See go/app-quality-unified-session-definition for more details. Keep in sync with
2723
* https://github.com/firebase/firebase-ios-sdk/blob/master/FirebaseSessions/ProtoSupport/Protos/sessions.proto
2824
*/
2925
// TODO(mrober): Add and populate all fields from sessions.proto
30-
// TODO(mrober): Can the firebase-encoders-processor work on Kotlin data classes?
3126
internal data class SessionEvent(
3227
/** The type of event being reported. */
3328
val eventType: EventType,
@@ -36,8 +31,10 @@ internal data class SessionEvent(
3631
val sessionData: SessionInfo,
3732
)
3833

39-
internal enum class EventType(override val number: Int) : NumberedEnum {
34+
/** Enum denoting all possible session event types. */
35+
internal enum class EventType(val number: Int) {
4036
EVENT_TYPE_UNKNOWN(0),
37+
4138
/** This event type is fired as soon as a new session begins. */
4239
SESSION_START(1),
4340
}
@@ -56,21 +53,4 @@ internal data class SessionInfo(
5653

5754
/** What order this Session came in this run of the app. For the first Session this will be 0. */
5855
val sessionIndex: Int,
59-
60-
/** The data collection status. */
61-
// TODO(mrober): Refactor to DataCollectionStatus to split out each SDK state, and sampling rate.
62-
val dataCollectionStatus: Boolean,
6356
)
64-
65-
/** Represents an explicitly numbered proto enum for serialization. */
66-
// TODO(mrober): Could NumberedEnum be part of firebase-encoders?
67-
internal interface NumberedEnum {
68-
val number: Int
69-
70-
companion object {
71-
/** Encode the enum as the number. */
72-
val ENCODER = ValueEncoder { numberedEnum: NumberedEnum, ctx: ValueEncoderContext ->
73-
ctx.add(numberedEnum.number)
74-
}
75-
}
76-
}

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionGenerator.kt

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,53 @@ package com.google.firebase.sessions
1818

1919
import java.util.UUID
2020

21+
/**
22+
* [SessionState] is a data class responsible for storing information about the current Session.
23+
*
24+
* @hide
25+
*/
26+
internal data class SessionState(
27+
val sessionId: String,
28+
val firstSessionId: String,
29+
val collectEvents: Boolean,
30+
val sessionIndex: Int,
31+
)
32+
2133
/**
2234
* The [SessionGenerator] is responsible for generating the Session ID, and keeping the
23-
* [SessionInfo] up to date with the latest values.
35+
* [SessionState] up to date with the latest values.
2436
*
2537
* @hide
2638
*/
27-
internal class SessionGenerator(collectEvents: Boolean) {
39+
internal class SessionGenerator(private var collectEvents: Boolean) {
2840
private var firstSessionId = ""
2941
private var sessionIndex: Int = -1
30-
private var dataCollectionStatus = collectEvents
3142

32-
private var thisSession: SessionInfo =
33-
SessionInfo(
43+
private var thisSession: SessionState =
44+
SessionState(
3445
sessionId = "",
3546
firstSessionId = "",
36-
dataCollectionStatus = collectEvents,
37-
sessionIndex = sessionIndex
47+
collectEvents,
48+
sessionIndex,
3849
)
3950

4051
// Generates a new Session ID. If there was already a generated Session ID
4152
// from the last session during the app's lifecycle, it will also set the last Session ID
42-
fun generateNewSession() {
53+
fun generateNewSession(): SessionState {
4354
val newSessionId = UUID.randomUUID().toString().replace("-", "").lowercase()
4455

4556
// If firstSessionId is set, use it. Otherwise set it to the
4657
// first generated Session ID
47-
firstSessionId = if (firstSessionId.isEmpty()) newSessionId else firstSessionId
58+
firstSessionId = firstSessionId.ifEmpty { newSessionId }
4859

4960
sessionIndex += 1
5061

5162
thisSession =
52-
SessionInfo(
53-
sessionId = newSessionId,
54-
firstSessionId = firstSessionId,
55-
dataCollectionStatus = dataCollectionStatus,
56-
sessionIndex = sessionIndex
57-
)
63+
SessionState(sessionId = newSessionId, firstSessionId, collectEvents, sessionIndex)
64+
65+
return thisSession
5866
}
5967

60-
val currentSession: SessionInfo
68+
val currentSession: SessionState
6169
get() = thisSession
6270
}

firebase-sessions/src/test/kotlin/com/google/firebase/sessions/EncodeSessionEventTest.kt

Lines changed: 0 additions & 112 deletions
This file was deleted.

firebase-sessions/src/test/kotlin/com/google/firebase/sessions/SessionGeneratorTest.kt

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,21 @@ class SessionGeneratorTest {
4242

4343
assertThat(sessionGenerator.currentSession.sessionId).isEqualTo("")
4444
assertThat(sessionGenerator.currentSession.firstSessionId).isEqualTo("")
45-
assertThat(sessionGenerator.currentSession.dataCollectionStatus).isEqualTo(false)
45+
assertThat(sessionGenerator.currentSession.collectEvents).isFalse()
4646
assertThat(sessionGenerator.currentSession.sessionIndex).isEqualTo(-1)
4747
}
4848

4949
@Test
50-
fun generateNewSessionID_generatesValidSessionInfo() {
50+
fun generateNewSessionID_generatesValidSessionState() {
5151
val sessionGenerator = SessionGenerator(collectEvents = true)
5252

5353
sessionGenerator.generateNewSession()
5454

55-
assertThat(isValidSessionId(sessionGenerator.currentSession.sessionId)).isEqualTo(true)
56-
assertThat(isValidSessionId(sessionGenerator.currentSession.firstSessionId)).isEqualTo(true)
55+
assertThat(isValidSessionId(sessionGenerator.currentSession.sessionId)).isTrue()
56+
assertThat(isValidSessionId(sessionGenerator.currentSession.firstSessionId)).isTrue()
5757
assertThat(sessionGenerator.currentSession.firstSessionId)
5858
.isEqualTo(sessionGenerator.currentSession.sessionId)
59-
assertThat(sessionGenerator.currentSession.dataCollectionStatus).isEqualTo(true)
59+
assertThat(sessionGenerator.currentSession.collectEvents).isTrue()
6060
assertThat(sessionGenerator.currentSession.sessionIndex).isEqualTo(0)
6161
}
6262

@@ -68,32 +68,32 @@ class SessionGeneratorTest {
6868

6969
sessionGenerator.generateNewSession()
7070

71-
val firstSessionInfo = sessionGenerator.currentSession
71+
val firstSessionState = sessionGenerator.currentSession
7272

73-
assertThat(isValidSessionId(firstSessionInfo.sessionId)).isEqualTo(true)
74-
assertThat(isValidSessionId(firstSessionInfo.firstSessionId)).isEqualTo(true)
75-
assertThat(firstSessionInfo.firstSessionId).isEqualTo(firstSessionInfo.sessionId)
76-
assertThat(firstSessionInfo.sessionIndex).isEqualTo(0)
73+
assertThat(isValidSessionId(firstSessionState.sessionId)).isTrue()
74+
assertThat(isValidSessionId(firstSessionState.firstSessionId)).isTrue()
75+
assertThat(firstSessionState.firstSessionId).isEqualTo(firstSessionState.sessionId)
76+
assertThat(firstSessionState.sessionIndex).isEqualTo(0)
7777

7878
sessionGenerator.generateNewSession()
79-
val secondSessionInfo = sessionGenerator.currentSession
79+
val secondSessionState = sessionGenerator.currentSession
8080

81-
assertThat(isValidSessionId(secondSessionInfo.sessionId)).isEqualTo(true)
82-
assertThat(isValidSessionId(secondSessionInfo.firstSessionId)).isEqualTo(true)
81+
assertThat(isValidSessionId(secondSessionState.sessionId)).isTrue()
82+
assertThat(isValidSessionId(secondSessionState.firstSessionId)).isTrue()
8383
// Ensure the new firstSessionId is equal to the first Session ID from earlier
84-
assertThat(secondSessionInfo.firstSessionId).isEqualTo(firstSessionInfo.sessionId)
84+
assertThat(secondSessionState.firstSessionId).isEqualTo(firstSessionState.sessionId)
8585
// Session Index should increase
86-
assertThat(secondSessionInfo.sessionIndex).isEqualTo(1)
86+
assertThat(secondSessionState.sessionIndex).isEqualTo(1)
8787

8888
// Do a third round just in case
8989
sessionGenerator.generateNewSession()
90-
val thirdSessionInfo = sessionGenerator.currentSession
90+
val thirdSessionState = sessionGenerator.currentSession
9191

92-
assertThat(isValidSessionId(thirdSessionInfo.sessionId)).isEqualTo(true)
93-
assertThat(isValidSessionId(thirdSessionInfo.firstSessionId)).isEqualTo(true)
92+
assertThat(isValidSessionId(thirdSessionState.sessionId)).isTrue()
93+
assertThat(isValidSessionId(thirdSessionState.firstSessionId)).isTrue()
9494
// Ensure the new firstSessionId is equal to the first Session ID from earlier
95-
assertThat(thirdSessionInfo.firstSessionId).isEqualTo(firstSessionInfo.sessionId)
95+
assertThat(thirdSessionState.firstSessionId).isEqualTo(firstSessionState.sessionId)
9696
// Session Index should increase
97-
assertThat(thirdSessionInfo.sessionIndex).isEqualTo(2)
97+
assertThat(thirdSessionState.sessionIndex).isEqualTo(2)
9898
}
9999
}

0 commit comments

Comments
 (0)