Skip to content

Commit

Permalink
Merge pull request #1908 from DataDog/xgouchet/RUM-3302/handle_has_fu…
Browse files Browse the repository at this point in the history
…ll_snapshot

RUM-3302 detect full snapshot from webview sr
  • Loading branch information
xgouchet authored Mar 11, 2024
2 parents 731551f + c3b45d2 commit dec970a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
1 change: 1 addition & 0 deletions detekt_custom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ datadog:
- "com.google.gson.JsonArray.add(com.google.gson.JsonElement?)"
- "com.google.gson.JsonArray.add(kotlin.String?)"
- "com.google.gson.JsonArray.addAll(com.google.gson.JsonArray?)"
- "com.google.gson.JsonArray.any(kotlin.Function1)"
- "com.google.gson.JsonArray.asSequence()"
- "com.google.gson.JsonArray.constructor()"
- "com.google.gson.JsonArray.constructor(kotlin.Int)"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,17 @@ internal class BatchesToSegmentsMapper(private val internalLogger: InternalLogge
Pair(rumContext, records)
}
}
.groupBy { it.first }
.mapValues {
it.value.fold(JsonArray()) { acc, pair ->
acc.addAll(pair.second)
acc
}
}
.filter { !it.value.isEmpty }
.mapNotNull {
mapToSegment(it.key, it.value)
.groupBy { it.first }
.mapValues {
it.value.fold(JsonArray()) { acc, pair ->
acc.addAll(pair.second)
acc
}
}
.filter { !it.value.isEmpty }
.mapNotNull {
mapToSegment(it.key, it.value)
}
}

@Suppress("ReturnCount")
Expand Down Expand Up @@ -142,10 +142,10 @@ internal class BatchesToSegmentsMapper(private val internalLogger: InternalLogge
}

private fun hasFullSnapshotRecord(records: JsonArray) =
records.firstOrNull {
it.asJsonObject.getAsJsonPrimitive(RECORD_TYPE_KEY)?.safeGetAsLong(internalLogger) ==
FULL_SNAPSHOT_RECORD_TYPE
} != null
records.any {
val typeAsLong = it.asJsonObject.getAsJsonPrimitive(RECORD_TYPE_KEY)?.safeGetAsLong(internalLogger)
typeAsLong == FULL_SNAPSHOT_RECORD_TYPE_MOBILE || typeAsLong == FULL_SNAPSHOT_RECORD_TYPE_BROWSER
}

private fun JsonObject.records(): JsonArray? {
return get(EnrichedRecord.RECORDS_KEY)?.safeGetAsJsonArray(internalLogger)
Expand Down Expand Up @@ -179,7 +179,10 @@ internal class BatchesToSegmentsMapper(private val internalLogger: InternalLogge
// endregion

companion object {
private const val FULL_SNAPSHOT_RECORD_TYPE = 10L

private const val FULL_SNAPSHOT_RECORD_TYPE_MOBILE = 10L
private const val FULL_SNAPSHOT_RECORD_TYPE_BROWSER = 2L

internal const val RECORDS_KEY = "records"
private const val RECORD_TYPE_KEY = "type"
internal const val TIMESTAMP_KEY = "timestamp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,8 @@ internal class BatchesToSegmentsMapperTest {
) {
// Given
val fakeRecordsSize = forge.anInt(min = 10, max = 20)
val fakeRecords = forge.aList<MobileSegment.MobileRecord>(fakeRecordsSize) {
forge.getForgery()
}.sortedBy { it.timestamp() }
val fakeRecords = forge.aList<MobileSegment.MobileRecord>(fakeRecordsSize) { forge.getForgery() }
.sortedBy { it.timestamp() }
val fakeEnrichedRecords = fakeRecords
.chunked(forge.anInt(min = 1, max = fakeRecordsSize))
.map {
Expand Down Expand Up @@ -666,16 +665,16 @@ internal class BatchesToSegmentsMapperTest {

private fun EnrichedRecord.toSegment(): MobileSegment {
return MobileSegment(
MobileSegment.Application(applicationId),
MobileSegment.Session(sessionId),
MobileSegment.View(viewId),
startTimestamp(),
endTimestamp(),
records.size.toLong(),
null,
hasFullSnapshot(),
MobileSegment.Source.ANDROID,
records
application = MobileSegment.Application(applicationId),
session = MobileSegment.Session(sessionId),
view = MobileSegment.View(viewId),
start = startTimestamp(),
end = endTimestamp(),
recordsCount = records.size.toLong(),
indexInView = null,
hasFullSnapshot = hasFullSnapshot(),
source = MobileSegment.Source.ANDROID,
records = records
)
}

Expand All @@ -692,7 +691,7 @@ internal class BatchesToSegmentsMapperTest {
}

private fun List<MobileSegment.MobileRecord>.hasFullSnapshot(): Boolean {
return firstOrNull { it is MobileSegment.MobileRecord.MobileFullSnapshotRecord } != null
return any { it is MobileSegment.MobileRecord.MobileFullSnapshotRecord }
}

private fun MobileSegment.MobileRecord.timestamp(): Long {
Expand Down

0 comments on commit dec970a

Please sign in to comment.