Skip to main content

MetricWidget

Struct MetricWidget 

Source
pub struct MetricWidget { /* private fields */ }
Expand description

A single metric widget in the dashboard.

Implementations§

Source§

impl MetricWidget

Source

pub fn counter(label: impl Into<String>, value: i64) -> Self

Creates a counter widget.

§Example
use envision::component::MetricWidget;

let widget = MetricWidget::counter("Requests", 42);
assert_eq!(widget.label(), "Requests");
assert_eq!(widget.display_value(), "42");
Source

pub fn gauge(label: impl Into<String>, value: u64, max: u64) -> Self

Creates a gauge widget with a maximum value.

§Example
use envision::component::MetricWidget;

let widget = MetricWidget::gauge("CPU %", 75, 100);
assert_eq!(widget.display_value(), "75/100");
Source

pub fn status(label: impl Into<String>, up: bool) -> Self

Creates a status indicator widget.

§Example
use envision::component::MetricWidget;

let widget = MetricWidget::status("API", true);
assert_eq!(widget.label(), "API");
assert_eq!(widget.display_value(), "UP");
Source

pub fn text(label: impl Into<String>, text: impl Into<String>) -> Self

Creates a text metric widget.

§Example
use envision::component::MetricWidget;

let widget = MetricWidget::text("Version", "1.2.3");
assert_eq!(widget.label(), "Version");
assert_eq!(widget.display_value(), "1.2.3");
Source

pub fn with_max_history(self, max: usize) -> Self

Sets the maximum history length for sparkline display (builder pattern).

§Example
use envision::component::MetricWidget;

let widget = MetricWidget::counter("Ops", 0).with_max_history(50);
assert_eq!(widget.history().len(), 0); // no values yet
Source

pub fn label(&self) -> &str

Returns the label.

§Example
use envision::component::MetricWidget;

let widget = MetricWidget::counter("Requests", 0);
assert_eq!(widget.label(), "Requests");
Source

pub fn kind(&self) -> &MetricKind

Returns the metric kind.

§Example
use envision::component::{MetricWidget, MetricKind};

let widget = MetricWidget::status("DB", false);
assert!(matches!(widget.kind(), MetricKind::Status { up: false }));
Source

pub fn history(&self) -> &[u64]

Returns the sparkline history.

§Example
use envision::component::MetricWidget;

let widget = MetricWidget::counter("Ops", 0);
assert!(widget.history().is_empty());
Source

pub fn display_value(&self) -> String

Returns the display value as a string.

§Example
use envision::component::MetricWidget;

assert_eq!(MetricWidget::counter("A", 42).display_value(), "42");
assert_eq!(MetricWidget::gauge("B", 75, 100).display_value(), "75/100");
assert_eq!(MetricWidget::status("C", true).display_value(), "UP");
assert_eq!(MetricWidget::status("D", false).display_value(), "DOWN");
assert_eq!(MetricWidget::text("E", "ok").display_value(), "ok");
Source

pub fn set_counter_value(&mut self, value: i64)

Sets the counter value.

§Example
use envision::component::MetricWidget;

let mut widget = MetricWidget::counter("Requests", 0);
widget.set_counter_value(100);
assert_eq!(widget.display_value(), "100");
Source

pub fn set_gauge_value(&mut self, value: u64)

Sets the gauge value.

§Example
use envision::component::MetricWidget;

let mut widget = MetricWidget::gauge("Memory", 0, 1024);
widget.set_gauge_value(512);
assert_eq!(widget.display_value(), "512/1024");
Source

pub fn set_status(&mut self, up: bool)

Sets the status.

§Example
use envision::component::MetricWidget;

let mut widget = MetricWidget::status("API", true);
widget.set_status(false);
assert_eq!(widget.display_value(), "DOWN");
Source

pub fn set_text(&mut self, text: impl Into<String>)

Sets the text value.

§Example
use envision::component::MetricWidget;

let mut widget = MetricWidget::text("Version", "1.0");
widget.set_text("2.0");
assert_eq!(widget.display_value(), "2.0");
Source

pub fn increment(&mut self, amount: i64)

Increments a counter by the given amount.

§Example
use envision::component::MetricWidget;

let mut widget = MetricWidget::counter("Hits", 10);
widget.increment(5);
assert_eq!(widget.display_value(), "15");
Source

pub fn gauge_percentage(&self) -> Option<f64>

Returns the gauge fill percentage (0.0 to 1.0).

Returns None for non-gauge widgets.

§Example
use envision::component::MetricWidget;

let widget = MetricWidget::gauge("CPU", 75, 100);
assert_eq!(widget.gauge_percentage(), Some(0.75));

let counter = MetricWidget::counter("Ops", 10);
assert_eq!(counter.gauge_percentage(), None);

Trait Implementations§

Source§

impl Clone for MetricWidget

Source§

fn clone(&self) -> MetricWidget

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 MetricWidget

Source§

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

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

impl<'de> Deserialize<'de> for MetricWidget

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 MetricWidget

Source§

fn eq(&self, other: &MetricWidget) -> 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 MetricWidget

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 MetricWidget

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>,