Diagnostics.kt
composeApp/src/commonMain/kotlin/eu/friedcutlet/yokan/services/logging/Diagnostics.kt
1package eu.friedcutlet.yokan.services.logging23import eu.friedcutlet.yokan.util.nowMillis4import kotlinx.coroutines.flow.MutableStateFlow5import kotlinx.coroutines.flow.StateFlow6import kotlinx.coroutines.flow.asStateFlow7import kotlinx.coroutines.flow.update89internal data class NetworkStats(10 val requests: Long = 0,11 /** Transport failures (DNS, connect, timeout, cancellation), not HTTP status codes. */12 val failures: Long = 0,13 /** Responses outside 200..399. */14 val httpErrors: Long = 0,15 val bytesReceived: Long = 0,16)1718internal data class ImageStats(19 val memoryHits: Long = 0,20 val diskHits: Long = 0,21 val networkLoads: Long = 0,22 val errors: Long = 0,23) {24 val loads: Long get() = memoryHits + diskHits + networkLoads25}2627/** Process-lifetime counters for Settings → Developer. Lock-free; they only ever grow. */28internal object Diagnostics {29 val startedAtMillis: Long = nowMillis()3031 private val _network = MutableStateFlow(NetworkStats())32 val network: StateFlow<NetworkStats> = _network.asStateFlow()3334 private val _images = MutableStateFlow(ImageStats())35 val images: StateFlow<ImageStats> = _images.asStateFlow()3637 fun recordResponse(statusCode: Int, bytes: Long) = _network.update {38 it.copy(requests = it.requests + 1, httpErrors = it.httpErrors + if (statusCode in 200..399) 0 else 1, bytesReceived = it.bytesReceived + bytes)39 }4041 fun recordRequestFailure() = _network.update { it.copy(requests = it.requests + 1, failures = it.failures + 1) }4243 fun recordImageMemoryHit() = _images.update { it.copy(memoryHits = it.memoryHits + 1) }44 fun recordImageDiskHit() = _images.update { it.copy(diskHits = it.diskHits + 1) }45 fun recordImageNetworkLoad() = _images.update { it.copy(networkLoads = it.networkLoads + 1) }46 fun recordImageError() = _images.update { it.copy(errors = it.errors + 1) }47}48