Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Mar 18, 2024
1 parent 6594e41 commit 1c0b13b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
13 changes: 3 additions & 10 deletions lib/util/stats.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,16 +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': {
hit: 1,
miss: 1,
localMiss: 1,
},
'https://example.com/foo': {
hit: 0,
miss: 0,
localHit: 2,
localMiss: 1,
'https://example.com': {
'/foo': { hit: 0, localHit: 2, localMiss: 1, miss: 0 },
'/bar': { hit: 1, localMiss: 1, miss: 1 },
},
});
});
Expand Down
27 changes: 25 additions & 2 deletions lib/util/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ interface HttpCacheHostStatsData {

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') ?? {};
Expand Down Expand Up @@ -319,7 +327,22 @@ export class HttpCacheStats {
}

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');
}
}

0 comments on commit 1c0b13b

Please sign in to comment.