From a356f978fdf0e4a1b76ad6fdd3478781fb58e6c5 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Tue, 5 Nov 2024 18:10:13 +0100 Subject: [PATCH] Adds some logic on the votes and watchdog --- Cargo.toml | 3 +++ README.md | 2 +- badges/coverage.svg | 1 + badges/release.svg | 1 - badges/test.svg | 2 +- badges/test_cov.svg | 1 - badges/version.svg | 1 + src/date/date_tools.rs | 24 +++++++++++------- src/votes.rs | 55 ++++++++++++++++++++++++++++++++++++------ src/watchdog.rs | 28 ++++++++++++++++----- 10 files changed, 92 insertions(+), 26 deletions(-) create mode 100644 badges/coverage.svg delete mode 100644 badges/release.svg delete mode 100644 badges/test_cov.svg create mode 100644 badges/version.svg diff --git a/Cargo.toml b/Cargo.toml index e29711f..540c7d5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,9 @@ simplelog = { version = "0.12.2", features = ["paris"] } log = { version = "0.4.22", features = ["std"] } nom = "7.1.3" criterion = "0.5" +serde_json = { version = "1.0.132", features = ["alloc"] } +postcard = { version = "1.0.10", features = ["alloc"] } +ron = "0.8.1" [profile.dev] debug = 0 diff --git a/README.md b/README.md index 982fec7..5a86b23 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # idlefox -![code coverage](badges/test_cov.svg) ![version](badges/release.svg) ![code coverage](badges/test.svg) +![code coverage](badges/coverage.svg) ![version](badges/version.svg) ![code coverage](badges/test.svg) IdleFox is a feature-rich Discord bot designed to streamline the process of finding meeting dates, organizing events, and making decisions among multiple options. With its user-friendly commands and robust command system, IdleFox helps facilitate effective communication within server communities. diff --git a/badges/coverage.svg b/badges/coverage.svg new file mode 100644 index 0000000..5bcb4dd --- /dev/null +++ b/badges/coverage.svg @@ -0,0 +1 @@ +test-coverage: 68%test-coverage68% diff --git a/badges/release.svg b/badges/release.svg deleted file mode 100644 index 99d9d9d..0000000 --- a/badges/release.svg +++ /dev/null @@ -1 +0,0 @@ -release: v0.1.0releasev0.1.0 diff --git a/badges/test.svg b/badges/test.svg index a34aaf8..a7859aa 100644 --- a/badges/test.svg +++ b/badges/test.svg @@ -1 +1 @@ -build: passingbuildpassing +test: passingtestpassing diff --git a/badges/test_cov.svg b/badges/test_cov.svg deleted file mode 100644 index f21e8b5..0000000 --- a/badges/test_cov.svg +++ /dev/null @@ -1 +0,0 @@ -test-coverage: 100%test-coverage100% diff --git a/badges/version.svg b/badges/version.svg new file mode 100644 index 0000000..c28c459 --- /dev/null +++ b/badges/version.svg @@ -0,0 +1 @@ +version: 0.1.0version0.1.0 diff --git a/src/date/date_tools.rs b/src/date/date_tools.rs index 616e595..bafa332 100644 --- a/src/date/date_tools.rs +++ b/src/date/date_tools.rs @@ -2,6 +2,7 @@ use crate::error_handling::IdleError; use chrono::{Datelike, Duration, Local, Month, Months, NaiveDate, Weekday}; +use serde::{Deserialize, Serialize}; use super::{ date_parse::{DateObject, DatePoint}, @@ -134,7 +135,7 @@ impl DatePoint { } /// An enum representing either a single [NaiveDate] or a date range between two [NaiveDate]. -#[derive(Clone, Debug, PartialEq, Eq)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] // #[cfg_attr(test, derive(PartialEq))] pub enum NaiveDateObject { /// A range between two dates, inclusive at both ends. @@ -153,11 +154,8 @@ pub enum NaiveDateObject { /// This struct provides an abstraction over a list of date objects, enabling both sequential iteration /// over the dates in order or direct access to any date by its index. The structure is designed to handle /// different sources of date information, allowing for flexible data manipulation based on the provided dates. -/// -/// # Example -/// - -#[cfg_attr(test, derive(PartialEq, Debug))] +#[derive(Debug, Default, Serialize, Deserialize)] +#[cfg_attr(test, derive(PartialEq))] pub struct NaiveDateList { /// The inner vector inner: Vec, @@ -320,10 +318,8 @@ pub fn parse_date_list( #[cfg(test)] pub(crate) mod tests { - - use crate::date::date_parse::DateRange; - use super::*; + use crate::date::date_parse::DateRange; #[test] fn test_relative_days() { @@ -475,4 +471,14 @@ pub(crate) mod tests { assert_eq!(list.unparsed, None); assert_eq!(list.len(), 2); } + #[test] + fn test_serialisation() { + let input = "Oct 5, Nov 15 - 18, sat, mon , tue-fri and some other text"; + let context = NaiveDate::from_ymd_opt(2023, 10, 1); + let list = parse_date_list(input, context).unwrap(); + + let s = ron::to_string(&list).unwrap(); + // Simple roundtrip assertion + assert_eq!(list, ron::from_str(&s).unwrap()); + } } diff --git a/src/votes.rs b/src/votes.rs index 97fbd4d..c5a90ab 100644 --- a/src/votes.rs +++ b/src/votes.rs @@ -22,6 +22,8 @@ use chrono::NaiveDateTime; use serde::{Deserialize, Serialize}; use uuid::Uuid; +use crate::date::date_tools::NaiveDateList; + /// Placeholder datatype /// /// Links to an event, active or planned @@ -79,11 +81,11 @@ pub(crate) struct Event { /// Date at which to autostart the vote process launch_date: NaiveDateTime, /// Date at which to autoclose the vote process - close_date: Option, + close_date: Option, /// The inner list of votes cast votes: Vec, /// The list of values to vote for, this list all the valid dates to vote for - vote_values: Vec, + vote_values: NaiveDateList, } // This split in two differents structs conceptually represents @@ -107,9 +109,30 @@ impl PendingEvents { } } - /// Run all upcomming events if the auto launch time was reached - pub fn run_upcoming(&mut self) { + /// Process and activate all upcomming events if the auto launch time was reached + /// TODO: Test & doc + pub fn process_upcoming(&mut self, current_time: NaiveDateTime) { self.pending_events.insert(Uuid::new_v4(), Event::default()); + let upcomming = self.find_upcomming(current_time); + + for uuid in upcomming { + let event = self.pending_events.remove(&uuid).unwrap(); + ONGOING_EVENTS.lock().unwrap().add_event(uuid, event); + } + } + + // fn launch(&self, event: Event) -> Result<(), IdleError> { + // todo!() + // } + + /// Find all event that are bound to be launched + /// TODO: Test & doc + fn find_upcomming(&self, current_time: NaiveDateTime) -> Vec { + self.pending_events + .iter() + .filter(|&(_, e)| e.launch_date > current_time) + .map(|(k, _v)| (*k)) + .collect() } } @@ -131,8 +154,17 @@ impl OngoingEvents { } /// Prune all outdated votes - pub fn prune_old(&mut self) { + /// + /// TODO: Test & doc + pub fn prune_old(&mut self, current_time: NaiveDateTime) { self.ongoing_events.clear(); + unimplemented!() + } + + /// Add a new event + /// TODO: Test & doc + pub(crate) fn add_event(&mut self, id: Uuid, event: Event) { + self.ongoing_events.insert(id, event); } } @@ -145,17 +177,26 @@ pub static ONGOING_EVENTS: LazyLock> = #[cfg(test)] mod tests { + use chrono::Local; + use super::*; // Test all structs can be safely initialized #[test] + #[ignore] fn init_test() { let _e = PendingEvents::new(); let _o = OngoingEvents::new(); assert!(PENDING_EVENTS.lock().is_ok()); assert!(ONGOING_EVENTS.lock().is_ok()); - PENDING_EVENTS.lock().unwrap().run_upcoming(); - ONGOING_EVENTS.lock().unwrap().prune_old(); + PENDING_EVENTS + .lock() + .unwrap() + .process_upcoming(Local::now().naive_local()); + ONGOING_EVENTS + .lock() + .unwrap() + .prune_old(Local::now().naive_local()); } } diff --git a/src/watchdog.rs b/src/watchdog.rs index 658627e..037fd93 100644 --- a/src/watchdog.rs +++ b/src/watchdog.rs @@ -42,17 +42,25 @@ async fn main_scheduler_loop(mut interval: tokio::time::Interval) { /// and executes them. Additionally, it schedules any ongoing tasks for future execution. /// /// If a new day has been detected (based on the current time), it prunes old events from the list of ongoing events. -/// +// TODO: Thouroughly test fn watchdog_update(current_time: DateTime) { // Update day and tell if a day passed let is_new_day = update_date(current_time.date_naive()); // List all upcoming events and run all events that should be ran - PENDING_EVENTS.lock().unwrap().run_upcoming(); + PENDING_EVENTS + .lock() + .unwrap() + .process_upcoming(current_time.naive_local()); // Execute all scheduled tasks // * Clean old events if a new day passed if is_new_day { - ONGOING_EVENTS.lock().unwrap().prune_old(); + ONGOING_EVENTS + .lock() + .unwrap() + .prune_old(current_time.naive_local()); } + + // TODO: Save global state } /// A lazily-initialized, thread-safe storage for the previous day's date. @@ -86,7 +94,7 @@ mod tests { use super::*; #[test] - fn test_initialisation() { + fn initialisation() { assert!(OLD_DATE.lock().is_ok()); let date = Local::now().date_naive(); update_date(date); @@ -94,13 +102,21 @@ mod tests { } #[test] - fn test_update() { - // TODO: Fill this test as the update functions are filled + #[ignore] + fn test_watchdog_update() { let date = Local::now(); watchdog_update(date); let date2 = date.checked_add_days(Days::new(12)).unwrap(); assert!(update_date(date2.date_naive())); let date2 = date.checked_add_days(Days::new(14)).unwrap(); watchdog_update(date2); + + todo!("Fill this test as the update functions are filled") + } + + #[test] + #[ignore] + fn watchdog_autosave() { + unimplemented!() } }