-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics-engine.js
More file actions
670 lines (568 loc) · 25.1 KB
/
Copy pathanalytics-engine.js
File metadata and controls
670 lines (568 loc) · 25.1 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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
const { PerformanceCache, DateUtils } = require('./performance-cache');
/**
* Advanced Analytics Engine for StepSyncAI
* Provides correlation analysis, predictive insights, and anomaly detection
*/
class AnalyticsEngine {
constructor(dashboard) {
this.dashboard = dashboard;
this.cache = new PerformanceCache(50, 600000); // 10-minute cache
}
/**
* Calculate correlation between two data series
* Returns Pearson correlation coefficient (-1 to 1)
*/
calculateCorrelation(series1, series2) {
if (series1.length !== series2.length || series1.length === 0) {
return null;
}
const n = series1.length;
const mean1 = series1.reduce((sum, val) => sum + val, 0) / n;
const mean2 = series2.reduce((sum, val) => sum + val, 0) / n;
let numerator = 0;
let sumSq1 = 0;
let sumSq2 = 0;
for (let i = 0; i < n; i++) {
const diff1 = series1[i] - mean1;
const diff2 = series2[i] - mean2;
numerator += diff1 * diff2;
sumSq1 += diff1 * diff1;
sumSq2 += diff2 * diff2;
}
const denominator = Math.sqrt(sumSq1 * sumSq2);
return denominator === 0 ? 0 : numerator / denominator;
}
/**
* Analyze correlation between sleep quality and exercise
*/
analyzeSleepExerciseCorrelation(days = 30) {
const cacheKey = this.cache.generateKey('sleep-exercise-corr', days);
const cached = this.cache.get(cacheKey);
if (cached) return cached;
const allData = this.dashboard.getAllWellnessData(days);
if (!allData.sleep || !allData.exercise) {
return { correlation: null, message: 'Insufficient data' };
}
// Get sleep data by date
const sleepByDate = new Map();
if (this.dashboard.sleepTracker && this.dashboard.sleepTracker.data.sleepEntries) {
this.dashboard.sleepTracker.data.sleepEntries.forEach(entry => {
const date = DateUtils.toDateKey(entry.timestamp);
sleepByDate.set(date, entry.quality);
});
}
// Get exercise duration by date
const exerciseByDate = new Map();
if (this.dashboard.exerciseTracker && this.dashboard.exerciseTracker.data.exercises) {
this.dashboard.exerciseTracker.data.exercises.forEach(ex => {
const date = DateUtils.toDateKey(ex.timestamp);
const current = exerciseByDate.get(date) || 0;
exerciseByDate.set(date, current + ex.duration);
});
}
// Find common dates
const commonDates = [...sleepByDate.keys()].filter(date => exerciseByDate.has(date));
if (commonDates.length < 5) {
return { correlation: null, message: 'Need at least 5 days of overlapping data' };
}
const sleepQuality = commonDates.map(date => sleepByDate.get(date));
const exerciseDuration = commonDates.map(date => exerciseByDate.get(date));
const correlation = this.calculateCorrelation(sleepQuality, exerciseDuration);
const result = {
correlation,
strength: this.interpretCorrelation(correlation),
sampleSize: commonDates.length,
insight: this.generateCorrelationInsight('sleep quality', 'exercise', correlation)
};
this.cache.set(cacheKey, result);
return result;
}
/**
* Analyze correlation between mood and sleep
*/
analyzeMoodSleepCorrelation(days = 30) {
const cacheKey = this.cache.generateKey('mood-sleep-corr', days);
const cached = this.cache.get(cacheKey);
if (cached) return cached;
// Get mood data by date
const moodByDate = new Map();
if (this.dashboard.mentalHealth && this.dashboard.mentalHealth.data.moodLogs) {
this.dashboard.mentalHealth.data.moodLogs.forEach(log => {
const date = DateUtils.toDateKey(log.timestamp);
moodByDate.set(date, log.mood);
});
}
// Get sleep quality by date
const sleepByDate = new Map();
if (this.dashboard.sleepTracker && this.dashboard.sleepTracker.data.sleepEntries) {
this.dashboard.sleepTracker.data.sleepEntries.forEach(entry => {
const date = DateUtils.toDateKey(entry.timestamp);
sleepByDate.set(date, entry.quality);
});
}
const commonDates = [...moodByDate.keys()].filter(date => sleepByDate.has(date));
if (commonDates.length < 5) {
return { correlation: null, message: 'Need at least 5 days of overlapping data' };
}
const mood = commonDates.map(date => moodByDate.get(date));
const sleep = commonDates.map(date => sleepByDate.get(date));
const correlation = this.calculateCorrelation(mood, sleep);
const result = {
correlation,
strength: this.interpretCorrelation(correlation),
sampleSize: commonDates.length,
insight: this.generateCorrelationInsight('mood', 'sleep quality', correlation)
};
this.cache.set(cacheKey, result);
return result;
}
/**
* Analyze correlation between mood and exercise
*/
analyzeMoodExerciseCorrelation(days = 30) {
const cacheKey = this.cache.generateKey('mood-exercise-corr', days);
const cached = this.cache.get(cacheKey);
if (cached) return cached;
// Get mood data by date
const moodByDate = new Map();
if (this.dashboard.mentalHealth && this.dashboard.mentalHealth.data.moodLogs) {
this.dashboard.mentalHealth.data.moodLogs.forEach(log => {
const date = DateUtils.toDateKey(log.timestamp);
moodByDate.set(date, log.mood);
});
}
// Get exercise duration by date
const exerciseByDate = new Map();
if (this.dashboard.exerciseTracker && this.dashboard.exerciseTracker.data.exercises) {
this.dashboard.exerciseTracker.data.exercises.forEach(ex => {
const date = DateUtils.toDateKey(ex.timestamp);
const current = exerciseByDate.get(date) || 0;
exerciseByDate.set(date, current + ex.duration);
});
}
const commonDates = [...moodByDate.keys()].filter(date => exerciseByDate.has(date));
if (commonDates.length < 5) {
return { correlation: null, message: 'Need at least 5 days of overlapping data' };
}
const mood = commonDates.map(date => moodByDate.get(date));
const exercise = commonDates.map(date => exerciseByDate.get(date));
const correlation = this.calculateCorrelation(mood, exercise);
const result = {
correlation,
strength: this.interpretCorrelation(correlation),
sampleSize: commonDates.length,
insight: this.generateCorrelationInsight('mood', 'exercise', correlation)
};
this.cache.set(cacheKey, result);
return result;
}
/**
* Interpret correlation strength
*/
interpretCorrelation(r) {
if (r === null) return 'Unknown';
const absR = Math.abs(r);
if (absR >= 0.7) return 'Strong';
if (absR >= 0.4) return 'Moderate';
if (absR >= 0.2) return 'Weak';
return 'Very Weak';
}
/**
* Generate insight from correlation
*/
generateCorrelationInsight(metric1, metric2, correlation) {
if (correlation === null) return 'Not enough data for analysis';
const absR = Math.abs(correlation);
const direction = correlation > 0 ? 'positively' : 'negatively';
if (absR >= 0.7) {
return `Strong ${direction} correlated: ${metric1} and ${metric2} move together significantly.`;
} else if (absR >= 0.4) {
return `Moderately ${direction} correlated: ${metric1} tends to ${correlation > 0 ? 'improve' : 'worsen'} with ${metric2}.`;
} else if (absR >= 0.2) {
return `Weakly ${direction} correlated: ${metric1} shows some relationship with ${metric2}.`;
} else {
return `Little to no correlation: ${metric1} and ${metric2} appear independent.`;
}
}
/**
* Predict future trend using linear regression
*/
predictTrend(data, daysAhead = 7) {
if (data.length < 2) return null;
// Simple linear regression: y = mx + b
const n = data.length;
const x = Array.from({ length: n }, (_, i) => i);
const y = data;
const sumX = x.reduce((sum, val) => sum + val, 0);
const sumY = y.reduce((sum, val) => sum + val, 0);
const sumXY = x.reduce((sum, val, i) => sum + val * y[i], 0);
const sumX2 = x.reduce((sum, val) => sum + val * val, 0);
const slope = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX);
const intercept = (sumY - slope * sumX) / n;
// Predict future values
const predictions = [];
for (let i = 0; i < daysAhead; i++) {
const futureX = n + i;
const prediction = slope * futureX + intercept;
predictions.push(Math.max(0, prediction)); // No negative predictions
}
return {
slope,
intercept,
predictions,
trend: slope > 0.1 ? 'improving' : slope < -0.1 ? 'declining' : 'stable'
};
}
/**
* Detect anomalies using Z-score
*/
detectAnomalies(data, threshold = 2.5) {
// Guard against non-array input
if (!Array.isArray(data) || data.length < 3) return [];
const mean = data.reduce((sum, val) => sum + val, 0) / data.length;
const variance = data.reduce((sum, val) => sum + Math.pow(val - mean, 2), 0) / data.length;
const stdDev = Math.sqrt(variance);
const anomalies = [];
data.forEach((value, index) => {
const zScore = stdDev === 0 ? 0 : (value - mean) / stdDev;
if (Math.abs(zScore) > threshold) {
anomalies.push({
index,
value,
zScore,
type: zScore > 0 ? 'unusually high' : 'unusually low'
});
}
});
return anomalies;
}
/**
* Analyze wellness score trends
*/
analyzeWellnessTrends(days = 30) {
const cacheKey = this.cache.generateKey('wellness-trends', days);
const cached = this.cache.get(cacheKey);
if (cached) return cached;
// Optimized approach: avoid per-day expensive queries by using aggregated scores
// If detailed per-day historical data is required, replace this with a proper time-series fetch.
const scores = [];
// Try to get an overall score for the period and use it as a fast proxy for per-day values
try {
const overall = this.dashboard.calculateWellnessScore(days);
if (overall && typeof overall.percentage === 'number') {
for (let i = 0; i < days; i++) scores.push(overall.percentage);
}
} catch (err) {
// Fallback to a light-weight per-day probe if calculateWellnessScore fails
for (let i = 0; i < days; i++) {
const score = this.dashboard.calculateWellnessScore(1);
if (score && typeof score.percentage === 'number') scores.push(score.percentage);
}
}
if (scores.length < 7) {
return { message: 'Need at least 7 days of data for trend analysis' };
}
// Predict future trend
const prediction = this.predictTrend(scores, 7);
// Detect anomalies
const anomalies = this.detectAnomalies(scores);
// Calculate moving average
const movingAvg = this.calculateMovingAverage(scores, 7);
const result = {
current: scores[scores.length - 1],
average: scores.reduce((sum, s) => sum + s, 0) / scores.length,
trend: prediction?.trend,
prediction: prediction?.predictions,
anomalies: anomalies.length,
anomalyDetails: anomalies,
movingAverage: movingAvg[movingAvg.length - 1]
};
this.cache.set(cacheKey, result);
return result;
}
/**
* Calculate moving average
*/
calculateMovingAverage(data, window = 7) {
const result = [];
for (let i = 0; i < data.length; i++) {
const start = Math.max(0, i - window + 1);
const windowData = data.slice(start, i + 1);
const avg = windowData.reduce((sum, val) => sum + val, 0) / windowData.length;
result.push(avg);
}
return result;
}
/**
* Generate comprehensive analytics report
*/
generateReport(days = 30) {
const report = {
period: `${days} days`,
generated: new Date().toISOString(),
correlations: {},
trends: {},
insights: []
};
// Correlation analysis
report.correlations.sleepExercise = this.analyzeSleepExerciseCorrelation(days);
report.correlations.moodSleep = this.analyzeMoodSleepCorrelation(days);
report.correlations.moodExercise = this.analyzeMoodExerciseCorrelation(days);
// Trend analysis
report.trends.wellness = this.analyzeWellnessTrends(days);
// Generate insights
const correlations = [
{ name: 'Sleep & Exercise', data: report.correlations.sleepExercise },
{ name: 'Mood & Sleep', data: report.correlations.moodSleep },
{ name: 'Mood & Exercise', data: report.correlations.moodExercise }
];
correlations.forEach(({ name, data }) => {
if (data.correlation !== null && Math.abs(data.correlation) >= 0.4) {
report.insights.push({
type: 'correlation',
title: `${name} Correlation`,
description: data.insight,
strength: data.strength,
actionable: this.generateActionableInsight(name, data.correlation)
});
}
});
// Trend insights
if (report.trends.wellness.trend) {
report.insights.push({
type: 'trend',
title: 'Wellness Trend',
description: `Your wellness is ${report.trends.wellness.trend}`,
actionable: this.generateTrendInsight(report.trends.wellness)
});
}
// Anomaly insights
if (report.trends.wellness.anomalies > 0) {
report.insights.push({
type: 'anomaly',
title: 'Unusual Patterns Detected',
description: `Found ${report.trends.wellness.anomalies} unusual day(s) in your wellness data`,
actionable: 'Review these days to identify what caused the deviation'
});
}
return report;
}
/**
* Generate actionable insight from correlation
*/
generateActionableInsight(correlationName, r) {
if (correlationName.includes('Sleep & Exercise')) {
return r > 0
? 'Increase exercise to potentially improve sleep quality'
: 'Monitor how exercise timing affects your sleep';
} else if (correlationName.includes('Mood & Sleep')) {
return r > 0
? 'Prioritize good sleep to maintain positive mood'
: 'Consider factors beyond sleep affecting your mood';
} else if (correlationName.includes('Mood & Exercise')) {
return r > 0
? 'Regular exercise may help improve your mood'
: 'Exercise effects on mood may vary - track patterns';
}
return 'Continue tracking to refine insights';
}
/**
* Generate trend insight
*/
generateTrendInsight(trendData) {
if (trendData.trend === 'improving') {
return 'Keep up the great work! Your wellness is on an upward trajectory.';
} else if (trendData.trend === 'declining') {
return 'Consider reviewing your recent habits and making adjustments.';
} else {
return 'Your wellness is stable. Set new goals to reach the next level.';
}
}
/**
* Generate comprehensive analytics report
* Alias for generateReport for compatibility with export-manager
*/
generateComprehensiveReport(days = 30) {
return this.generateReport(days);
}
/**
* Display analytics dashboard
*/
displayDashboard(days = 30) {
const report = this.generateReport(days);
console.log('\n╔═══════════════════════════════════════════════════════════════╗');
console.log('║ Wellness Dashboard ║');
console.log('╚═══════════════════════════════════════════════════════════════╝');
console.log(`\nAnalysis Period: ${report.period}`);
console.log(`Generated: ${new Date(report.generated).toLocaleString()}\n`);
// Correlations
console.log('═'.repeat(65));
console.log('📊 CORRELATION ANALYSIS');
console.log('═'.repeat(65));
const corrData = [
{ name: 'Sleep Quality & Exercise', ...report.correlations.sleepExercise },
{ name: 'Mood & Sleep Quality', ...report.correlations.moodSleep },
{ name: 'Mood & Exercise', ...report.correlations.moodExercise }
];
corrData.forEach(corr => {
const icon = corr.correlation === null ? '⚠️ ' :
Math.abs(corr.correlation) >= 0.7 ? '🔥' :
Math.abs(corr.correlation) >= 0.4 ? '📈' : '📉';
console.log(`\n${icon} ${corr.name}`);
if (corr.correlation !== null) {
console.log(` Correlation: ${corr.correlation.toFixed(3)} (${corr.strength})`);
console.log(` Sample Size: ${corr.sampleSize} days`);
console.log(` ${corr.insight}`);
} else {
console.log(` ${corr.message}`);
}
});
// Trends
console.log('\n' + '═'.repeat(65));
console.log('📈 WELLNESS TRENDS');
console.log('═'.repeat(65));
if (report.trends.wellness.message) {
console.log(`\n⚠️ ${report.trends.wellness.message}`);
} else {
console.log(`\nCurrent: ${report.trends.wellness.current?.toFixed(1)}%`);
console.log(`Average: ${report.trends.wellness.average?.toFixed(1)}%`);
console.log(`Trend: ${report.trends.wellness.trend}`);
if (report.trends.wellness.prediction) {
console.log('\n7-Day Forecast:');
report.trends.wellness.prediction.slice(0, 3).forEach((pred, i) => {
console.log(` Day ${i + 1}: ${pred.toFixed(1)}%`);
});
}
if (report.trends.wellness.anomalies > 0) {
console.log(`\n⚠️ Detected ${report.trends.wellness.anomalies} anomalous day(s)`);
}
}
// Key Insights
console.log('\n' + '═'.repeat(65));
console.log('💡 KEY INSIGHTS & RECOMMENDATIONS');
console.log('═'.repeat(65));
if (report.insights.length === 0) {
console.log('\n📭 Not enough data yet. Keep tracking to unlock insights!');
} else {
report.insights.forEach((insight, index) => {
const icon = insight.type === 'correlation' ? '🔗' :
insight.type === 'trend' ? '📈' : '⚠️ ';
console.log(`\n${index + 1}. ${icon} ${insight.title}`);
console.log(` ${insight.description}`);
console.log(` 💡 ${insight.actionable}`);
});
}
console.log('\n' + '═'.repeat(65));
}
/**
* Find optimal time for activity based on historical patterns
* Analyzes when user typically performs activities successfully
* @param {string} type - Type of activity ('exercise', 'medication', 'sleep')
* @param {number} days - Number of days to analyze (default: 30)
* @returns {object} Optimal time and confidence data
*/
findOptimalTimeForActivity(type, days = 30) {
const cacheKey = this.cache.generateKey('optimal-time', type, days);
const cached = this.cache.get(cacheKey);
if (cached) return cached;
let timeData = [];
try {
if (type === 'exercise' && this.dashboard.exerciseTracker) {
// Get exercise data
const exercises = this.dashboard.exerciseTracker.data.exercises || [];
const cutoff = new Date();
cutoff.setDate(cutoff.getDate() - days);
timeData = exercises
.filter(ex => new Date(ex.timestamp) >= cutoff)
.map(ex => {
const hour = new Date(ex.timestamp).getHours();
return { hour, quality: ex.intensity === 'high' ? 3 : ex.intensity === 'medium' ? 2 : 1 };
});
} else if (type === 'medication' && this.dashboard.medication) {
// Get medication adherence data
const history = this.dashboard.medication.data.adherenceHistory || [];
const cutoff = new Date();
cutoff.setDate(cutoff.getDate() - days);
timeData = history
.filter(h => h.taken && new Date(h.takenAt) >= cutoff)
.map(h => {
const hour = new Date(h.takenAt).getHours();
return { hour, quality: 1 }; // Successfully taken = quality 1
});
} else if (type === 'sleep' && this.dashboard.sleepTracker) {
// Get sleep data - find when user typically goes to bed
const sleepEntries = this.dashboard.sleepTracker.data.sleepEntries || [];
const cutoff = new Date();
cutoff.setDate(cutoff.getDate() - days);
timeData = sleepEntries
.filter(entry => new Date(entry.timestamp) >= cutoff && entry.quality)
.map(entry => {
const bedtime = entry.bedtime || '22:00';
const [hour] = bedtime.split(':').map(Number);
return { hour, quality: entry.quality };
});
}
// Analyze time patterns
if (timeData.length < 3) {
return {
success: false,
message: 'Need at least 3 days of data to suggest optimal time',
defaultTime: this.getDefaultTime(type)
};
}
// Group by hour and calculate weighted scores
const hourStats = {};
timeData.forEach(({ hour, quality }) => {
if (!hourStats[hour]) {
hourStats[hour] = { count: 0, totalQuality: 0 };
}
hourStats[hour].count++;
hourStats[hour].totalQuality += quality;
});
// Find hour with highest frequency and quality
let bestHour = null;
let bestScore = 0;
Object.entries(hourStats).forEach(([hour, stats]) => {
// Score = frequency * average quality
const avgQuality = stats.totalQuality / stats.count;
const score = stats.count * avgQuality;
if (score > bestScore) {
bestScore = score;
bestHour = parseInt(hour);
}
});
const confidence = Math.min((timeData.length / 30) * 100, 100); // Max confidence at 30 days
const result = {
success: true,
optimalHour: bestHour,
optimalTime: `${String(bestHour).padStart(2, '0')}:00`,
confidence: Math.round(confidence),
sampleSize: timeData.length,
message: `Based on ${timeData.length} days of data, ${String(bestHour).padStart(2, '0')}:00 appears to be your most consistent time`
};
this.cache.set(cacheKey, result);
return result;
} catch (error) {
return {
success: false,
message: 'Error analyzing activity patterns',
defaultTime: this.getDefaultTime(type)
};
}
}
/**
* Get default time for activity type
* @param {string} type - Activity type
* @returns {string} Default time in HH:mm format
*/
getDefaultTime(type) {
const defaults = {
medication: '08:00',
exercise: '18:00',
sleep: '22:00',
custom: '12:00'
};
return defaults[type] || defaults.custom;
}
}
module.exports = AnalyticsEngine;