Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tijl/-/add-filler-goal2 #454

Merged
merged 8 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,23 @@ pub fn run_scheduler(
activity_generator::generate_simple_goal_activities(&calendar, goals);
dbg!(&simple_goal_activities);

let simple_filler_activities =
activity_generator::generate_simple_filler_goal_activities(&calendar, goals);
dbg!(&simple_filler_activities);

//generate and place budget goal activities
let budget_goal_activities: Vec<Activity> =
activity_generator::generate_budget_goal_activities(&calendar, goals);
dbg!(&budget_goal_activities);
dbg!(&calendar);

activity_placer::place(&mut calendar, simple_goal_activities);
activity_placer::place(&mut calendar, budget_goal_activities);
let activities: Vec<Activity> = [
simple_goal_activities,
simple_filler_activities,
budget_goal_activities,
]
.concat();
activity_placer::place(&mut calendar, activities);

calendar.log_impossible_min_day_budgets();

Expand Down
57 changes: 55 additions & 2 deletions src/models/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,14 @@ impl Activity {
pub(crate) fn get_activities_from_simple_goal(
goal: &Goal,
calendar: &Calendar,
parent_goal: Option<Goal>,
) -> Vec<Activity> {
if goal.children.is_some() || goal.filters.as_ref().is_some() {
return vec![];
}
let (adjusted_goal_start, adjusted_goal_deadline) = goal.get_adj_start_deadline(calendar);

let (adjusted_goal_start, adjusted_goal_deadline) =
goal.get_adj_start_deadline(calendar, parent_goal);
let mut activities: Vec<Activity> = Vec::with_capacity(1);

if let Some(activity_total_duration) = goal.min_duration {
Expand Down Expand Up @@ -237,7 +240,8 @@ impl Activity {
return vec![];
}
}
let (adjusted_goal_start, adjusted_goal_deadline) = goal.get_adj_start_deadline(calendar);
let (adjusted_goal_start, adjusted_goal_deadline) =
goal.get_adj_start_deadline(calendar, None);
let mut activities: Vec<Activity> = Vec::with_capacity(1);

for day in 0..(adjusted_goal_deadline - adjusted_goal_start).num_days() as u64 {
Expand Down Expand Up @@ -472,6 +476,55 @@ impl Activity {
}
self.calendar_overlay = empty_overlay;
}

pub(crate) fn get_filler_activities_from_simple_goal(
goal: &Goal,
calendar: &Calendar,
parent_goal: Option<Goal>,
) -> Vec<Activity> {
if goal.children.is_none() || goal.filters.as_ref().is_some() {
return vec![];
}
let (adjusted_goal_start, adjusted_goal_deadline) =
goal.get_adj_start_deadline(calendar, parent_goal);
let mut activities: Vec<Activity> = Vec::with_capacity(1);

if let Some(activity_total_duration) = goal.min_duration {
let mut min_block_size = activity_total_duration;
if activity_total_duration > 8 {
min_block_size = 1;
//todo!() //split into multiple activities so flexibilities are correct??
// or yield flex 1 or maximum of the set from activity.flex()?
};

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

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

let activity = Activity {
goal_id: goal.id.clone(),
activity_type: ActivityType::SimpleGoal,
title: goal.title.clone(),
min_block_size,
max_block_size: min_block_size,
calendar_overlay: compatible_hours_overlay,
time_budgets: vec![],
total_duration: activity_total_duration,
duration_left: activity_total_duration,
status: Status::Unprocessed,
};
dbg!(&activity);
activities.push(activity);
}

activities
}
}

#[derive(Debug, PartialEq, Clone, Deserialize)]
Expand Down
32 changes: 31 additions & 1 deletion src/models/goal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ 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,
parent_goal: Option<Goal>,
) -> (NaiveDateTime, 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;
Expand All @@ -55,6 +59,19 @@ impl Goal {
if self.deadline.year() == 1970 {
adjusted_goal_deadline = calendar.end_date_time;
}

// Make sure child goal not fall outside of parent goal start and deadline
if let Some(parent_goal) = parent_goal {
// means this is a child goal
if adjusted_goal_start < parent_goal.start {
adjusted_goal_start = parent_goal.start;
}
if adjusted_goal_deadline > parent_goal.deadline && parent_goal.deadline.year() != 1970
{
adjusted_goal_deadline = parent_goal.deadline;
}
}

if self.filters.is_none() {
return (adjusted_goal_start, adjusted_goal_deadline);
}
Expand All @@ -78,4 +95,17 @@ impl Goal {
}
(adjusted_goal_start, adjusted_goal_deadline)
}

/// Get parent goal of this goal based in provided list of goals
pub fn get_parent_goal(&self, goals: &[Goal]) -> Option<Goal> {
let parent_goal = goals.iter().find(|goal| {
if let Some(childs) = &goal.children {
childs.contains(&self.id)
} else {
false
}
});

parent_goal.cloned()
}
}
35 changes: 34 additions & 1 deletion src/services/activity_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,43 @@ use crate::models::{activity::Activity, budget::TimeBudgetType, calendar::Calend
pub fn generate_simple_goal_activities(calendar: &Calendar, goals: &[Goal]) -> Vec<Activity> {
goals
.iter()
.flat_map(|goal| Activity::get_activities_from_simple_goal(goal, calendar))
.flat_map(|goal| {
Activity::get_activities_from_simple_goal(goal, calendar, goal.get_parent_goal(goals))
})
.collect::<Vec<_>>()
}

pub fn generate_simple_filler_goal_activities(
calendar: &Calendar,
goals: &[Goal],
) -> Vec<Activity> {
let mut activities = goals
.iter()
.flat_map(|goal| {
Activity::get_filler_activities_from_simple_goal(
goal,
calendar,
goal.get_parent_goal(goals),
)
})
.collect::<Vec<_>>();
for activity in &mut activities {
if let Some(goal) = goals.iter().find(|g| g.id == activity.goal_id) {
let children: Vec<&Goal> = goals
.iter()
.filter(|child| goal.children.clone().unwrap().contains(&child.id))
.collect();
for c in children {
activity.min_block_size -= c.min_duration.unwrap();
activity.max_block_size -= c.min_duration.unwrap();
activity.total_duration -= c.min_duration.unwrap();
activity.duration_left -= c.min_duration.unwrap();
}
}
}
activities
}

pub fn generate_budget_goal_activities(calendar: &Calendar, goals: &[Goal]) -> Vec<Activity> {
goals
.iter()
Expand Down
18 changes: 13 additions & 5 deletions tests/jsons/stable/budget-and-goal-one-day/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,26 @@
},
{
"taskid": 3,
"goalid": "free",
"title": "free",
"duration": 2,
"start": "2022-09-01T13:00:00",
"deadline": "2022-09-01T15:00:00"
},
{
"taskid": 4,
"goalid": "2",
"title": "work",
"duration": 4,
"start": "2022-09-01T13:00:00",
"deadline": "2022-09-01T17:00:00"
"start": "2022-09-01T15:00:00",
"deadline": "2022-09-01T19:00:00"
},
{
"taskid": 4,
"taskid": 5,
"goalid": "free",
"title": "free",
"duration": 7,
"start": "2022-09-01T17:00:00",
"duration": 5,
"start": "2022-09-01T19:00:00",
"deadline": "2022-09-02T00:00:00"
}
]
Expand Down
18 changes: 13 additions & 5 deletions tests/jsons/stable/budget-and-goal-one-day/observed.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,26 @@
},
{
"taskid": 3,
"goalid": "free",
"title": "free",
"duration": 2,
"start": "2022-09-01T13:00:00",
"deadline": "2022-09-01T15:00:00"
},
{
"taskid": 4,
"goalid": "2",
"title": "work",
"duration": 4,
"start": "2022-09-01T13:00:00",
"deadline": "2022-09-01T17:00:00"
"start": "2022-09-01T15:00:00",
"deadline": "2022-09-01T19:00:00"
},
{
"taskid": 4,
"taskid": 5,
"goalid": "free",
"title": "free",
"duration": 7,
"start": "2022-09-01T17:00:00",
"duration": 5,
"start": "2022-09-01T19:00:00",
"deadline": "2022-09-02T00:00:00"
}
]
Expand Down
11 changes: 6 additions & 5 deletions tests/jsons/stable/default-budgets/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
"title": "Daily habits 🔁",
"createdAt": "2024-01-08T10:59:47.134Z",
"children": [
"breakfast",
"lunch",
"dinner",
"meTime",
"walk"
"40842a7d-c282-406f-9cdf-3d1fbd8e4f61",
"18be6978-ffac-46db-8b70-d321c311428a",
"103b2eff-6ba5-47b5-ad4f-81d8e8ef5998",
"445f787b-d742-4441-9744-e81c286aa3c8",
"77e1f762-3a4f-44a3-8f24-1560641a3548",
"49b05463-56a0-4af5-9034-83822abf24f6"
]
},
{
Expand Down
50 changes: 50 additions & 0 deletions tests/jsons/stable/filler-goal/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"scheduled": [
{
"day": "2022-01-01",
"tasks": [
{
"taskid": 0,
"goalid": "free",
"title": "free",
"duration": 10,
"start": "2022-01-01T00:00:00",
"deadline": "2022-01-01T10:00:00"
},
{
"taskid": 1,
"goalid": "2",
"title": "Buy stuff",
"duration": 1,
"start": "2022-01-01T10:00:00",
"deadline": "2022-01-01T11:00:00"
},
{
"taskid": 2,
"goalid": "3",
"title": "Invite friends",
"duration": 1,
"start": "2022-01-01T11:00:00",
"deadline": "2022-01-01T12:00:00"
},
{
"taskid": 3,
"goalid": "1",
"title": "Plan a party",
"duration": 2,
"start": "2022-01-01T12:00:00",
"deadline": "2022-01-01T14:00:00"
},
{
"taskid": 4,
"goalid": "free",
"title": "free",
"duration": 10,
"start": "2022-01-01T14:00:00",
"deadline": "2022-01-02T00:00:00"
}
]
}
],
"impossible": []
}
27 changes: 27 additions & 0 deletions tests/jsons/stable/filler-goal/input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"startDate": "2022-01-01T00:00:00",
"endDate": "2022-01-02T00:00:00",
"goals": [
{
"id": "1",
"title": "Plan a party",
"minDuration": 4,
"start": "2022-01-01T10:00:00",
"deadline": "2022-01-01T18:00:00",
"children": [
"2",
"3"
]
},
{
"id": "2",
"title": "Buy stuff",
"minDuration": 1
},
{
"id": "3",
"title": "Invite friends",
"minDuration": 1
}
]
}
Loading
Loading