Skip to main content

Stat

Struct Stat 

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

One number that matters, with a label above it and a note below. With room for it, the number is drawn large.

use tinsel::app::{Frame, Stat};
use tinsel::palette;

tinsel::set_color_mode(tinsel::ColorMode::None);
let mut frame = Frame::new(16, 3);
frame.render(frame.area(), Stat::new("uptime", "99.9%").note(palette::SAGE, "30 days"));
assert_eq!(frame.snapshot().plain(), "uptime\n99.9%\n30 days");

Implementations§

Source§

impl Stat

Source

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

A stat called label, showing value.

Examples found in repository?
examples/dashboard.rs (line 147)
139    fn overview(&self, frame: &mut Frame, area: app::Rect) {
140        let [stats, body] = area.rows_gap(0, [Fixed(7), Fill(1)]);
141        let cells = stats.columns_gap(1, [Fill(1), Fill(1), Fill(1), Fill(1)]);
142        let total: f64 = self.services.iter().map(|s| 12_000.0 / self.latest(s).max(1.0)).sum();
143        let p95 = self.latest(&self.services[self.selected]);
144        let slow = self.services.iter().filter(|s| self.slow(s)).count();
145        let secs = frame.time() as u64;
146        let stat = |label: &str, value: String, note: &str, color| {
147            Panel::new("").body(Stat::new(label, value).note(color, note))
148        };
149        frame.render(
150            cells[0],
151            stat(
152                "requests / s",
153                format!("{:.1}k", total / 1000.0),
154                "▲ 12% today",
155                palette::SAGE,
156            ),
157        );
158        frame.render(
159            cells[1],
160            stat("p95 latency", format!("{p95:.0}"), "milliseconds", palette::SOFT),
161        );
162        frame.render(
163            cells[2],
164            stat(
165                "running slow",
166                slow.to_string(),
167                if slow == 0 { "all calm ✿" } else { "keep an eye out" },
168                if slow == 0 { palette::SAGE } else { palette::AMBER },
169            ),
170        );
171        frame.render(
172            cells[3],
173            stat(
174                "watching for",
175                format!("{:02}:{:02}", secs / 60, secs % 60),
176                "minutes : seconds",
177                palette::SOFT,
178            ),
179        );
180
181        let [side, main] = body.columns_gap(1, [Fixed(26), Fill(1)]);
182        let names = self.services.iter().map(|s| format!("{} {}", self.dot(s), s.name));
183        frame.render(
184            side,
185            Panel::new("services")
186                .body(List::new(names.collect::<Vec<_>>()).selected(self.selected))
187                .footer(format!("{}/{}", self.selected + 1, self.services.len()))
188                .focused(true),
189        );
190
191        let [chart, details] = main.rows([Fill(3), Fixed(8)]);
192        let s = &self.services[self.selected];
193        frame.render(
194            chart,
195            Panel::new(format!(
196                "{} {}",
197                self.dot(s),
198                paint(palette::TEXT, &format!("{} latency", s.name))
199            ))
200            .status(format!("last {} readings", s.latency.len()))
201            .body(Plot::new(&s.latency))
202            .footer(format!("now {:.0}ms", self.latest(s))),
203        );
204
205        let [gauges, table] = details.columns_gap(1, [Fill(1), Fill(1)]);
206        let wobble = |seed: f64| 50.0 + 30.0 * (frame.time() / 3.0 + seed).sin();
207        let lines: tinsel::Block = [("cpu   ", wobble(0.0)), ("memory", wobble(2.0)), ("disk  ", 88.0)]
208            .iter()
209            .map(|(label, v)| meter(label, *v, 100.0, 14))
210            .collect();
211        frame.render(gauges, Panel::new("resources").body(lines).padding(1, 1));
212        let regions = Table::new(["region", "pods"])
213            .row(["eu-west-2", "12"])
214            .row(["us-east-1", "8"])
215            .row(["ap-south-1", "3"]);
216        frame.render(table, Panel::new("regions").status("23 pods").body(regions));
217    }
Source

pub fn note(self, color: Rgb, text: impl Into<String>) -> Self

A line under the number, in a colour: ▲ 12% in sage, 3 failing in red.

