Skip to main content

nexus_common/types/
timeframe.rs

1use serde::Deserialize;
2use std::fmt::Display;
3use utoipa::ToSchema;
4
5#[derive(Deserialize, Debug, ToSchema, Clone, PartialEq)]
6#[serde(rename_all = "snake_case")]
7pub enum Timeframe {
8    Today,
9    ThisMonth,
10    AllTime,
11}
12
13impl Display for Timeframe {
14    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15        match self {
16            Timeframe::Today => write!(f, "Today"),
17            Timeframe::ThisMonth => write!(f, "ThisMonth"),
18            Timeframe::AllTime => write!(f, "AllTime"),
19        }
20    }
21}
22
23impl Timeframe {
24    pub fn to_timestamp_range(&self) -> (i64, i64) {
25        let now = chrono::Utc::now();
26        let start = match self {
27            Timeframe::Today => (now - chrono::Duration::hours(24)).timestamp_millis(),
28            Timeframe::ThisMonth => (now - chrono::Duration::days(30)).timestamp_millis(),
29            Timeframe::AllTime => 0,
30        };
31        (start, now.timestamp_millis())
32    }
33
34    pub fn to_cache_period(&self) -> i64 {
35        match self {
36            Timeframe::Today => 60 * 60,
37            Timeframe::ThisMonth => 60 * 60 * 24,
38            Timeframe::AllTime => 60 * 60 * 24,
39        }
40    }
41}