Spaces:
Runtime error
Runtime error
File size: 3,541 Bytes
cd8bd0a | 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 | import { getDbInstance } from "./core";
export interface CacheStatsEntry {
provider: string;
model?: string;
compressionMode: string;
cacheControlPresent: boolean;
estimatedCacheHit: boolean;
tokensSavedCompression: number;
tokensSavedCaching: number;
netSavings: number;
}
export interface CacheStatsSummary {
totalRequests: number;
avgNetSavings: number;
cacheHitRate: number;
byProvider: Record<string, { count: number; avgNetSavings: number; cacheHitRate: number }>;
}
export function recordCacheStats(entry: CacheStatsEntry): void {
const db = getDbInstance();
const sql = `INSERT INTO compression_cache_stats (
provider,
model,
compression_mode,
cache_control_present,
estimated_cache_hit,
tokens_saved_compression,
tokens_saved_caching,
net_savings
) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`;
db.prepare(sql).run(
entry.provider,
entry.model ?? "",
entry.compressionMode,
entry.cacheControlPresent ? 1 : 0,
entry.estimatedCacheHit ? 1 : 0,
entry.tokensSavedCompression,
entry.tokensSavedCaching,
entry.netSavings
);
}
export function getCacheStatsSummary(since?: Date): CacheStatsSummary {
const db = getDbInstance();
const whereClause = since ? "WHERE created_at >= ?" : "";
const params = since ? [since.toISOString()] : [];
// Global aggregates
const globalRow = since
? (db
.prepare(
`SELECT COUNT(*) as totalRequests, AVG(net_savings) as avgNetSavings, SUM(estimated_cache_hit) * 1.0 / COUNT(*) as cacheHitRate FROM compression_cache_stats WHERE created_at >= ?`
)
.get(since.toISOString()) as
| { totalRequests: number; avgNetSavings: number; cacheHitRate: number }
| undefined)
: (db
.prepare(
`SELECT COUNT(*) as totalRequests, AVG(net_savings) as avgNetSavings, SUM(estimated_cache_hit) * 1.0 / COUNT(*) as cacheHitRate FROM compression_cache_stats`
)
.get() as
| { totalRequests: number; avgNetSavings: number; cacheHitRate: number }
| undefined);
if (!globalRow || globalRow.totalRequests === 0) {
return { totalRequests: 0, avgNetSavings: 0, cacheHitRate: 0, byProvider: {} };
}
// Per-provider aggregates
const providerRows = since
? (db
.prepare(
`SELECT provider, COUNT(*) as count, AVG(net_savings) as avgNetSavings, SUM(estimated_cache_hit) * 1.0 / COUNT(*) as cacheHitRate FROM compression_cache_stats WHERE created_at >= ? GROUP BY provider`
)
.all(since.toISOString()) as Array<{
provider: string;
count: number;
avgNetSavings: number;
cacheHitRate: number;
}>)
: (db
.prepare(
`SELECT provider, COUNT(*) as count, AVG(net_savings) as avgNetSavings, SUM(estimated_cache_hit) * 1.0 / COUNT(*) as cacheHitRate FROM compression_cache_stats GROUP BY provider`
)
.all() as Array<{
provider: string;
count: number;
avgNetSavings: number;
cacheHitRate: number;
}>);
const byProvider: Record<string, { count: number; avgNetSavings: number; cacheHitRate: number }> =
{};
for (const row of providerRows) {
byProvider[row.provider] = {
count: row.count,
avgNetSavings: row.avgNetSavings,
cacheHitRate: row.cacheHitRate,
};
}
return {
totalRequests: globalRow.totalRequests,
avgNetSavings: globalRow.avgNetSavings ?? 0,
cacheHitRate: globalRow.cacheHitRate ?? 0,
byProvider,
};
}
|