diff --git a/compliance-dashboard/src/components/scan_runs.rs b/compliance-dashboard/src/components/scan_runs.rs index de5b74e..378f8dc 100644 --- a/compliance-dashboard/src/components/scan_runs.rs +++ b/compliance-dashboard/src/components/scan_runs.rs @@ -33,18 +33,30 @@ const EXPECT_RUN_POLLS: u32 = 12; /// `started` counts the scans started on this page: each one makes the list /// look again until the new run shows up, even when every run before it /// had finished. +/// +/// `on_finished` is called when a run seen running here has finished, so the +/// page can reload what the scan changed (findings, SBOM, CRA status). #[component] -pub fn ScanRunList(target_id: String, started: Option>) -> Element { +pub fn ScanRunList( + target_id: String, + started: Option>, + on_finished: Option>, +) -> Element { let lang = use_lang()(); let id = use_signal(|| target_id.clone()); // A scan was started here and its run has not been seen running yet. let mut expect_run = use_signal(|| false); + // How many of the `started` scans were already expected: a poll reruns + // the resource, and must not expect the same scan again. + let mut expected_starts = use_signal(|| 0u32); // Only the browser build restarts it (the polling below). #[cfg_attr(not(feature = "web"), allow(unused_mut))] let mut runs = use_resource(move || async move { // Reading `started` reruns this as soon as a scan is started. if let Some(started) = started { - if started() > 0 { + let count = started(); + if count > *expected_starts.peek() { + expected_starts.set(count); expect_run.set(true); } } @@ -58,13 +70,16 @@ pub fn ScanRunList(target_id: String, started: Option>) -> Element { let Some(Some(list)) = runs.read().clone() else { return; }; - for run in &list { - let id = run.id.map(|id| id.to_hex()).unwrap_or_default(); - if run.status == ScanRunStatus::Running { - expect_run.set(false); - seen_running.write().insert(id); - } else if seen_running.peek().contains(&id) && !finished_here.peek().contains(&id) { - finished_here.write().insert(id); + let (running, finished) = + run_transitions(&list, &seen_running.peek(), &finished_here.peek()); + if !running.is_empty() { + expect_run.set(false); + seen_running.write().extend(running); + } + if !finished.is_empty() { + finished_here.write().extend(finished); + if let Some(on_finished) = on_finished { + on_finished.call(()); } } }); @@ -134,6 +149,26 @@ pub fn ScanRunList(target_id: String, started: Option>) -> Element { } } +/// What one look at the history changed: the ids of the runs now running, +/// and of those seen running before that have finished since (each only once). +fn run_transitions( + runs: &[ScanRun], + seen_running: &HashSet, + finished_here: &HashSet, +) -> (Vec, Vec) { + let mut running = Vec::new(); + let mut finished = Vec::new(); + for run in runs { + let id = run.id.map(|id| id.to_hex()).unwrap_or_default(); + if run.status == ScanRunStatus::Running { + running.push(id); + } else if seen_running.contains(&id) && !finished_here.contains(&id) { + finished.push(id); + } + } + (running, finished) +} + /// One run: a status line, then its error and skipped stages, if any. fn scan_run_card(run: &ScanRun, lang: Lang) -> Element { let (badge, status) = status_badge(&run.status); @@ -457,6 +492,28 @@ pub fn phase(phase: &ScanPhase) -> Text { mod tests { use super::*; + #[test] + fn a_run_seen_running_counts_as_finished_once() { + let mut run = ScanRun::new("t".into(), compliance_core::models::ScanTrigger::Manual); + // A run without an id is keyed by the empty string. + let id = String::new(); + let none = HashSet::new(); + + let (running, finished) = run_transitions(std::slice::from_ref(&run), &none, &none); + assert_eq!((running, finished), (vec![id.clone()], vec![])); + + run.status = ScanRunStatus::Completed; + let seen = HashSet::from([id.clone()]); + let (running, finished) = run_transitions(std::slice::from_ref(&run), &seen, &none); + assert_eq!((running, finished), (vec![], vec![id.clone()])); + + // Already reported, or never seen running (an old run): nothing new. + let (_, finished) = run_transitions(std::slice::from_ref(&run), &seen, &seen); + assert!(finished.is_empty()); + let (_, finished) = run_transitions(std::slice::from_ref(&run), &none, &none); + assert!(finished.is_empty()); + } + #[test] fn the_stepper_marks_done_current_and_pending_phases() { let mut run = ScanRun::new("t".into(), compliance_core::models::ScanTrigger::Manual); diff --git a/compliance-dashboard/src/pages/target.rs b/compliance-dashboard/src/pages/target.rs index e9512cc..72b97ac 100644 --- a/compliance-dashboard/src/pages/target.rs +++ b/compliance-dashboard/src/pages/target.rs @@ -277,7 +277,17 @@ pub fn TargetPage(id: String) -> Element { div { class: "target-columns", section { class: "card", div { class: "target-card-head", h3 { {tx::SCAN_RUNS.get(lang)} } } - div { class: "target-card-body", ScanRunList { target_id: id.clone(), started: scans_started } } + div { class: "target-card-body", ScanRunList { + target_id: id.clone(), + started: scans_started, + // A finished scan changed the findings, the SBOM and + // the CRA status: load them again. + on_finished: move |_| { + status.restart(); + open_findings.restart(); + target.restart(); + }, + } } } section { class: "card", div { class: "target-card-head", h3 { {tx::SCOPE.get(lang)} } }