-
Notifications
You must be signed in to change notification settings - Fork 1
/
filters.rs
34 lines (31 loc) · 1.2 KB
/
filters.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use chrono::{DateTime, Local};
use eyre::Result;
use serde::{Deserialize, Serialize};
use twitch_api::pubsub::predictions::Event;
use crate::types::StreamerState;
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "web_api", derive(utoipa::ToSchema))]
pub enum Filter {
TotalUsers(u32),
DelaySeconds(u32),
DelayPercentage(f64),
}
pub fn filter_matches(prediction: &Event, filter: &Filter, _: &StreamerState) -> Result<bool> {
let res = match filter {
Filter::TotalUsers(t) => {
prediction.outcomes.iter().fold(0, |a, b| a + b.total_users) as u32 >= *t
}
Filter::DelaySeconds(d) => {
let created_at: DateTime<Local> =
DateTime::parse_from_rfc3339(prediction.created_at.as_str())?.into();
(chrono::Local::now() - created_at).num_seconds() as u32 >= *d
}
Filter::DelayPercentage(d) => {
let created_at: DateTime<Local> =
DateTime::parse_from_rfc3339(prediction.created_at.as_str())?.into();
let d = prediction.prediction_window_seconds as f64 * (d / 100.0);
(chrono::Local::now() - created_at).num_seconds() as f64 >= d
}
};
Ok(res)
}