pub struct AlertPanelState { /* private fields */ }Expand description
State for the AlertPanel component.
Contains the metrics, layout configuration, and navigation state.
§Example
use envision::component::{
AlertPanelState, AlertMetric, AlertThreshold,
};
let state = AlertPanelState::new()
.with_metrics(vec![
AlertMetric::new("cpu", "CPU", AlertThreshold::new(70.0, 90.0))
.with_value(45.0),
])
.with_columns(2)
.with_title("Alerts");
assert_eq!(state.metrics().len(), 1);
assert_eq!(state.ok_count(), 1);Implementations§
Source§impl AlertPanelState
impl AlertPanelState
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty alert panel state.
§Example
use envision::component::AlertPanelState;
let state = AlertPanelState::new();
assert!(state.metrics().is_empty());
assert_eq!(state.columns(), 2);Sourcepub fn with_metrics(self, metrics: Vec<AlertMetric>) -> Self
pub fn with_metrics(self, metrics: Vec<AlertMetric>) -> Self
Sets the initial metrics (builder pattern).
§Example
use envision::component::{AlertPanelState, AlertMetric, AlertThreshold};
let state = AlertPanelState::new().with_metrics(vec![
AlertMetric::new("cpu", "CPU", AlertThreshold::new(70.0, 90.0)),
]);
assert_eq!(state.metrics().len(), 1);Sourcepub fn with_columns(self, columns: usize) -> Self
pub fn with_columns(self, columns: usize) -> Self
Sets the number of grid columns (builder pattern).
§Example
use envision::component::AlertPanelState;
let state = AlertPanelState::new().with_columns(3);
assert_eq!(state.columns(), 3);Sourcepub fn with_title(self, title: impl Into<String>) -> Self
pub fn with_title(self, title: impl Into<String>) -> Self
Sets the title (builder pattern).
§Example
use envision::component::AlertPanelState;
let state = AlertPanelState::new().with_title("System Alerts");
assert_eq!(state.title(), Some("System Alerts"));Sourcepub fn with_show_sparklines(self, show: bool) -> Self
pub fn with_show_sparklines(self, show: bool) -> Self
Sets whether to show sparklines (builder pattern).
§Example
use envision::component::AlertPanelState;
let state = AlertPanelState::new().with_show_sparklines(false);
assert!(!state.show_sparklines());Sourcepub fn with_show_thresholds(self, show: bool) -> Self
pub fn with_show_thresholds(self, show: bool) -> Self
Sets whether to show threshold values (builder pattern).
§Example
use envision::component::AlertPanelState;
let state = AlertPanelState::new().with_show_thresholds(true);
assert!(state.show_thresholds());Sourcepub fn metrics(&self) -> &[AlertMetric]
pub fn metrics(&self) -> &[AlertMetric]
Returns the metrics.
§Example
use envision::component::{AlertPanelState, AlertMetric, AlertThreshold};
let state = AlertPanelState::new().with_metrics(vec![
AlertMetric::new("cpu", "CPU", AlertThreshold::new(70.0, 90.0)),
]);
assert_eq!(state.metrics().len(), 1);Sourcepub fn metrics_mut(&mut self) -> &mut Vec<AlertMetric>
pub fn metrics_mut(&mut self) -> &mut Vec<AlertMetric>
Returns a mutable reference to the alert metrics.
This is safe because metrics are simple data containers. Selection state is index-based and unaffected by value mutation.
§Example
use envision::component::{AlertPanelState, AlertMetric, AlertThreshold};
let mut state = AlertPanelState::new().with_metrics(vec![
AlertMetric::new("cpu", "CPU", AlertThreshold::new(70.0, 90.0))
.with_value(50.0),
]);
state.metrics_mut()[0].update_value(85.0);
assert_eq!(state.metrics()[0].value(), 85.0);Sourcepub fn columns(&self) -> usize
pub fn columns(&self) -> usize
Returns the number of grid columns.
§Example
use envision::component::AlertPanelState;
let state = AlertPanelState::new().with_columns(4);
assert_eq!(state.columns(), 4);Sourcepub fn selected(&self) -> Option<usize>
pub fn selected(&self) -> Option<usize>
Returns the selected metric index.
§Example
use envision::component::{AlertPanelState, AlertMetric, AlertThreshold};
let state = AlertPanelState::new().with_metrics(vec![
AlertMetric::new("cpu", "CPU", AlertThreshold::new(70.0, 90.0)),
]);
assert_eq!(state.selected(), Some(0));Sourcepub fn title(&self) -> Option<&str>
pub fn title(&self) -> Option<&str>
Returns the title.
§Example
use envision::component::AlertPanelState;
let state = AlertPanelState::new().with_title("Alerts");
assert_eq!(state.title(), Some("Alerts"));Sourcepub fn set_title(&mut self, title: impl Into<String>)
pub fn set_title(&mut self, title: impl Into<String>)
Sets the title.
§Example
use envision::component::AlertPanelState;
let mut state = AlertPanelState::new();
state.set_title("System Alerts");
assert_eq!(state.title(), Some("System Alerts"));Sourcepub fn show_sparklines(&self) -> bool
pub fn show_sparklines(&self) -> bool
Returns whether sparklines are shown.
§Example
use envision::component::AlertPanelState;
let state = AlertPanelState::new();
assert!(state.show_sparklines());Sourcepub fn show_thresholds(&self) -> bool
pub fn show_thresholds(&self) -> bool
Returns whether threshold values are shown.
§Example
use envision::component::AlertPanelState;
let state = AlertPanelState::new();
assert!(!state.show_thresholds());Sourcepub fn set_show_sparklines(&mut self, show: bool)
pub fn set_show_sparklines(&mut self, show: bool)
Sets whether sparklines are shown.
§Example
use envision::component::AlertPanelState;
let mut state = AlertPanelState::new();
state.set_show_sparklines(false);
assert!(!state.show_sparklines());Sourcepub fn set_show_thresholds(&mut self, show: bool)
pub fn set_show_thresholds(&mut self, show: bool)
Sets whether threshold values are shown.
§Example
use envision::component::AlertPanelState;
let mut state = AlertPanelState::new();
state.set_show_thresholds(true);
assert!(state.show_thresholds());Sourcepub fn add_metric(&mut self, metric: AlertMetric)
pub fn add_metric(&mut self, metric: AlertMetric)
Adds a metric to the panel.
§Example
use envision::component::{AlertPanelState, AlertMetric, AlertThreshold};
let mut state = AlertPanelState::new();
state.add_metric(
AlertMetric::new("cpu", "CPU", AlertThreshold::new(70.0, 90.0))
);
assert_eq!(state.metrics().len(), 1);
assert_eq!(state.selected(), Some(0));Sourcepub fn update_metric(
&mut self,
id: &str,
value: f64,
) -> Option<(AlertState, AlertState)>
pub fn update_metric( &mut self, id: &str, value: f64, ) -> Option<(AlertState, AlertState)>
Updates a metric’s value by id.
Returns Some((old_state, new_state)) if the alert state changed,
or None if the state did not change or the metric was not found.
§Example
use envision::component::{AlertPanelState, AlertMetric, AlertThreshold, AlertState};
let mut state = AlertPanelState::new().with_metrics(vec![
AlertMetric::new("cpu", "CPU", AlertThreshold::new(70.0, 90.0))
.with_value(50.0),
]);
let result = state.update_metric("cpu", 80.0);
assert_eq!(result, Some((AlertState::Ok, AlertState::Warning)));Sourcepub fn metric_by_id(&self, id: &str) -> Option<&AlertMetric>
pub fn metric_by_id(&self, id: &str) -> Option<&AlertMetric>
Returns a reference to a metric by id.
§Example
use envision::component::{AlertPanelState, AlertMetric, AlertThreshold};
let state = AlertPanelState::new().with_metrics(vec![
AlertMetric::new("cpu", "CPU", AlertThreshold::new(70.0, 90.0)),
]);
assert!(state.metric_by_id("cpu").is_some());
assert!(state.metric_by_id("unknown").is_none());Sourcepub fn ok_count(&self) -> usize
pub fn ok_count(&self) -> usize
Returns the count of metrics in OK state.
§Example
use envision::component::{AlertPanelState, AlertMetric, AlertThreshold};
let state = AlertPanelState::new().with_metrics(vec![
AlertMetric::new("cpu", "CPU", AlertThreshold::new(70.0, 90.0))
.with_value(50.0),
AlertMetric::new("mem", "Memory", AlertThreshold::new(80.0, 95.0))
.with_value(30.0),
]);
assert_eq!(state.ok_count(), 2);Sourcepub fn warning_count(&self) -> usize
pub fn warning_count(&self) -> usize
Returns the count of metrics in Warning state.
§Example
use envision::component::{AlertPanelState, AlertMetric, AlertThreshold};
let state = AlertPanelState::new().with_metrics(vec![
AlertMetric::new("cpu", "CPU", AlertThreshold::new(70.0, 90.0))
.with_value(80.0),
]);
assert_eq!(state.warning_count(), 1);Sourcepub fn critical_count(&self) -> usize
pub fn critical_count(&self) -> usize
Returns the count of metrics in Critical state.
§Example
use envision::component::{AlertPanelState, AlertMetric, AlertThreshold};
let state = AlertPanelState::new().with_metrics(vec![
AlertMetric::new("cpu", "CPU", AlertThreshold::new(70.0, 90.0))
.with_value(95.0),
]);
assert_eq!(state.critical_count(), 1);Sourcepub fn unknown_count(&self) -> usize
pub fn unknown_count(&self) -> usize
Returns the count of metrics in Unknown state.
§Example
use envision::component::AlertPanelState;
let state = AlertPanelState::new();
assert_eq!(state.unknown_count(), 0);Sourcepub fn selected_metric(&self) -> Option<&AlertMetric>
pub fn selected_metric(&self) -> Option<&AlertMetric>
Returns a reference to the currently selected metric.
§Example
use envision::component::{AlertPanelState, AlertMetric, AlertThreshold};
let state = AlertPanelState::new().with_metrics(vec![
AlertMetric::new("cpu", "CPU", AlertThreshold::new(70.0, 90.0)),
]);
assert_eq!(state.selected_metric().unwrap().id(), "cpu");Sourcepub fn rows(&self) -> usize
pub fn rows(&self) -> usize
Returns the number of rows in the grid.
The row count is derived from the number of metrics and the configured column count (rounded up so partial rows still count).
§Examples
use envision::component::{AlertMetric, AlertPanelState, AlertThreshold};
let state = AlertPanelState::new()
.with_metrics(vec![
AlertMetric::new("a", "A", AlertThreshold::new(70.0, 90.0)),
AlertMetric::new("b", "B", AlertThreshold::new(70.0, 90.0)),
AlertMetric::new("c", "C", AlertThreshold::new(70.0, 90.0)),
])
.with_columns(2);
assert_eq!(state.rows(), 2);Sourcepub fn update(&mut self, msg: AlertPanelMessage) -> Option<AlertPanelOutput>
pub fn update(&mut self, msg: AlertPanelMessage) -> Option<AlertPanelOutput>
Updates the state with a message, returning any output.
§Example
use envision::component::{
AlertPanelState, AlertPanelMessage, AlertPanelOutput,
AlertMetric, AlertThreshold,
};
let mut state = AlertPanelState::new().with_metrics(vec![
AlertMetric::new("cpu", "CPU", AlertThreshold::new(70.0, 90.0)),
]);
let output = state.update(AlertPanelMessage::Select);
assert_eq!(output, Some(AlertPanelOutput::MetricSelected("cpu".into())));Trait Implementations§
Source§impl Clone for AlertPanelState
impl Clone for AlertPanelState
Source§fn clone(&self) -> AlertPanelState
fn clone(&self) -> AlertPanelState
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for AlertPanelState
impl Debug for AlertPanelState
Source§impl Default for AlertPanelState
impl Default for AlertPanelState
Source§impl<'de> Deserialize<'de> for AlertPanelState
impl<'de> Deserialize<'de> for AlertPanelState
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl PartialEq for AlertPanelState
impl PartialEq for AlertPanelState
Source§impl Serialize for AlertPanelState
impl Serialize for AlertPanelState
impl StructuralPartialEq for AlertPanelState
Auto Trait Implementations§
impl Freeze for AlertPanelState
impl RefUnwindSafe for AlertPanelState
impl Send for AlertPanelState
impl Sync for AlertPanelState
impl Unpin for AlertPanelState
impl UnsafeUnpin for AlertPanelState
impl UnwindSafe for AlertPanelState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more