Skip to content

Commit

Permalink
Merge pull request #439 from tijlleenders/tijl/-/add-test-impossible
Browse files Browse the repository at this point in the history
Tijl/-/add-test-impossible
  • Loading branch information
tijlleenders authored Mar 24, 2024
2 parents d1b4d78 + aaeb796 commit fc1bacb
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 54 deletions.
28 changes: 22 additions & 6 deletions src/models/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ pub struct Activity {
pub total_duration: usize,
pub duration_left: usize,
pub status: Status,
pub deadline: NaiveDateTime,
pub start: NaiveDateTime,
pub deadline: Option<NaiveDateTime>,
}
impl Activity {
pub fn get_compatible_hours_overlay(
calendar: &Calendar,
filter_option: Option<Filters>,
adjusted_goal_start: NaiveDateTime,
adjusted_goal_deadline: NaiveDateTime,
adjusted_activity_deadline: NaiveDateTime,
not_on: Option<Vec<Slot>>,
) -> Vec<Option<Weak<Hour>>> {
let mut compatible_hours_overlay: Vec<Option<Weak<Hour>>> =
Expand Down Expand Up @@ -81,7 +82,7 @@ impl Activity {
if hour_index < calendar.get_index_of(adjusted_goal_start) {
compatible = false;
}
if hour_index >= calendar.get_index_of(adjusted_goal_deadline) {
if hour_index >= calendar.get_index_of(adjusted_activity_deadline) {
compatible = false;
}

Expand Down Expand Up @@ -193,11 +194,14 @@ impl Activity {

let filters_option: Option<Filters> = calendar.get_filters_for(goal.id.clone());

let adjusted_activity_deadline =
adjusted_goal_deadline.unwrap_or(calendar.end_date_time);

let compatible_hours_overlay = Activity::get_compatible_hours_overlay(
calendar,
filters_option,
adjusted_goal_start,
adjusted_goal_deadline,
adjusted_activity_deadline,
goal.not_on.clone(),
);

Expand All @@ -211,6 +215,7 @@ impl Activity {
total_duration: activity_total_duration,
duration_left: activity_total_duration,
status: Status::Unprocessed,
start: adjusted_goal_start,
deadline: goal.deadline,
};
dbg!(&activity);
Expand All @@ -232,7 +237,10 @@ impl Activity {
let (adjusted_goal_start, adjusted_goal_deadline) = goal.get_adj_start_deadline(calendar);
let mut activities: Vec<Activity> = Vec::with_capacity(1);

for day in 0..(adjusted_goal_deadline - adjusted_goal_start).num_days() as u64 {
for day in 0..(adjusted_goal_deadline.unwrap_or(calendar.end_date_time)
- adjusted_goal_start)
.num_days() as u64
{
if let Some(filter_option) = &goal.filters {
if filter_option
.on_days
Expand Down Expand Up @@ -273,6 +281,7 @@ impl Activity {
total_duration: adjusted_min_block_size,
duration_left: config.min_per_day,
status: Status::Unprocessed,
start: adjusted_goal_start,
deadline: goal.deadline,
};
dbg!(&activity);
Expand Down Expand Up @@ -315,6 +324,7 @@ impl Activity {
total_duration: max_hours,
duration_left: max_hours,
status: Status::Unprocessed,
start: goal_to_use.start,
deadline: goal_to_use.deadline,
});

Expand Down Expand Up @@ -354,6 +364,7 @@ impl Activity {
total_duration: max_hours,
duration_left: max_hours,
status: Status::Unprocessed,
start: goal_to_use.start,
deadline: goal_to_use.deadline,
});

Expand Down Expand Up @@ -454,6 +465,7 @@ impl Activity {

if self.flex() == 0 {
self.status = Status::Impossible;
self.release_claims();
}
}

Expand Down Expand Up @@ -483,11 +495,14 @@ impl Activity {

let filters_option: Option<Filters> = calendar.get_filters_for(goal.id.clone());

let adjusted_activity_deadline =
adjusted_goal_deadline.unwrap_or(calendar.end_date_time);

let compatible_hours_overlay = Activity::get_compatible_hours_overlay(
calendar,
filters_option,
adjusted_goal_start,
adjusted_goal_deadline,
adjusted_activity_deadline,
goal.not_on.clone(),
);

Expand All @@ -501,6 +516,7 @@ impl Activity {
total_duration: activity_total_duration,
duration_left: activity_total_duration,
status: Status::Unprocessed,
start: adjusted_goal_start,
deadline: goal.deadline,
};
dbg!(&activity);
Expand Down
16 changes: 8 additions & 8 deletions src/models/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct ImpossibleActivity {
pub id: String,
pub hours_missing: usize,
pub period_start_date_time: NaiveDateTime,
pub period_end_date_time: NaiveDateTime,
pub period_end_date_time: Option<NaiveDateTime>,
}

pub struct Calendar {
Expand Down Expand Up @@ -306,14 +306,13 @@ impl Calendar {

pub fn log_impossible_base_activities(&mut self, activities: Vec<Activity>) {
for activity in activities {
if activity.status == super::activity::Status::Impossible
&& activity.deadline.year() != 1970
if activity.status == super::activity::Status::Impossible && activity.deadline.is_some()
{
self.impossible_activities.push(ImpossibleActivity {
id: activity.goal_id.clone(),
hours_missing: activity.duration_left,
period_start_date_time: self.start_date_time,
period_end_date_time: self.end_date_time,
period_start_date_time: activity.start,
period_end_date_time: activity.deadline,
})
}
}
Expand All @@ -335,9 +334,10 @@ impl Calendar {
period_start_date_time: self
.start_date_time
.add(Duration::hours(time_budget.calendar_start_index as i64)),
period_end_date_time: self
.start_date_time
.add(Duration::hours(time_budget.calendar_end_index as i64)),
period_end_date_time: Some(
self.start_date_time
.add(Duration::hours(time_budget.calendar_end_index as i64)),
),
});
}
}
Expand Down
22 changes: 15 additions & 7 deletions src/models/goal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct Goal {
#[serde(default)]
pub start: NaiveDateTime,
#[serde(default)]
pub deadline: NaiveDateTime,
pub deadline: Option<NaiveDateTime>,
#[serde(rename = "budget")]
pub budget_config: Option<BudgetConfig>,
pub filters: Option<Filters>,
Expand Down Expand Up @@ -46,14 +46,17 @@ pub struct BudgetConfig {
}

impl Goal {
pub fn get_adj_start_deadline(&self, calendar: &Calendar) -> (NaiveDateTime, NaiveDateTime) {
pub fn get_adj_start_deadline(
&self,
calendar: &Calendar,
) -> (NaiveDateTime, Option<NaiveDateTime>) {
let mut adjusted_goal_start = self.start;
if self.start.year() == 1970 || self.start < calendar.start_date_time {
adjusted_goal_start = calendar.start_date_time;
}
let mut adjusted_goal_deadline = self.deadline;
if self.deadline.year() == 1970 {
adjusted_goal_deadline = calendar.end_date_time;
if self.deadline.is_none() {
adjusted_goal_deadline = None;
}

if self.filters.is_none() {
Expand All @@ -73,9 +76,14 @@ impl Goal {
.sub(Duration::hours(24))
.add(Duration::hours(filter_option.after_time as i64));
println!("... to {:?}", &adjusted_goal_start);
adjusted_goal_deadline = adjusted_goal_start.add(Duration::days(
(adjusted_goal_deadline - adjusted_goal_start).num_days() + 1,
));
adjusted_goal_deadline = Some(
adjusted_goal_start.add(Duration::days(
(adjusted_goal_deadline.unwrap_or(calendar.end_date_time)
- adjusted_goal_start)
.num_days()
+ 1,
)),
);
}
(adjusted_goal_start, adjusted_goal_deadline)
}
Expand Down
7 changes: 3 additions & 4 deletions src/services/activity_placer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::{cmp, rc::Rc};

use crate::models::{
activity::{Activity, ActivityType, Status},
calendar::{Calendar, Hour, ImpossibleActivity},
};
use std::{cmp, rc::Rc};

pub fn place(calendar: &mut Calendar, mut activities: Vec<Activity>) -> Vec<Activity> {
loop {
Expand Down Expand Up @@ -55,8 +54,8 @@ pub fn place(calendar: &mut Calendar, mut activities: Vec<Activity>) -> Vec<Acti
let impossible_activity = ImpossibleActivity {
id: activities[act_index_to_schedule].goal_id.clone(),
hours_missing: activities[act_index_to_schedule].duration_left,
period_start_date_time: calendar.start_date_time,
period_end_date_time: calendar.end_date_time,
period_start_date_time: activities[act_index_to_schedule].start,
period_end_date_time: activities[act_index_to_schedule].deadline,
};
calendar.impossible_activities.push(impossible_activity);
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,28 @@
"taskid": 0,
"goalid": "free",
"title": "free",
"duration": 11,
"duration": 10,
"start": "2022-01-01T00:00:00",
"deadline": "2022-01-01T11:00:00"
"deadline": "2022-01-01T10:00:00"
},
{
"taskid": 1,
"goalid": "1",
"title": "shopping",
"duration": 1,
"start": "2022-01-01T10:00:00",
"deadline": "2022-01-01T11:00:00"
},
{
"taskid": 2,
"goalid": "2",
"title": "dentist",
"duration": 1,
"start": "2022-01-01T11:00:00",
"deadline": "2022-01-01T12:00:00"
},
{
"taskid": 2,
"taskid": 3,
"goalid": "free",
"title": "free",
"duration": 12,
Expand All @@ -30,5 +38,12 @@
]
}
],
"impossible": []
"impossible": [
{
"id": "3",
"hoursMissing": 1,
"periodStartDateTime": "2022-01-01T10:00:00",
"periodEndDateTime": "2022-01-01T12:00:00"
}
]
}
33 changes: 33 additions & 0 deletions tests/jsons/stable/impossible/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"startDate": "2022-01-01T00:00:00",
"endDate": "2022-01-02T00:00:00",
"goals": [
{
"id": "1",
"title": "shopping",
"minDuration": 1,
"start": "2022-01-01T10:00:00",
"deadline": "2022-01-01T12:00:00"
},
{
"id": "2",
"title": "dentist",
"minDuration": 1,
"start": "2022-01-01T10:00:00",
"deadline": "2022-01-01T12:00:00"
},
{
"id": "3",
"title": "exercise",
"minDuration": 1,
"start": "2022-01-01T10:00:00",
"deadline": "2022-01-01T12:00:00"
},
{
"id": "4",
"title": "someday-but-not-impossible-since-no-deadline",
"minDuration": 3,
"start": "2022-01-01T23:00:00"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,28 @@
"taskid": 0,
"goalid": "free",
"title": "free",
"duration": 11,
"duration": 10,
"start": "2022-01-01T00:00:00",
"deadline": "2022-01-01T11:00:00"
"deadline": "2022-01-01T10:00:00"
},
{
"taskid": 1,
"goalid": "1",
"title": "shopping",
"duration": 1,
"start": "2022-01-01T10:00:00",
"deadline": "2022-01-01T11:00:00"
},
{
"taskid": 2,
"goalid": "2",
"title": "dentist",
"duration": 1,
"start": "2022-01-01T11:00:00",
"deadline": "2022-01-01T12:00:00"
},
{
"taskid": 2,
"taskid": 3,
"goalid": "free",
"title": "free",
"duration": 12,
Expand All @@ -30,5 +38,12 @@
]
}
],
"impossible": []
"impossible": [
{
"id": "3",
"hoursMissing": 1,
"periodStartDateTime": "2022-01-01T10:00:00",
"periodEndDateTime": "2022-01-01T12:00:00"
}
]
}
19 changes: 0 additions & 19 deletions tests/jsons/stable/not-on/input.json

This file was deleted.

2 changes: 1 addition & 1 deletion tests/jsons/stable/simple-without-deadline/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
{
"id": "3",
"hoursMissing": 1,
"periodStartDateTime": "2022-01-01T00:00:00",
"periodStartDateTime": "2022-01-01T23:00:00",
"periodEndDateTime": "2022-01-02T00:00:00"
}
]
Expand Down
2 changes: 1 addition & 1 deletion tests/jsons/stable/simple-without-deadline/observed.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
{
"id": "3",
"hoursMissing": 1,
"periodStartDateTime": "2022-01-01T00:00:00",
"periodStartDateTime": "2022-01-01T23:00:00",
"periodEndDateTime": "2022-01-02T00:00:00"
}
]
Expand Down

0 comments on commit fc1bacb

Please sign in to comment.