From 3480461ffb406f74bfc6633b72d53e89aeab14e2 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 16 Oct 2026 03:43:29 +0100 Subject: [PATCH] Show the last run on every pipeline row MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each pipeline row in the Android list now renders its most recent run: the status chip, how long ago it started, its recorded duration and its run id. Pipelines that have never run say "no runs recorded" instead of leaving a blank line, and the line is withheld until the run history has loaded so a missing history is never reported as "no runs". The list_runs history already arrives whole (id DESC), so the join is client-side: PipelineLastRun.kt folds it onto a Map keyed by pipeline id, recency decided from the timestamps with the higher run id breaking an exact tie, and formats "ago · duration · run N", omitting any genuinely absent fragment. The change is android/-only, adds no server field or dependency, keeps the existing title/secondary structure and selected-row highlight, and never starts a run. PipelineLastRunTest covers most-recent-wins, no runs, unknown pipeline id, equal timestamps, a bad timestamp, an unknown status word, the formatRanAgo scales and the "no runs recorded" wording. README records the verification: :app:testDebugUnitTest (153 tests, 0 failures) and a full --rerun-tasks assembleDebug (17,863,337 bytes, sha256 b2255066…). --- android/README.md | 59 +++++++++ .../pipelines/ui/screens/PipelineLastRun.kt | 96 ++++++++++++++ .../ui/screens/PipelineListScreen.kt | 70 +++++++++- .../ui/screens/PipelineLastRunTest.kt | 123 ++++++++++++++++++ 4 files changed, 343 insertions(+), 5 deletions(-) create mode 100644 android/app/src/main/java/net/awan/pipelines/ui/screens/PipelineLastRun.kt create mode 100644 android/app/src/test/java/net/awan/pipelines/ui/screens/PipelineLastRunTest.kt diff --git a/android/README.md b/android/README.md index 390a9d7c..84fe1c87 100644 --- a/android/README.md +++ b/android/README.md @@ -1376,3 +1376,62 @@ tree (JDK 17 from Homebrew `openjdk@17`, Gradle 8.11.1, `--offline`): http://10.0.0.249:8080/artifacts/72/1/pipelines-debug-wip-24584d8.apk` returned `HTTP/1.1 200 OK`, `Content-Type: application/vnd.android.package-archive`, `Content-Length: 18009527`, and a streamed `GET | shasum -a 256` matched. + +## Last-run facts on every pipeline row (16 Oct 2026) + +`android/` only. No server field was added: the whole run history already arrives +from `list_runs` (`internal/pipeline/runs.go`, id DESC), so the join is +client-side. + +- **Pure join.** `ui/screens/PipelineLastRun.kt` adds `pipelineRunBadges(runs, now)`, + which folds the history onto a `Map` keyed by + pipeline id with `runId`, `state` (`runStateOf`), `ranAgo` and `duration`. + Recency comes from the run timestamps, not the input order — the higher run id + breaks an exact tie (or a missing/blank `created_at`), so an unsorted or + partial response is still correct. `PipelineRunBadge.metaLine()` formats + `ago · duration · run N` and drops any fragment that is genuinely absent (a + still-running run has no `finished_at`); it never prints a blank separator. + `formatRanAgo` mirrors the `formatDuration` integer rhythm (`just now`, then + `Nm ago`/`Nh ago`/`Nd ago`) and clamps clock skew to `just now`. A pipeline with + no run is simply absent from the map and the row prints the explicit + `NO_RUNS_RECORDED` + ("no runs recorded") instead of a blank line. The last-run line is withheld + until `runsState.data != null` (`runsKnown`), so a not-yet-loaded history never + makes the false "no runs recorded" claim. +- **Row.** `PipelineListScreen.kt` refreshes runs alongside pipelines, anchors + "now" once per composition (`remember`), and renders the chip + meta line in a + `Row` under the unchanged title/secondary lines; the selected-row highlight is + untouched. +- **Tests.** `test/.../PipelineLastRunTest.kt` (10 cases) covers most-recent-wins + on unsorted input, no runs, unknown pipeline id, equal timestamps, an + unparseable timestamp, an unknown status word (`RunState.Unknown`, id still + rendered), each `formatRanAgo` scale plus blank/bad input, and the + `NO_RUNS_RECORDED` wording. + +Verified on this tree (macOS, JDK 17 from Homebrew `openjdk@17`, Gradle 9.7.1, +`ANDROID_HOME=/Users/alan/Library/Android/sdk`): + +- `gradle :app:testDebugUnitTest` → **153 tests, 0 failures, 0 errors, 0 + skipped** (`PipelineLastRunTest` 10/10). +- `gradle :app:assembleDebug --rerun-tasks --no-build-cache` succeeded → + `app-debug.apk` **17,863,337 bytes**, sha256 + `b22550660c1ce9f42719eb5a32a296055d9f3df97fa0fd94bb26fa3868730506`. This is a + full rebuild of the current tree; the review's blocker was that the earlier + published `artifacts/83/1/pipelines-debug-wip-5444390.apk` (17,997,811 bytes, + sha256 `0fb07207…`) was a different revision of `PipelineListScreen.kt`, so it + has been rebuilt and republished (see below). +- Screenshots on `emulator-5556` (`FoldB_API_36`, 2208x1840, density 420, dark + mode) with `android/scripts/capture_png.sh -s emulator-5556`: the narrow window + via `adb shell wm size 1080x2340`, the wide window at the default 2208x1840 + (`wm size reset`). Both show the running run omitting its duration, the failed + and canceled rows carrying `ago · duration · run N`, and pipeline id 3 "Plan, + implement, review" printing `no runs recorded`. +- **APK publish.** Copied to + `…/artifacts/83/3/pipelines-debug-wip-5444390.apk` (`5444390` = `git rev-parse + --short HEAD`). `curl -I + http://10.0.0.249:8080/artifacts/83/3/pipelines-debug-wip-5444390.apk` returned + `HTTP/1.1 200 OK`, `Content-Type: application/vnd.android.package-archive`, + `Content-Length: 17863337`, and a streamed `GET | shasum -a 256` returned the + same `b2255066…` as the local file. The same APK was installed on the emulator + and the pulled installed `base.apk` hashed to `b2255066…`, so the reviewed + source is what was actually rendered. diff --git a/android/app/src/main/java/net/awan/pipelines/ui/screens/PipelineLastRun.kt b/android/app/src/main/java/net/awan/pipelines/ui/screens/PipelineLastRun.kt new file mode 100644 index 00000000..26cca427 --- /dev/null +++ b/android/app/src/main/java/net/awan/pipelines/ui/screens/PipelineLastRun.kt @@ -0,0 +1,96 @@ +package net.awan.pipelines.ui.screens + +import java.time.Duration +import java.time.OffsetDateTime +import net.awan.pipelines.data.protocol.RunSummary +import net.awan.pipelines.ui.components.RunState +import net.awan.pipelines.ui.components.runStateOf +import net.awan.pipelines.ui.formatDuration + +/** Shown for a pipeline that appears in list_runs but has recorded no run. */ +const val NO_RUNS_RECORDED = "no runs recorded" + +/** + * The last-run facts one pipeline row shows: the status chip's [state], how long + * ago the run started, its recorded duration and its run id. Kept as a plain data + * class so the join and its edge cases (no runs, ties, unknown status) are + * unit-testable without composing anything. + */ +internal data class PipelineRunBadge( + val runId: Long, + val state: RunState, + val ranAgo: String?, + val duration: String?, +) + +/** + * The last-run secondary line, e.g. `2h ago · 1m 5s · run 42`. Missing fragments + * (an unparseable start, a run still executing) are omitted so no blank separator + * is ever printed; the run id is always present. + */ +internal fun PipelineRunBadge.metaLine(): String = buildList { + ranAgo?.let(::add) + duration?.let(::add) + add("run $runId") +}.joinToString(" · ") + +/** + * Joins list_runs (the whole history, delivered newest-first) onto pipelines by + * pipeline id: one badge per pipeline that has run, the most recent run winning. + * The input order never matters — recency is decided from the timestamps, with the + * higher run id breaking an exact tie — so a pipeline with no runs is simply + * absent from the result (the caller renders [NO_RUNS_RECORDED] for it). No server + * field is involved; this is a pure client-side join. + */ +internal fun pipelineRunBadges( + runs: List, + now: OffsetDateTime, +): Map { + val latest = HashMap() + for (run in runs) { + val current = latest[run.pipelineId] + if (current == null || isMoreRecent(run, current)) latest[run.pipelineId] = run + } + return latest.mapValues { (_, run) -> + PipelineRunBadge( + runId = run.id, + state = runStateOf(run.status), + ranAgo = formatRanAgo(run.createdAt, now), + duration = formatDuration(run.createdAt, run.finishedAt), + ) + } +} + +/** Timestamps decide recency; the higher run id breaks an exact tie (or a bad timestamp). */ +private fun isMoreRecent(candidate: RunSummary, current: RunSummary): Boolean { + val a = parseInstant(candidate.createdAt) + val b = parseInstant(current.createdAt) + if (a != null && b != null) { + val order = a.compareTo(b) + if (order != 0) return order > 0 + } + return candidate.id > current.id +} + +/** + * A compact "how long ago" for a run's start: `just now` under a minute, then + * `Nm ago`/`Nh ago`/`Nd ago`, the same integer rhythm as [formatDuration]. Null + * when the timestamp is blank or unparseable, so the caller omits the fragment + * instead of printing a blank. A future start (clock skew) reads as `just now`, + * never a negative age. + */ +internal fun formatRanAgo(createdAt: String, now: OffsetDateTime): String? { + val start = parseInstant(createdAt) ?: return null + val seconds = Duration.between(start, now).seconds.coerceAtLeast(0) + return when { + seconds < 60 -> "just now" + seconds < 3600 -> "${seconds / 60}m ago" + seconds < 86_400 -> "${seconds / 3600}h ago" + else -> "${seconds / 86_400}d ago" + } +} + +private fun parseInstant(text: String): OffsetDateTime? { + if (text.isBlank()) return null + return runCatching { OffsetDateTime.parse(text) }.getOrNull() +} diff --git a/android/app/src/main/java/net/awan/pipelines/ui/screens/PipelineListScreen.kt b/android/app/src/main/java/net/awan/pipelines/ui/screens/PipelineListScreen.kt index 8b8efac9..96664a6a 100644 --- a/android/app/src/main/java/net/awan/pipelines/ui/screens/PipelineListScreen.kt +++ b/android/app/src/main/java/net/awan/pipelines/ui/screens/PipelineListScreen.kt @@ -1,7 +1,9 @@ package net.awan.pipelines.ui.screens +import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues +import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding @@ -25,10 +27,12 @@ import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue +import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp import androidx.lifecycle.compose.collectAsStateWithLifecycle +import java.time.OffsetDateTime import net.awan.pipelines.data.protocol.PipelineSummary import net.awan.pipelines.ui.AppViewModel import net.awan.pipelines.ui.components.EmptyState @@ -36,6 +40,7 @@ import net.awan.pipelines.ui.components.ErrorState import net.awan.pipelines.ui.components.ListRow import net.awan.pipelines.ui.components.LoadingState import net.awan.pipelines.ui.components.PaneIdentity +import net.awan.pipelines.ui.components.RunStatusChip import net.awan.pipelines.ui.theme.pipelinesText @OptIn(ExperimentalMaterial3Api::class) @@ -48,9 +53,24 @@ fun PipelineListScreen( covered: Boolean = false, ) { val state by vm.pipelines.collectAsStateWithLifecycle() + val runsState by vm.runs.collectAsStateWithLifecycle() var query by remember { mutableStateOf("") } - LaunchedEffect(Unit) { vm.refreshPipelines() } + // The last-run line joins list_runs onto the pipelines client-side; runs are + // fetched here too so a pipeline list opened first still shows its history. + LaunchedEffect(Unit) { + vm.refreshPipelines() + vm.refreshRuns() + } + + // "How long ago" is anchored once per composition, not recomputed per row. + val now = remember { OffsetDateTime.now() } + val badges = remember(runsState.data, now) { + runsState.data?.let { pipelineRunBadges(it.runs, now) }.orEmpty() + } + // Until the run history has loaded, a missing badge is "not known yet", never + // the explicit "no runs recorded" claim. + val runsKnown = runsState.data != null // A covered pane shows its compact identity only; the search field and list // must never be drawn squeezed into the peek strip. @@ -70,7 +90,12 @@ fun PipelineListScreen( TopAppBar( title = { Text("Pipelines") }, actions = { - IconButton(onClick = vm::refreshPipelines) { + IconButton( + onClick = { + vm.refreshPipelines() + vm.refreshRuns() + }, + ) { Icon(Icons.Default.Refresh, contentDescription = "Refresh pipelines") } }, @@ -106,6 +131,8 @@ fun PipelineListScreen( PipelineRow( pipeline = pipeline, selected = pipeline.id == selectedId, + badge = badges[pipeline.id], + runsKnown = runsKnown, onClick = { onOpen(pipeline.id, pipeline.version) }, ) } @@ -126,11 +153,20 @@ fun PipelineListScreen( /** * One pipeline as a row of the shared real list: a leading workflow glyph, the - * name as the title and the id/version/stage-count as the secondary line. The - * row is selected when its pipeline is the one open in the detail pane. + * name as the title, the id/version/stage-count as the secondary line and the + * last-run facts (status chip, how long ago, duration, run id) below. The row is + * selected when its pipeline is the one open in the detail pane. Before the run + * history has loaded the last-run line is omitted; once it has, a pipeline with + * no recorded run says so explicitly. */ @Composable -private fun PipelineRow(pipeline: PipelineSummary, selected: Boolean, onClick: () -> Unit) { +private fun PipelineRow( + pipeline: PipelineSummary, + selected: Boolean, + badge: PipelineRunBadge?, + runsKnown: Boolean, + onClick: () -> Unit, +) { val text = pipelinesText() ListRow( selected = selected, @@ -163,5 +199,29 @@ private fun PipelineRow(pipeline: PipelineSummary, selected: Boolean, onClick: ( overflow = TextOverflow.Ellipsis, ) } + if (runsKnown) { + if (badge == null) { + Text( + NO_RUNS_RECORDED, + style = text.meta, + color = MaterialTheme.colorScheme.onSurfaceVariant, + maxLines = 1, + overflow = TextOverflow.Ellipsis, + ) + } else { + Row( + verticalAlignment = Alignment.CenterVertically, + horizontalArrangement = Arrangement.spacedBy(8.dp), + ) { + RunStatusChip(badge.state) + Text( + badge.metaLine(), + style = text.meta, + maxLines = 1, + overflow = TextOverflow.Ellipsis, + ) + } + } + } } } diff --git a/android/app/src/test/java/net/awan/pipelines/ui/screens/PipelineLastRunTest.kt b/android/app/src/test/java/net/awan/pipelines/ui/screens/PipelineLastRunTest.kt new file mode 100644 index 00000000..0ae593a5 --- /dev/null +++ b/android/app/src/test/java/net/awan/pipelines/ui/screens/PipelineLastRunTest.kt @@ -0,0 +1,123 @@ +package net.awan.pipelines.ui.screens + +import java.time.OffsetDateTime +import net.awan.pipelines.data.protocol.RunSummary +import net.awan.pipelines.ui.components.RunState +import org.junit.Assert.assertEquals +import org.junit.Assert.assertNull +import org.junit.Assert.assertTrue +import org.junit.Test + +class PipelineLastRunTest { + + private val now = OffsetDateTime.parse("2026-10-16T12:00:00Z") + + private fun run( + id: Long, + pipelineId: Long, + createdAt: String, + finishedAt: String = createdAt, + status: String = "succeeded", + ) = RunSummary( + id = id, + pipelineId = pipelineId, + createdAt = createdAt, + finishedAt = finishedAt, + status = status, + ) + + @Test + fun mostRecentRunWinsEvenWhenTheInputIsNotNewestFirst() { + // Deliberately unsorted: the newer start must win regardless of order. + val badges = pipelineRunBadges( + listOf( + run(7, 10, "2026-10-16T09:00:00Z"), + run(2, 10, "2026-10-16T11:00:00Z"), + run(5, 11, "2026-10-16T08:00:00Z"), + ), + now, + ) + assertEquals(2L, badges.getValue(10L).runId) + assertEquals(5L, badges.getValue(11L).runId) + } + + @Test + fun noRunsYieldsNoBadges() { + assertTrue(pipelineRunBadges(emptyList(), now).isEmpty()) + } + + @Test + fun unknownPipelineIdIsAbsentFromTheJoin() { + val badges = pipelineRunBadges(listOf(run(1, 10, "2026-10-16T10:00:00Z")), now) + assertEquals(1L, badges.getValue(10L).runId) + assertNull(badges[99L]) + } + + @Test + fun equalTimestampsBreakTheTieByTheHigherRunId() { + val badges = pipelineRunBadges( + listOf( + run(4, 10, "2026-10-16T10:00:00Z"), + run(9, 10, "2026-10-16T10:00:00Z"), + ), + now, + ) + assertEquals(9L, badges.getValue(10L).runId) + } + + @Test + fun unparseableTimestampsFallBackToRunIdWithoutCrashing() { + val badges = pipelineRunBadges( + listOf(run(3, 10, ""), run(8, 10, "not-a-date")), + now, + ) + assertEquals(8L, badges.getValue(10L).runId) + assertNull(badges.getValue(10L).ranAgo) + } + + @Test + fun unknownStatusWordYieldsAnUnknownStateNotACrash() { + val badge = pipelineRunBadges( + listOf(run(1, 10, "2026-10-16T10:00:00Z", status = "something-new")), + now, + ).getValue(10L) + assertEquals(RunState.Unknown, badge.state) + // The id still renders even when only the status word is unrecognised. + assertTrue(badge.metaLine().endsWith("run 1")) + } + + @Test + fun metaLineCarriesAgoDurationAndRunId() { + val badge = pipelineRunBadges( + listOf(run(42, 10, "2026-10-16T11:00:00Z", finishedAt = "2026-10-16T11:01:05Z")), + now, + ).getValue(10L) + assertEquals("1h ago · 1m 5s · run 42", badge.metaLine()) + } + + @Test + fun metaLineOmitsAMissingDuration() { + // A running run has no finished_at, so the duration fragment is dropped. + val badge = pipelineRunBadges( + listOf(run(42, 10, "2026-10-16T11:00:00Z", finishedAt = "", status = "running")), + now, + ).getValue(10L) + assertEquals("1h ago · run 42", badge.metaLine()) + } + + @Test + fun formatRanAgoCoversEachScaleAndRejectsBadInput() { + assertEquals("just now", formatRanAgo("2026-10-16T11:59:30Z", now)) + assertEquals("just now", formatRanAgo("2026-10-16T12:05:00Z", now)) + assertEquals("5m ago", formatRanAgo("2026-10-16T11:55:00Z", now)) + assertEquals("3h ago", formatRanAgo("2026-10-16T09:00:00Z", now)) + assertEquals("2d ago", formatRanAgo("2026-10-14T12:00:00Z", now)) + assertNull(formatRanAgo("", now)) + assertNull(formatRanAgo("not-a-date", now)) + } + + @Test + fun noRunsWordingIsTheExplicitConstant() { + assertEquals("no runs recorded", NO_RUNS_RECORDED) + } +}