renovate/lib/workers/repository/stats.ts
2020-08-31 13:13:02 +02:00

48 lines
1.5 KiB
TypeScript

import URL from 'url';
import { logger } from '../../logger';
import * as memCache from '../../util/cache/memory';
interface RequestStats {
method: string;
url: string;
duration: number;
}
export function printRequestStats(): void {
const httpRequests = memCache.get<RequestStats[]>('http-requests');
if (!httpRequests) {
return;
}
httpRequests.sort((a, b) => {
if (a.url === b.url) {
return 0;
}
if (a.url < b.url) {
return -1;
}
return 1;
});
const allRequests: string[] = [];
const requestHosts: Record<string, number[]> = {};
for (const request of httpRequests) {
allRequests.push(
`${request.method.toUpperCase()} ${request.url} ${request.duration}`
);
const { hostname } = URL.parse(request.url);
requestHosts[hostname] = requestHosts[hostname] || [];
requestHosts[hostname].push(request.duration);
}
logger.trace({ allRequests, requestHosts }, 'full stats');
const hostStats: string[] = [];
let totalRequests = 0;
for (const [hostname, requests] of Object.entries(requestHosts)) {
const hostRequests = requests.length;
totalRequests += hostRequests;
const requestSum = requests.reduce((a, b) => a + b, 0);
const avg = Math.round(requestSum / hostRequests);
const requestCount =
`${hostRequests} ` + (hostRequests > 1 ? 'requests' : 'request');
hostStats.push(`${hostname}, ${requestCount}, ${avg}ms average`);
}
logger.debug({ hostStats, totalRequests }, 'http statistics');
}