feat: cache last-good stats snapshot per server for instant Stats open
This commit is contained in:
1 parent
b8ac68b363
commit
97fda5a3df
4 files changed
+73
-5
No files matched your search
@@ -21,6 +21,7 @@ data class ServerProfile(
|
||||
val slowConnection: Boolean = false
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class ServerStatus(
|
||||
val version: String,
|
||||
val buildDate: String,
|
||||
@@ -95,8 +96,10 @@ data class StagedRow(
|
||||
val status: String
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class PieStatBucket(val name: String, val value: Int)
|
||||
|
||||
@Serializable
|
||||
data class PieStats(
|
||||
val totalFiles: Int,
|
||||
val totalTranscodeCount: Int,
|
||||
@@ -107,6 +110,7 @@ data class PieStats(
|
||||
val videoCodecs: List<PieStatBucket>
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class ResStats(
|
||||
val cpuPerc: Double,
|
||||
val memUsedGB: Double,
|
||||
@@ -114,6 +118,15 @@ data class ResStats(
|
||||
val heapUsedMB: Double
|
||||
)
|
||||
|
||||
/** Last-good stats snapshot persisted so the Stats screen opens instantly. */
|
||||
@Serializable
|
||||
data class StatsSnapshot(
|
||||
val savedAtMs: Long,
|
||||
val status: ServerStatus? = null,
|
||||
val res: ResStats? = null,
|
||||
val pies: PieStats? = null
|
||||
)
|
||||
|
||||
data class FlowPluginInfo(
|
||||
val id: String,
|
||||
val name: String,
|
||||
|
||||
@@ -5,8 +5,10 @@ import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import com.trafero.app.data.model.ServerProfile
|
||||
import com.trafero.app.data.model.StatsSnapshot
|
||||
import com.trafero.app.util.TraferoJson
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.serialization.builtins.ListSerializer
|
||||
|
||||
@@ -45,6 +47,7 @@ class ServerStore(private val context: Context) {
|
||||
val updated = book.profiles.filterNot { it.id == id }
|
||||
prefs[keyServers] = TraferoJson.encodeToString(ListSerializer(ServerProfile.serializer()), updated)
|
||||
if (prefs[keyActive] == id) prefs[keyActive] = updated.firstOrNull()?.id ?: ""
|
||||
prefs.remove(statsKey(id))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,6 +55,29 @@ class ServerStore(private val context: Context) {
|
||||
context.traferoDataStore.edit { prefs -> prefs[keyActive] = id }
|
||||
}
|
||||
|
||||
// ---- last-good stats snapshot (per server) ----
|
||||
|
||||
private fun statsKey(serverId: String) = stringPreferencesKey("stats_$serverId")
|
||||
|
||||
suspend fun saveStatsSnapshot(serverId: String, snapshot: StatsSnapshot) {
|
||||
context.traferoDataStore.edit { prefs ->
|
||||
prefs[statsKey(serverId)] =
|
||||
TraferoJson.encodeToString(StatsSnapshot.serializer(), snapshot)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun loadStatsSnapshot(serverId: String): StatsSnapshot? {
|
||||
val json = context.traferoDataStore.data.first()[statsKey(serverId)]
|
||||
if (json.isNullOrBlank()) return null
|
||||
return runCatching {
|
||||
TraferoJson.decodeFromString(StatsSnapshot.serializer(), json)
|
||||
}.getOrNull()
|
||||
}
|
||||
|
||||
suspend fun removeStatsSnapshot(serverId: String) {
|
||||
context.traferoDataStore.edit { prefs -> prefs.remove(statsKey(serverId)) }
|
||||
}
|
||||
|
||||
private fun decodeBook(serversJson: String?, activeId: String?): ServerBook {
|
||||
val list = if (serversJson.isNullOrBlank()) emptyList() else
|
||||
runCatching {
|
||||
|
||||
@@ -55,6 +55,7 @@ val LocalServerCtx = compositionLocalOf<ServerCtx> { ServerCtx() }
|
||||
|
||||
data class ServerCtx(
|
||||
val refreshMs: Long = 15_000L,
|
||||
val serverId: String = "",
|
||||
val onError: (String) -> Unit = {}
|
||||
)
|
||||
|
||||
@@ -92,7 +93,7 @@ fun AppRoot(container: AppContainer) {
|
||||
var refreshMs by remember { mutableStateOf(15_000L) }
|
||||
CompositionLocalProvider(
|
||||
LocalRepo provides repo,
|
||||
LocalServerCtx provides ServerCtx(refreshMs = refreshMs)
|
||||
LocalServerCtx provides ServerCtx(refreshMs = refreshMs, serverId = active.id)
|
||||
) {
|
||||
MainScaffold(container, state.profiles, active, refreshMs) { refreshMs = it }
|
||||
}
|
||||
@@ -165,7 +166,7 @@ private fun MainScaffold(
|
||||
composable("jobs") { JobsScreen(nav) }
|
||||
composable("library") { LibraryScreen(nav) }
|
||||
composable("nodes") { NodesScreen() }
|
||||
composable("stats") { StatsScreen() }
|
||||
composable("stats") { StatsScreen(container) }
|
||||
composable("more") { MoreScreen(nav) }
|
||||
composable("logs") { LogsScreen() }
|
||||
composable("settings") { SettingsScreen(container) }
|
||||
|
||||
@@ -14,6 +14,7 @@ import androidx.compose.foundation.verticalScroll
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
@@ -25,7 +26,9 @@ import androidx.compose.ui.unit.dp
|
||||
import com.trafero.app.data.model.PieStats
|
||||
import com.trafero.app.data.model.ResStats
|
||||
import com.trafero.app.data.model.ServerStatus
|
||||
import com.trafero.app.data.model.StatsSnapshot
|
||||
import com.trafero.app.data.repo.TraferoRepo
|
||||
import com.trafero.app.di.AppContainer
|
||||
import com.trafero.app.ui.LocalRepo
|
||||
import com.trafero.app.ui.LocalServerCtx
|
||||
import com.trafero.app.ui.common.ChartPalette
|
||||
@@ -40,23 +43,48 @@ import com.trafero.app.ui.common.pollLoop
|
||||
import com.trafero.app.ui.common.rememberPollState
|
||||
|
||||
@Composable
|
||||
fun StatsScreen() {
|
||||
fun StatsScreen(container: AppContainer) {
|
||||
val repo = LocalRepo.current ?: return
|
||||
val ctx = LocalServerCtx.current
|
||||
var status by remember { mutableStateOf<ServerStatus?>(null) }
|
||||
var pies by remember { mutableStateOf<PieStats?>(null) }
|
||||
var res by remember { mutableStateOf<ResStats?>(null) }
|
||||
var error by remember { mutableStateOf<String?>(null) }
|
||||
var updatedAt by remember { mutableStateOf(0L) }
|
||||
var live by remember { mutableStateOf(false) }
|
||||
val poll = rememberPollState()
|
||||
pollLoop(ctx.refreshMs, listOf<Any?>("stats"), poll) {
|
||||
|
||||
// Seed from the last-good snapshot on open (per server) so the screen
|
||||
// renders immediately while the first poll refreshes in the background.
|
||||
LaunchedEffect(ctx.serverId) {
|
||||
status = null; pies = null; res = null; error = null; live = false; updatedAt = 0L
|
||||
val snap = container.serverStore.loadStatsSnapshot(ctx.serverId) ?: return@LaunchedEffect
|
||||
if (status == null && pies == null && res == null) { // don't clobber a faster live poll
|
||||
status = snap.status; pies = snap.pies; res = snap.res
|
||||
updatedAt = snap.savedAtMs
|
||||
}
|
||||
}
|
||||
|
||||
pollLoop(ctx.refreshMs, listOf<Any?>("stats", ctx.serverId), poll) {
|
||||
runCatching {
|
||||
Triple(repo.status(), repo.pies(), repo.resStats())
|
||||
}.onSuccess { (s, p, r) -> status = s; pies = p; res = r; error = null; poll.success() }
|
||||
}.onSuccess { (s, p, r) ->
|
||||
status = s; pies = p; res = r; error = null; live = true; poll.success()
|
||||
updatedAt = System.currentTimeMillis()
|
||||
val snap = StatsSnapshot(updatedAt, s, r, p)
|
||||
runCatching { container.serverStore.saveStatsSnapshot(ctx.serverId, snap) }
|
||||
}
|
||||
.onFailure { poll.failure(it, hasData = status != null)?.let { msg -> error = msg } }
|
||||
}
|
||||
Column(Modifier.fillMaxSize().verticalScroll(rememberScrollState())) {
|
||||
error?.let { ErrorBanner(it) }
|
||||
if (poll.reconnecting) ReconnectChip()
|
||||
if (!live && updatedAt > 0 && status != null) {
|
||||
Text("Cached - updated " + Fmt.timeAgo(updatedAt),
|
||||
Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 2.dp),
|
||||
style = MaterialTheme.typography.labelSmall,
|
||||
color = MaterialTheme.colorScheme.outline)
|
||||
}
|
||||
status?.let { s ->
|
||||
SectionCard("Server") {
|
||||
Text("Tdarr " + s.version + " (" + s.serverEngine + ")", fontWeight = FontWeight.SemiBold)
|
||||
|
||||
Reference in new issue
Block a user