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 Dec 1, 2024
1 parent 4572cdb commit 2d69a70
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 64 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
35 changes: 17 additions & 18 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 average_7day_rpe = 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 @@ -327,7 +326,7 @@ pub fn view(model: &Model, data_model: &data::Model) -> Node<Msg> {
short_term_load,
long_term_load,
total_7day_set_volume,
avg_rpe_per_week,
&average_7day_rpe,
&model.interval,
data_model.theme(),
data_model.settings.show_rpe,
Expand Down Expand Up @@ -507,8 +506,8 @@ fn view_training_sessions_dialog(
pub fn view_charts<Ms>(
short_term_load: Vec<(NaiveDate, f32)>,
long_term_load: Vec<(NaiveDate, f32)>,
total_set_volume: Vec<(NaiveDate, f32)>,
avg_rpe_per_week: Vec<(NaiveDate, f32)>,
total_7day_set_volume: Vec<(NaiveDate, f32)>,
average_7day_rpe: &[Vec<(NaiveDate, f32)>],
interval: &common::Interval,
theme: &data::Theme,
show_rpe: bool,
Expand Down Expand Up @@ -562,7 +561,7 @@ pub fn view_charts<Ms>(
&[("Set volume (7 day total)", common::COLOR_SET_VOLUME)],
common::plot_chart(
&[common::PlotData {
values: total_set_volume,
values: total_7day_set_volume,
plots: [common::PlotType::Line(common::COLOR_SET_VOLUME, 2)].to_vec(),
params: common::PlotParams::primary_range(0., 10.),
}],
Expand All @@ -575,12 +574,12 @@ 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),
&average_7day_rpe.iter().map(|values| common::PlotData{values: values.clone(),
plots: common::plot_line(common::COLOR_RPE),
params: common::PlotParams::primary_range(5., 10.)
}],
}).collect::<Vec<_>>(),
interval.first,
interval.last,
theme,
Expand Down

0 comments on commit 2d69a70

Please sign in to comment.