skilltally 1.0.3 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -35,22 +35,65 @@ export function parseSince(sinceStr, now = new Date()) {
35
35
  throw new Error(`Invalid --since value: "${sinceStr}" (use e.g. 7d, 30d, 90d, 4w, 6m, 2026-01-01, or all)`);
36
36
  }
37
37
 
38
- function hostMatches(eventHost, filterHost) {
38
+ export function formatDurationCompact(seconds) {
39
+ const sec = Number(seconds);
40
+ if (!Number.isFinite(sec) || sec <= 0) return '0s';
41
+ const rounded = Math.round(sec);
42
+ if (rounded < 60) return `${rounded}s`;
43
+ if (rounded < 3600) {
44
+ const m = Math.floor(rounded / 60);
45
+ const s = rounded % 60;
46
+ return s > 0 ? `${m}m ${s}s` : `${m}m`;
47
+ }
48
+ const h = Math.floor(rounded / 3600);
49
+ const m = Math.floor((rounded % 3600) / 60);
50
+ return m > 0 ? `${h}h ${m}m` : `${h}h`;
51
+ }
52
+
53
+ export function hostMatches(eventHost, filterHost) {
39
54
  if (!filterHost || filterHost === 'all') return true;
40
55
  if (eventHost === filterHost) return true;
41
56
  if (filterHost === 'antigravity' && String(eventHost).startsWith('antigravity')) return true;
42
57
  return false;
43
58
  }
44
59
 
45
- export function aggregateStats({
60
+ const SPARK_CHARS = ['▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'];
61
+
62
+ export function isManualVia(via) {
63
+ return via === 'mention' || via === 'slash_command';
64
+ }
65
+
66
+ export function buildDailySparklineSeries(daySetsMap, now = new Date(), days = 14) {
67
+ const endUtcMs = Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate());
68
+ const series = [];
69
+ for (let i = days - 1; i >= 0; i--) {
70
+ const dayKey = new Date(endUtcMs - i * 86400_000).toISOString().slice(0, 10);
71
+ series.push(daySetsMap?.get(dayKey)?.size || 0);
72
+ }
73
+ return series;
74
+ }
75
+
76
+ export function encodeSparkline(counts) {
77
+ if (!Array.isArray(counts) || counts.length === 0) return '';
78
+ const max = Math.max(0, ...counts);
79
+ if (max === 0) return '·'.repeat(counts.length);
80
+ return counts
81
+ .map((c) => {
82
+ if (!c || c <= 0) return '·';
83
+ if (max <= 1) return '█';
84
+ const idx = Math.min(7, Math.floor(((c - 1) / Math.max(1, max - 1)) * 7));
85
+ return SPARK_CHARS[idx];
86
+ })
87
+ .join('');
88
+ }
89
+
90
+ export function prepareFilteredEvents({
46
91
  events = [],
47
92
  inventory = null,
48
93
  config = {},
49
94
  since = '30d',
50
95
  host = null,
51
96
  project = null,
52
- bySource = false,
53
- metric = 'activations',
54
97
  now = new Date(),
55
98
  } = {}) {
56
99
  const sinceDate = since === null ? null : parseSince(since, now);
@@ -76,13 +119,90 @@ export function aggregateStats({
76
119
  filtered.push(ev);
77
120
  }
78
121
 
122
+ return {
123
+ sinceDate,
124
+ exclSkills,
125
+ exclSources,
126
+ filtered,
127
+ };
128
+ }
129
+
130
+ function findInventoryEntry(inventory, skill, source, bySource) {
131
+ if (!inventory) return null;
132
+ if (bySource && Array.isArray(inventory.skills)) {
133
+ const exact = inventory.skills.find(
134
+ (s) => (s.name === skill || s.dir_name === skill) && s.source === source
135
+ );
136
+ if (exact) return exact;
137
+ }
138
+ return inventory.index?.byName?.get(skill) ?? inventory.index?.byDirName?.get(skill) ?? null;
139
+ }
140
+
141
+ export function aggregateStats({
142
+ events = [],
143
+ inventory = null,
144
+ config = {},
145
+ since = '30d',
146
+ host = null,
147
+ project = null,
148
+ bySource = false,
149
+ metric = 'activations',
150
+ now = new Date(),
151
+ } = {}) {
152
+ const { sinceDate, exclSkills, exclSources, filtered } = prepareFilteredEvents({
153
+ events,
154
+ inventory,
155
+ config,
156
+ since,
157
+ host,
158
+ project,
159
+ now,
160
+ });
161
+
79
162
  const hostTotals = {};
163
+ const viaTotals = {};
164
+ const modelTotals = {};
165
+ const projectTotals = {};
166
+ const allSessions = new Set();
167
+ const sessionBounds = new Map();
168
+ const sessionExplicitDur = new Map();
169
+ const sessionTokensMap = new Map();
170
+ const sessionBodyFallbackMap = new Map();
171
+ const promptMap = new Map();
172
+
173
+ let manualTotal = 0;
174
+ let autoTotal = 0;
175
+ let totalInputTokens = 0;
176
+ let totalOutputTokens = 0;
177
+ let totalCacheReadTokens = 0;
178
+ let totalCacheCreationTokens = 0;
179
+ let totalRecordedTokens = 0;
180
+ let maxSingleDuration = 0;
80
181
  const groups = new Map();
81
182
 
82
- for (const ev of filtered) {
183
+ for (let idx = 0; idx < filtered.length; idx++) {
184
+ const ev = filtered[idx];
83
185
  const h = ev.host || 'unknown';
84
186
  hostTotals[h] = (hostTotals[h] || 0) + 1;
85
187
 
188
+ const viaKey = ev.via || 'unknown';
189
+ viaTotals[viaKey] = (viaTotals[viaKey] || 0) + 1;
190
+ const isManual = isManualVia(viaKey);
191
+ if (isManual) manualTotal += 1;
192
+ else autoTotal += 1;
193
+
194
+ if (ev.model) {
195
+ modelTotals[ev.model] = (modelTotals[ev.model] || 0) + 1;
196
+ }
197
+ if (ev.project) {
198
+ projectTotals[ev.project] = (projectTotals[ev.project] || 0) + 1;
199
+ }
200
+
201
+ const globalSessKey = ev.session_id
202
+ ? `${h}\0${ev.session_id}`
203
+ : ev.id || `${h}\0${ev.ts || ''}\0${idx}`;
204
+ allSessions.add(globalSessKey);
205
+
86
206
  const key = bySource ? `${ev.skill}\0${ev.source || 'unknown'}` : ev.skill;
87
207
  let bucket = groups.get(key);
88
208
  if (!bucket) {
@@ -91,8 +211,16 @@ export function aggregateStats({
91
211
  source: ev.source || 'unknown',
92
212
  sources: new Set([ev.source || 'unknown']),
93
213
  reads: 0,
214
+ manualReads: 0,
215
+ autoReads: 0,
216
+ via: {},
94
217
  sessions: new Set(),
95
218
  hosts: new Set(),
219
+ projects: new Set(),
220
+ models: new Set(),
221
+ daySessions: new Map(),
222
+ durations: [],
223
+ tokens: { input: 0, output: 0, cacheRead: 0, cacheCreation: 0, total: 0 },
96
224
  last_used: ev.ts || '',
97
225
  };
98
226
  groups.set(key, bucket);
@@ -104,22 +232,128 @@ export function aggregateStats({
104
232
  }
105
233
 
106
234
  bucket.reads += 1;
107
- bucket.sessions.add(ev.session_id || ev.id || `${ev.ts}:${bucket.reads}`);
235
+ if (isManual) bucket.manualReads += 1;
236
+ else bucket.autoReads += 1;
237
+ bucket.via[viaKey] = (bucket.via[viaKey] || 0) + 1;
238
+
239
+ const sessKey = ev.session_id || ev.id || `${ev.ts}:${bucket.reads}`;
240
+ bucket.sessions.add(sessKey);
108
241
  bucket.hosts.add(h);
109
- if (ev.ts && (!bucket.last_used || ev.ts > bucket.last_used)) {
110
- bucket.last_used = ev.ts;
242
+ if (ev.project) bucket.projects.add(ev.project);
243
+ if (ev.model) bucket.models.add(ev.model);
244
+
245
+ if (typeof ev.duration_seconds === 'number' && Number.isFinite(ev.duration_seconds) && ev.duration_seconds > 0) {
246
+ const dur = Math.min(86400, Math.round(ev.duration_seconds));
247
+ bucket.durations.push(dur);
248
+ sessionExplicitDur.set(globalSessKey, (sessionExplicitDur.get(globalSessKey) || 0) + dur);
249
+ if (dur > maxSingleDuration) maxSingleDuration = dur;
250
+ }
251
+
252
+ const inTok = typeof ev.input_tokens === 'number' && Number.isFinite(ev.input_tokens) ? ev.input_tokens : 0;
253
+ const outTok = typeof ev.output_tokens === 'number' && Number.isFinite(ev.output_tokens) ? ev.output_tokens : 0;
254
+ const crTok = typeof ev.cache_read_tokens === 'number' && Number.isFinite(ev.cache_read_tokens) ? ev.cache_read_tokens : 0;
255
+ const ccTok = typeof ev.cache_creation_tokens === 'number' && Number.isFinite(ev.cache_creation_tokens) ? ev.cache_creation_tokens : 0;
256
+ const totTok = typeof ev.total_tokens === 'number' && Number.isFinite(ev.total_tokens) ? ev.total_tokens : 0;
257
+ if (totTok > 0 || inTok > 0 || outTok > 0) {
258
+ const effectiveTotal = totTok > 0 ? totTok : inTok + outTok + crTok + ccTok;
259
+ bucket.tokens.input += inTok;
260
+ bucket.tokens.output += outTok;
261
+ bucket.tokens.cacheRead += crTok;
262
+ bucket.tokens.cacheCreation += ccTok;
263
+ bucket.tokens.total += effectiveTotal;
264
+ totalInputTokens += inTok;
265
+ totalOutputTokens += outTok;
266
+ totalCacheReadTokens += crTok;
267
+ totalCacheCreationTokens += ccTok;
268
+ totalRecordedTokens += effectiveTotal;
269
+ sessionTokensMap.set(globalSessKey, (sessionTokensMap.get(globalSessKey) || 0) + effectiveTotal);
270
+ }
271
+
272
+ if (typeof ev.prompt_preview === 'string' && ev.prompt_preview.trim()) {
273
+ const pKey = ev.prompt_preview.trim();
274
+ let pEntry = promptMap.get(pKey);
275
+ if (!pEntry) {
276
+ pEntry = { prompt: pKey, count: 0, skills: new Set() };
277
+ promptMap.set(pKey, pEntry);
278
+ }
279
+ pEntry.count += 1;
280
+ pEntry.skills.add(ev.skill);
281
+ }
282
+
283
+ if (ev.ts) {
284
+ const tsMs = Date.parse(ev.ts);
285
+ if (Number.isFinite(tsMs)) {
286
+ const bounds = sessionBounds.get(globalSessKey);
287
+ if (!bounds) {
288
+ sessionBounds.set(globalSessKey, { min: tsMs, max: tsMs });
289
+ } else {
290
+ if (tsMs < bounds.min) bounds.min = tsMs;
291
+ if (tsMs > bounds.max) bounds.max = tsMs;
292
+ }
293
+ const dayKey = new Date(tsMs).toISOString().slice(0, 10);
294
+ let daySet = bucket.daySessions.get(dayKey);
295
+ if (!daySet) {
296
+ daySet = new Set();
297
+ bucket.daySessions.set(dayKey, daySet);
298
+ }
299
+ daySet.add(sessKey);
300
+ }
301
+ if (!bucket.last_used || ev.ts > bucket.last_used) {
302
+ bucket.last_used = ev.ts;
303
+ }
111
304
  }
112
305
  }
113
306
 
114
- const rows = [...groups.values()].map((b) => ({
115
- skill: b.skill,
116
- source: b.source,
117
- sources: [...b.sources].sort(),
118
- activations: b.sessions.size,
119
- reads: b.reads,
120
- last_used: b.last_used,
121
- hosts: [...b.hosts].sort(),
122
- }));
307
+ const totalSessions = Math.max(1, allSessions.size);
308
+
309
+ const rows = [...groups.values()].map((b) => {
310
+ const invEntry = findInventoryEntry(inventory, b.skill, b.source, bySource);
311
+ const estTokens = invEntry?.est_tokens || 0;
312
+ const descChars = invEntry?.desc_chars || 0;
313
+ const bodyTokens =
314
+ invEntry?.body_tokens || (invEntry?.bytes ? Math.ceil(invEntry.bytes / 4) : 0);
315
+ const activations = b.sessions.size;
316
+ const activeLoadTokens = activations * bodyTokens;
317
+ const dailyCounts = buildDailySparklineSeries(b.daySessions, now, 14);
318
+ const totalDur = b.durations.reduce((acc, d) => acc + d, 0);
319
+ const avgDur = b.durations.length > 0 ? Math.round(totalDur / b.durations.length) : 0;
320
+ const maxDur = b.durations.length > 0 ? Math.max(...b.durations) : 0;
321
+
322
+ return {
323
+ skill: b.skill,
324
+ source: b.source,
325
+ sources: [...b.sources].sort(),
326
+ activations,
327
+ reads: b.reads,
328
+ last_used: b.last_used,
329
+ hosts: [...b.hosts].sort(),
330
+ via: b.via,
331
+ manualReads: b.manualReads,
332
+ autoReads: b.autoReads,
333
+ manualRatio: b.reads > 0 ? Number((b.manualReads / b.reads).toFixed(2)) : 0,
334
+ projects: [...b.projects].sort(),
335
+ models: [...b.models].sort(),
336
+ est_tokens: estTokens,
337
+ desc_chars: descChars,
338
+ body_tokens: bodyTokens,
339
+ activeLoadTokens,
340
+ tokenCostPerActivation: estTokens > 0 ? Math.round((totalSessions * estTokens) / Math.max(1, activations)) : 0,
341
+ tokens: b.tokens,
342
+ total_duration_seconds: totalDur,
343
+ avg_duration_seconds: avgDur,
344
+ max_duration_seconds: maxDur,
345
+ dailyCounts,
346
+ sparkline: encodeSparkline(dailyCounts),
347
+ };
348
+ });
349
+
350
+ // Track fallback session body tokens for sessions lacking transcript usage
351
+ for (const r of rows) {
352
+ if (r.body_tokens > 0) {
353
+ const k = r.skill;
354
+ sessionBodyFallbackMap.set(k, r.body_tokens);
355
+ }
356
+ }
123
357
 
124
358
  rows.sort((a, b) => {
125
359
  if (metric === 'reads') {
@@ -148,6 +382,7 @@ export function aggregateStats({
148
382
  const disabledCount = invSkills.filter(
149
383
  (s) => !s.enabled && !exclSkills.has(s.name) && !exclSources.has(s.source)
150
384
  ).length;
385
+ const enabledEstTokens = enabledSkills.reduce((acc, s) => acc + (s.est_tokens || 0), 0);
151
386
 
152
387
  const usedNames = new Set(rows.map((r) => r.skill));
153
388
  const usedBySource = new Set(rows.map((r) => `${r.skill}\0${r.source}`));
@@ -188,6 +423,139 @@ export function aggregateStats({
188
423
  (a, b) => b.estTokens - a.estTokens || b.skillCount - a.skillCount || a.source.localeCompare(b.source)
189
424
  );
190
425
 
426
+ const manualOnlySkills = rows
427
+ .filter((r) => r.reads > 0 && r.autoReads === 0 && r.manualReads > 0)
428
+ .sort((a, b) => b.est_tokens - a.est_tokens || b.reads - a.reads)
429
+ .map((r) => ({
430
+ skill: r.skill,
431
+ source: r.source,
432
+ activations: r.activations,
433
+ reads: r.reads,
434
+ est_tokens: r.est_tokens,
435
+ via: r.via,
436
+ }));
437
+
438
+ const projectScopeCandidates = rows
439
+ .filter((r) => r.projects.length === 1 && r.source !== 'project' && r.source !== 'builtin')
440
+ .sort((a, b) => b.est_tokens - a.est_tokens || b.activations - a.activations)
441
+ .map((r) => ({
442
+ skill: r.skill,
443
+ source: r.source,
444
+ project: r.projects[0],
445
+ activations: r.activations,
446
+ reads: r.reads,
447
+ est_tokens: r.est_tokens,
448
+ }));
449
+
450
+ const highCostSkills = rows
451
+ .filter((r) => r.est_tokens > 0)
452
+ .sort((a, b) => b.tokenCostPerActivation - a.tokenCostPerActivation || b.est_tokens - a.est_tokens)
453
+ .slice(0, 10)
454
+ .map((r) => ({
455
+ skill: r.skill,
456
+ source: r.source,
457
+ activations: r.activations,
458
+ est_tokens: r.est_tokens,
459
+ tokenCostPerActivation: r.tokenCostPerActivation,
460
+ }));
461
+
462
+ // Heavy SKILL.md bodies (>= 1500 body tokens)
463
+ const heavySeen = new Set();
464
+ const heavyCandidates = [];
465
+ for (const r of rows) {
466
+ if (r.body_tokens >= 1500) {
467
+ heavySeen.add(`${r.skill}\0${r.source}`);
468
+ heavyCandidates.push({
469
+ skill: r.skill,
470
+ source: r.source,
471
+ activations: r.activations,
472
+ est_tokens: r.est_tokens,
473
+ body_tokens: r.body_tokens,
474
+ activeLoadTokens: r.activeLoadTokens,
475
+ });
476
+ }
477
+ }
478
+ for (const s of enabledSkills) {
479
+ const bt = s.body_tokens || (s.bytes ? Math.ceil(s.bytes / 4) : 0);
480
+ if (bt >= 1500 && !heavySeen.has(`${s.name}\0${s.source}`)) {
481
+ heavySeen.add(`${s.name}\0${s.source}`);
482
+ heavyCandidates.push({
483
+ skill: s.name,
484
+ source: s.source,
485
+ activations: 0,
486
+ est_tokens: s.est_tokens || 0,
487
+ body_tokens: bt,
488
+ activeLoadTokens: 0,
489
+ });
490
+ }
491
+ }
492
+ const heavyBodySkills = heavyCandidates
493
+ .sort(
494
+ (a, b) =>
495
+ b.body_tokens * Math.max(1, b.activations) - a.body_tokens * Math.max(1, a.activations) ||
496
+ b.body_tokens - a.body_tokens
497
+ )
498
+ .slice(0, 10);
499
+
500
+ const slowestSkills = rows
501
+ .filter((r) => r.total_duration_seconds > 0)
502
+ .sort((a, b) => b.total_duration_seconds - a.total_duration_seconds || b.max_duration_seconds - a.max_duration_seconds)
503
+ .slice(0, 10)
504
+ .map((r) => ({
505
+ skill: r.skill,
506
+ source: r.source,
507
+ activations: r.activations,
508
+ avg_duration_seconds: r.avg_duration_seconds,
509
+ total_duration_seconds: r.total_duration_seconds,
510
+ max_duration_seconds: r.max_duration_seconds,
511
+ }));
512
+
513
+ const promptPatterns = [...promptMap.values()]
514
+ .sort((a, b) => b.count - a.count || a.prompt.localeCompare(b.prompt))
515
+ .slice(0, 10)
516
+ .map((p) => ({
517
+ prompt: p.prompt,
518
+ count: p.count,
519
+ skills: [...p.skills].sort(),
520
+ }));
521
+
522
+ // Compute duration & token aggregates across sessions
523
+ let totalDurationSeconds = 0;
524
+ let longestTaskSeconds = maxSingleDuration;
525
+ for (const sKey of allSessions) {
526
+ const expDur = sessionExplicitDur.get(sKey) || 0;
527
+ let sessDur = expDur;
528
+ if (sessDur === 0) {
529
+ const bounds = sessionBounds.get(sKey);
530
+ if (bounds && bounds.max > bounds.min) {
531
+ const span = Math.round((bounds.max - bounds.min) / 1000);
532
+ if (span > 0 && span <= 86400) sessDur = span;
533
+ }
534
+ }
535
+ totalDurationSeconds += sessDur;
536
+ if (sessDur > longestTaskSeconds) longestTaskSeconds = sessDur;
537
+ }
538
+
539
+ const totalActivations = rows.reduce((acc, r) => acc + r.activations, 0);
540
+ const totalActiveBodyTokens = rows.reduce((acc, r) => acc + (r.activeLoadTokens || 0), 0);
541
+ const cacheDenominator = totalInputTokens + totalCacheReadTokens + totalCacheCreationTokens;
542
+
543
+ let peakSessionTokens = 0;
544
+ if (sessionTokensMap.size > 0) {
545
+ peakSessionTokens = Math.max(...sessionTokensMap.values());
546
+ } else if (filtered.length > 0) {
547
+ const perSessionFallback = new Map();
548
+ for (let idx = 0; idx < filtered.length; idx++) {
549
+ const ev = filtered[idx];
550
+ const h = ev.host || 'unknown';
551
+ const gKey = ev.session_id ? `${h}\0${ev.session_id}` : ev.id || `${h}\0${ev.ts || ''}\0${idx}`;
552
+ const bt = sessionBodyFallbackMap.get(ev.skill) || 0;
553
+ perSessionFallback.set(gKey, (perSessionFallback.get(gKey) || 0) + bt);
554
+ }
555
+ const maxBodyPerSess = perSessionFallback.size > 0 ? Math.max(...perSessionFallback.values()) : 0;
556
+ peakSessionTokens = maxBodyPerSess + enabledEstTokens;
557
+ }
558
+
191
559
  return {
192
560
  window: {
193
561
  since: since ?? 'all',
@@ -196,15 +564,42 @@ export function aggregateStats({
196
564
  },
197
565
  totals: {
198
566
  events: filtered.length,
199
- activations: rows.reduce((acc, r) => acc + r.activations, 0),
567
+ activations: totalActivations,
200
568
  uniqueSkills: rows.length,
569
+ sessions: allSessions.size,
201
570
  hosts: hostTotals,
571
+ via: viaTotals,
572
+ models: modelTotals,
573
+ projects: projectTotals,
574
+ manualReads: manualTotal,
575
+ autoReads: autoTotal,
576
+ tokens: {
577
+ input: totalInputTokens,
578
+ output: totalOutputTokens,
579
+ cacheRead: totalCacheReadTokens,
580
+ cacheCreation: totalCacheCreationTokens,
581
+ total: totalRecordedTokens,
582
+ cacheHitRate: cacheDenominator > 0 ? Number((totalCacheReadTokens / cacheDenominator).toFixed(2)) : 0,
583
+ activeBodyTokens: totalActiveBodyTokens,
584
+ lifetimeTokens:
585
+ totalRecordedTokens > 0
586
+ ? totalRecordedTokens
587
+ : totalActiveBodyTokens + enabledEstTokens * (filtered.length > 0 ? Math.max(1, allSessions.size) : 0),
588
+ peakSessionTokens,
589
+ },
590
+ duration: {
591
+ totalSeconds: totalDurationSeconds,
592
+ avgSeconds: rows.length > 0 ? Math.round(totalDurationSeconds / Math.max(1, totalActivations)) : 0,
593
+ longestTaskSeconds,
594
+ },
202
595
  },
203
596
  inventory: {
204
597
  enabled: enabledSkills.length,
205
598
  disabled: disabledCount,
206
599
  used: usedInstalledCount,
207
600
  unused: unusedSkills.length,
601
+ enabledEstTokens,
602
+ usedEstTokens: Math.max(0, enabledEstTokens - totalEstTokens),
208
603
  },
209
604
  rows,
210
605
  unused: {
@@ -213,5 +608,13 @@ export function aggregateStats({
213
608
  totalEstTokens,
214
609
  bySource: unusedGroups,
215
610
  },
611
+ insights: {
612
+ manualOnlySkills,
613
+ projectScopeCandidates,
614
+ highCostSkills,
615
+ heavyBodySkills,
616
+ slowestSkills,
617
+ promptPatterns,
618
+ },
216
619
  };
217
620
  }
@@ -0,0 +1,187 @@
1
+ import { prepareFilteredEvents } from './aggregate.mjs';
2
+
3
+ /**
4
+ * Build skill transition chains, 3-step sequences, and co-occurrence pairs
5
+ * grouped by session_id within the requested filter window.
6
+ */
7
+ export function buildChains({
8
+ events = [],
9
+ inventory = null,
10
+ config = {},
11
+ since = '30d',
12
+ host = null,
13
+ project = null,
14
+ limit = 20,
15
+ now = new Date(),
16
+ } = {}) {
17
+ const { sinceDate, filtered } = prepareFilteredEvents({
18
+ events,
19
+ inventory,
20
+ config,
21
+ since,
22
+ host,
23
+ project,
24
+ now,
25
+ });
26
+
27
+ const sessionMap = new Map();
28
+ for (let idx = 0; idx < filtered.length; idx++) {
29
+ const ev = filtered[idx];
30
+ if (!ev.session_id || !ev.skill) continue;
31
+ const sKey = `${ev.host || 'unknown'}\0${ev.session_id}`;
32
+ let list = sessionMap.get(sKey);
33
+ if (!list) {
34
+ list = [];
35
+ sessionMap.set(sKey, list);
36
+ }
37
+ const tsMs = Date.parse(ev.ts);
38
+ list.push({
39
+ skill: ev.skill,
40
+ tsMs: Number.isFinite(tsMs) ? tsMs : 0,
41
+ order: idx,
42
+ });
43
+ }
44
+
45
+ const skillSessions = new Map();
46
+ const transitionMap = new Map();
47
+ const sequenceMap = new Map();
48
+ const pairMap = new Map();
49
+ let multiSkillSessions = 0;
50
+
51
+ for (const [sKey, rawList] of sessionMap.entries()) {
52
+ rawList.sort((a, b) => a.tsMs - b.tsMs || a.order - b.order);
53
+
54
+ const seq = [];
55
+ for (const item of rawList) {
56
+ if (seq.length === 0 || seq[seq.length - 1].skill !== item.skill) {
57
+ seq.push({ skill: item.skill, tsMs: item.tsMs });
58
+ }
59
+ }
60
+
61
+ const uniqueSkills = [...new Set(seq.map((x) => x.skill))].sort();
62
+ for (const s of uniqueSkills) {
63
+ skillSessions.set(s, (skillSessions.get(s) || 0) + 1);
64
+ }
65
+
66
+ if (uniqueSkills.length >= 2) {
67
+ multiSkillSessions += 1;
68
+ for (let i = 0; i < uniqueSkills.length; i++) {
69
+ for (let j = i + 1; j < uniqueSkills.length; j++) {
70
+ const a = uniqueSkills[i];
71
+ const b = uniqueSkills[j];
72
+ const pKey = `${a}\0${b}`;
73
+ let p = pairMap.get(pKey);
74
+ if (!p) {
75
+ p = { a, b, sessions: new Set() };
76
+ pairMap.set(pKey, p);
77
+ }
78
+ p.sessions.add(sKey);
79
+ }
80
+ }
81
+ }
82
+
83
+ if (seq.length >= 2) {
84
+ for (let i = 0; i < seq.length - 1; i++) {
85
+ const from = seq[i].skill;
86
+ const to = seq[i + 1].skill;
87
+ const tKey = `${from}\0${to}`;
88
+ let t = transitionMap.get(tKey);
89
+ if (!t) {
90
+ t = { from, to, occurrences: 0, sessions: new Set(), totalDeltaMs: 0 };
91
+ transitionMap.set(tKey, t);
92
+ }
93
+ t.occurrences += 1;
94
+ t.sessions.add(sKey);
95
+ t.totalDeltaMs += Math.max(0, seq[i + 1].tsMs - seq[i].tsMs);
96
+ }
97
+ }
98
+
99
+ if (seq.length >= 3) {
100
+ for (let i = 0; i < seq.length - 2; i++) {
101
+ const s1 = seq[i].skill;
102
+ const s2 = seq[i + 1].skill;
103
+ const s3 = seq[i + 2].skill;
104
+ const label = `${s1} -> ${s2} -> ${s3}`;
105
+ let seqEntry = sequenceMap.get(label);
106
+ if (!seqEntry) {
107
+ seqEntry = { sequence: [s1, s2, s3], label, sessions: new Set() };
108
+ sequenceMap.set(label, seqEntry);
109
+ }
110
+ seqEntry.sessions.add(sKey);
111
+ }
112
+ }
113
+ }
114
+
115
+ const cap = (arr) => (limit && limit > 0 ? arr.slice(0, limit) : arr);
116
+
117
+ const transitions = cap(
118
+ [...transitionMap.values()]
119
+ .map((t) => {
120
+ const sessCount = t.sessions.size;
121
+ const fromSessions = skillSessions.get(t.from) || 1;
122
+ return {
123
+ from: t.from,
124
+ to: t.to,
125
+ sessions: sessCount,
126
+ occurrences: t.occurrences,
127
+ probability: Number((sessCount / Math.max(1, fromSessions)).toFixed(2)),
128
+ avgGapSeconds: Math.round(t.totalDeltaMs / Math.max(1, t.occurrences) / 1000),
129
+ };
130
+ })
131
+ .sort(
132
+ (a, b) =>
133
+ b.sessions - a.sessions ||
134
+ b.occurrences - a.occurrences ||
135
+ a.from.localeCompare(b.from) ||
136
+ a.to.localeCompare(b.to)
137
+ )
138
+ );
139
+
140
+ const sequences = cap(
141
+ [...sequenceMap.values()]
142
+ .map((s) => ({
143
+ sequence: s.sequence,
144
+ label: s.label,
145
+ sessions: s.sessions.size,
146
+ }))
147
+ .sort((a, b) => b.sessions - a.sessions || a.label.localeCompare(b.label))
148
+ );
149
+
150
+ const pairs = cap(
151
+ [...pairMap.values()]
152
+ .map((p) => {
153
+ const pairSessions = p.sessions.size;
154
+ const sesA = skillSessions.get(p.a) || 0;
155
+ const sesB = skillSessions.get(p.b) || 0;
156
+ return {
157
+ skills: [p.a, p.b],
158
+ sessions: pairSessions,
159
+ jaccard: Number((pairSessions / Math.max(1, sesA + sesB - pairSessions)).toFixed(2)),
160
+ };
161
+ })
162
+ .sort(
163
+ (a, b) =>
164
+ b.sessions - a.sessions ||
165
+ b.jaccard - a.jaccard ||
166
+ a.skills[0].localeCompare(b.skills[0]) ||
167
+ a.skills[1].localeCompare(b.skills[1])
168
+ )
169
+ );
170
+
171
+ return {
172
+ window: {
173
+ since: since ?? 'all',
174
+ from: sinceDate ? sinceDate.toISOString().slice(0, 10) : null,
175
+ to: now.toISOString().slice(0, 10),
176
+ },
177
+ totals: {
178
+ sessions: sessionMap.size,
179
+ multiSkillSessions,
180
+ transitions: transitions.length,
181
+ pairs: pairs.length,
182
+ },
183
+ transitions,
184
+ sequences,
185
+ pairs,
186
+ };
187
+ }