forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsights.worker.ts
More file actions
477 lines (421 loc) · 12 KB
/
Copy pathinsights.worker.ts
File metadata and controls
477 lines (421 loc) · 12 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
// Web Worker for computing memory insights off the main thread
// This prevents blocking the UI during expensive computations
import type { Memory } from '@/types/conversation';
// Life balance categories with keywords for auto-categorization
const LIFE_CATEGORIES = {
work: [
'work',
'meeting',
'team',
'project',
'hiring',
'company',
'office',
'client',
'deadline',
'presentation',
'employee',
'manager',
'boss',
'job',
'career',
'business',
'sales',
'marketing',
'product',
'engineering',
'startup',
'investor',
'funding',
'revenue',
],
family: [
'family',
'kids',
'children',
'son',
'daughter',
'spouse',
'wife',
'husband',
'home',
'parent',
'mom',
'dad',
'mother',
'father',
'sister',
'brother',
'grandma',
'grandpa',
'baby',
'toddler',
],
health: [
'health',
'gym',
'workout',
'exercise',
'sleep',
'diet',
'meditation',
'doctor',
'fitness',
'running',
'yoga',
'weight',
'nutrition',
'mental',
'therapy',
'stress',
'anxiety',
'wellness',
],
learning: [
'learn',
'study',
'course',
'book',
'read',
'AI',
'skill',
'education',
'training',
'tutorial',
'research',
'knowledge',
'coding',
'programming',
'language',
'certificate',
'degree',
],
social: [
'friend',
'dinner',
'party',
'event',
'social',
'hangout',
'call',
'catch up',
'lunch',
'coffee',
'drinks',
'birthday',
'wedding',
'celebration',
'network',
'community',
],
hobbies: [
'travel',
'vacation',
'music',
'game',
'hobby',
'sport',
'movie',
'show',
'art',
'photography',
'cooking',
'garden',
'hiking',
'camping',
'beach',
'concert',
'museum',
'theater',
],
} as const;
type LifeCategory = keyof typeof LIFE_CATEGORIES;
// Category colors
const CATEGORY_COLORS: Record<LifeCategory, string> = {
work: '#8B5CF6', // Purple
family: '#EC4899', // Pink
health: '#10B981', // Green
learning: '#3B82F6', // Blue
social: '#F59E0B', // Amber
hobbies: '#06B6D4', // Cyan
};
// Helper: Get day of week name
const DAY_NAMES = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
// Helper: Calculate streak
function calculateStreak(memories: Memory[]): number {
if (memories.length === 0) return 0;
// Sort by date descending
const sorted = [...memories].sort((a, b) => new Date(b.created_at).getTime() - new Date(a.created_at).getTime());
const today = new Date();
today.setHours(0, 0, 0, 0);
let streak = 0;
let currentDate = today;
// Group memories by date
const memoryDates = new Set(sorted.map((m) => m.created_at.split('T')[0]));
// Check if today has memories, if not start from yesterday
const todayStr = today.toISOString().split('T')[0];
if (!memoryDates.has(todayStr)) {
currentDate = new Date(today);
currentDate.setDate(currentDate.getDate() - 1);
}
// Count consecutive days
while (true) {
const dateStr = currentDate.toISOString().split('T')[0];
if (memoryDates.has(dateStr)) {
streak++;
currentDate.setDate(currentDate.getDate() - 1);
} else {
break;
}
}
return streak;
}
// Helper: Categorize a tag
function categorizeTag(tag: string): LifeCategory | null {
const lowerTag = tag.toLowerCase();
for (const [category, keywords] of Object.entries(LIFE_CATEGORIES)) {
for (const keyword of keywords) {
if (lowerTag.includes(keyword.toLowerCase())) {
return category as LifeCategory;
}
}
}
return null;
}
// Main computation function
function computeInsights(memories: Memory[]) {
if (!memories || memories.length === 0) {
return {
summary: null,
lifeBalance: [],
risingTags: [],
fadingTags: [],
activityCalendar: [],
dayOfWeekPattern: [],
hourPattern: [],
allTags: [],
};
}
const now = new Date();
const thirtyDaysAgo = new Date(now);
thirtyDaysAgo.setDate(thirtyDaysAgo.getDate() - 30);
const sixtyDaysAgo = new Date(now);
sixtyDaysAgo.setDate(sixtyDaysAgo.getDate() - 60);
// ==================== 1. TAG COMPUTATION (BASE DATA) ====================
const tagCounts = new Map<string, number>();
const tagLastSeen = new Map<string, Date>();
const recentTagCounts = new Map<string, number>();
const priorTagCounts = new Map<string, number>();
const cooccurrence = new Map<string, Map<string, number>>();
for (const memory of memories) {
const memoryDate = new Date(memory.created_at);
const tags = memory.tags || [];
for (const tag of tags) {
// Overall counts
tagCounts.set(tag, (tagCounts.get(tag) || 0) + 1);
// Last seen
if (!tagLastSeen.has(tag) || memoryDate > tagLastSeen.get(tag)!) {
tagLastSeen.set(tag, memoryDate);
}
// Recent vs Prior (for trending)
if (memoryDate >= thirtyDaysAgo) {
recentTagCounts.set(tag, (recentTagCounts.get(tag) || 0) + 1);
} else if (memoryDate >= sixtyDaysAgo) {
priorTagCounts.set(tag, (priorTagCounts.get(tag) || 0) + 1);
}
// Co-occurrence
for (const otherTag of tags) {
if (tag !== otherTag) {
if (!cooccurrence.has(tag)) {
cooccurrence.set(tag, new Map());
}
const tagCooccur = cooccurrence.get(tag)!;
tagCooccur.set(otherTag, (tagCooccur.get(otherTag) || 0) + 1);
}
}
}
}
// ==================== 2. DATE-BASED METRICS ====================
const dateMetrics = (() => {
const dailyCounts = new Map<string, number>();
const dayOfWeekCounts = new Array(7).fill(0);
const hourCounts = new Array(24).fill(0);
const thisMonthCount = memories.filter(
(m) => new Date(m.created_at).getMonth() === now.getMonth() && new Date(m.created_at).getFullYear() === now.getFullYear()
).length;
for (const memory of memories) {
const date = new Date(memory.created_at);
const dateStr = date.toISOString().split('T')[0];
dailyCounts.set(dateStr, (dailyCounts.get(dateStr) || 0) + 1);
const dayOfWeek = date.getDay();
dayOfWeekCounts[dayOfWeek]++;
const hour = date.getHours();
hourCounts[hour]++;
}
// Find most active day
let mostActiveDay = '';
let maxCount = 0;
for (const [date, count] of dailyCounts.entries()) {
if (count > maxCount) {
maxCount = count;
mostActiveDay = date;
}
}
const totalDays = Math.max(
1,
Math.ceil((now.getTime() - new Date(memories[memories.length - 1]?.created_at || now).getTime()) / (1000 * 60 * 60 * 24))
);
const avgMemoriesPerDay = memories.length / totalDays;
return {
dailyCounts,
dayOfWeekCounts,
hourCounts,
thisMonthCount,
mostActiveDay,
avgMemoriesPerDay,
};
})();
// ==================== 3. ALL TAGS ====================
const allTags = Array.from(tagCounts.entries())
.map(([tag, count]) => ({ tag, count }))
.sort((a, b) => b.count - a.count);
// ==================== 4. SUMMARY STATS ====================
const topTags = allTags.slice(0, 5);
const currentStreak = calculateStreak(memories);
// Find surprising connection
let surprisingConnection: { tag1: string; tag2: string; count: number } | undefined;
let maxCooccur = 0;
for (const [tag1, relatedTags] of cooccurrence.entries()) {
for (const [tag2, count] of relatedTags.entries()) {
if (count > maxCooccur && count >= 3) {
maxCooccur = count;
surprisingConnection = { tag1, tag2, count };
}
}
}
const summary = {
totalMemories: memories.length,
memoriesThisMonth: dateMetrics.thisMonthCount,
topTags,
mostActiveDay: dateMetrics.mostActiveDay,
currentStreak,
avgMemoriesPerDay: Math.round(dateMetrics.avgMemoriesPerDay * 10) / 10,
surprisingConnection,
};
// ==================== 5. LIFE BALANCE ====================
const categoryCounts = new Map<LifeCategory, number>();
for (const tag of tagCounts.keys()) {
const category = categorizeTag(tag);
if (category) {
const count = tagCounts.get(tag) || 0;
categoryCounts.set(category, (categoryCounts.get(category) || 0) + count);
}
}
const totalCategorized = Array.from(categoryCounts.values()).reduce((sum, count) => sum + count, 0);
const lifeBalance = (Object.keys(CATEGORY_COLORS) as LifeCategory[])
.map((category) => {
const rawCount = categoryCounts.get(category) || 0;
const value = totalCategorized > 0 ? Math.round((rawCount / totalCategorized) * 100) : 0;
return {
category,
label: category.charAt(0).toUpperCase() + category.slice(1),
value,
rawCount,
color: CATEGORY_COLORS[category],
};
})
.sort((a, b) => b.value - a.value);
// ==================== 6. TRENDING TAGS (RISING & FADING) ====================
const trendingData: Array<{
tag: string;
recentCount: number;
priorCount: number;
change: number;
daysSinceLastMention?: number;
}> = [];
for (const [tag, recentCount] of recentTagCounts.entries()) {
const priorCount = priorTagCounts.get(tag) || 0;
const totalCount = tagCounts.get(tag) || 0;
// Only consider tags with meaningful data
if (totalCount >= 3 && (recentCount > 0 || priorCount > 0)) {
const change = priorCount > 0 ? ((recentCount - priorCount) / priorCount) * 100 : recentCount > 0 ? 100 : 0;
const lastSeen = tagLastSeen.get(tag);
const daysSinceLastMention = lastSeen ? Math.floor((now.getTime() - lastSeen.getTime()) / (1000 * 60 * 60 * 24)) : undefined;
trendingData.push({
tag,
recentCount,
priorCount,
change,
daysSinceLastMention,
});
}
}
const risingTags = trendingData
.filter((t) => t.change > 0 && t.recentCount >= 2)
.sort((a, b) => b.change - a.change)
.slice(0, 5);
const fadingTags = trendingData
.filter((t) => t.change < 0 && t.priorCount >= 2)
.sort((a, b) => a.change - b.change)
.slice(0, 5);
// ==================== 7. ACTIVITY CALENDAR ====================
const activityCalendar = (() => {
const last90Days = new Date(now);
last90Days.setDate(last90Days.getDate() - 90);
const calendar: Array<{
date: string;
count: number;
dayOfWeek: number;
weekNumber: number;
}> = [];
for (let i = 90; i >= 0; i--) {
const date = new Date(now);
date.setDate(date.getDate() - i);
const dateStr = date.toISOString().split('T')[0];
const count = dateMetrics.dailyCounts.get(dateStr) || 0;
calendar.push({
date: dateStr,
count,
dayOfWeek: date.getDay(),
weekNumber: Math.floor((90 - i) / 7),
});
}
return calendar;
})();
// ==================== 8. TIME PATTERNS ====================
const dayOfWeekPattern = DAY_NAMES.map((label, i) => {
const count = dateMetrics.dayOfWeekCounts[i];
const percentage = memories.length > 0 ? Math.round((count / memories.length) * 100) : 0;
return { label, count, percentage };
});
const hourPattern = Array.from({ length: 24 }, (_, hour) => {
const count = dateMetrics.hourCounts[hour];
const percentage = memories.length > 0 ? Math.round((count / memories.length) * 100) : 0;
const label = `${hour.toString().padStart(2, '0')}:00`;
return { label, count, percentage };
});
return {
summary,
lifeBalance,
risingTags,
fadingTags,
activityCalendar,
dayOfWeekPattern,
hourPattern,
allTags,
};
}
// Worker message handler
self.onmessage = (e: MessageEvent) => {
const { type, memories } = e.data;
if (type === 'compute') {
try {
const insights = computeInsights(memories);
self.postMessage({ type: 'result', insights });
} catch (error) {
self.postMessage({ type: 'error', error: error instanceof Error ? error.message : 'Unknown error' });
}
}
};
// For TypeScript
export {};