#![windows_subsystem = "windows"]
use std::time::{Duration, Instant};
use taut::{Align, Chart, Column, Length, Series, Table, Ui, Window};
const PARTS: [(&str, f32); 5] = [("/search", 0.38), ("/pages", 0.27), ("/login", 0.14), ("/upload", 0.12), ("/reports", 0.09)];
const SPANS: [&str; 2] = ["Last minute", "Last 5 minutes"];
const KEPT: usize = 300;
#[derive(Clone, Copy)]
pub struct Second {
requests: f32,
answer: f32,
failed: f32,
}
pub struct Dashboard {
seconds: Vec<Second>,
span: usize,
paused: bool,
table: Table,
now: Duration,
next: Duration,
count: u64,
}
impl Default for Dashboard {
fn default() -> Self {
Dashboard { seconds: Vec::new(), span: 0, paused: false, table: Table::default(), now: Duration::ZERO, next: Duration::from_secs(1), count: 0 }
}
}
impl Dashboard {
fn take_in(&mut self) {
while self.next <= self.now {
if !self.paused {
self.seconds.push(made_up(self.count));
self.count += 1;
}
self.next += Duration::from_secs(1);
}
let over = self.seconds.len().saturating_sub(KEPT);
self.seconds.drain(..over);
}
fn shown(&self) -> &[Second] {
let span = [60, KEPT][self.span];
&self.seconds[self.seconds.len().saturating_sub(span)..]
}
}
fn made_up(nth: u64) -> Second {
let at = nth as f32;
let noise = |salt: u64| ((nth.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(salt) >> 33) % 1000) as f32 / 1000.0;
let requests = 420.0 + 160.0 * (at / 37.0).sin() + 60.0 * (at / 9.0).cos() + 40.0 * noise(1);
let answer = 38.0 + requests / 25.0 + 12.0 * noise(2);
let failed = if nth % 90 > 80 { 6.0 + 10.0 * noise(3) } else { (2.0 * noise(4)).floor() };
Second { requests: requests.round(), answer: answer.round(), failed }
}
pub fn describe(app: &mut Dashboard, ui: &mut Ui) {
let t = ui.theme;
app.take_in();
if !app.paused {
ui.wake_after(app.next.saturating_sub(app.now));
}
ui.column().grow().scroll().pad(16.0).gap(16.0).open();
ui.row().gap(12.0).align(Align::Start, Align::Center).open();
ui.text("Service", t.font.bold(), t.text);
ui.spacer();
ui.segmented(&mut app.span, &SPANS);
ui.toggle(&mut app.paused, "Paused");
ui.close();
let shown = app.shown();
let last = shown.last().copied().filter(|_| shown.len() > 1);
match last {
Some(last) => numbers(shown, last, ui),
None => _ = ui.note("Waiting for the first numbers\u{2026}"),
}
parts(app, last, ui);
ui.close();
}
fn numbers(shown: &[Second], last: Second, ui: &mut Ui) {
let t = ui.theme;
let minute = &shown[shown.len().saturating_sub(60)..];
let mean = |of: fn(&Second) -> f32| minute.iter().map(of).sum::<f32>() / minute.len() as f32;
ui.wrapping_row().gap(12.0).open();
tile(ui, "Requests a second", &format!("{}", last.requests), &format!("{:.0} on average this minute", mean(|second| second.requests)));
tile(ui, "Answer time", &format!("{} ms", last.answer), &format!("{:.0} ms on average", mean(|second| second.answer)));
tile(ui, "Failed this minute", &format!("{:.0}", minute.iter().map(|second| second.failed).sum::<f32>()), "requests");
ui.close();
let ends = (-(shown.len() as f32 - 1.0), 0.0);
let [requests, answers, failed] = [|second: &Second| second.requests, |second: &Second| second.answer, |second: &Second| second.failed].map(|of| shown.iter().map(of).collect::<Vec<f32>>());
ui.wrapping_row().gap(12.0).open();
let wide = ((ui.available_size().w - 24.0) / 3.0).floor().max(240.0);
chart(ui, "Requests a second", &Chart::of(&[Series::area("", &requests)]).x_range(ends.0, ends.1), wide);
chart(ui, "Answer time, ms", &Chart::of(&[Series::line("", &answers)]).x_range(ends.0, ends.1), wide);
let most = failed.iter().fold(5.0f32, |most, failed| most.max(*failed));
chart(ui, "Failed requests", &Chart::of(&[Series::bars("", &failed).color(t.red)]).y_range(0.0, most), wide);
ui.close();
}
fn tile(ui: &mut Ui, name: &str, number: &str, beside: &str) {
let t = ui.theme;
ui.column().w(Length::Px(200.0)).pad(12.0).gap(4.0).fill(t.panel).border(t.line, 1.0).radius(8.0).open();
ui.text(name, t.small_font, t.text_dim);
ui.text(number, taut::Font::new(t.font.size * 2.0).bold(), t.text);
ui.text(beside, t.small_font, t.text_faint);
ui.close();
}
fn chart(ui: &mut Ui, name: &str, chart: &Chart, wide: f32) {
let t = ui.theme;
ui.column().pad(12.0).gap(6.0).fill(t.panel).border(t.line, 1.0).radius(8.0).open();
ui.text(name, t.small_font, t.text_dim);
ui.chart(chart, wide - 24.0, 160.0);
ui.close();
}
fn parts(app: &mut Dashboard, last: Option<Second>, ui: &mut Ui) {
let t = ui.theme;
let mut wide = |widest: &str| ui.text_width(widest, t.font).ceil() + 4.0;
let columns = [Column::new("Part"), Column::new("Requests a second").px(wide("Requests a second")).align(Align::End), Column::new("Failed").px(wide("Failed")).align(Align::End)];
let last = last.unwrap_or(Second { requests: f32::NAN, answer: f32::NAN, failed: f32::NAN });
let mut rows: Vec<(&str, f32, f32)> = PARTS.iter().map(|(name, share)| (*name, (last.requests * share).round(), (last.failed * share * 2.0).round())).collect();
ui.column().open();
ui.table(&mut app.table, &columns);
rows.sort_by(|a, b| {
let mut order = std::cmp::Ordering::Equal;
for (column, down) in &app.table.sort {
let one = match column {
1 => a.1.total_cmp(&b.1),
2 => a.2.total_cmp(&b.2),
_ => a.0.cmp(b.0),
};
order = order.then(if *down { one.reverse() } else { one });
}
order.then(a.0.cmp(b.0))
});
for row in ui.rows(rows.len(), 26.0) {
ui.table_row().key(rows[row].0).open();
ui.label(rows[row].0);
for number in [rows[row].1, rows[row].2] {
ui.label(&if number.is_nan() { "-".to_string() } else { number.to_string() });
}
ui.close();
}
ui.close();
ui.close();
ui.close();
}
fn main() {
let (mut app, started) = (Dashboard::default(), Instant::now());
let window = Window { title: "Dashboard", width: 960.0, height: 720.0, min_width: 420.0, min_height: 300.0, ..Default::default() };
taut::run(window, move |ui| {
app.now = started.elapsed();
describe(&mut app, ui);
});
}
#[cfg(test)]
mod tests {
use super::*;
use taut::Role;
use taut::testing::Trial;
#[test]
fn new_numbers_come_every_second_until_it_is_paused() {
let mut trial = Trial::new(Dashboard::default(), describe);
trial.set_time(|dashboard, now| dashboard.now = now);
trial.resize(900.0, 700.0, 1.0);
assert!(trial.has(Role::Text, "Waiting for the first numbers\u{2026}"));
trial.wait(Duration::from_secs(90));
assert_eq!(trial.app.seconds.len(), 90);
assert_eq!(trial.app.shown().len(), 60);
let last = made_up(89);
assert!(trial.has(Role::Text, &last.requests.to_string()) && trial.has(Role::Text, &format!("{} ms", last.answer)));
trial.looks_like("tests/looks/dashboard.png");
trial.press(Role::Tab, "Last 5 minutes");
assert_eq!(trial.app.shown().len(), 90);
trial.press(Role::Button, "Paused");
assert_eq!(trial.ui.wake_in(), None, "nothing to wake for");
trial.wait(Duration::from_secs(10));
assert_eq!(trial.app.seconds.len(), 90);
trial.press(Role::Button, "Paused");
trial.wait(Duration::from_secs(3));
assert_eq!(trial.app.seconds.len(), 93);
trial.press(Role::ColumnHeader, "Requests a second");
let first = |trial: &Trial<Dashboard>| trial.visible_access_nodes().iter().find(|node| node.role == Role::Cell && node.grid == Some([0, 0])).map(|node| node.name.clone());
assert_eq!(first(&trial).as_deref(), Some("/reports"), "the least first");
trial.press(Role::ColumnHeader, "Requests a second");
assert_eq!(first(&trial).as_deref(), Some("/search"));
}
}