Adds some logic on the votes and watchdog

This commit is contained in:
Dandaedre 2024-11-05 18:10:13 +01:00
parent 1e02cfb3d1
commit 72bf7ea3bb
10 changed files with 92 additions and 26 deletions

View File

@ -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

View File

@ -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.

1
badges/coverage.svg Normal file
View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="122" height="18" role="img" aria-label="test-coverage: 68%"><title>test-coverage: 68%</title><linearGradient id="bms-e5683aba5d0a18fe" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-e5683aba5d0a18fe"><rect width="122" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-e5683aba5d0a18fe)"><rect width="87" height="18" fill="#555"/><rect x="87" width="35" height="18" fill="#e05d44"/><rect width="122" height="18" fill="url(#bms-e5683aba5d0a18fe)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="445" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="770">test-coverage</text><text x="445" y="130" transform="scale(.1)" fill="#fff" textLength="770">test-coverage</text><text aria-hidden="true" x="1035" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="250">68%</text><text x="1035" y="130" transform="scale(.1)" fill="#fff" textLength="250">68%</text></g></svg>

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="94" height="18" role="img" aria-label="release: v0.1.0"><title>release: v0.1.0</title><linearGradient id="bms-162829b957040378" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-162829b957040378"><rect width="94" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-162829b957040378)"><rect width="49" height="18" fill="#555"/><rect x="49" width="45" height="18" fill="#fe7d37"/><rect width="94" height="18" fill="url(#bms-162829b957040378)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="255" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="390">release</text><text x="255" y="130" transform="scale(.1)" fill="#fff" textLength="390">release</text><text aria-hidden="true" x="705" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="350">v0.1.0</text><text x="705" y="130" transform="scale(.1)" fill="#fff" textLength="350">v0.1.0</text></g></svg>

Before

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="88" height="18" role="img" aria-label="build: passing"><title>build: passing</title><linearGradient id="bms-de44f1c0747536f7" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-de44f1c0747536f7"><rect width="88" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-de44f1c0747536f7)"><rect width="37" height="18" fill="#555"/><rect x="37" width="51" height="18" fill="#97ca00"/><rect width="88" height="18" fill="url(#bms-de44f1c0747536f7)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="195" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="270">build</text><text x="195" y="130" transform="scale(.1)" fill="#fff" textLength="270">build</text><text aria-hidden="true" x="615" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="410">passing</text><text x="615" y="130" transform="scale(.1)" fill="#fff" textLength="410">passing</text></g></svg>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="82" height="18" role="img" aria-label="test: passing"><title>test: passing</title><linearGradient id="bms-5402c39dce014bd3" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-5402c39dce014bd3"><rect width="82" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-5402c39dce014bd3)"><rect width="31" height="18" fill="#555"/><rect x="31" width="51" height="18" fill="#97ca00"/><rect width="82" height="18" fill="url(#bms-5402c39dce014bd3)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="165" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="210">test</text><text x="165" y="130" transform="scale(.1)" fill="#fff" textLength="210">test</text><text aria-hidden="true" x="555" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="410">passing</text><text x="555" y="130" transform="scale(.1)" fill="#fff" textLength="410">passing</text></g></svg>

Before

Width:  |  Height:  |  Size: 1.3 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="130" height="18" role="img" aria-label="test-coverage: 100%"><title>test-coverage: 100%</title><linearGradient id="bms-0d308125ce4bf4b9" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-0d308125ce4bf4b9"><rect width="130" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-0d308125ce4bf4b9)"><rect width="87" height="18" fill="#555"/><rect x="87" width="43" height="18" fill="#97ca00"/><rect width="130" height="18" fill="url(#bms-0d308125ce4bf4b9)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="445" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="770">test-coverage</text><text x="445" y="130" transform="scale(.1)" fill="#fff" textLength="770">test-coverage</text><text aria-hidden="true" x="1075" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="330">100%</text><text x="1075" y="130" transform="scale(.1)" fill="#fff" textLength="330">100%</text></g></svg>

Before

Width:  |  Height:  |  Size: 1.3 KiB

1
badges/version.svg Normal file
View File

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="90" height="18" role="img" aria-label="version: 0.1.0"><title>version: 0.1.0</title><linearGradient id="bms-0431e50b7af7883f" x2="0" y2="100%"><stop offset="0" stop-color="#fff" stop-opacity=".7"/><stop offset=".1" stop-color="#aaa" stop-opacity=".1"/><stop offset=".9" stop-color="#000" stop-opacity=".3"/><stop offset="1" stop-color="#000" stop-opacity=".5"/></linearGradient><clipPath id="bmr-0431e50b7af7883f"><rect width="90" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-0431e50b7af7883f)"><rect width="51" height="18" fill="#555"/><rect x="51" width="39" height="18" fill="#fe7d37"/><rect width="90" height="18" fill="url(#bms-0431e50b7af7883f)"/></g><g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110"><text aria-hidden="true" x="265" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="410">version</text><text x="265" y="130" transform="scale(.1)" fill="#fff" textLength="410">version</text><text aria-hidden="true" x="695" y="140" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="290">0.1.0</text><text x="695" y="130" transform="scale(.1)" fill="#fff" textLength="290">0.1.0</text></g></svg>

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -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<NaiveDateObject>,
@ -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());
}
}

View File

@ -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<DateDataType>,
close_date: Option<NaiveDateTime>,
/// The inner list of votes cast
votes: Vec<Vote>,
/// The list of values to vote for, this list all the valid dates to vote for
vote_values: Vec<DateDataType>,
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<Uuid> {
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<Mutex<OngoingEvents>> =
#[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());
}
}

View File

@ -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<Local>) {
// 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!()
}
}