Skip to main content

AlertPanelState

Struct AlertPanelState 

Source
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

Source

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);
Source

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);
Source

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);
Source

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"));
Source

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());
Source

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());
Source

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);
Source

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);
Source

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);
Source

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));
Source

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"));
Source

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"));
Source

pub fn show_sparklines(&self) -> bool

Returns whether sparklines are shown.

§Example
use envision::component::AlertPanelState;

let state = AlertPanelState::new();
assert!(state.show_sparklines());
Source

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());
Source

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());
Source

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());
Source

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));
Source

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)));
Source

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());
Source

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);
Source

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);
Source

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);
Source

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);
Source

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");
Source

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);
Source

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

Source§

fn clone(&self) -> AlertPanelState

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for AlertPanelState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for AlertPanelState

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for AlertPanelState

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for AlertPanelState

Source§

fn eq(&self, other: &AlertPanelState) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for AlertPanelState

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for AlertPanelState

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> StateExt for T

Source§

fn updated(self, cmd: Command<impl Clone>) -> UpdateResult<Self, impl Clone>

Updates self and returns a command.
Source§

fn unchanged(self) -> UpdateResult<Self, ()>

Returns self with no command.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,