diff --git a/frontend/src/ui/data.rs b/frontend/src/ui/data.rs index 07ba902..8a15755 100644 --- a/frontend/src/ui/data.rs +++ b/frontend/src/ui/data.rs @@ -68,7 +68,6 @@ pub fn init(url: Url, _orders: &mut impl Orders) -> Model { training_stats: TrainingStats { short_term_load: Vec::new(), long_term_load: Vec::new(), - avg_rpe_per_week: Vec::new(), }, settings, ongoing_training_session, @@ -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 { @@ -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(); } } @@ -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), } } @@ -938,48 +934,6 @@ fn calculate_average_weighted_sum_of_load( .collect::>() } -fn calculate_avg_rpe_per_week(training_sessions: &[&TrainingSession]) -> Vec<(NaiveDate, f32)> { - let mut result: BTreeMap> = 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::() / values.len() as f32 - }, - ) - }) - .collect() -} - -fn training_session_weeks( - training_sessions: &[&TrainingSession], -) -> BTreeMap { - let mut result: BTreeMap = 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 // ------ ------ diff --git a/frontend/src/ui/page/training.rs b/frontend/src/ui/page/training.rs index 9828508..09ce6ad 100644 --- a/frontend/src/ui/page/training.rs +++ b/frontend/src/ui/page/training.rs @@ -243,16 +243,15 @@ pub fn view(model: &Model, data_model: &data::Model) -> Node { 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::>(); + 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::>(), + &model.interval, + 3, + ); let training_sessions = data_model .training_sessions .values() @@ -327,7 +326,7 @@ pub fn view(model: &Model, data_model: &data::Model) -> Node { 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, @@ -507,8 +506,8 @@ fn view_training_sessions_dialog( pub fn view_charts( 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, @@ -562,7 +561,7 @@ pub fn view_charts( &[("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.), }], @@ -575,12 +574,12 @@ pub fn view_charts( 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::>(), interval.first, interval.last, theme,