Skip to content

Commit

Permalink
Merge pull request #460 from tijlleenders/tijl/-/test-tasks-done-today
Browse files Browse the repository at this point in the history
Tijl/-/test-tasks-done-today
  • Loading branch information
tijlleenders authored Apr 27, 2024
2 parents fde4c80 + e6fe1fc commit af79c30
Show file tree
Hide file tree
Showing 26 changed files with 1,980 additions and 25 deletions.
7 changes: 6 additions & 1 deletion build_templates/tests_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ mod TEST_MODULE_NAME {
let input: Input = input_output::get_input_from_json(input_path).unwrap();
let desired_output: String = input_output::get_output_string_from_json(output_path);

let output = scheduler::run_scheduler(input.start_date, input.end_date, &input.goals);
let output = scheduler::run_scheduler(
input.start_date,
input.end_date,
&input.goals,
&input.tasks_completed_today,
);

let actual_output = serde_json::to_string_pretty(&output).unwrap();

Expand Down
13 changes: 11 additions & 2 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use serde::Deserialize;
use serde_json::Value;
use std::{fs, path::Path};
extern crate scheduler;
use scheduler::{models::goal::Goal, run_scheduler};
use scheduler::{
models::{goal::Goal, task::TaskCompletedToday},
run_scheduler,
};
fn main() {
println!("Running!");
let path = Path::new("./tests/jsons/stable/algorithm-challenge/input.json");
Expand All @@ -12,7 +15,12 @@ fn main() {
dbg!(&json);
let input: Input = serde_json::from_value(json).unwrap();
dbg!(&input);
run_scheduler(input.start_date, input.end_date, &input.goals);
run_scheduler(
input.start_date,
input.end_date,
&input.goals,
&input.tasks_completed_today,
);
}

#[derive(Deserialize, Debug)]
Expand All @@ -21,4 +29,5 @@ struct Input {
start_date: NaiveDateTime,
end_date: NaiveDateTime,
goals: Vec<Goal>,
tasks_completed_today: Vec<TaskCompletedToday>,
}
15 changes: 14 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
//! quality perception of the ZinZen&reg; projects.
use chrono::NaiveDateTime;
use models::task::TaskCompletedToday;
use models::{calendar::Calendar, goal::Goal, task::FinalTasks};
use serde_wasm_bindgen::{from_value, to_value};
use services::activity_generator;
Expand Down Expand Up @@ -80,14 +81,20 @@ pub fn schedule(input: &JsValue) -> Result<JsValue, JsError> {
console_error_panic_hook::set_once();
// JsError implements From<Error>, so we can just use `?` on any Error
let input: Input = from_value(input.clone())?;
let final_tasks = run_scheduler(input.start_date, input.end_date, &input.goals);
let final_tasks = run_scheduler(
input.start_date,
input.end_date,
&input.goals,
&input.tasks_completed_today,
);
Ok(to_value(&final_tasks)?)
}

pub fn run_scheduler(
start_date: NaiveDateTime,
end_date: NaiveDateTime,
goals: &[Goal],
tasks_completed_today: &[TaskCompletedToday],
) -> FinalTasks {
let mut calendar = Calendar::new(start_date, end_date);
dbg!(&calendar);
Expand All @@ -96,6 +103,12 @@ pub fn run_scheduler(

let mut base_activities = activity_generator::get_base_activities(&calendar, goals);

base_activities = activity_placer::place_tasks_completed_today(
&mut calendar,
base_activities,
tasks_completed_today,
);

base_activities = activity_placer::place(&mut calendar, base_activities);

calendar.log_impossible_min_day_budgets();
Expand Down
6 changes: 3 additions & 3 deletions src/models/budget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ pub struct Budget {
pub time_filters: Filters,
}
impl Budget {
pub fn reduce_for_(&mut self, goal: &str, duration_offset: usize) {
pub fn reduce_for_(&mut self, goal: &str, cal_index: usize) {
if self.participating_goals.contains(&goal.to_string()) {
let iterator = self.time_budgets.iter_mut().enumerate();
for (_, time_budget) in iterator {
if duration_offset >= time_budget.calendar_start_index
&& duration_offset < time_budget.calendar_end_index
if cal_index >= time_budget.calendar_start_index
&& cal_index < time_budget.calendar_end_index
{
time_budget.scheduled += 1
}
Expand Down
4 changes: 2 additions & 2 deletions src/models/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,10 @@ impl Calendar {
}
}

pub fn update_budgets_for(&mut self, goal: &str, duration_offset: usize) {
pub fn update_budgets_for(&mut self, goal: &str, cal_index: usize) {
let iterator = self.budgets.iter_mut();
for budget in iterator {
budget.reduce_for_(goal, duration_offset);
budget.reduce_for_(goal, cal_index);
}
}

Expand Down
9 changes: 8 additions & 1 deletion src/models/task.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
///Tasks are only used for outputting
use chrono::{NaiveDate, NaiveDateTime};
use serde::Serialize;
use serde::{Deserialize, Serialize};

use super::calendar::ImpossibleActivity;

Expand All @@ -26,3 +26,10 @@ pub struct DayTasks {
pub day: NaiveDate,
pub tasks: Vec<Task>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct TaskCompletedToday {
pub goalid: String,
pub start: NaiveDateTime,
pub deadline: NaiveDateTime,
}
57 changes: 57 additions & 0 deletions src/services/activity_placer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::models::{
activity::{Activity, ActivityType, Status},
calendar::{Calendar, Hour, ImpossibleActivity},
task::TaskCompletedToday,
};
use std::{cmp, rc::Rc};

Expand Down Expand Up @@ -132,3 +133,59 @@ pub(crate) fn reset_postponed(mut base_activities: Vec<Activity>) -> Vec<Activit
}
base_activities
}

pub(crate) fn place_tasks_completed_today(
calendar: &mut Calendar,
mut base_activities: Vec<Activity>,
tasks_completed_today: &[TaskCompletedToday],
) -> Vec<Activity> {
dbg!(&calendar);
for task in tasks_completed_today.iter() {
let start_index = calendar.get_index_of(task.start);
let end_index = calendar.get_index_of(task.deadline);

for hour_index in start_index..end_index {
let mut act_index_to_schedule: Option<usize> = None;
for (act_index, activity) in base_activities.iter().enumerate() {
if activity.goal_id == task.goalid
//by default, just pick the first
//but if you find one that has is claiming the index - use that
{
if act_index_to_schedule.is_none() {
act_index_to_schedule = Some(act_index);
}
if activity.calendar_overlay[hour_index].is_some() {
//check if hour is claimed and if so - override and break
act_index_to_schedule = Some(act_index);
break;
}
}
}

//hardcode hours in calendar per hour:
if let Some(act_index_to_schedule) = act_index_to_schedule {
println!("hardcoding {:?} hours...", end_index - start_index);
Rc::make_mut(&mut calendar.hours[hour_index]);
calendar.hours[hour_index] = Rc::new(Hour::Occupied {
activity_index: act_index_to_schedule,
activity_title: base_activities[act_index_to_schedule].title.clone(),
activity_goalid: base_activities[act_index_to_schedule].goal_id.clone(),
});
calendar.update_budgets_for(
&base_activities[act_index_to_schedule].goal_id.clone(),
hour_index,
);
if base_activities[act_index_to_schedule].duration_left > 0 {
base_activities[act_index_to_schedule].duration_left -= 1;
};
if base_activities[act_index_to_schedule].duration_left == 0 {
base_activities[act_index_to_schedule].status = Status::Scheduled;
(base_activities[act_index_to_schedule]).release_claims();
}
}
}
}
dbg!(&calendar);

base_activities
}
2 changes: 2 additions & 0 deletions src/technical/input_output.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::models::goal::Goal;
use crate::models::task::TaskCompletedToday;
use chrono::NaiveDateTime;
use serde::Deserialize;
use std::error::Error;
Expand All @@ -14,6 +15,7 @@ pub struct Input {
pub start_date: NaiveDateTime,
pub end_date: NaiveDateTime,
pub goals: Vec<Goal>,
pub tasks_completed_today: Vec<TaskCompletedToday>,
}

pub fn get_input_from_json<P: AsRef<Path>>(path: P) -> Result<Input, Box<dyn Error>> {
Expand Down
3 changes: 2 additions & 1 deletion tests/jsons/stable/after-12/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
"maxPerWeek": 7
}
}
]
],
"tasksCompletedToday": []
}
3 changes: 2 additions & 1 deletion tests/jsons/stable/algorithm-challenge/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@
"start": "2022-01-01T12:00:00",
"deadline": "2022-01-01T14:00:00"
}
]
],
"tasksCompletedToday": []
}
3 changes: 2 additions & 1 deletion tests/jsons/stable/basic-1/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@
"start": "2022-01-01T13:00:00",
"deadline": "2022-01-01T18:00:00"
}
]
],
"tasksCompletedToday": []
}
3 changes: 2 additions & 1 deletion tests/jsons/stable/budget-and-goal-one-day/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@
"maxPerWeek": 56
}
}
]
],
"tasksCompletedToday": []
}
3 changes: 2 additions & 1 deletion tests/jsons/stable/budget-with-subgoal/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
"createdAt": "2024-01-08T10:59:47.137Z",
"minDuration": 1
}
]
],
"tasksCompletedToday": []
}
3 changes: 2 additions & 1 deletion tests/jsons/stable/bug-start-missing/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"createdAt": "2024-01-08T19:44:40.695Z",
"minDuration": 1
}
]
],
"tasksCompletedToday": []
}
3 changes: 2 additions & 1 deletion tests/jsons/stable/conflict-without-deadline/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
"start": "2022-01-01T00:00:00",
"deadline": "2022-01-01T12:00:00"
}
]
],
"tasksCompletedToday": []
}
3 changes: 2 additions & 1 deletion tests/jsons/stable/default-budgets/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -248,5 +248,6 @@
"maxPerWeek": 7
}
}
]
],
"tasksCompletedToday": []
}
3 changes: 2 additions & 1 deletion tests/jsons/stable/filler-goal/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
"start": "2022-01-01T10:00:00",
"deadline": "2022-01-01T18:00:00"
}
]
],
"tasksCompletedToday": []
}
3 changes: 2 additions & 1 deletion tests/jsons/stable/impossible/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@
"minDuration": 3,
"start": "2022-01-01T23:00:00"
}
]
],
"tasksCompletedToday": []
}
3 changes: 2 additions & 1 deletion tests/jsons/stable/issue-overflow/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
"createdAt": 1691295894105,
"minDuration": 20
}
]
],
"tasksCompletedToday": []
}
3 changes: 2 additions & 1 deletion tests/jsons/stable/not-on/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
}
]
}
]
],
"tasksCompletedToday": []
}
3 changes: 2 additions & 1 deletion tests/jsons/stable/simple-without-deadline/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
"start": "2022-01-01T23:00:00",
"deadline": "2022-01-02T00:00:00"
}
]
],
"tasksCompletedToday": []
}
3 changes: 2 additions & 1 deletion tests/jsons/stable/sleep-1/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
]
}
}
]
],
"tasksCompletedToday": []
}
3 changes: 2 additions & 1 deletion tests/jsons/stable/start-before-cal-start/input.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"start": "2000-01-01T00:00:00",
"deadline": "2022-01-01T13:00:00"
}
]
],
"tasksCompletedToday": []
}
Loading

0 comments on commit af79c30

Please sign in to comment.