"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.printRequestStats = exports.printLookupStats = void 0;
const tslib_1 = require("tslib");
const url_1 = tslib_1.__importDefault(require("url"));
const logger_1 = require("../../logger");
const array_1 = require("../../util/array");
const memCache = tslib_1.__importStar(require("../../util/cache/memory"));
function printLookupStats() {
    const lookups = memCache.get('lookup-stats') ?? [];
    const datasourceDurations = {};
    for (const lookup of lookups) {
        datasourceDurations[lookup.datasource] ??= [];
        datasourceDurations[lookup.datasource].push(lookup.duration);
    }
    const data = {};
    for (const [datasource, durations] of Object.entries(datasourceDurations)) {
        const count = durations.length;
        const totalMs = durations.reduce((a, c) => a + c, 0);
        const averageMs = Math.round(totalMs / count);
        const maximumMs = Math.max(...durations);
        data[datasource] = { count, averageMs, totalMs, maximumMs };
    }
    logger_1.logger.debug(data, 'Package lookup durations');
}
exports.printLookupStats = printLookupStats;
function printRequestStats() {
    const packageCacheGets = (memCache.get('package-cache-gets') ?? []).sort(array_1.sortNumeric);
    const packageCacheSets = (memCache.get('package-cache-sets') ?? []).sort(array_1.sortNumeric);
    const packageCacheStats = {
        get: {
            count: packageCacheGets.length,
        },
        set: {
            count: packageCacheSets.length,
        },
    };
    if (packageCacheGets.length) {
        packageCacheStats.get.avgMs = Math.round(packageCacheGets.reduce((a, b) => a + b, 0) / packageCacheGets.length);
        if (packageCacheGets.length > 1) {
            packageCacheStats.get.medianMs =
                packageCacheGets[Math.round(packageCacheGets.length / 2) - 1];
            packageCacheStats.get.maxMs =
                packageCacheGets[packageCacheGets.length - 1];
        }
    }
    if (packageCacheSets.length) {
        packageCacheStats.set.avgMs = Math.round(packageCacheSets.reduce((a, b) => a + b, 0) / packageCacheSets.length);
        if (packageCacheSets.length > 1) {
            packageCacheStats.set.medianMs =
                packageCacheSets[Math.round(packageCacheSets.length / 2) - 1];
            packageCacheStats.set.maxMs =
                packageCacheSets[packageCacheSets.length - 1];
        }
    }
    logger_1.logger.debug(packageCacheStats, 'Package cache statistics');
    const httpRequests = memCache.get('http-requests');
    // istanbul ignore next
    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 = [];
    const requestHosts = {};
    const rawUrls = {};
    for (const httpRequest of httpRequests) {
        const { method, url, duration, queueDuration, statusCode } = httpRequest;
        const [baseUrl] = url.split('?');
        // put method last for better sorting
        const urlKey = `${baseUrl} (${method.toUpperCase()},${statusCode})`;
        if (rawUrls[urlKey]) {
            rawUrls[urlKey] += 1;
        }
        else {
            rawUrls[urlKey] = 1;
        }
        allRequests.push(`${method.toUpperCase()} ${url} ${statusCode} ${duration} ${queueDuration}`);
        const { hostname } = url_1.default.parse(url);
        // istanbul ignore if: TODO: fix types (#9610)
        if (!hostname) {
            return;
        }
        requestHosts[hostname] = requestHosts[hostname] || [];
        requestHosts[hostname].push(httpRequest);
    }
    const urls = {};
    // Sort urls for easier reading
    for (const url of Object.keys(rawUrls).sort()) {
        urls[url] = rawUrls[url];
    }
    logger_1.logger.trace({ allRequests, requestHosts }, 'full stats');
    const hostStats = {};
    let totalRequests = 0;
    for (const [hostname, requests] of Object.entries(requestHosts)) {
        const requestCount = requests.length;
        totalRequests += requestCount;
        const requestSum = requests
            .map(({ duration }) => duration)
            .reduce((a, b) => a + b, 0);
        const requestAvgMs = Math.round(requestSum / requestCount);
        const queueSum = requests
            .map(({ queueDuration }) => queueDuration)
            .reduce((a, b) => a + b, 0);
        const queueAvgMs = Math.round(queueSum / requestCount);
        hostStats[hostname] = { requestCount, requestAvgMs, queueAvgMs };
    }
    logger_1.logger.debug({ urls, hostStats, totalRequests }, 'http statistics');
}
exports.printRequestStats = printRequestStats;
//# sourceMappingURL=stats.js.map