Skip to content

Commit

Permalink
Update call stats json format (#1032)
Browse files Browse the repository at this point in the history
  • Loading branch information
liviu-timar authored Mar 14, 2024
1 parent f76e503 commit 466a94f
Showing 1 changed file with 36 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.getstream.video.android.core

import android.util.Log
import io.getstream.video.android.core.call.stats.model.RtcStatsReport
import io.getstream.video.android.core.internal.InternalStreamVideoApi
import io.getstream.video.android.core.model.StreamPeerType
Expand All @@ -33,43 +34,42 @@ data class CallStatsReport(
)

fun CallStatsReport.toJson(peerType: StreamPeerType): String {
val statsKey: String
val stats: Map<String, RTCStats>?

if (peerType == StreamPeerType.PUBLISHER) {
statsKey = "publisherStats"
stats = publisher?.origin?.statsMap
val stats: Map<String, RTCStats>? = if (peerType == StreamPeerType.PUBLISHER) {
publisher?.origin?.statsMap
} else {
statsKey = "subscriberStats"
stats = subscriber?.origin?.statsMap
subscriber?.origin?.statsMap
}

return JSONObject().apply {
put(
statsKey,
JSONArray().also { array ->
stats?.forEach { statsEntry ->
array.put(
JSONObject().apply {
put(
statsEntry.key,
JSONObject().apply {
statsEntry.value.members.forEach { (key, value) ->
put(
key,
try {
JSONObject(value.toString())
} catch (e: JSONException) {
value
},
)
}
},
)
},
)
}
},
)
}.toString(4)
return JSONArray().also { array ->
stats?.forEach { statsEntry ->
array.put(
JSONObject().apply {
// Cannot use statsEntry.value.toString() because it's not valid JSON sometimes
// So we add the properties one by one
put("timestamp", statsEntry.value.timestampUs)
put("id", statsEntry.value.id)
put("type", statsEntry.value.type)
statsEntry.value.members.forEach { (key, value) ->
put(
key,
try {
// Sometimes the member is a JSON string, so we try to parse it
when (value) {
is Array<*> -> JSONArray(value)
is Map<*, *> -> JSONObject(value.toString())
else -> value // Or we just add the value
}
} catch (e: JSONException) {
Log.d(
"CallStatsReport",
"sendStats. Error for $key with $value: ${e.message}",
)
""
},
)
}
},
)
}
}.toString(2)
}

0 comments on commit 466a94f

Please sign in to comment.