-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexercise-tracker.js
More file actions
320 lines (267 loc) · 10.2 KB
/
Copy pathexercise-tracker.js
File metadata and controls
320 lines (267 loc) · 10.2 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
const fs = require('fs');
const ValidationUtils = require('./validation-utils');
class ExerciseTracker {
constructor(dataFile = 'exercise-data.json') {
this.dataFile = dataFile;
this.data = this.loadData();
this.idCounter = Date.now();
}
generateId() {
return ++this.idCounter;
}
loadData() {
try {
if (fs.existsSync(this.dataFile)) {
const rawData = fs.readFileSync(this.dataFile, 'utf8');
return JSON.parse(rawData);
}
} catch (error) {
console.error('Error loading exercise data:', error.message);
}
return {
exercises: []
};
}
saveData() {
try {
fs.writeFileSync(this.dataFile, JSON.stringify(this.data, null, 2));
return true;
} catch (error) {
console.error('Error saving exercise data:', error.message);
return false;
}
}
logExercise(type, duration, intensity = 'moderate', notes = '') {
// Validate exercise type using ValidationUtils
const validatedType = ValidationUtils.validateString(type, {
minLength: 1,
maxLength: 100,
fieldName: 'exercise type'
});
if (!validatedType) {
console.log('💡 Example: node exercise-tracker.js log "Running" 30 high\n');
return false;
}
// Validate duration using ValidationUtils
const durationNum = ValidationUtils.parseFloat(duration, {
min: 1,
max: 600,
fieldName: 'duration'
});
if (durationNum === null) {
console.log('💡 Duration should be in minutes (e.g., 30 for 30 minutes)\n');
return false;
}
// Validate intensity
const validIntensities = ['low', 'moderate', 'high'];
const intensityLower = (intensity || 'moderate').toLowerCase();
if (!validIntensities.includes(intensityLower)) {
console.error('❌ Invalid intensity: Intensity must be low, moderate, or high');
console.log('💡 Example: node exercise-tracker.js log "Yoga" 45 low\n');
return false;
}
const exercise = {
id: this.generateId(),
date: new Date().toISOString().split('T')[0],
timestamp: new Date().toISOString(),
type: validatedType,
duration: durationNum,
intensity: intensityLower,
notes: (notes && typeof notes === 'string') ? notes.trim() : ''
};
this.data.exercises.push(exercise);
this.saveData();
console.log('\n✅ Exercise logged successfully!');
console.log(`📝 ${type} for ${durationNum} minutes (${intensityLower} intensity)`);
// Provide feedback
if (durationNum >= 30) {
console.log('🎯 Great! You hit the recommended 30 minutes of exercise!');
} else {
console.log(`💪 Good start! ${30 - durationNum} more minutes to reach the daily goal.`);
}
return true;
}
getHistory(days = 7) {
if (this.data.exercises.length === 0) {
console.log('\n📭 No exercise data yet. Start logging your workouts!');
return;
}
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - days);
cutoffDate.setHours(0, 0, 0, 0);
const recentExercises = this.data.exercises
.filter(ex => new Date(ex.timestamp) >= cutoffDate)
.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp));
if (recentExercises.length === 0) {
console.log(`\n📭 No exercise logged in the last ${days} days.`);
return;
}
console.log(`\n📊 Exercise History (Last ${days} Days)`);
console.log('─'.repeat(60));
recentExercises.forEach(ex => {
const intensityEmoji = this.getIntensityEmoji(ex.intensity);
console.log(`\n📅 ${ex.date}`);
console.log(` ${intensityEmoji} ${ex.type} - ${ex.duration} min (${ex.intensity})`);
if (ex.notes) {
console.log(` 💭 ${ex.notes}`);
}
});
}
getIntensityEmoji(intensity) {
const emojis = {
'low': '🚶',
'moderate': '🏃',
'high': '💨'
};
return emojis[intensity] || '🏃';
}
getStats(days = 30) {
if (this.data.exercises.length === 0) {
console.log('\n📭 No exercise data yet.');
return null;
}
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - days);
cutoffDate.setHours(0, 0, 0, 0);
const recentExercises = this.data.exercises
.filter(ex => new Date(ex.timestamp) >= cutoffDate);
if (recentExercises.length === 0) {
console.log(`\n📭 No exercise logged in the last ${days} days.`);
return null;
}
// PERFORMANCE: Single-pass statistics calculation (6+ iterations → 1)
const uniqueDates = new Set();
const intensityCounts = { low: 0, moderate: 0, high: 0 };
const typeCounts = {};
let totalMinutes = 0;
recentExercises.forEach(ex => {
totalMinutes += ex.duration;
uniqueDates.add(ex.date);
// Count intensity
if (ex.intensity in intensityCounts) {
intensityCounts[ex.intensity]++;
}
// Count exercise types
typeCounts[ex.type] = (typeCounts[ex.type] || 0) + 1;
});
const avgMinutes = totalMinutes / recentExercises.length;
const daysWithExercise = uniqueDates.size;
const mostCommon = Object.entries(typeCounts)
.sort(([,a], [,b]) => b - a)[0];
const stats = {
totalWorkouts: recentExercises.length,
totalMinutes,
avgMinutes,
daysWithExercise,
intensityCounts,
mostCommonType: mostCommon ? mostCommon[0] : 'N/A'
};
console.log(`\n📊 Exercise Stats (Last ${days} Days)`);
console.log('─'.repeat(60));
console.log(`Total Workouts: ${stats.totalWorkouts}`);
console.log(`Total Minutes: ${stats.totalMinutes} min`);
console.log(`Average per Session: ${stats.avgMinutes.toFixed(1)} min`);
console.log(`Days Active: ${stats.daysWithExercise} / ${days}`);
console.log('\nIntensity Breakdown:');
console.log(` 🚶 Low: ${intensityCounts.low}`);
console.log(` 🏃 Moderate: ${intensityCounts.moderate}`);
console.log(` 💨 High: ${intensityCounts.high}`);
console.log(`\nMost Common: ${stats.mostCommonType}`);
return stats;
}
getTodayMinutes() {
const today = new Date().toISOString().split('T')[0];
const todayExercises = this.data.exercises
.filter(ex => ex.date === today);
return todayExercises.reduce((sum, ex) => sum + ex.duration, 0);
}
getExerciseDataForDashboard(days = 7) {
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - days);
cutoffDate.setHours(0, 0, 0, 0);
const recentExercises = this.data.exercises
.filter(ex => new Date(ex.timestamp) >= cutoffDate);
const totalMinutes = recentExercises.reduce((sum, ex) => sum + ex.duration, 0);
const daysWithExercise = new Set(recentExercises.map(ex => ex.date)).size;
const todayMinutes = this.getTodayMinutes();
return {
totalMinutes,
avgMinutes: recentExercises.length > 0 ? totalMinutes / days : 0,
daysActive: daysWithExercise,
todayMinutes,
workoutCount: recentExercises.length
};
}
}
// CLI Interface
if (require.main === module) {
const tracker = new ExerciseTracker();
const args = process.argv.slice(2);
const command = args[0];
switch(command) {
case 'log':
const type = args[1];
const duration = args[2];
const intensity = args[3] || 'moderate';
const notes = args.slice(4).join(' ');
tracker.logExercise(type, duration, intensity, notes);
break;
case 'history':
const days = ValidationUtils.parseInteger(args[1], {
min: 1,
max: 365,
default: 7,
fieldName: 'days'
});
tracker.getHistory(days);
break;
case 'stats':
const statsDays = ValidationUtils.parseInteger(args[1], {
min: 1,
max: 365,
default: 30,
fieldName: 'days'
});
tracker.getStats(statsDays);
break;
case 'today':
const todayMinutes = tracker.getTodayMinutes();
console.log(`\n📊 Today's Exercise: ${todayMinutes} minutes`);
if (todayMinutes >= 30) {
console.log('🎯 Daily goal achieved!');
} else {
console.log(`💪 ${30 - todayMinutes} more minutes to reach daily goal`);
}
break;
case 'help':
default:
console.log(`
🏃 Exercise Tracker - Track Your Physical Activity
USAGE:
node exercise-tracker.js <command> [options]
COMMANDS:
log <type> <minutes> [intensity] [notes]
Log an exercise session
Intensity: low, moderate, high (default: moderate)
Example: node exercise-tracker.js log "Running" 30 high "Morning jog"
history [days]
View exercise history (default: 7 days)
Example: node exercise-tracker.js history 14
stats [days]
View exercise statistics (default: 30 days)
Example: node exercise-tracker.js stats
today
View today's total exercise minutes
Example: node exercise-tracker.js today
help
Show this help message
EXAMPLES:
node exercise-tracker.js log "Yoga" 45 low "Evening session"
node exercise-tracker.js log "Cycling" 60 moderate
node exercise-tracker.js history
node exercise-tracker.js stats
`);
break;
}
}
module.exports = ExerciseTracker;