-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualization-cli.js
More file actions
577 lines (479 loc) · 17.9 KB
/
visualization-cli.js
File metadata and controls
577 lines (479 loc) · 17.9 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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#!/usr/bin/env node
const DailyDashboard = require('./daily-dashboard');
const AnalyticsEngine = require('./analytics-engine');
const ReportGenerator = require('./report-generator');
const ExportManager = require('./export-manager');
const asciichart = require('asciichart');
/**
* Visualization CLI - Interactive interface for reports and exports
*
* Commands:
* - report: Generate PDF wellness report
* - export-csv: Export data to CSV
* - export-json: Export data to JSON
* - export-all: Export complete backup
* - list-exports: List all exported files
* - list-reports: List all generated reports
* - chart: Display ASCII charts
* - heatmap: Display wellness heatmap
* - stats: Show export statistics
* - help: Show help message
*/
class VisualizationCLI {
constructor() {
this.dashboard = new DailyDashboard();
this.analytics = new AnalyticsEngine(this.dashboard);
this.reportGenerator = new ReportGenerator(this.dashboard, this.analytics);
this.exportManager = new ExportManager(this.dashboard);
}
/**
* Show help message
*/
showHelp() {
console.log(`
╔════════════════════════════════════════════════════════════╗
║ Enhanced Visualization & Reports - StepSyncAI ║
╚════════════════════════════════════════════════════════════╝
COMMANDS:
📄 Reports:
report [days] Generate PDF wellness report (default: 30 days)
list-reports List all generated PDF reports
💾 Exports:
export-csv [days] Export data to CSV format
export-json [days] Export data to JSON format
export-meds Export medications to CSV
export-analytics Export analytics summary
export-all Export complete backup (all data)
list-exports List all exported files
📊 Visualizations:
chart <type> [days] Display ASCII chart (mood/sleep/exercise)
heatmap [days] Display wellness heatmap
trends [days] Show trend analysis with charts
📈 Statistics:
stats Show export statistics
summary [days] Show data summary with visualizations
ℹ️ Help:
help Show this help message
EXAMPLES:
node visualization-cli.js report 30
node visualization-cli.js export-csv 60
node visualization-cli.js chart mood 14
node visualization-cli.js heatmap 30
node visualization-cli.js export-all
═══════════════════════════════════════════════════════════
`);
}
/**
* Generate PDF report
*/
generateReport(days = 30) {
try {
console.log(`\n📄 Generating wellness report for last ${days} days...`);
this.reportGenerator.generatePDFReport({
days,
includeAnalytics: true,
includeCharts: true
});
} catch (error) {
console.error(`❌ Error generating report: ${error.message}`);
}
}
/**
* List all reports
*/
listReports() {
const reports = this.reportGenerator.listReports();
if (reports.length === 0) {
console.log('\n📭 No reports generated yet.');
console.log(' Use "report" command to generate your first report!');
return;
}
console.log(`\n📄 Generated Reports (${reports.length})\n`);
console.log('═'.repeat(80));
reports.forEach((report, index) => {
console.log(`\n${index + 1}. ${report.filename}`);
console.log(` Created: ${report.created.toLocaleString()}`);
console.log(` Size: ${report.size}`);
console.log(` Path: ${report.path}`);
});
console.log('\n' + '═'.repeat(80));
}
/**
* Export to CSV
*/
exportCSV(days = 30) {
try {
console.log(`\n💾 Exporting last ${days} days to CSV...`);
this.exportManager.exportToCSV({ days });
} catch (error) {
console.error(`❌ Error exporting to CSV: ${error.message}`);
}
}
/**
* Export to JSON
*/
exportJSON(days = 30) {
try {
console.log(`\n💾 Exporting last ${days} days to JSON...`);
this.exportManager.exportToJSON({ days });
} catch (error) {
console.error(`❌ Error exporting to JSON: ${error.message}`);
}
}
/**
* Export medications
*/
exportMedications() {
try {
console.log('\n💾 Exporting medications to CSV...');
this.exportManager.exportMedicationsToCSV();
} catch (error) {
console.error(`❌ Error exporting medications: ${error.message}`);
}
}
/**
* Export analytics summary
*/
exportAnalytics(days = 30) {
try {
console.log('\n💾 Exporting analytics summary...');
this.exportManager.exportAnalyticsSummary(this.analytics, { days });
} catch (error) {
console.error(`❌ Error exporting analytics: ${error.message}`);
}
}
/**
* Export complete backup
*/
exportAll() {
try {
console.log('\n💾 Creating complete backup...');
this.exportManager.exportCompleteBackup();
} catch (error) {
console.error(`❌ Error creating backup: ${error.message}`);
}
}
/**
* List all exports
*/
listExports() {
const exports = this.exportManager.listExports();
if (exports.length === 0) {
console.log('\n📭 No exports created yet.');
console.log(' Use export commands to create your first export!');
return;
}
console.log(`\n💾 Exported Files (${exports.length})\n`);
console.log('═'.repeat(80));
exports.forEach((exp, index) => {
console.log(`\n${index + 1}. ${exp.filename}`);
console.log(` Type: ${exp.type}`);
console.log(` Created: ${exp.created.toLocaleString()}`);
console.log(` Size: ${exp.size}`);
});
console.log('\n' + '═'.repeat(80));
}
/**
* Display ASCII chart
*/
displayChart(type, days = 30) {
const validTypes = ['mood', 'sleep', 'exercise'];
if (!validTypes.includes(type)) {
console.error(`❌ Invalid chart type. Use: ${validTypes.join(', ')}`);
return;
}
const entries = this.dashboard.getAllEntries().slice(-days);
if (entries.length === 0) {
console.log('\n📭 No data available to chart.');
return;
}
let data = [];
let label = '';
let unit = '';
switch (type) {
case 'mood':
data = entries.filter(e => e.mood).map(e => e.mood);
label = 'Mood Rating';
unit = '/10';
break;
case 'sleep':
data = entries.filter(e => e.sleep_hours).map(e => e.sleep_hours);
label = 'Sleep Hours';
unit = 'hours';
break;
case 'exercise':
data = entries.filter(e => e.exercise_minutes).map(e => e.exercise_minutes);
label = 'Exercise Minutes';
unit = 'min';
break;
}
if (data.length === 0) {
console.log(`\n📭 No ${type} data available.`);
return;
}
console.log(`\n📊 ${label} - Last ${Math.min(data.length, days)} Days\n`);
// Configure chart
const config = {
height: 15,
colors: [asciichart.blue]
};
// Display chart
console.log(asciichart.plot(data.slice(-60), config));
// Statistics
const avg = data.reduce((sum, v) => sum + v, 0) / data.length;
const min = Math.min(...data);
const max = Math.max(...data);
console.log('\n📈 Statistics:');
console.log(` Average: ${avg.toFixed(1)} ${unit}`);
console.log(` Range: ${min} - ${max} ${unit}`);
console.log(` Data points: ${data.length}`);
console.log('');
}
/**
* Display wellness heatmap
*/
displayHeatmap(days = 30) {
const entries = this.dashboard.getAllEntries().slice(-days);
if (entries.length === 0) {
console.log('\n📭 No data available for heatmap.');
return;
}
console.log(`\n🗓️ Wellness Heatmap - Last ${days} Days\n`);
console.log('═'.repeat(70));
// Create weekly grid
const weeks = Math.ceil(entries.length / 7);
console.log('\n Mon Tue Wed Thu Fri Sat Sun');
console.log(' ' + '─'.repeat(36));
for (let week = 0; week < weeks; week++) {
const startIdx = week * 7;
const weekEntries = entries.slice(startIdx, startIdx + 7);
let weekRow = `W${week + 1} `;
weekEntries.forEach(entry => {
const score = this.calculateWellnessScore(entry);
const symbol = this.getHeatmapSymbol(score);
weekRow += ` ${symbol} `;
});
console.log(weekRow);
}
console.log('\n Legend:');
console.log(' ██ Excellent (8-10) ▓▓ Good (6-8) ▒▒ Fair (4-6) ░░ Poor (0-4) -- No data');
console.log('\n' + '═'.repeat(70));
}
/**
* Calculate overall wellness score for an entry
*/
calculateWellnessScore(entry) {
let score = 0;
let count = 0;
if (entry.mood) {
score += entry.mood;
count++;
}
if (entry.sleep_hours) {
// Convert sleep hours to 0-10 scale (7-9 hours = 10)
const sleepScore = entry.sleep_hours >= 7 && entry.sleep_hours <= 9
? 10
: Math.max(0, 10 - Math.abs(8 - entry.sleep_hours) * 2);
score += sleepScore;
count++;
}
if (entry.exercise_minutes) {
// Convert exercise to 0-10 scale (30+ min = 10)
const exerciseScore = Math.min(10, (entry.exercise_minutes / 30) * 10);
score += exerciseScore;
count++;
}
return count > 0 ? score / count : null;
}
/**
* Get heatmap symbol based on score
*/
getHeatmapSymbol(score) {
if (score === null) return '--';
if (score >= 8) return '██';
if (score >= 6) return '▓▓';
if (score >= 4) return '▒▒';
return '░░';
}
/**
* Display trend analysis with charts
*/
displayTrends(days = 30) {
console.log(`\n📈 Trend Analysis - Last ${days} Days\n`);
console.log('═'.repeat(70));
const trends = this.analytics.analyzeWellnessTrends(days);
if (!trends || Object.keys(trends).length === 0) {
console.log('\n📭 Insufficient data for trend analysis.');
return;
}
// Mood trends
if (trends.mood) {
console.log('\n😊 Mood Trends:');
console.log(` Trend: ${this.getTrendEmoji(trends.mood.trend)} ${trends.mood.trend}`);
if (trends.mood.predictions && trends.mood.predictions.length > 0) {
const nextWeek = trends.mood.predictions.slice(0, 7);
console.log(` 7-day forecast: ${nextWeek.map(v => v.toFixed(1)).join(', ')}`);
}
}
// Sleep trends
if (trends.sleep) {
console.log('\n😴 Sleep Trends:');
console.log(` Trend: ${this.getTrendEmoji(trends.sleep.trend)} ${trends.sleep.trend}`);
if (trends.sleep.predictions && trends.sleep.predictions.length > 0) {
const nextWeek = trends.sleep.predictions.slice(0, 7);
console.log(` 7-day forecast: ${nextWeek.map(v => v.toFixed(1)).join(', ')} hours`);
}
}
// Exercise trends
if (trends.exercise) {
console.log('\n🏃 Exercise Trends:');
console.log(` Trend: ${this.getTrendEmoji(trends.exercise.trend)} ${trends.exercise.trend}`);
if (trends.exercise.predictions && trends.exercise.predictions.length > 0) {
const nextWeek = trends.exercise.predictions.slice(0, 7);
console.log(` 7-day forecast: ${nextWeek.map(v => v.toFixed(0)).join(', ')} minutes`);
}
}
console.log('\n' + '═'.repeat(70));
}
/**
* Alias for displayTrends (for backward compatibility)
*/
showTrends(days = 30) {
return this.displayTrends(days);
}
/**
* Get trend emoji
*/
getTrendEmoji(trend) {
if (trend === 'improving') return '📈';
if (trend === 'declining') return '📉';
return '➡️';
}
/**
* Show export statistics
*/
showStats() {
const stats = this.exportManager.getExportStats();
console.log('\n📊 Export Statistics\n');
console.log('═'.repeat(60));
console.log('\n📁 Overview:');
console.log(` Total files: ${stats.totalFiles}`);
console.log(` Total size: ${stats.totalSize}`);
if (stats.oldest) {
console.log(` Oldest export: ${new Date(stats.oldest).toLocaleDateString()}`);
}
if (stats.newest) {
console.log(` Newest export: ${new Date(stats.newest).toLocaleDateString()}`);
}
if (Object.keys(stats.byType).length > 0) {
console.log('\n📋 By Type:');
Object.entries(stats.byType).forEach(([type, count]) => {
console.log(` ${type}: ${count} file(s)`);
});
}
console.log('\n' + '═'.repeat(60));
}
/**
* Show data summary with visualizations
*/
showSummary(days = 30) {
const entries = this.dashboard.getAllEntries().slice(-days);
if (entries.length === 0) {
console.log('\n📭 No data available for summary.');
return;
}
console.log(`\n📊 Data Summary - Last ${days} Days\n`);
console.log('═'.repeat(70));
// Overall statistics
console.log('\n📈 Overview:');
console.log(` Total entries: ${entries.length}`);
console.log(` Date range: ${entries[0].date} to ${entries[entries.length - 1].date}`);
console.log(` Tracking consistency: ${((entries.length / days) * 100).toFixed(0)}%`);
// Quick metrics
const moodEntries = entries.filter(e => e.mood);
const sleepEntries = entries.filter(e => e.sleep_hours);
const exerciseEntries = entries.filter(e => e.exercise_minutes);
if (moodEntries.length > 0) {
const avgMood = moodEntries.reduce((sum, e) => sum + e.mood, 0) / moodEntries.length;
console.log(`\n😊 Mood: ${avgMood.toFixed(1)}/10 average (${moodEntries.length} entries)`);
}
if (sleepEntries.length > 0) {
const avgSleep = sleepEntries.reduce((sum, e) => sum + e.sleep_hours, 0) / sleepEntries.length;
console.log(`😴 Sleep: ${avgSleep.toFixed(1)} hours average (${sleepEntries.length} nights)`);
}
if (exerciseEntries.length > 0) {
const totalExercise = exerciseEntries.reduce((sum, e) => sum + e.exercise_minutes, 0);
const avgExercise = totalExercise / exerciseEntries.length;
console.log(`🏃 Exercise: ${totalExercise} total minutes, ${avgExercise.toFixed(0)} average/day`);
}
console.log('\n' + '═'.repeat(70));
}
/**
* Run the CLI
*/
run() {
const args = process.argv.slice(2);
const command = args[0] || 'help';
const param1 = args[1] ? parseInt(args[1]) || args[1] : undefined;
const param2 = args[2] ? parseInt(args[2]) : undefined;
switch (command.toLowerCase()) {
case 'report':
this.generateReport(param1 || 30);
break;
case 'list-reports':
this.listReports();
break;
case 'export-csv':
this.exportCSV(param1 || 30);
break;
case 'export-json':
this.exportJSON(param1 || 30);
break;
case 'export-meds':
case 'export-medications':
this.exportMedications();
break;
case 'export-analytics':
this.exportAnalytics(param1 || 30);
break;
case 'export-all':
this.exportAll();
break;
case 'list-exports':
this.listExports();
break;
case 'chart':
if (!param1) {
console.error('❌ Please specify chart type: mood, sleep, or exercise');
} else {
this.displayChart(param1, param2 || 30);
}
break;
case 'heatmap':
this.displayHeatmap(param1 || 30);
break;
case 'trends':
this.displayTrends(param1 || 30);
break;
case 'stats':
case 'statistics':
this.showStats();
break;
case 'summary':
this.showSummary(param1 || 30);
break;
case 'help':
case '--help':
case '-h':
default:
this.showHelp();
break;
}
}
}
// Run the CLI if executed directly
if (require.main === module) {
const cli = new VisualizationCLI();
cli.run();
}
module.exports = VisualizationCLI;