Skip to content

Commit

Permalink
Use 7 day average for training RPE
Browse files Browse the repository at this point in the history
  • Loading branch information
senier committed Nov 21, 2024
1 parent dc8c616 commit ac7d9ab
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 58 deletions.
46 changes: 0 additions & 46 deletions frontend/src/ui/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ pub fn init(url: Url, _orders: &mut impl Orders<Msg>) -> Model {
training_stats: TrainingStats {
short_term_load: Vec::new(),
long_term_load: Vec::new(),
avg_rpe_per_week: Vec::new(),
},
settings,
ongoing_training_session,
Expand Down Expand Up @@ -272,7 +271,6 @@ pub struct CycleStats {
pub struct TrainingStats {
pub short_term_load: Vec<(NaiveDate, f32)>,
pub long_term_load: Vec<(NaiveDate, f32)>,
pub avg_rpe_per_week: Vec<(NaiveDate, f32)>,
}

impl TrainingStats {
Expand All @@ -292,7 +290,6 @@ impl TrainingStats {
pub fn clear(&mut self) {
self.short_term_load.clear();
self.long_term_load.clear();
self.avg_rpe_per_week.clear();
}
}

Expand Down Expand Up @@ -876,7 +873,6 @@ fn calculate_training_stats(training_sessions: &[&TrainingSession]) -> TrainingS
TrainingStats {
short_term_load,
long_term_load,
avg_rpe_per_week: calculate_avg_rpe_per_week(training_sessions),
}
}

Expand Down Expand Up @@ -938,48 +934,6 @@ fn calculate_average_weighted_sum_of_load(
.collect::<Vec<_>>()
}

fn calculate_avg_rpe_per_week(training_sessions: &[&TrainingSession]) -> Vec<(NaiveDate, f32)> {
let mut result: BTreeMap<NaiveDate, Vec<f32>> = training_session_weeks(training_sessions);

for t in training_sessions {
if let Some(avg_rpe) = t.avg_rpe() {
result
.entry(t.date.week(Weekday::Mon).last_day())
.and_modify(|e| e.push(avg_rpe));
}
}

#[allow(clippy::cast_precision_loss)]
result
.into_iter()
.map(|(date, values)| {
(
date,
if values.is_empty() {
0.0
} else {
values.iter().sum::<f32>() / values.len() as f32
},
)
})
.collect()
}

fn training_session_weeks<T: Default>(
training_sessions: &[&TrainingSession],
) -> BTreeMap<NaiveDate, T> {
let mut result: BTreeMap<NaiveDate, T> = BTreeMap::new();

let today = Local::now().date_naive();
let mut day = training_sessions.first().map_or(today, |t| t.date);
while day <= today.week(Weekday::Mon).last_day() {
result.insert(day.week(Weekday::Mon).last_day(), T::default());
day += Duration::days(7);
}

result
}

// ------ ------
// Update
// ------ ------
Expand Down
23 changes: 11 additions & 12 deletions frontend/src/ui/page/training.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,15 @@ pub fn view(model: &Model, data_model: &data::Model) -> Node<Msg> {
3,
);

let avg_rpe_per_week = data_model
.training_stats
.avg_rpe_per_week
.iter()
.filter(|(date, _)| {
*date >= model.interval.first
&& *date <= model.interval.last.week(Weekday::Mon).last_day()
})
.copied()
.collect::<Vec<_>>();
let avg_rpe_per_week = common::centered_moving_average(
&data_model
.training_sessions
.iter()
.filter_map(|(_, s)| s.avg_rpe().map(|v| (s.date, v)))
.collect::<Vec<_>>(),
&model.interval,
3,
);
let training_sessions = data_model
.training_sessions
.values()
Expand Down Expand Up @@ -575,10 +574,10 @@ pub fn view_charts<Ms>(
IF![
show_rpe =>
common::view_chart(
&[("RPE (weekly average)", common::COLOR_RPE)],
&[("RPE (7 day average)", common::COLOR_RPE)],
common::plot_chart(
&[common::PlotData{values: avg_rpe_per_week,
plots: common::plot_line_with_dots(common::COLOR_RPE),
plots: common::plot_line(common::COLOR_RPE),
params: common::PlotParams::primary_range(5., 10.)
}],
interval.first,
Expand Down

0 comments on commit ac7d9ab

Please sign in to comment.