Examples found in repository?
examples/dashboard.rs (line 147)
139    fn overview(&self, frame: &mut Frame, area: app::Rect) {
140        let [stats, body] = area.rows_gap(0, [Fixed(7), Fill(1)]);
141        let cells = stats.columns_gap(1, [Fill(1), Fill(1), Fill(1), Fill(1)]);
142        let total: f64 = self.services.iter().map(|s| 12_000.0 / self.latest(s).max(1.0)).sum();
143        let p95 = self.latest(&self.services[self.selected]);
144        let slow = self.services.iter().filter(|s| self.slow(s)).count();
145        let secs = frame.time() as u64;
146        let stat = |label: &str, value: String, note: &str, color| {
147            Panel::new("").body(Stat::new(label, value).note(color, note))
148        };
149        frame.render(
150            cells[0],
151            stat(
152                "requests / s",
153                format!("{:.1}k", total / 1000.0),
154                "▲ 12% today",
155                palette::SAGE,
156            ),
157        );
158        frame.render(
159            cells[1],
160            stat("p95 latency", format!("{p95:.0}"), "milliseconds", palette::SOFT),
161        );
162        frame.render(
163            cells[2],
164            stat(
165                "running slow",
166                slow.to_string(),
167                if slow == 0 { "all calm ✿" } else { "keep an eye out" },
168                if slow == 0 { palette::SAGE } else { palette::AMBER },
169            ),
170        );
171        frame.render(
172            cells[3],
173            stat(
174                "watching for",
175                format!("{:02}:{:02}", secs / 60, secs % 60),
176                "minutes : seconds",
177                palette::SOFT,
178            ),
179        );
180
181        let [side, main] = body.columns_gap(1, [Fixed(26), Fill(1)]);
182        let names = self.services.iter().map(|s| format!("{} {}", self.dot(s), s.name));
183        frame.render(
184            side,
185            Panel::new("services")
186                .body(List::new(names.collect::<Vec<_>>()).selected(self.selected))
187                .footer(format!("{}/{}", self.selected + 1, self.services.len()))
188                .focused(true),
189        );
190
191        let [chart, details] = main.rows([Fill(3), Fixed(8)]);
192        let s = &self.services[self.selected];
193        frame.render(
194            chart,
195            Panel::new(format!(
196                "{} {}",
197                self.dot(s),
198                paint(palette::TEXT, &format!("{} latency", s.name))
199            ))
200            .status(format!("last {} readings", s.latency.len()))
201            .body(Plot::new(&s.latency))
202            .footer(format!("now {:.0}ms", self.latest(s))),
203        );
204
205        let [gauges, table] = details.columns_gap(1, [Fill(1), Fill(1)]);
206        let wobble = |seed: f64| 50.0 + 30.0 * (frame.time() / 3.0 + seed).sin();
207        let lines: tinsel::Block = [("cpu   ", wobble(0.0)), ("memory", wobble(2.0)), ("disk  ", 88.0)]
208            .iter()
209            .map(|(label, v)| meter(label, *v, 100.0, 14))
210            .collect();
211        frame.render(gauges, Panel::new("resources").body(lines).padding(1, 1));
212        let regions = Table::new(["region", "pods"])
213            .row(["eu-west-2", "12"])
214            .row(["us-east-1", "8"])
215            .row(["ap-south-1", "3"]);
216        frame.render(table, Panel::new("regions").status("23 pods").body(regions));
217    }

Trait Implementations§

Source§

impl Clone for Stat

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Stat

Source§

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

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

impl Widget for Stat

Source§

fn render(&self, area: Rect, frame: &mut Frame)

Draw into area of frame. area is already clipped to the screen. A widget that moves asks for the frames it needs, with Frame::animate or Frame::animate_at, so whoever draws it needn’t.
Source§

fn size(&self) -> Option<(usize, usize)>

How many columns and rows it would like, as (width, height) like every size in tinsel, if it knows: what a Modal is sized to. None unless implemented. Read more
Source§

fn inner(&self, area: Rect) -> Rect

Where what it holds goes when it’s drawn at area: inside a Panel’s border and padding, say. The whole area unless implemented; Frame::modal gives it back, so what’s in a modal can be drawn there. Read more

Auto Trait Implementations§

§

impl Forget for Stat

§

impl Freeze for Stat

§

impl Move for Stat

§

impl RefUnwindSafe for Stat

§

impl Send for Stat

§

impl Sync for Stat

§

impl Unpin for Stat

§

impl UnsafeUnpin for Stat

§

impl UnwindSafe for Stat

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> 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.