Spaces:
Running
Running
File size: 15,239 Bytes
35527e2 0f92957 35527e2 bccd229 324e3c4 d831612 35527e2 f63ce21 a586c52 35527e2 f63ce21 35527e2 51450d9 35527e2 631f3a3 324e3c4 51450d9 a586c52 f63ce21 631f3a3 51450d9 631f3a3 324e3c4 51450d9 324e3c4 631f3a3 f63ce21 51450d9 f63ce21 7f733b0 bccd229 324e3c4 f63ce21 35527e2 d831612 35527e2 d831612 35527e2 51450d9 d831612 35527e2 51450d9 f63ce21 35527e2 f63ce21 bccd229 324e3c4 35527e2 324e3c4 d67291e 35527e2 324e3c4 51450d9 324e3c4 51450d9 324e3c4 d67291e 324e3c4 35527e2 e12dab7 58c055f e12dab7 58c055f e12dab7 35527e2 bccd229 bef818b 35527e2 bccd229 bef818b 35527e2 bccd229 bef818b 35527e2 0f92957 bccd229 bef818b 0f92957 35527e2 0f92957 35527e2 51450d9 35527e2 |
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 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
import './style.css';
import { parseCSV } from './utils/csvParser';
import { calculateMetricACWR } from './utils/metricAcwr';
import type { Activity } from './types';
import {
createDistanceChart,
createDurationChart,
createTSSChart,
createCaloriesChart,
destroyAllCharts,
} from './components/charts';
// DOM elements
const csvUpload = document.getElementById('csv-upload') as HTMLInputElement;
const ftpInput = document.getElementById('ftp-input') as HTMLInputElement;
const targetAcwrInput = document.getElementById('target-acwr-input') as HTMLInputElement;
const predictTodayInput = document.getElementById('predict-today-input') as HTMLInputElement;
const thresholdHrInput = document.getElementById('threshold-hr-input') as HTMLInputElement;
const restingHrInput = document.getElementById('resting-hr-input') as HTMLInputElement;
const uploadStatus = document.getElementById('upload-status') as HTMLDivElement;
const chartsSection = document.getElementById('charts-section') as HTMLElement;
const filterSection = document.getElementById('filter-section') as HTMLElement;
const filterContainer = document.getElementById('filter-container') as HTMLElement;
const helpButton = document.getElementById('help-button') as HTMLButtonElement;
const helpPopover = document.getElementById('help-popover') as HTMLElement;
const helpClose = document.getElementById('help-close') as HTMLButtonElement;
// Store all activities globally
let allActivities: Activity[] = [];
let selectedActivityTypes: Set<string> = new Set(['Running']);
// Local storage keys
const STORAGE_KEYS = {
CSV_DATA: 'trainingload_csv_data',
FTP: 'trainingload_ftp',
THRESHOLD_HR: 'trainingload_threshold_hr',
RESTING_HR: 'trainingload_resting_hr',
TARGET_ACWR: 'trainingload_target_acwr',
PREDICT_TODAY: 'trainingload_predict_today',
SELECTED_FILTERS: 'trainingload_selected_filters',
};
// Load saved settings on startup
function loadSavedSettings(): void {
// Load FTP
const savedFtp = localStorage.getItem(STORAGE_KEYS.FTP);
if (savedFtp && ftpInput) {
ftpInput.value = savedFtp;
}
// Load Threshold HR
const savedThresholdHr = localStorage.getItem(STORAGE_KEYS.THRESHOLD_HR);
if (savedThresholdHr && thresholdHrInput) {
thresholdHrInput.value = savedThresholdHr;
}
// Load Resting HR
const savedRestingHr = localStorage.getItem(STORAGE_KEYS.RESTING_HR);
if (savedRestingHr && restingHrInput) {
restingHrInput.value = savedRestingHr;
}
// Load Target ACWR
const savedTargetAcwr = localStorage.getItem(STORAGE_KEYS.TARGET_ACWR);
if (savedTargetAcwr && targetAcwrInput) {
targetAcwrInput.value = savedTargetAcwr;
}
// Load Predict Today
const savedPredictToday = localStorage.getItem(STORAGE_KEYS.PREDICT_TODAY);
if (savedPredictToday && predictTodayInput) {
predictTodayInput.checked = savedPredictToday === 'true';
}
// Load Selected Filters
const savedFilters = localStorage.getItem(STORAGE_KEYS.SELECTED_FILTERS);
if (savedFilters) {
try {
const filters = JSON.parse(savedFilters);
selectedActivityTypes = new Set(filters);
} catch (e) {
console.error('Failed to parse saved filters', e);
}
}
}
// Save settings to local storage
function saveSettings(): void {
if (ftpInput) localStorage.setItem(STORAGE_KEYS.FTP, ftpInput.value);
if (thresholdHrInput) localStorage.setItem(STORAGE_KEYS.THRESHOLD_HR, thresholdHrInput.value);
if (restingHrInput) localStorage.setItem(STORAGE_KEYS.RESTING_HR, restingHrInput.value);
if (targetAcwrInput) localStorage.setItem(STORAGE_KEYS.TARGET_ACWR, targetAcwrInput.value);
if (predictTodayInput) localStorage.setItem(STORAGE_KEYS.PREDICT_TODAY, String(predictTodayInput.checked));
localStorage.setItem(STORAGE_KEYS.SELECTED_FILTERS, JSON.stringify(Array.from(selectedActivityTypes)));
}
// Load CSV from local storage
async function loadSavedCSV(): Promise<void> {
const savedCSV = localStorage.getItem(STORAGE_KEYS.CSV_DATA);
if (!savedCSV) return;
uploadStatus.textContent = 'Loading saved data...';
uploadStatus.className = '';
try {
const ftp = parseInt(ftpInput.value) || 343;
const thresholdHR = parseInt(thresholdHrInput.value) || 170;
const restingHR = parseInt(restingHrInput.value) || 50;
// Create a Blob from the saved CSV string
const blob = new Blob([savedCSV], { type: 'text/csv' });
const file = new File([blob], 'saved_activities.csv', { type: 'text/csv' });
allActivities = await parseCSV(file, ftp, thresholdHR, restingHR);
if (allActivities.length === 0) {
throw new Error('No valid activities found in saved data');
}
createActivityTypeFilters(allActivities);
const filteredActivities = selectedActivityTypes.size > 0
? allActivities.filter(activity => selectedActivityTypes.has(activity.activityType || ''))
: allActivities;
const targetAcwr = parseFloat(targetAcwrInput.value) || 1.3;
const predictToday = predictTodayInput?.checked || false;
renderCharts(filteredActivities, targetAcwr, predictToday);
filterSection.classList.remove('hidden');
chartsSection.classList.remove('hidden');
uploadStatus.textContent = `Successfully loaded ${allActivities.length} activities from saved data`;
uploadStatus.className = 'success';
} catch (error) {
console.error('Error loading saved data:', error);
// Clear corrupted data
localStorage.removeItem(STORAGE_KEYS.CSV_DATA);
uploadStatus.textContent = '';
uploadStatus.className = '';
}
}
// Event listeners
csvUpload?.addEventListener('change', handleFileUpload);
targetAcwrInput?.addEventListener('input', handleTargetAcwrChange);
predictTodayInput?.addEventListener('change', handlePredictTodayChange);
ftpInput?.addEventListener('input', saveSettings);
thresholdHrInput?.addEventListener('input', saveSettings);
restingHrInput?.addEventListener('input', saveSettings);
helpButton?.addEventListener('click', () => helpPopover?.classList.remove('hidden'));
helpClose?.addEventListener('click', () => helpPopover?.classList.add('hidden'));
helpPopover?.addEventListener('click', (e) => {
if (e.target === helpPopover) {
helpPopover.classList.add('hidden');
}
});
function handleTargetAcwrChange(): void {
// Only refresh if we have activities loaded
if (allActivities.length === 0) return;
// Save to local storage
saveSettings();
// Filter and render with new target ACWR
const filteredActivities = selectedActivityTypes.size > 0
? allActivities.filter(activity => selectedActivityTypes.has(activity.activityType || ''))
: allActivities;
const targetAcwr = parseFloat(targetAcwrInput.value) || 1.3;
const predictToday = predictTodayInput?.checked || false;
renderCharts(filteredActivities, targetAcwr, predictToday);
}
function handlePredictTodayChange(): void {
// Only refresh if we have activities loaded
if (allActivities.length === 0) return;
// Save to local storage
saveSettings();
// Filter and render with new predict today setting
const filteredActivities = selectedActivityTypes.size > 0
? allActivities.filter(activity => selectedActivityTypes.has(activity.activityType || ''))
: allActivities;
const targetAcwr = parseFloat(targetAcwrInput.value) || 1.3;
const predictToday = predictTodayInput?.checked || false;
renderCharts(filteredActivities, targetAcwr, predictToday);
}
function createActivityTypeFilters(activities: Activity[]): void {
// Get unique activity types
const activityTypes = new Set<string>();
activities.forEach(activity => {
if (activity.activityType) {
activityTypes.add(activity.activityType);
}
});
// Define priority order
const priorityOrder = ['Running', 'Cycling', 'Strength Training'];
const sortedTypes: string[] = [];
// Add priority types first (if they exist)
priorityOrder.forEach(type => {
if (activityTypes.has(type)) {
sortedTypes.push(type);
activityTypes.delete(type);
}
});
// Add remaining types alphabetically
sortedTypes.push(...Array.from(activityTypes).sort());
// Clear existing filters
filterContainer.innerHTML = '';
// Create checkbox for each activity type
sortedTypes.forEach(type => {
const label = document.createElement('label');
label.className = 'filter-checkbox';
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = `filter-${type.replace(/\s+/g, '-').toLowerCase()}`;
checkbox.value = type;
checkbox.checked = selectedActivityTypes.has(type);
checkbox.addEventListener('change', handleFilterChange);
const icon = getActivityIcon(type);
const span = document.createElement('span');
span.textContent = `${icon} ${type}`;
label.appendChild(checkbox);
label.appendChild(span);
filterContainer.appendChild(label);
});
}
function getActivityIcon(activityType: string): string {
const icons: { [key: string]: string } = {
'Running': 'π',
'Cycling': 'π΄',
'Strength Training': 'ποΈ',
'HIIT': 'πͺ',
'Cardio': 'β€οΈ',
};
return icons[activityType] || 'π';
}
function handleFilterChange(): void {
// Update selected activity types based on checkboxes
const checkboxes = filterContainer.querySelectorAll('input[type="checkbox"]') as NodeListOf<HTMLInputElement>;
selectedActivityTypes.clear();
checkboxes.forEach(checkbox => {
if (checkbox.checked) {
selectedActivityTypes.add(checkbox.value);
}
});
// Save to local storage
saveSettings();
// Filter and render
const filteredActivities = selectedActivityTypes.size > 0
? allActivities.filter(activity => selectedActivityTypes.has(activity.activityType || ''))
: allActivities;
const targetAcwr = parseFloat(targetAcwrInput.value) || 1.3;
const predictToday = predictTodayInput?.checked || false;
renderCharts(filteredActivities, targetAcwr, predictToday);
}
async function handleFileUpload(event: Event): Promise<void> {
const input = event.target as HTMLInputElement;
const file = input.files?.[0];
if (!file) {
return;
}
uploadStatus.textContent = 'Processing CSV file...';
uploadStatus.className = '';
try {
// Get FTP and HR values from inputs
const ftp = parseInt(ftpInput.value) || 343;
const thresholdHR = parseInt(thresholdHrInput.value) || 170;
const restingHR = parseInt(restingHrInput.value) || 50;
// Read file content to save it
const csvContent = await file.text();
// Parse CSV with user-provided FTP and HR values
allActivities = await parseCSV(file, ftp, thresholdHR, restingHR);
if (allActivities.length === 0) {
throw new Error('No valid activities found in the CSV file');
}
// Save CSV content and settings to local storage
localStorage.setItem(STORAGE_KEYS.CSV_DATA, csvContent);
saveSettings();
// Create dynamic activity type filters
createActivityTypeFilters(allActivities);
// Initial render with selected activity types
const filteredActivities = selectedActivityTypes.size > 0
? allActivities.filter(activity => selectedActivityTypes.has(activity.activityType || ''))
: allActivities;
const targetAcwr = parseFloat(targetAcwrInput.value) || 1.3;
const predictToday = predictTodayInput?.checked || false;
renderCharts(filteredActivities, targetAcwr, predictToday);
// Show filter and charts sections
filterSection.classList.remove('hidden');
chartsSection.classList.remove('hidden');
// Update status
uploadStatus.textContent = `Successfully loaded ${allActivities.length} activities`;
uploadStatus.className = 'success';
} catch (error) {
console.error('Error processing file:', error);
uploadStatus.textContent = error instanceof Error ? error.message : 'Failed to process CSV file';
uploadStatus.className = 'error';
filterSection.classList.add('hidden');
chartsSection.classList.add('hidden');
}
}
function renderCharts(activities: Activity[], targetAcwr: number, predictToday: boolean = false): void {
// Calculate date range from first activity to today
let dateRange: { start: Date; end: Date } | undefined;
if (allActivities.length > 0) {
// Determine end date based on whether we'll include today in predictions
const today = new Date();
today.setHours(0, 0, 0, 0);
// Check if today has activities
const todayHasActivities = activities.some(activity => {
const activityDate = new Date(activity.date);
activityDate.setHours(0, 0, 0, 0);
return activityDate.getTime() === today.getTime();
});
// If predictToday is checked and today has no activities, end range at yesterday
// This prevents today from appearing twice (once as null, once as prediction)
const endDate = new Date();
if (predictToday && !todayHasActivities) {
// End at yesterday so today only appears in predictions
endDate.setDate(endDate.getDate() - 1);
}
endDate.setHours(23, 59, 59, 999);
// Calculate start date: always go back 56 days (2 x 28 days chronic period)
// This ensures we have enough historical data for accurate ACWR calculation
const startDate = new Date(endDate);
startDate.setDate(startDate.getDate() - 56);
startDate.setHours(0, 0, 0, 0);
dateRange = {
start: startDate,
end: endDate,
};
}
// Calculate ACWR for each metric with consistent date range
const distanceData = calculateMetricACWR(
activities,
(activity) => activity.distance,
dateRange,
targetAcwr
);
const durationData = calculateMetricACWR(
activities,
(activity) => activity.duration,
dateRange,
targetAcwr
);
const tssData = calculateMetricACWR(
activities,
(activity) => activity.trainingStressScore,
dateRange,
targetAcwr
);
const caloriesData = calculateMetricACWR(
activities,
(activity) => activity.calories,
dateRange,
targetAcwr
);
// Destroy existing charts
destroyAllCharts();
// Create new charts
createDistanceChart(distanceData);
createDurationChart(durationData);
createTSSChart(tssData);
createCaloriesChart(caloriesData);
}
// Initialize
loadSavedSettings();
loadSavedCSV();
console.log('Training Load Data Visualization initialized');
|