diff --git a/lib/util/stats.spec.ts b/lib/util/stats.spec.ts index 4ea3c275855af6641b2556737100a0fbd53099cc..1d7ceb3ba951e49dad4afd7dcae316467a56330d 100644 --- a/lib/util/stats.spec.ts +++ b/lib/util/stats.spec.ts @@ -480,20 +480,15 @@ describe('util/stats', () => { expect(res).toEqual({ 'https://example.com/bar': { - localHits: 0, - localMisses: 1, - localTotal: 1, - remoteHits: 1, - remoteMisses: 1, - remoteTotal: 2, + hit: 1, + miss: 1, + localMiss: 1, }, 'https://example.com/foo': { - localHits: 2, - localMisses: 1, - localTotal: 3, - remoteHits: 0, - remoteMisses: 0, - remoteTotal: 0, + hit: 0, + miss: 0, + localHit: 2, + localMiss: 1, }, }); }); @@ -512,21 +507,9 @@ describe('util/stats', () => { const [data, msg] = logger.logger.debug.mock.calls[0]; expect(msg).toBe('HTTP cache statistics'); expect(data).toEqual({ - 'https://example.com/bar': { - localHits: 0, - localMisses: 1, - localTotal: 1, - remoteHits: 1, - remoteMisses: 1, - remoteTotal: 2, - }, - 'https://example.com/foo': { - localHits: 2, - localMisses: 1, - localTotal: 3, - remoteHits: 0, - remoteMisses: 0, - remoteTotal: 0, + 'https://example.com': { + '/foo': { hit: 0, localHit: 2, localMiss: 1, miss: 0 }, + '/bar': { hit: 1, localMiss: 1, miss: 1 }, }, }); }); diff --git a/lib/util/stats.ts b/lib/util/stats.ts index c0e6996e360db5d415d4d69cdd8985245f90f8a9..48267fff3ba677405cebd5044b461695c12f0c8e 100644 --- a/lib/util/stats.ts +++ b/lib/util/stats.ts @@ -237,16 +237,22 @@ export class HttpStats { } interface HttpCacheHostStatsData { - localHits: number; - localMisses: number; - localTotal: number; - remoteHits: number; - remoteMisses: number; - remoteTotal: number; + hit: number; + miss: number; + localHit?: number; + localMiss?: number; } type HttpCacheStatsData = Record<string, HttpCacheHostStatsData>; +function sortObject<T>(obj: Record<string, T>): Record<string, T> { + const result: Record<string, T> = {}; + for (const key of Object.keys(obj).sort()) { + result[key] = obj[key]; + } + return result; +} + export class HttpCacheStats { static getData(): HttpCacheStatsData { return memCache.get<HttpCacheStatsData>('http-cache-stats') ?? {}; @@ -255,12 +261,8 @@ export class HttpCacheStats { static read(key: string): HttpCacheHostStatsData { return ( this.getData()?.[key] ?? { - localHits: 0, - localMisses: 0, - localTotal: 0, - remoteHits: 0, - remoteMisses: 0, - remoteTotal: 0, + hit: 0, + miss: 0, } ); } @@ -287,8 +289,8 @@ export class HttpCacheStats { if (baseUrl) { const host = baseUrl; const stats = HttpCacheStats.read(host); - stats.localHits += 1; - stats.localTotal += 1; + stats.localHit ??= 0; + stats.localHit += 1; HttpCacheStats.write(host, stats); } } @@ -298,8 +300,8 @@ export class HttpCacheStats { if (baseUrl) { const host = baseUrl; const stats = HttpCacheStats.read(host); - stats.localMisses += 1; - stats.localTotal += 1; + stats.localMiss ??= 0; + stats.localMiss += 1; HttpCacheStats.write(host, stats); } } @@ -309,8 +311,7 @@ export class HttpCacheStats { if (baseUrl) { const host = baseUrl; const stats = HttpCacheStats.read(host); - stats.remoteHits += 1; - stats.remoteTotal += 1; + stats.hit += 1; HttpCacheStats.write(host, stats); } } @@ -320,14 +321,28 @@ export class HttpCacheStats { if (baseUrl) { const host = baseUrl; const stats = HttpCacheStats.read(host); - stats.remoteMisses += 1; - stats.remoteTotal += 1; + stats.miss += 1; HttpCacheStats.write(host, stats); } } static report(): void { - const stats = HttpCacheStats.getData(); - logger.debug(stats, 'HTTP cache statistics'); + const data = HttpCacheStats.getData(); + let report: Record<string, Record<string, HttpCacheHostStatsData>> = {}; + for (const [url, stats] of Object.entries(data)) { + const parsedUrl = parseUrl(url); + if (parsedUrl) { + const { origin, pathname } = parsedUrl; + report[origin] ??= {}; + report[origin][pathname] = stats; + } + } + + for (const [host, hostStats] of Object.entries(report)) { + report[host] = sortObject(hostStats); + } + report = sortObject(report); + + logger.debug(report, 'HTTP cache statistics'); } }