stats: add 1h, 4h, 3d time range windows

Adds Hour (5-min buckets), FourHour (15-min buckets), and ThreeDay
(hourly buckets) to the Window enum, plus the matching tab buttons
in stats.html. Simplifies web_ui.rs to use Window::span_secs()
instead of a duplicate match.

Closes #25
This commit is contained in:
iris 2026-05-20 20:10:20 +02:00 committed by Mara
parent 4bb3877460
commit f9d1e69a50
3 changed files with 24 additions and 10 deletions

View file

@ -20,7 +20,10 @@ use hive_sh4re::ReminderStats;
/// total span + the bucket width used to roll up trend series.
#[derive(Debug, Clone, Copy)]
pub enum Window {
Hour,
FourHour,
Day,
ThreeDay,
Week,
Month,
}
@ -28,6 +31,9 @@ pub enum Window {
impl Window {
pub fn parse(s: &str) -> Self {
match s {
"1h" => Self::Hour,
"4h" => Self::FourHour,
"3d" => Self::ThreeDay,
"7d" => Self::Week,
"30d" => Self::Month,
_ => Self::Day,
@ -36,15 +42,21 @@ impl Window {
fn label(self) -> &'static str {
match self {
Self::Hour => "1h",
Self::FourHour => "4h",
Self::Day => "24h",
Self::ThreeDay => "3d",
Self::Week => "7d",
Self::Month => "30d",
}
}
fn span_secs(self) -> i64 {
pub fn span_secs(self) -> i64 {
match self {
Self::Hour => 3600,
Self::FourHour => 4 * 3600,
Self::Day => 24 * 3600,
Self::ThreeDay => 3 * 24 * 3600,
Self::Week => 7 * 24 * 3600,
Self::Month => 30 * 24 * 3600,
}
@ -52,8 +64,11 @@ impl Window {
fn bucket_secs(self) -> i64 {
match self {
// hourly for the 24h view, daily for the longer ranges.
Self::Day => 3600,
// 5-min buckets for 1h (12 buckets), 15-min for 4h (16 buckets),
// hourly for 24h + 3d, daily for 7d + 30d.
Self::Hour => 300,
Self::FourHour => 900,
Self::Day | Self::ThreeDay => 3600,
Self::Week | Self::Month => 24 * 3600,
}
}

View file

@ -319,13 +319,9 @@ async fn api_stats(
) -> axum::Json<crate::stats::Snapshot> {
let window = crate::stats::Window::parse(q.window.as_deref().unwrap_or("24h"));
let mut snapshot = crate::stats::snapshot_default(window);
// Fetch reminder stats from the broker. The window spans are:
// 24h = 86400s, 7d = 604800s, 30d = 2592000s.
let window_secs = match window {
crate::stats::Window::Day => 24 * 3600,
crate::stats::Window::Week => 7 * 24 * 3600,
crate::stats::Window::Month => 30 * 24 * 3600,
};
// Pass the window span to the reminder-stats RPC so the broker
// filters its counts to the same time range as the chart data.
let window_secs = window.span_secs();
snapshot.reminder_stats = fetch_reminder_stats(&state.socket, state.flavor(), window_secs as u64).await;
axum::Json(snapshot)
}