perf(domain): батчить вывод SSH-команд одним обновлением лога

This commit is contained in:
samosvalishe committed 2026-06-10 16:06:53 +03:00
1 parent e2f9b0c966
commit 8379eb46e1
1 file changed
+14 -8
@@ -44,7 +44,8 @@ class SshRepository(
private val timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss") private val timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss")
private fun timestamp(): String = LocalTime.now().format(timeFormatter) private fun timestamp(): String = LocalTime.now().format(timeFormatter)
private fun appendSshLog(vararg lines: String) { private fun appendSshLog(lines: List<String>) {
if (lines.isEmpty()) return
_sshLog.update { current -> _sshLog.update { current ->
var next = current + lines var next = current + lines
if (next.size > 500) next = next.drop(next.size - 500) if (next.size > 500) next = next.drop(next.size - 500)
@@ -52,18 +53,23 @@ class SshRepository(
} }
} }
private fun appendSshLog(vararg lines: String) = appendSshLog(lines.toList())
private fun logHeader(label: String, target: String) { private fun logHeader(label: String, target: String) {
appendSshLog("", "=== $label [${timestamp()}] ===", " ssh $target") appendSshLog("", "=== $label [${timestamp()}] ===", " ssh $target")
} }
private fun logCmdResult(result: CmdResult) { private fun logCmdResult(result: CmdResult) {
result.logs.forEach { appendSshLog(it) } // Одним обновлением состояния: построчный append на длинном выводе (журнал
when (result) { // на 200 строк) дёргал подписчиков UI — бейдж и автоскролл — на каждую строку.
is CmdResult.Ok -> val batch = buildList {
result.kv.forEach { (k, v) -> appendSshLog(" $k=$v") } addAll(result.logs)
is CmdResult.Err -> when (result) {
appendSshLog("ERROR: ${result.message}") is CmdResult.Ok -> result.kv.forEach { (k, v) -> add(" $k=$v") }
is CmdResult.Err -> add("ERROR: ${result.message}")
}
} }
appendSshLog(batch)
} }
private suspend fun runCmd(cfg: SshConfig, label: String, cmd: ServerCommand): CmdResult { private suspend fun runCmd(cfg: SshConfig, label: String, cmd: ServerCommand): CmdResult {
@@ -81,7 +87,7 @@ class SshRepository(
knownFingerprint = cfg.hostFingerprint.ifEmpty { null }, knownFingerprint = cfg.hostFingerprint.ifEmpty { null },
sshKey = if (cfg.authType == "SSH_KEY") cfg.sshKey else "" sshKey = if (cfg.authType == "SSH_KEY") cfg.sshKey else ""
) )
result.lines().filter { it.isNotBlank() }.forEach { appendSshLog(it) } appendSshLog(result.lines().filter { it.isNotBlank() })
return result return result
} }