From c11a509090d4620a4b57d7d9498e40990a7e6fa7 Mon Sep 17 00:00:00 2001 From: Willem Vanhulle Date: Wed, 21 Oct 2026 11:44:46 +0200 Subject: [PATCH] refactor: keep unit timestamps as one Activity --- src/boot_model.rs | 5 +--- src/boot_model/job_ingestion.rs | 35 +++++++--------------------- src/boot_model/reached_milestones.rs | 6 ++--- src/boot_model/unit_facts.rs | 11 +++++++++ src/boot_model/waiting_tree.rs | 2 +- 5 files changed, 25 insertions(+), 34 deletions(-) diff --git a/src/boot_model.rs b/src/boot_model.rs index a1a0ecb..9fc40a3 100644 --- a/src/boot_model.rs +++ b/src/boot_model.rs @@ -55,10 +55,7 @@ enum Progress { #[derive(Clone, Debug)] struct UnitRecord { facts: UnitFacts, - state: ActiveState, - inactive_exit: Option, - active_enter: Option, - active_exit: Option, + activity: Activity, progress: Progress, } diff --git a/src/boot_model/job_ingestion.rs b/src/boot_model/job_ingestion.rs index d779a69..4906ae5 100644 --- a/src/boot_model/job_ingestion.rs +++ b/src/boot_model/job_ingestion.rs @@ -7,25 +7,10 @@ use super::{ }; impl UnitRecord { - fn apply(&mut self, activity: Activity) { - if let Some(state) = activity.state { - self.state = state; - } - if activity.inactive_exit.is_some() { - self.inactive_exit = activity.inactive_exit; - } - if activity.active_enter.is_some() { - self.active_enter = activity.active_enter; - } - if activity.active_exit.is_some() { - self.active_exit = activity.active_exit; - } - } - fn transition_started(&self, kind: JobKind) -> Option { match kind { - JobKind::TakesDown => self.active_exit, - JobKind::BringsUp | JobKind::Unknown => self.inactive_exit, + JobKind::TakesDown => self.activity.active_exit, + JobKind::BringsUp | JobKind::Unknown => self.activity.inactive_exit, } } @@ -34,8 +19,8 @@ impl UnitRecord { return None; }; let transitioning = matches!( - self.state, - ActiveState::Activating | ActiveState::Deactivating | ActiveState::Reloading + self.activity.state, + Some(ActiveState::Activating | ActiveState::Deactivating | ActiveState::Reloading) ); transitioning.then(|| self.transition_started(job.kind).unwrap_or(job.queued_at)) } @@ -113,19 +98,16 @@ impl BootState { .entry(facts.name.clone()) .or_insert_with(|| UnitRecord { facts: facts.clone(), - state: ActiveState::Inactive, - inactive_exit: None, - active_enter: None, - active_exit: None, + activity: Activity::default(), progress: Progress::Untouched, }); record.facts = facts; - record.apply(activity); + record.activity = record.activity.merge(activity); } pub fn update_activity(&mut self, unit: &str, activity: Activity) { if let Some(record) = self.units.get_mut(unit) { - record.apply(activity); + record.activity = record.activity.merge(activity); } } @@ -234,7 +216,8 @@ impl BootState { .handoff_unit .as_deref() .and_then(|unit| self.units.get(unit)) - .is_some_and(|handoff| handoff.state != ActiveState::Inactive) + .and_then(|handoff| handoff.activity.state) + .is_some_and(|state| state != ActiveState::Inactive) { return true; } diff --git a/src/boot_model/reached_milestones.rs b/src/boot_model/reached_milestones.rs index 8bfafb9..39f5687 100644 --- a/src/boot_model/reached_milestones.rs +++ b/src/boot_model/reached_milestones.rs @@ -41,9 +41,9 @@ impl BootState { let record = self.units.get(target)?; match record.progress { Progress::Finished(Outcome::Succeeded { at }) => Some(at), - Progress::Untouched => record - .active_enter - .filter(|entered| record.state == ActiveState::Active && *entered >= self.origin), + Progress::Untouched => record.activity.active_enter.filter(|entered| { + record.activity.state == Some(ActiveState::Active) && *entered >= self.origin + }), Progress::Pending(_) | Progress::Finished(Outcome::Failed | Outcome::Abandoned) => None, } } diff --git a/src/boot_model/unit_facts.rs b/src/boot_model/unit_facts.rs index b791d46..2c0be91 100644 --- a/src/boot_model/unit_facts.rs +++ b/src/boot_model/unit_facts.rs @@ -64,6 +64,17 @@ pub struct Activity { pub active_exit: Option, } +impl Activity { + pub(super) fn merge(self, newer: Self) -> Self { + Self { + state: newer.state.or(self.state), + inactive_exit: newer.inactive_exit.or(self.inactive_exit), + active_enter: newer.active_enter.or(self.active_enter), + active_exit: newer.active_exit.or(self.active_exit), + } + } +} + #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum JobKind { BringsUp, diff --git a/src/boot_model/waiting_tree.rs b/src/boot_model/waiting_tree.rs index a7eab59..5a5c4b1 100644 --- a/src/boot_model/waiting_tree.rs +++ b/src/boot_model/waiting_tree.rs @@ -226,7 +226,7 @@ impl BootState { .running_since() .map_or(SiblingRank::Waiting, SiblingRank::RunningSince), Progress::Finished(Outcome::Succeeded { at }) => { - SiblingRank::RunningSince(record.inactive_exit.unwrap_or(at)) + SiblingRank::RunningSince(record.activity.inactive_exit.unwrap_or(at)) } Progress::Finished(Outcome::Failed) => SiblingRank::Failed, Progress::Finished(Outcome::Abandoned) | Progress::Untouched => {