MCPcopy Index your code
hub / github.com/claude-code-best/claude-code / createStatsStore

Function createStatsStore

src/context/stats.tsx:32–100  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

30};
31
32export function createStatsStore(): StatsStore {
33 const metrics = new Map<string, number>();
34 const histograms = new Map<string, Histogram>();
35 const sets = new Map<string, Set<string>>();
36
37 return {
38 increment(name: string, value = 1) {
39 metrics.set(name, (metrics.get(name) ?? 0) + value);
40 },
41 set(name: string, value: number) {
42 metrics.set(name, value);
43 },
44 observe(name: string, value: number) {
45 let h = histograms.get(name);
46 if (!h) {
47 h = { reservoir: [], count: 0, sum: 0, min: value, max: value };
48 histograms.set(name, h);
49 }
50 h.count++;
51 h.sum += value;
52 if (value < h.min) {
53 h.min = value;
54 }
55 if (value > h.max) {
56 h.max = value;
57 }
58 // Reservoir sampling (Algorithm R)
59 if (h.reservoir.length < RESERVOIR_SIZE) {
60 h.reservoir.push(value);
61 } else {
62 const j = Math.floor(Math.random() * h.count);
63 if (j < RESERVOIR_SIZE) {
64 h.reservoir[j] = value;
65 }
66 }
67 },
68 add(name: string, value: string) {
69 let s = sets.get(name);
70 if (!s) {
71 s = new Set();
72 sets.set(name, s);
73 }
74 s.add(value);
75 },
76 getAll() {
77 const result: Record<string, number> = Object.fromEntries(metrics);
78
79 for (const [name, h] of histograms) {
80 if (h.count === 0) {
81 continue;
82 }
83 result[`${name}_count`] = h.count;
84 result[`${name}_min`] = h.min;
85 result[`${name}_max`] = h.max;
86 result[`${name}_avg`] = h.sum / h.count;
87 const sorted = [...h.reservoir].sort((a, b) => a - b);
88 result[`${name}_p50`] = percentile(sorted, 50);
89 result[`${name}_p95`] = percentile(sorted, 95);

Callers 2

getRenderContextFunction · 0.85
StatsProviderFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected