dan #37
|
|
@ -1 +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: 89%"><title>test-coverage: 89%</title><linearGradient id="bms-72c388a7ffe9bb07" 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-72c388a7ffe9bb07"><rect width="122" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-72c388a7ffe9bb07)"><rect width="87" height="18" fill="#555"/><rect x="87" width="35" height="18" fill="#fe7d37"/><rect width="122" height="18" fill="url(#bms-72c388a7ffe9bb07)"/></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">89%</text><text x="1035" y="130" transform="scale(.1)" fill="#fff" textLength="250">89%</text></g></svg>
|
||||
<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: 87%"><title>test-coverage: 87%</title><linearGradient id="bms-094034b04678331f" 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-094034b04678331f"><rect width="122" height="18" rx="4" fill="#fff"/></clipPath><g clip-path="url(#bmr-094034b04678331f)"><rect width="87" height="18" fill="#555"/><rect x="87" width="35" height="18" fill="#fe7d37"/><rect width="122" height="18" fill="url(#bms-094034b04678331f)"/></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">87%</text><text x="1035" y="130" transform="scale(.1)" fill="#fff" textLength="250">87%</text></g></svg>
|
||||
|
|
|
|||
|
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
|
|
@ -11,9 +11,6 @@ use poise::serenity_prelude;
|
|||
use thiserror::Error;
|
||||
use uuid::Uuid;
|
||||
|
||||
// // Since the errors themselves are very verbose, this seemed like complete overkill
|
||||
// #[allow(clippy::missing_docs_in_private_items)]
|
||||
|
||||
/// Global error struct for idlefox
|
||||
///
|
||||
/// This enum represents various errors that can occur when interacting with most of the section of the program
|
||||
|
|
|
|||
|
|
@ -20,24 +20,59 @@ use crate::{date::NaiveDateList, io::VoteContainer, IdleError};
|
|||
|
||||
use super::Ballot;
|
||||
|
||||
/// An enumeration representing the global status of an event
|
||||
/// A pending event, waiting to be opened. In this state the even can be safely modified
|
||||
/// and either manual input or the autostart date will start the vote.
|
||||
#[derive(Serialize, Deserialize, Default)]
|
||||
#[cfg_attr(test, derive(Clone))]
|
||||
pub struct EventPending {
|
||||
/// The parsed representation of the vote associated with this event.
|
||||
/// It holds the details required for the voting process.
|
||||
pub(super) parsed_vote: ParsedVote,
|
||||
}
|
||||
|
||||
impl EventPending {
|
||||
pub fn into_ongoing(self) -> EventOngoing {
|
||||
EventOngoing {
|
||||
parsed_vote: self.parsed_vote,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[cfg_attr(test, derive(Clone))]
|
||||
// An ongoing event, waiting to be closed thus allowing the ballots to be counted.
|
||||
pub struct EventOngoing {
|
||||
/// The parsed representation of the vote associated with this event.
|
||||
/// It holds the details required for the voting process.
|
||||
pub(super) parsed_vote: ParsedVote,
|
||||
}
|
||||
|
||||
impl Display for EventOngoing {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.parsed_vote.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl EventOngoing {
|
||||
pub fn into_closed(self) -> EventClosed {
|
||||
EventClosed {
|
||||
parsed_vote: self.parsed_vote,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[cfg_attr(test, derive(Clone))]
|
||||
/// A closed event, while the users cannot interract with it in this state,
|
||||
/// this event can still be reopened and it's results can be read.
|
||||
/// All events have an expiry date and in this state the event will be cleaned
|
||||
/// up after a while.
|
||||
///
|
||||
/// This enum is used to track the progress of an event from its creation to completion,
|
||||
/// including various states that can be transitioned through. It is primarily intended
|
||||
/// for internal use within the application.
|
||||
#[derive(Serialize, Deserialize, Default, Debug, Clone, Copy)]
|
||||
pub(crate) enum EventStatus {
|
||||
#[default]
|
||||
/// The event exists but wasn't started yet
|
||||
Pending,
|
||||
/// The event was started but the finish date wasn't reached yet
|
||||
Started,
|
||||
/// The event was started and the date finish date was reached without being closed
|
||||
Finished,
|
||||
/// The event was closed and the outcome was made public
|
||||
Closed,
|
||||
/// The event was cancelled
|
||||
Cancelled,
|
||||
/// Closing an event will call the cleanup and completion functions.
|
||||
pub struct EventClosed {
|
||||
/// The parsed representation of the vote associated with this event.
|
||||
/// It holds the details required for the voting process.
|
||||
pub(super) parsed_vote: ParsedVote,
|
||||
}
|
||||
|
||||
/// A `ParsedVote` represents a voting event that has been parsed and validated. It
|
||||
|
|
@ -48,13 +83,11 @@ pub(crate) enum EventStatus {
|
|||
/// data throughout their lifecycle.
|
||||
#[derive(Debug, Serialize, Deserialize, Default)]
|
||||
#[cfg_attr(test, derive(Clone))]
|
||||
pub struct ParsedVote {
|
||||
pub(super) struct ParsedVote {
|
||||
/// The title of the even
|
||||
pub(super) title: String,
|
||||
/// Description of the event
|
||||
pub(super) description: String,
|
||||
/// The global status of the vote
|
||||
pub(super) status: EventStatus,
|
||||
/// Date at which to autostart the vote process
|
||||
pub(super) launch_date: NaiveDateTime,
|
||||
/// Date at which to autoclose the vote process
|
||||
|
|
@ -65,37 +98,17 @@ pub struct ParsedVote {
|
|||
pub(super) votes: Vec<Ballot>,
|
||||
/// The list of values to vote for, this list all the valid dates to vote for
|
||||
pub(super) list_candidates: NaiveDateList,
|
||||
// / The vote type
|
||||
}
|
||||
|
||||
impl ParsedVote {
|
||||
// #[allow(dead_code, unused_variables)]
|
||||
// pub(crate) fn new(
|
||||
// title: String,
|
||||
// description: String,
|
||||
// status: EventStatus,
|
||||
// launch_date: NaiveDateTime,
|
||||
// close_date: NaiveDateTime,
|
||||
// // ballots: Vec<Ballot>,
|
||||
// list_candidates: NaiveDateList,
|
||||
// ) -> Self {
|
||||
// // Have a rather smart close date if not specified
|
||||
// todo!()
|
||||
// }
|
||||
|
||||
/// All the logic behind a vote becoming live
|
||||
pub(super) fn launch(&mut self) -> Result<(), IdleError> {
|
||||
// TODO: Define this
|
||||
self.status = EventStatus::Started;
|
||||
// self.status = EventStatus::Started;
|
||||
// Maybe send a message ? the bot will need the Uuid of the event to be able to add reaction
|
||||
// TODO: This is a vkypoint, as this method will need an IO interaction
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Close the event
|
||||
pub(super) fn close(&mut self) {
|
||||
self.status = EventStatus::Closed;
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<VoteContainer> for ParsedVote {
|
||||
|
|
|
|||
|
|
@ -7,7 +7,11 @@ use uuid::Uuid;
|
|||
|
||||
use crate::IdleError;
|
||||
|
||||
use super::{OngoingEvents, ParsedVote, PendingEvents};
|
||||
use super::{
|
||||
events::{EventOngoing, EventPending},
|
||||
holders::ListClosedEvents,
|
||||
ListOngoingEvents, ListPendingEvents,
|
||||
};
|
||||
|
||||
/// Adds a parsed vote to the list of planned events.
|
||||
///
|
||||
|
|
@ -19,12 +23,12 @@ use super::{OngoingEvents, ParsedVote, PendingEvents};
|
|||
///
|
||||
/// ## Example:
|
||||
/// ```rust
|
||||
/// # use idlefox::votes::{add_pending_event, EventHolder, ParsedVote};
|
||||
/// use idlefox::votes::{add_pending_event, EventHolder, EventPending};
|
||||
/// let ev_ctx = EventHolder::default();
|
||||
/// let parsed_vote = ParsedVote::default();
|
||||
/// let parsed_vote = EventPending::default();
|
||||
/// let id = add_pending_event(parsed_vote, &ev_ctx).unwrap();
|
||||
/// ```
|
||||
pub fn add_pending_event(event: ParsedVote, ev_ctx: &EventHolder) -> Result<Uuid, IdleError> {
|
||||
pub fn add_pending_event(event: EventPending, ev_ctx: &EventHolder) -> Result<Uuid, IdleError> {
|
||||
ev_ctx
|
||||
.pending_events
|
||||
.lock()
|
||||
|
|
@ -42,9 +46,9 @@ pub fn add_pending_event(event: ParsedVote, ev_ctx: &EventHolder) -> Result<Uuid
|
|||
///
|
||||
/// ## Example:
|
||||
/// ```rust
|
||||
/// # use idlefox::votes::{add_pending_event, make_into_ongoing_event, ParsedVote, EventHolder};
|
||||
/// use idlefox::votes::{add_pending_event, make_into_ongoing_event, EventPending, EventHolder};
|
||||
/// let ev_ctx = EventHolder::default();
|
||||
/// let id = add_pending_event(ParsedVote::default(), &ev_ctx).unwrap();
|
||||
/// let id = add_pending_event(EventPending::default(), &ev_ctx).unwrap();
|
||||
///
|
||||
/// make_into_ongoing_event(id, &ev_ctx).unwrap()
|
||||
/// ```
|
||||
|
|
@ -70,16 +74,16 @@ pub fn make_into_ongoing_event(uuid: Uuid, ev_ctx: &EventHolder) -> Result<(), I
|
|||
/// ## Example:
|
||||
/// ```rust
|
||||
/// # use uuid::Uuid;
|
||||
/// # use idlefox::votes::{add_ongoing_event, ParsedVote, EventHolder};
|
||||
/// use idlefox::votes::{add_ongoing_event, EventPending, EventHolder};
|
||||
/// let ev_ctx = EventHolder::default();
|
||||
/// let uuid = Uuid::new_v4(); // A new id
|
||||
/// let parsed_vote = ParsedVote::default(); // A very legit event
|
||||
/// let ongoing = EventPending::default().into_ongoing(); // A very legit event
|
||||
///
|
||||
/// add_ongoing_event(uuid, parsed_vote, &ev_ctx).unwrap();
|
||||
/// add_ongoing_event(uuid, ongoing, &ev_ctx).unwrap();
|
||||
/// ```
|
||||
pub fn add_ongoing_event(
|
||||
uuid: Uuid,
|
||||
event: ParsedVote,
|
||||
event: EventOngoing,
|
||||
ev_ctx: &EventHolder,
|
||||
) -> Result<(), IdleError> {
|
||||
ev_ctx
|
||||
|
|
@ -100,15 +104,15 @@ pub fn add_ongoing_event(
|
|||
/// ## Example:
|
||||
/// ```rust
|
||||
/// # use idlefox::votes::{
|
||||
/// # add_pending_event, del_ongoing_event, make_into_ongoing_event, ParsedVote, EventHolder
|
||||
/// # add_pending_event, del_ongoing_event, make_into_ongoing_event, EventPending, EventHolder
|
||||
/// # };
|
||||
/// let ev_ctx = EventHolder::default();
|
||||
/// let id = add_pending_event(ParsedVote::default(), &ev_ctx).unwrap();
|
||||
/// let id = add_pending_event(EventPending::default(), &ev_ctx).unwrap();
|
||||
/// make_into_ongoing_event(id, &ev_ctx).unwrap();
|
||||
///
|
||||
/// del_ongoing_event(id, &ev_ctx).unwrap();
|
||||
/// ```
|
||||
pub fn del_ongoing_event(uuid: Uuid, ev_ctx: &EventHolder) -> Result<ParsedVote, IdleError> {
|
||||
pub fn del_ongoing_event(uuid: Uuid, ev_ctx: &EventHolder) -> Result<EventOngoing, IdleError> {
|
||||
ev_ctx
|
||||
.ongoing_events
|
||||
.lock()
|
||||
|
|
@ -126,13 +130,13 @@ pub fn del_ongoing_event(uuid: Uuid, ev_ctx: &EventHolder) -> Result<ParsedVote,
|
|||
/// ## Example:
|
||||
/// ```rust
|
||||
/// # use idlefox::votes::{
|
||||
/// # add_pending_event, del_pending_event, make_into_ongoing_event, ParsedVote, EventHolder
|
||||
/// # add_pending_event, del_pending_event, make_into_ongoing_event, EventPending, EventHolder
|
||||
/// # };
|
||||
/// let ev_ctx = EventHolder::default();
|
||||
/// let id = add_pending_event(ParsedVote::default(), &ev_ctx).unwrap();
|
||||
/// let id = add_pending_event(EventPending::default(), &ev_ctx).unwrap();
|
||||
/// del_pending_event(id, &ev_ctx).unwrap();
|
||||
/// ```
|
||||
pub fn del_pending_event(uuid: Uuid, ev_ctx: &EventHolder) -> Result<ParsedVote, IdleError> {
|
||||
pub fn del_pending_event(uuid: Uuid, ev_ctx: &EventHolder) -> Result<EventPending, IdleError> {
|
||||
ev_ctx
|
||||
.pending_events
|
||||
.lock()
|
||||
|
|
@ -149,20 +153,20 @@ pub fn del_pending_event(uuid: Uuid, ev_ctx: &EventHolder) -> Result<ParsedVote,
|
|||
/// ## Example:
|
||||
/// ```rust
|
||||
/// # use idlefox::votes::{
|
||||
/// # add_pending_event, close_ongoing_event, make_into_ongoing_event, ParsedVote, EventHolder,
|
||||
/// # add_pending_event, close_ongoing_event, make_into_ongoing_event, EventPending, EventHolder,
|
||||
/// # };
|
||||
/// let ev_ctx = EventHolder::default();
|
||||
/// let id = add_pending_event(ParsedVote::default(), &ev_ctx).unwrap();
|
||||
/// let id = add_pending_event(EventPending::default(), &ev_ctx).unwrap();
|
||||
/// make_into_ongoing_event(id, &ev_ctx).unwrap();
|
||||
/// close_ongoing_event(id, &ev_ctx).unwrap();
|
||||
/// ```
|
||||
pub fn close_ongoing_event(uuid: Uuid, ev_ctx: &EventHolder) -> Result<ParsedVote, IdleError> {
|
||||
pub fn close_ongoing_event(uuid: Uuid, ev_ctx: &EventHolder) -> Result<(), IdleError> {
|
||||
ev_ctx
|
||||
.ongoing_events
|
||||
.lock()
|
||||
.unwrap()
|
||||
// .map_err(|_| IdleError::MutexError)?
|
||||
.close_event(uuid)
|
||||
.close_event(uuid, ev_ctx)
|
||||
}
|
||||
|
||||
/// A link to all events, to be shared between threads related to this event pool.
|
||||
|
|
@ -176,16 +180,17 @@ pub struct EventHolder {
|
|||
/// A reference-counted pointer to a mutex-protected `PendingEvents` instance, managing all pending events.
|
||||
///
|
||||
/// Pending events are those that have been scheduled but not yet started
|
||||
pub pending_events: Arc<Mutex<PendingEvents>>,
|
||||
pub pending_events: Arc<Mutex<ListPendingEvents>>,
|
||||
/// A reference-counted pointer to a mutex-protected `OngoingEvents` instance, managing all ongoing events.
|
||||
///
|
||||
/// Ongoing events are currently running but not yet closed.
|
||||
pub ongoing_events: Arc<Mutex<OngoingEvents>>,
|
||||
// /// A reference-counted pointer to a mutex-protected `OngoingEvents` instance, managing all closed events.
|
||||
// ///
|
||||
// /// Closed events have been voted up but havent been deleted and can therefore be watched or opened again.
|
||||
// /// These events will be autodeleted after a while
|
||||
// pub closed_events: Mutex<ClosedEvents>,
|
||||
pub ongoing_events: Arc<Mutex<ListOngoingEvents>>,
|
||||
/// A reference-counted pointer to a mutex-protected `OngoingEvents` instance, managing all closed events.
|
||||
///
|
||||
/// Closed events have been voted up but havent been deleted and can therefore be watched or opened again.
|
||||
/// These events will be autodeleted after a while
|
||||
pub closed_events: Arc<Mutex<ListClosedEvents>>,
|
||||
|
||||
/// A reference-counted pointer to a mutex-protected `NaiveDate` instance, representing the last date the watchdog was updated.
|
||||
pub old_date: Arc<Mutex<NaiveDate>>,
|
||||
|
||||
|
|
|
|||
|
|
@ -17,31 +17,26 @@ use uuid::Uuid;
|
|||
|
||||
use crate::IdleError;
|
||||
|
||||
use super::{add_ongoing_event, events::EventStatus, EventHolder, EventListType, ParsedVote};
|
||||
use super::{
|
||||
add_ongoing_event,
|
||||
events::{EventClosed, EventOngoing, EventPending},
|
||||
ClosedHashmap, EventHolder, OngoingHashmap, PendingHashmap,
|
||||
};
|
||||
|
||||
/// The list of pending events, not yet started
|
||||
///
|
||||
/// This struct represents the collection of events that have been scheduled but have not yet begun.
|
||||
#[derive(Serialize, Deserialize, Default)]
|
||||
pub struct PendingEvents {
|
||||
pub struct ListPendingEvents {
|
||||
/// The list of pending events
|
||||
pending_events: EventListType,
|
||||
pending_events: PendingHashmap,
|
||||
}
|
||||
|
||||
impl PendingEvents {
|
||||
// /// Create a new empty list of events, called in case of a first initialization.
|
||||
// ///
|
||||
// /// However, a deserialization will always be performed if possible.
|
||||
// fn new() -> Self {
|
||||
// Self {
|
||||
// pending_events: EventListType::new(),
|
||||
// }
|
||||
// }
|
||||
|
||||
impl ListPendingEvents {
|
||||
/// Add a new event to the list of upcoming events.
|
||||
///
|
||||
/// This function generates a new UUID for the event before adding it to the list.
|
||||
pub(super) fn add_pending_event(&mut self, event: ParsedVote) -> Result<Uuid, IdleError> {
|
||||
pub(super) fn add_pending_event(&mut self, event: EventPending) -> Result<Uuid, IdleError> {
|
||||
let id = Uuid::new_v4();
|
||||
self.pending_events.insert(id, event);
|
||||
Ok(id)
|
||||
|
|
@ -71,7 +66,7 @@ impl PendingEvents {
|
|||
.pending_events
|
||||
.remove(&uuid)
|
||||
.ok_or(IdleError::EventLaunch(uuid))?;
|
||||
add_ongoing_event(uuid, event, ev_ctx)?;
|
||||
add_ongoing_event(uuid, event.into_ongoing(), ev_ctx)?;
|
||||
// make_into_ongoing_event(uuid)?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -82,12 +77,12 @@ impl PendingEvents {
|
|||
pub(super) fn find_upcoming(&self, current_time: NaiveDateTime) -> Vec<Uuid> {
|
||||
self.pending_events
|
||||
.iter()
|
||||
.filter(|&(_, e)| e.launch_date <= current_time)
|
||||
.filter(|&(_, e)| e.parsed_vote.launch_date <= current_time)
|
||||
.map(|(k, _v)| (*k))
|
||||
.collect()
|
||||
}
|
||||
/// delete the event without launching it
|
||||
pub fn del_event(&mut self, uuid: Uuid) -> Result<ParsedVote, IdleError> {
|
||||
pub fn del_event(&mut self, uuid: Uuid) -> Result<EventPending, IdleError> {
|
||||
self.pending_events
|
||||
.remove(&uuid)
|
||||
.ok_or(IdleError::DeleteEvent(uuid))
|
||||
|
|
@ -100,21 +95,106 @@ impl PendingEvents {
|
|||
///
|
||||
/// This struct represents the collection of events that have been initiated but have not yet concluded.
|
||||
#[derive(Serialize, Deserialize, Default)]
|
||||
pub struct OngoingEvents {
|
||||
pub struct ListOngoingEvents {
|
||||
/// The list of ongoing events
|
||||
ongoing_events: EventListType,
|
||||
ongoing_events: OngoingHashmap,
|
||||
}
|
||||
|
||||
impl OngoingEvents {
|
||||
// /// Create a new empty list of ongoing events, called in case of a first initialization.
|
||||
// ///
|
||||
// /// However, a deserialization will always be performed if possible.
|
||||
// fn new() -> Self {
|
||||
// Self {
|
||||
// ongoing_events: EventListType::new(),
|
||||
// }
|
||||
// }
|
||||
impl ListOngoingEvents {
|
||||
/// Add a new event to the list of ongoing events.
|
||||
///
|
||||
/// This function also launch the event based and adds a callback with the Uuid of the original caller.
|
||||
pub(crate) fn add_event(&mut self, id: Uuid, mut event: EventOngoing) -> Result<(), IdleError> {
|
||||
event.parsed_vote.launch()?;
|
||||
self.ongoing_events.insert(id, event);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Remove all outdated votes for the current user
|
||||
///
|
||||
/// This function is expected to be called periodically by the watchdog.
|
||||
pub(crate) fn autoclose(
|
||||
&mut self,
|
||||
current_time: NaiveDateTime,
|
||||
ev_ctx: &EventHolder,
|
||||
) -> Result<(), IdleError> {
|
||||
let failure = Vec::new();
|
||||
for uuid in self.find_autoclose(current_time) {
|
||||
self.close_event(uuid, ev_ctx)?;
|
||||
}
|
||||
match failure.is_empty() {
|
||||
true => Ok(()),
|
||||
// Should only happen during hard to reproduce race conditions
|
||||
#[cfg(not(tarpaulin_include))]
|
||||
false => Err(IdleError::EventPrune(failure)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Close an event removing it from the ongoing event list and
|
||||
/// puting it into the closed event list.
|
||||
///
|
||||
/// Doing so allow for the vote result to be read while it is valid
|
||||
pub fn close_event(&mut self, uuid: Uuid, ev_ctx: &EventHolder) -> Result<(), IdleError> {
|
||||
// TODO: Another vkypoint, as we cant continue without knowing how to send a message
|
||||
let event = self
|
||||
.ongoing_events
|
||||
.remove(&uuid)
|
||||
.ok_or(IdleError::LaunchingEvent(uuid))?;
|
||||
ev_ctx
|
||||
.closed_events
|
||||
.lock()
|
||||
.unwrap()
|
||||
.add_event(uuid, event.into_closed())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// delete the event without launching it
|
||||
pub fn del_event(&mut self, uuid: Uuid) -> Result<EventOngoing, IdleError> {
|
||||
self.ongoing_events
|
||||
.remove(&uuid)
|
||||
.ok_or(IdleError::DeleteEvent(uuid))
|
||||
}
|
||||
|
||||
/// Find all events that are scheduled to be autoclosed.
|
||||
///
|
||||
/// Returns a vector of UUIDs corresponding to the old events.
|
||||
pub(super) fn find_autoclose(&mut self, current_time: NaiveDateTime) -> Vec<Uuid> {
|
||||
self.ongoing_events
|
||||
.iter()
|
||||
.filter(|(_, e)| e.parsed_vote.close_date <= current_time)
|
||||
.map(|(k, _)| *k)
|
||||
.collect()
|
||||
}
|
||||
// TODO: maybe add an update_ongoing(date) ?
|
||||
}
|
||||
|
||||
/// The list of closed events
|
||||
///
|
||||
/// This struct represents the collection of events that have been concluded,
|
||||
/// to display the results of the votes.
|
||||
#[derive(Serialize, Deserialize, Default)]
|
||||
pub struct ListClosedEvents {
|
||||
/// The list of closed events
|
||||
closed_events: ClosedHashmap,
|
||||
}
|
||||
|
||||
impl ListClosedEvents {
|
||||
/// Add a closed event to the list of closed events
|
||||
pub fn add_event(&mut self, uuid: Uuid, event: EventClosed) -> Result<(), IdleError> {
|
||||
self.closed_events.insert(uuid, event);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Find all events that are too old and supposed to be pruned.
|
||||
///
|
||||
/// Returns a vector of UUIDs corresponding to the old events.
|
||||
pub(super) fn find_old(&mut self, current_time: NaiveDateTime) -> Vec<Uuid> {
|
||||
self.closed_events
|
||||
.iter()
|
||||
.filter(|(_, e)| e.parsed_vote.auto_delete_date <= current_time)
|
||||
.map(|(k, _)| *k)
|
||||
.collect()
|
||||
}
|
||||
/// Remove all outdated votes for the current user
|
||||
///
|
||||
/// This function is expected to be called periodically by the watchdog.
|
||||
|
|
@ -122,7 +202,7 @@ impl OngoingEvents {
|
|||
let mut failure = Vec::new();
|
||||
for uuid in self.find_old(current_time) {
|
||||
println!("{}", uuid);
|
||||
if self.ongoing_events.remove(&uuid).is_none() {
|
||||
if self.closed_events.remove(&uuid).is_none() {
|
||||
// Should only happen during hard to reproduce race conditions
|
||||
#[cfg(not(tarpaulin_include))]
|
||||
failure.push(uuid);
|
||||
|
|
@ -134,78 +214,6 @@ impl OngoingEvents {
|
|||
false => Err(IdleError::EventPrune(failure)),
|
||||
}
|
||||
}
|
||||
/// Remove all outdated votes for the current user
|
||||
///
|
||||
/// This function is expected to be called periodically by the watchdog.
|
||||
pub(crate) fn autoclose(&mut self, current_time: NaiveDateTime) -> Result<(), IdleError> {
|
||||
let failure = Vec::new();
|
||||
for uuid in self.find_autoclose(current_time) {
|
||||
// println!("{}", uuid);
|
||||
self.ongoing_events
|
||||
.get_mut(&uuid)
|
||||
.ok_or(IdleError::EventLaunch(uuid))?
|
||||
.close()
|
||||
}
|
||||
match failure.is_empty() {
|
||||
true => Ok(()),
|
||||
// Should only happen during hard to reproduce race conditions
|
||||
#[cfg(not(tarpaulin_include))]
|
||||
false => Err(IdleError::EventPrune(failure)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Find all events that are scheduled to be autoclosed.
|
||||
///
|
||||
/// Returns a vector of UUIDs corresponding to the old events.
|
||||
pub(super) fn find_autoclose(&mut self, current_time: NaiveDateTime) -> Vec<Uuid> {
|
||||
self.ongoing_events
|
||||
.iter()
|
||||
// TODO: Remove this when you do the sensible thing and add a CLOSED_EVENT structure
|
||||
.filter(|(_, e)| {
|
||||
e.close_date <= current_time && !matches!(e.status, EventStatus::Closed)
|
||||
})
|
||||
.map(|(k, _)| *k)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Find all events that are too old and supposed to be pruned.
|
||||
///
|
||||
/// Returns a vector of UUIDs corresponding to the old events.
|
||||
pub(super) fn find_old(&mut self, current_time: NaiveDateTime) -> Vec<Uuid> {
|
||||
self.ongoing_events
|
||||
.iter()
|
||||
.filter(|(_, e)| e.auto_delete_date <= current_time)
|
||||
.map(|(k, _)| *k)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Add a new event to the list of ongoing events.
|
||||
///
|
||||
/// This function also launch the event based and adds a callback with the Uuid of the original caller.
|
||||
pub(crate) fn add_event(&mut self, id: Uuid, mut event: ParsedVote) -> Result<(), IdleError> {
|
||||
event.launch()?;
|
||||
self.ongoing_events.insert(id, event);
|
||||
Ok(())
|
||||
}
|
||||
/// delete the event without launching it
|
||||
pub fn del_event(&mut self, uuid: Uuid) -> Result<ParsedVote, IdleError> {
|
||||
self.ongoing_events
|
||||
.remove(&uuid)
|
||||
.ok_or(IdleError::DeleteEvent(uuid))
|
||||
}
|
||||
|
||||
/// Close an event removing it from the ongoing event list
|
||||
pub fn close_event(&mut self, uuid: Uuid) -> Result<ParsedVote, IdleError> {
|
||||
// TODO: Another vkypoint, as we cant continue without knowing how to send a message
|
||||
let event = self
|
||||
.ongoing_events
|
||||
.remove(&uuid)
|
||||
.ok_or(IdleError::LaunchingEvent(uuid))?;
|
||||
let _str_event = event.to_string();
|
||||
// TODO: Do something with this string
|
||||
Ok(event)
|
||||
}
|
||||
// TODO: maybe add an update_ongoing(date) ?
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
@ -214,7 +222,7 @@ mod tests {
|
|||
|
||||
use crate::votes::{
|
||||
add_pending_event, close_ongoing_event, del_ongoing_event, del_pending_event,
|
||||
make_into_ongoing_event,
|
||||
events::ParsedVote, make_into_ongoing_event,
|
||||
};
|
||||
|
||||
use super::*;
|
||||
|
|
@ -234,8 +242,7 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn test_base_vote_handling() {
|
||||
// assert!(ParsedVote::default().launch(Uuid::new_v4()).is_ok());
|
||||
let ev = ParsedVote::default();
|
||||
let ev = EventPending::default();
|
||||
let ev_ctx = EventHolder::default();
|
||||
|
||||
// Can we add a vote ?
|
||||
|
|
@ -265,63 +272,70 @@ mod tests {
|
|||
let later = now.checked_add_days(Days::new(1)).unwrap();
|
||||
let very_later = now.checked_add_days(Days::new(3)).unwrap();
|
||||
let very_very_later = now.checked_add_days(Days::new(7)).unwrap();
|
||||
let event = ParsedVote {
|
||||
launch_date: later,
|
||||
close_date: very_later,
|
||||
auto_delete_date: very_very_later,
|
||||
..Default::default()
|
||||
let event = EventPending {
|
||||
parsed_vote: ParsedVote {
|
||||
launch_date: later,
|
||||
close_date: very_later,
|
||||
auto_delete_date: very_very_later,
|
||||
..Default::default()
|
||||
},
|
||||
};
|
||||
assert!(now < later);
|
||||
assert!(later < very_later);
|
||||
|
||||
let id = add_pending_event(event, &ev).unwrap();
|
||||
assert!(g!(ev.pending_events).find_upcoming(now).is_empty()); // no upcomming events
|
||||
assert!(g!(ev.ongoing_events).find_old(now).is_empty()); // no old events
|
||||
assert!(g!(ev.closed_events).find_old(now).is_empty()); // no old events
|
||||
assert!(g!(ev.ongoing_events).find_autoclose(now).is_empty()); // no old events
|
||||
g!(ev.pending_events).process_upcoming(now, &ev).unwrap(); // nothing should be processed
|
||||
g!(ev.ongoing_events).prune_old(now).unwrap(); // nothing should be processed
|
||||
g!(ev.ongoing_events).autoclose(now).unwrap(); // nothing should be processed
|
||||
g!(ev.closed_events).prune_old(now).unwrap(); // nothing should be processed
|
||||
g!(ev.ongoing_events).autoclose(now, &ev).unwrap(); // nothing should be processed
|
||||
assert!(g!(ev.pending_events).find_upcoming(now).is_empty()); // no upcomming events
|
||||
assert!(g!(ev.ongoing_events).find_old(now).is_empty()); // no old events
|
||||
assert!(g!(ev.closed_events).find_old(now).is_empty()); // no old events
|
||||
assert!(g!(ev.ongoing_events).find_autoclose(now).is_empty()); // no old events
|
||||
assert!(g!(ev.pending_events).pending_events.contains_key(&id)); // it's still here
|
||||
assert!(!g!(ev.ongoing_events).ongoing_events.contains_key(&id)); // it's wasn't processed
|
||||
assert!(!g!(ev.closed_events).closed_events.contains_key(&id)); // not deleted
|
||||
|
||||
// Now some time passed
|
||||
assert_eq!(g!(ev.pending_events).find_upcoming(later), vec![id]); // one upcomming event
|
||||
assert!(g!(ev.ongoing_events).find_old(later).is_empty()); // no old events
|
||||
assert!(g!(ev.closed_events).find_old(later).is_empty()); // no old events
|
||||
assert!(g!(ev.ongoing_events).find_autoclose(later).is_empty()); // no old events
|
||||
g!(ev.pending_events).process_upcoming(later, &ev).unwrap(); // one event should be processed
|
||||
g!(ev.ongoing_events).prune_old(later).unwrap(); // nothing should be processed
|
||||
g!(ev.ongoing_events).autoclose(later).unwrap(); // nothing should be processed
|
||||
g!(ev.closed_events).prune_old(later).unwrap(); // nothing should be processed
|
||||
g!(ev.ongoing_events).autoclose(later, &ev).unwrap(); // nothing should be processed
|
||||
assert!(!g!(ev.pending_events).pending_events.contains_key(&id)); // it's not here anymore
|
||||
assert!(g!(ev.ongoing_events).ongoing_events.contains_key(&id)); // it was processed
|
||||
assert!(!g!(ev.closed_events).closed_events.contains_key(&id)); // not deleted
|
||||
|
||||
// More time passed
|
||||
assert!(g!(ev.pending_events).find_upcoming(very_later).is_empty()); // one upcomming event
|
||||
assert_eq!(g!(ev.ongoing_events).find_autoclose(very_later), vec![id]); // one old event
|
||||
assert!(g!(ev.ongoing_events).find_old(very_later).is_empty()); // no old events
|
||||
assert!(g!(ev.closed_events).find_old(very_later).is_empty()); // no old events
|
||||
g!(ev.pending_events)
|
||||
.process_upcoming(very_later, &ev)
|
||||
.unwrap(); // no event should be processed
|
||||
g!(ev.ongoing_events).prune_old(very_later).unwrap(); // nothing should be processed
|
||||
g!(ev.ongoing_events).autoclose(very_later).unwrap(); // nothing should be processed
|
||||
g!(ev.closed_events).prune_old(very_later).unwrap(); // nothing should be processed
|
||||
g!(ev.ongoing_events).autoclose(very_later, &ev).unwrap(); // nothing should be processed
|
||||
assert!(!g!(ev.pending_events).pending_events.contains_key(&id)); // it's not here anymore
|
||||
assert!(g!(ev.ongoing_events).ongoing_events.contains_key(&id)); // it was processed
|
||||
assert!(!g!(ev.ongoing_events).ongoing_events.contains_key(&id)); // it was processed
|
||||
assert!(g!(ev.closed_events).closed_events.contains_key(&id)); // it was processed
|
||||
|
||||
// More more time passed
|
||||
assert!(g!(ev.pending_events)
|
||||
.find_upcoming(very_very_later)
|
||||
.is_empty()); // one upcomming event
|
||||
assert_eq!(g!(ev.ongoing_events).find_old(very_very_later), vec![id]); // one old event
|
||||
assert_eq!(g!(ev.closed_events).find_old(very_very_later), vec![id]); // one old event
|
||||
assert!(g!(ev.ongoing_events)
|
||||
.find_autoclose(very_very_later)
|
||||
.is_empty()); // no old events
|
||||
g!(ev.pending_events)
|
||||
.process_upcoming(very_very_later, &ev)
|
||||
.unwrap(); // no event should be processed
|
||||
g!(ev.ongoing_events).prune_old(very_very_later).unwrap(); // nothing should be processed
|
||||
g!(ev.ongoing_events).autoclose(very_very_later).unwrap(); // nothing should be processed
|
||||
g!(ev.closed_events).prune_old(very_very_later).unwrap(); // nothing should be processed
|
||||
g!(ev.ongoing_events)
|
||||
.autoclose(very_very_later, &ev)
|
||||
.unwrap(); // nothing should be processed
|
||||
assert!(!g!(ev.pending_events).pending_events.contains_key(&id)); // it's not here anymore
|
||||
assert!(!g!(ev.ongoing_events).ongoing_events.contains_key(&id)); // it was processed
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
//! They are placeholders for the actual data structures which need to be
|
||||
//! defined correctly.
|
||||
#![allow(deprecated)]
|
||||
#![warn(clippy::missing_docs_in_private_items)]
|
||||
|
||||
mod ballots;
|
||||
mod events;
|
||||
|
|
@ -21,18 +22,21 @@ mod holders;
|
|||
|
||||
// Republish
|
||||
pub use ballots::Ballot;
|
||||
pub use events::ParsedVote;
|
||||
pub use events::{EventClosed, EventOngoing, EventPending};
|
||||
pub use helper_functions::EventHolder;
|
||||
pub use helper_functions::*;
|
||||
pub use holders::{OngoingEvents, PendingEvents};
|
||||
pub use holders::{ListOngoingEvents, ListPendingEvents};
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use uuid::Uuid;
|
||||
|
||||
/// This split in two differents structs conceptually represents
|
||||
/// the fact that very differents actions will be performed for each struct
|
||||
type EventListType = HashMap<EventIdType, ParsedVote>;
|
||||
/// The hashmap of pending votes
|
||||
type PendingHashmap = HashMap<EventIdType, EventPending>;
|
||||
/// The hashmap of ongoing, opened votes
|
||||
type OngoingHashmap = HashMap<EventIdType, EventOngoing>;
|
||||
/// The hashmap of closed votes, still available for reading to check the results
|
||||
type ClosedHashmap = HashMap<EventIdType, EventClosed>;
|
||||
|
||||
/// Placeholder datatype
|
||||
///
|
||||
|
|
|
|||
|
|
@ -72,7 +72,7 @@ fn watchdog_update(current_time: DateTime<Local>, ev_ctx: EventHolder) {
|
|||
// * Clean old events if a new day passed
|
||||
if is_new_day {
|
||||
if let Err(e) = ev_ctx
|
||||
.ongoing_events
|
||||
.closed_events
|
||||
.lock()
|
||||
.unwrap()
|
||||
.prune_old(current_time.naive_local())
|
||||
|
|
@ -86,7 +86,7 @@ fn watchdog_update(current_time: DateTime<Local>, ev_ctx: EventHolder) {
|
|||
.ongoing_events
|
||||
.lock()
|
||||
.unwrap()
|
||||
.autoclose(current_time.naive_local())
|
||||
.autoclose(current_time.naive_local(), &ev_ctx)
|
||||
{
|
||||
// Process the error, log it and try to recover, this error can only happen during a difficult
|
||||
// to reproduce data race where a task would be edited or externally deleted just as it is about to be closed
|
||||
|
|
|
|||
|
|
@ -3,12 +3,12 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn generic_integration_test() {
|
||||
use idlefox::votes::{add_ongoing_event, EventHolder, ParsedVote};
|
||||
use uuid::Uuid;
|
||||
let uuid = Uuid::new_v4(); // A new id
|
||||
let parsed_vote = ParsedVote::default(); // A very legit event
|
||||
use idlefox::votes::{
|
||||
add_pending_event, make_into_ongoing_event, EventHolder, EventPending,
|
||||
};
|
||||
let ev_ctx = EventHolder::default();
|
||||
let id = add_pending_event(EventPending::default(), &ev_ctx).unwrap();
|
||||
|
||||
add_ongoing_event(uuid, parsed_vote, &ev_ctx).unwrap();
|
||||
make_into_ongoing_event(id, &ev_ctx).unwrap()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue