Skip to content

Commit

Permalink
Merge pull request #455 from tijlleenders/tijl/-/refactor_2024-03-24
Browse files Browse the repository at this point in the history
Tijl/-/refactor_2024-03-24
  • Loading branch information
tijlleenders authored Mar 24, 2024
2 parents ae3ed65 + ce8c6b4 commit d1b4d78
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 81 deletions.
39 changes: 0 additions & 39 deletions src/services/activity_generator.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,5 @@
use crate::models::{activity::Activity, budget::TimeBudgetType, calendar::Calendar, goal::Goal};

pub fn generate_simple_goal_activities(calendar: &Calendar, goals: &[Goal]) -> Vec<Activity> {
goals
.iter()
.flat_map(|goal| Activity::get_simple_activities(goal, calendar))
.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_simple_filler_activities(goal, calendar))
.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()
.flat_map(|goal| Activity::get_budget_min_day_activities(goal, calendar))
.collect::<Vec<_>>()
}

pub fn get_budget_min_week_activities(calendar: &Calendar, goals: &[Goal]) -> Vec<Activity> {
let mut get_to_week_min_budget_activities = vec![];
for budget in &calendar.budgets {
Expand Down
72 changes: 30 additions & 42 deletions src/services/activity_placer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::rc::Rc;
use std::{cmp, rc::Rc};

use crate::models::{
activity::{Activity, ActivityType, Status},
Expand All @@ -11,30 +11,39 @@ pub fn place(calendar: &mut Calendar, mut activities: Vec<Activity>) -> Vec<Acti
activity.update_overlay_with(&calendar.budgets);
}
if let Some(act_index_to_schedule) = find_act_index_to_schedule(&activities) {
if activities[act_index_to_schedule].goal_id.len() > 5 {
println!(
"Next to schedule: {:?} {:?}",
&activities[act_index_to_schedule].title,
&activities[act_index_to_schedule].goal_id[0..5]
);
} else {
println!(
"Next to schedule: {:?} {:?}",
&activities[act_index_to_schedule].title,
&activities[act_index_to_schedule].goal_id
);
}
let best_hour_index_and_size: Option<(usize, usize)> =
activities[act_index_to_schedule].get_best_scheduling_index_and_length();
let best_hour_index: usize;
let best_size: usize;
if let Some(best_hour_index_and_size) = best_hour_index_and_size {
best_hour_index = best_hour_index_and_size.0;
best_size = best_hour_index_and_size.1;
println!(
"Next to schedule: {:?} {:?}",
&activities[act_index_to_schedule].title,
&activities[act_index_to_schedule].goal_id
[0..cmp::min(activities[act_index_to_schedule].goal_id.len(), 5)]
);
if let Some((best_hour_index, best_size)) =
activities[act_index_to_schedule].get_best_scheduling_index_and_length()
{
println!(
"Best index:{:?} and size {:?}",
&best_hour_index, &best_size
);
println!("reserving {:?} hours...", best_size);
for duration_offset in 0..best_size {
Rc::make_mut(&mut calendar.hours[best_hour_index + duration_offset]);
calendar.hours[best_hour_index + duration_offset] = Rc::new(Hour::Occupied {
activity_index: act_index_to_schedule,
activity_title: activities[act_index_to_schedule].title.clone(),
activity_goalid: activities[act_index_to_schedule].goal_id.clone(),
});
calendar.update_budgets_for(
&activities[act_index_to_schedule].goal_id.clone(),
best_hour_index + duration_offset,
);
activities[act_index_to_schedule].duration_left -= 1;
}
if activities[act_index_to_schedule].duration_left == 0 {
activities[act_index_to_schedule].status = Status::Scheduled;
(activities[act_index_to_schedule]).release_claims();
}

dbg!(&calendar);
} else {
activities[act_index_to_schedule].release_claims();
if activities[act_index_to_schedule].activity_type == ActivityType::BudgetMinDay {
Expand All @@ -52,27 +61,6 @@ pub fn place(calendar: &mut Calendar, mut activities: Vec<Activity>) -> Vec<Acti
calendar.impossible_activities.push(impossible_activity);
continue;
}

println!("reserving {:?} hours...", best_size);
for duration_offset in 0..best_size {
Rc::make_mut(&mut calendar.hours[best_hour_index + duration_offset]);
calendar.hours[best_hour_index + duration_offset] = Rc::new(Hour::Occupied {
activity_index: act_index_to_schedule,
activity_title: activities[act_index_to_schedule].title.clone(),
activity_goalid: activities[act_index_to_schedule].goal_id.clone(),
});
calendar.update_budgets_for(
&activities[act_index_to_schedule].goal_id.clone(),
best_hour_index + duration_offset,
);
activities[act_index_to_schedule].duration_left -= 1;
}
if activities[act_index_to_schedule].duration_left == 0 {
activities[act_index_to_schedule].status = Status::Scheduled;
(activities[act_index_to_schedule]).release_claims();
}

dbg!(&calendar);
} else {
println!("Tried to schedule activity index None");
break;
Expand Down

0 comments on commit d1b4d78

Please sign in to comment.