-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheatmap.rs
More file actions
354 lines (316 loc) Β· 11.6 KB
/
Copy pathheatmap.rs
File metadata and controls
354 lines (316 loc) Β· 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
use std::collections::HashMap;
use chrono::{Datelike, NaiveDate, Weekday, Month};
use num_traits::cast::FromPrimitive;
use crate::api::{ApiClient, DataAccess};
use crate::models::{JDay, Exercise};
use crate::workouts;
use crate::utils;
use crate::table::{calculate_1rm, parse_date_and_filter_arguments};
/// Metric to use for intensity calculation
#[derive(Debug, Clone, Copy)]
pub enum Metric {
Sets,
Reps,
Volume,
Weight,
OneRm,
}
/// Compute the metric value for a single workout
pub fn compute_metric(jday: &JDay, metric: Metric, filters: &[String]) -> f64 {
// Build exercise ID -> Exercise map
let mut ex_map: HashMap<String, &Exercise> = HashMap::new();
for ex_wrap in &jday.exercises {
ex_map.insert(ex_wrap.exercise.id.clone(), &ex_wrap.exercise);
}
let mut total = 0.0f64;
let mut max_val = 0.0f32;
for eblock in &jday.eblocks {
if let Some(ex) = ex_map.get(&eblock.eid) {
// Check if this exercise matches our filters
if !filters.is_empty() {
let name_lower = ex.name.to_lowercase();
if !filters.iter().any(|f| name_lower.contains(&f.to_lowercase())) {
continue;
}
}
// Process each set in this exercise block
for set in &eblock.sets {
let weight = set.w.unwrap_or(0.0);
let reps = set.r.unwrap_or(0);
let sets = set.s.unwrap_or(0);
if weight > 0.0 && reps > 0 && sets > 0 {
match metric {
Metric::Sets => total += sets as f64,
Metric::Reps => total += reps as f64 * sets as f64,
Metric::Volume => total += weight as f64 * (reps as f64 * sets as f64),
Metric::Weight => {
if weight > max_val {
max_val = weight;
}
}
Metric::OneRm => {
let onerm = calculate_1rm(weight, reps);
if onerm > max_val {
max_val = onerm;
}
}
}
}
}
}
}
match metric {
Metric::Sets | Metric::Reps | Metric::Volume => total,
Metric::Weight | Metric::OneRm => max_val as f64,
}
}
/// Handle the heatmap command
pub async fn handle_heatmap<C: ApiClient + Clone + Send + Sync + 'static>(
client: &C,
token: &Option<String>,
data_access: DataAccess<'_, C>,
metric: Metric,
green: bool,
args: &[String],
verbose: bool,
) {
// Parse arguments into dates and exercise filters
let (date_args, filters) = parse_date_and_filter_arguments(args);
if verbose {
let msg = format!("Date args: {:?}, Filters: {:?}", date_args, filters);
if *crate::formatters::STDERR_COLOR_ENABLED {
eprintln!("{}", ansi_term::Colour::Blue.paint(msg));
} else {
eprintln!("{}", msg);
}
}
let terminal_width = if let Some((w, _)) = term_size::dimensions() {
w
} else {
80 // Default width
};
let max_weeks = (terminal_width - 5) / 3;
let max_workouts = max_weeks * 7;
// Get dates to process
let dates = if date_args.is_empty() {
// Default: get all dates from cache/server
match workouts::get_dates(&data_access, None, None, max_workouts as u32, false).await {
Ok(d) => d,
Err(e) => {
utils::exit_with_error(format!("Failed to get dates: {}", e));
}
}
} else {
match workouts::get_dates_from_ranges(&data_access, &date_args).await {
Ok(d) => d,
Err(e) => {
utils::exit_with_error(format!("Failed to get dates from ranges: {}", e));
}
}
};
if dates.is_empty() {
utils::exit_with_error("No workouts found in the specified range");
}
if verbose {
let msg = format!("Processing {} dates", dates.len());
if *crate::formatters::STDERR_COLOR_ENABLED {
eprintln!("{}", ansi_term::Colour::Blue.paint(msg));
} else {
eprintln!("{}", msg);
}
}
// Fetch workouts asynchronously
let (tx, mut rx) = tokio::sync::mpsc::channel(32);
for date in dates.iter() {
let date = date.clone();
let client_clone = client.clone();
let token_clone = token.clone();
let use_network = data_access.use_network;
let use_cache = data_access.use_cache;
let write_cache = data_access.write_cache;
let uid = data_access.uid;
let tx_clone = tx.clone();
tokio::spawn(async move {
let data_access_clone = crate::api::DataAccess {
client: &client_clone,
token: token_clone.as_deref(),
uid,
use_network,
use_cache,
write_cache,
};
let result = match workouts::get_jday(&data_access_clone, &date, verbose).await {
Ok(jday) => Some(jday),
Err(e) => {
if verbose {
eprintln!("Error getting workout for {}: {}", date, e);
}
None
}
};
let _ = tx_clone.send((date.clone(), result)).await;
});
}
drop(tx);
// Collect results
let mut results = Vec::new();
while let Some(result) = rx.recv().await {
results.push(result);
}
// Sort by date
results.sort_by(|a, b| a.0.cmp(&b.0));
// Compute daily metrics
let mut daily_values: HashMap<NaiveDate, f64> = HashMap::new();
for (date_str, jday_opt) in results {
if let Some(jday) = jday_opt
&& let Ok(date) = NaiveDate::parse_from_str(&date_str, "%Y-%m-%d") {
let value = compute_metric(&jday, metric, &filters);
if value > 0.0 {
daily_values.insert(date, value);
}
}
}
if daily_values.is_empty() {
println!("No data to display.");
return;
}
// Find date range
let mut date_list: Vec<NaiveDate> = daily_values.keys().cloned().collect();
date_list.sort();
let start_date = *date_list.first().unwrap();
let end_date = *date_list.last().unwrap();
// Find min and max values
let max_value = daily_values.values().cloned().fold(f64::NEG_INFINITY, f64::max);
let min_value = daily_values.values().cloned().fold(f64::INFINITY, f64::min);
if verbose {
let msg = format!("Range: {} .. {} kg", min_value, max_value);
if *crate::formatters::STDERR_COLOR_ENABLED {
eprintln!("{}", ansi_term::Colour::Blue.paint(msg));
} else {
eprintln!("{}", msg);
}
}
// Draw the heatmap
draw_heatmap(daily_values, start_date, end_date, min_value, max_value, green);
}
/// Draw the heatmap to the console.
fn draw_heatmap(
daily_values: HashMap<NaiveDate, f64>,
start_date: NaiveDate,
end_date: NaiveDate,
min_value: f64,
max_value: f64,
green: bool,
) {
let mut first_monday = start_date;
while first_monday.weekday() != Weekday::Mon {
first_monday = first_monday.pred_opt().unwrap();
}
let mut weeks: Vec<Vec<Option<f64>>> = Vec::new();
let mut week_dates: Vec<NaiveDate> = Vec::new();
let mut current_week = vec![None; 7];
let mut current_date = first_monday;
while current_date <= end_date {
let day_of_week = current_date.weekday().num_days_from_monday() as usize;
if day_of_week == 0 {
week_dates.push(current_date);
}
current_week[day_of_week] = daily_values.get(¤t_date).cloned();
if current_date.weekday() == Weekday::Sun {
weeks.push(current_week);
current_week = vec![None; 7];
}
current_date = current_date.succ_opt().unwrap();
}
if !current_week.iter().all(Option::is_none) {
weeks.push(current_week);
}
let terminal_width = if let Some((w, _)) = term_size::dimensions() {
w
} else {
80 // Default width
};
let max_weeks = (terminal_width - 5) / 3;
if weeks.len() > max_weeks {
let start_index = weeks.len() - max_weeks;
weeks = weeks.into_iter().skip(start_index).collect();
week_dates = week_dates.into_iter().skip(start_index).collect();
}
// Print header
print!(" ");
for date in &week_dates {
print!("{:2} ", date.day());
}
println!();
// Print body
for day_of_week in 0..7 {
let day_label = match day_of_week {
0 => "Mon ",
2 => "Wed ",
4 => "Fri ",
6 => "Sun ",
_ => " ",
};
print!("{}", day_label);
for (week_index, week) in weeks.iter().enumerate() {
let cell = week[day_of_week];
let current_day = week_dates[week_index] + chrono::Duration::days(day_of_week as i64);
if current_day < start_date || current_day > end_date {
print!(" ");
} else {
let symbol = match cell {
Some(value) if value > 0.0 => {
let range = max_value - min_value;
let intensity = if range > 0.0 {
((value - min_value) / range * 230.0) as u8 + 25
} else {
128 // middle intensity if all values are the same
};
if *crate::formatters::COLOR_ENABLED {
let color_code = if green {
// Green gradient: RGB(0, intensity, 0)
format!("\x1b[38;2;0;{};0m", intensity)
} else {
// Solarized: use table gradient colors (default)
let gradient_index = (intensity as usize * (crate::table::GRADIENT.len() - 1) / 255).min(crate::table::GRADIENT.len() - 1);
format!("\x1b[38;5;{}m", crate::table::GRADIENT[gradient_index])
};
format!("{} ββΆ\x1b[0m", color_code)
} else {
// Map to 4 levels
let level = ((value / max_value * 3.0) as usize).min(3);
match level {
0 => " ββΆ",
1 => " ββ·",
2 => " ββΈ",
3 => " ββΉ",
_ => " ββΆ",
}.to_string()
}
}
_ => {
if *crate::formatters::COLOR_ENABLED {
"\x1b[38;2;20;20;20m ββΆ\x1b[0m".to_string()
} else {
" ".to_string()
}
}
};
print!("{}", symbol);
}
}
println!();
}
// Print footer
print!(" ");
let mut last_month = 0;
for date in &week_dates {
if date.month() != last_month {
print!("{:3}", Month::from_u32(date.month()).unwrap().name()[..3].to_string());
last_month = date.month();
} else {
print!(" ");
}
}
println!();
}