frequenz_microgrid/microgrid/telemetry_tracker/component_partition.rs
1// License: MIT
2// Copyright © 2026 Frequenz Energy-as-a-Service GmbH
3
4//! A set of components partitioned by health status.
5
6use std::collections::HashMap;
7
8use crate::client::proto::common::microgrid::electrical_components::ElectricalComponentTelemetry;
9
10/// A set of components partitioned by health status and annotated with the
11/// latest telemetry sample for each.
12///
13/// `healthy` holds the most recent [`ElectricalComponentTelemetry`] observed
14/// for each healthy component. `unhealthy` holds the last telemetry observed
15/// before the component became unhealthy, or `None` if no sample has been
16/// received yet. Consumers can use the telemetry (including per-metric bounds)
17/// directly without subscribing to the raw streams again.
18#[derive(Clone, Debug, Default, PartialEq)]
19pub struct ComponentHealthPartition {
20 pub healthy: HashMap<u64, ElectricalComponentTelemetry>,
21 pub unhealthy: HashMap<u64, Option<ElectricalComponentTelemetry>>,
22}
23
24impl ComponentHealthPartition {
25 /// Records `data` as the latest telemetry for the now-healthy component
26 /// `id`, removing it from the unhealthy set.
27 pub(crate) fn mark_healthy(&mut self, id: u64, data: ElectricalComponentTelemetry) {
28 self.healthy.insert(id, data);
29 self.unhealthy.remove(&id);
30 }
31
32 /// Records component `id` as unhealthy, carrying its last telemetry sample
33 /// if any, and removing it from the healthy set.
34 pub(crate) fn mark_unhealthy(&mut self, id: u64, data: Option<ElectricalComponentTelemetry>) {
35 self.unhealthy.insert(id, data);
36 self.healthy.remove(&id);
37 }
38}