From 3c74a098cf475be5951e506e74e9ce612643b337 Mon Sep 17 00:00:00 2001 From: Chris Date: Wed, 12 Aug 2026 00:42:56 -0700 Subject: [PATCH] feat: add metrics collection and export - New MetricsCollector class for tracking cache performance - JSON export support for analytics - Hit rate averaging over time --- src/metrics.ts | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/metrics.ts diff --git a/src/metrics.ts b/src/metrics.ts new file mode 100644 index 0000000..a74523c --- /dev/null +++ b/src/metrics.ts @@ -0,0 +1,31 @@ +import { CachedPromptStats } from './index'; + +export interface MetricsSnapshot { + timestamp: number; + hitRate: number; + tokensSaved: number; + stats: CachedPromptStats; +} + +export class MetricsCollector { + private snapshots: MetricsSnapshot[] = []; + + recordSnapshot(hitRate: number, tokensSaved: number, stats: CachedPromptStats): void { + this.snapshots.push({ + timestamp: Date.now(), + hitRate, + tokensSaved, + stats, + }); + } + + exportJSON(): string { + return JSON.stringify(this.snapshots, null, 2); + } + + averageHitRate(): number { + if (this.snapshots.length === 0) return 0; + const sum = this.snapshots.reduce((acc, s) => acc + s.hitRate, 0); + return sum / this.snapshots.length; + } +}