fix(dashboard): target page reloads findings, SBOM and CRA status when a scan finishes (#530)
This commit was merged in pull request #530.
This commit is contained in:
1 parent
0586f2c639
commit
10cebd894f
3 files changed
+81
-10
No files matched your search
@@ -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<Signal<u32>>) -> Element {
|
||||
pub fn ScanRunList(
|
||||
target_id: String,
|
||||
started: Option<Signal<u32>>,
|
||||
on_finished: Option<EventHandler<()>>,
|
||||
) -> 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<Signal<u32>>) -> 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<Signal<u32>>) -> 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<String>,
|
||||
finished_here: &HashSet<String>,
|
||||
) -> (Vec<String>, Vec<String>) {
|
||||
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);
|
||||
|
||||
@@ -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)} } }
|
||||
|
||||
@@ -116,6 +116,10 @@ first. Each run has one of these statuses:
|
||||
| **Fehlgeschlagen** (Failed) | The scan stopped because of an error; the reason is shown under **Fehler**. |
|
||||
| **Unterbrochen** (Interrupted) | The service was restarted (for example by an update) while the scan was running. Nothing in your code caused it; start the scan again. |
|
||||
|
||||
While a scan runs, the target's page follows it without a reload; when the run
|
||||
finishes, the **CRA-Stand**, **Stückliste** and **Offene Befunde** cards update
|
||||
to its results.
|
||||
|
||||
The **Prüfobjekte** list shows each target's last scan, and the step a running
|
||||
scan is at.
|
||||
|
||||
|
||||
Reference in new issue
Block a user