"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getHostsWithTimeout = exports.getMostSevereHostStatus = exports.getClusterDetailId = exports.getClusterMemoryAmount = exports.getClustervCPUCount = exports.getWorkerCount = exports.getMasterCount = exports.downloadClusterInstallationLogs = void 0;
const tslib_1 = require("tslib");
const get_js_1 = tslib_1.__importDefault(require("lodash-es/get.js"));
const api_1 = require("../../../common/api");
const common_1 = require("../../../common");
const apis_1 = require("../../services/apis");
const services_1 = require("../../services");
const utils_1 = require("../../../common/utils");
const downloadClusterInstallationLogs = (addAlert, clusterId, clearAlerts) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
    clearAlerts();
    try {
        if (api_1.isInOcm) {
            const { data } = yield apis_1.ClustersAPI.getPresignedForClusterFiles({
                clusterId,
                fileName: 'logs',
                hostId: undefined,
                logsType: 'all',
            });
            (0, utils_1.downloadFile)(data.url);
        }
        else {
            const { data, fileName } = yield services_1.ClustersService.downloadLogs(clusterId);
            (0, utils_1.downloadFile)('', data, fileName);
        }
    }
    catch (e) {
        (0, api_1.handleApiError)(e, (e) => {
            addAlert({
                title: 'Could not download cluster installation logs.',
                message: (0, api_1.getApiErrorMessage)(e),
            });
        });
    }
});
exports.downloadClusterInstallationLogs = downloadClusterInstallationLogs;
const getHostRoleCount = (hosts, role) => hosts.filter((host) => host.status !== 'disabled' && host.role === role).length;
const getMasterCount = (hosts) => getHostRoleCount(hosts, 'master');
exports.getMasterCount = getMasterCount;
const getWorkerCount = (hosts) => getHostRoleCount(hosts, 'worker');
exports.getWorkerCount = getWorkerCount;
const getClusterResources = (cluster, resourcePath) => {
    if (!cluster.hosts) {
        return 0;
    }
    const masterCount = (0, exports.getMasterCount)(cluster.hosts);
    const workerCount = (0, exports.getWorkerCount)(cluster.hosts);
    // Cluster contain only master hosts
    const countMastersOnly = masterCount >= 3 && workerCount === 0;
    // Cluster contain master and worker hosts
    const countWorkersOnly = masterCount >= 3 && workerCount >= 2;
    const singleNodeMode = masterCount === 1;
    const result = cluster.hosts
        .filter((host) => 'inventory' in host)
        .filter((host) => (host.role === 'worker' && countWorkersOnly) ||
        (host.role === 'master' && countMastersOnly) ||
        (host.role === 'master' && singleNodeMode))
        .map((host) => (0, utils_1.stringToJSON)(host.inventory) || {})
        .map((inventory) => (0, get_js_1.default)(inventory, resourcePath, 0))
        .reduce((total, value) => total + value, 0);
    return result;
};
const getClustervCPUCount = (cluster) => getClusterResources(cluster, 'cpu.count');
exports.getClustervCPUCount = getClustervCPUCount;
const getClusterMemoryAmount = (cluster) => getClusterResources(cluster, 'memory.physicalBytes');
exports.getClusterMemoryAmount = getClusterMemoryAmount;
const getClusterDetailId = (suffix) => `cluster-detail-${suffix}`;
exports.getClusterDetailId = getClusterDetailId;
const getMostSevereHostStatus = (hosts) => {
    let status = null;
    let mostSevereStatusIndex = common_1.hostStatusOrder.length;
    for (let i = 0; i < hosts.length; i++) {
        const host = hosts[i];
        const hostStatusOrderIndex = common_1.hostStatusOrder.indexOf(host.status);
        if (hostStatusOrderIndex < mostSevereStatusIndex) {
            mostSevereStatusIndex = hostStatusOrderIndex;
            status = host.status;
        }
    }
    return status;
};
exports.getMostSevereHostStatus = getMostSevereHostStatus;
const getHostsWithTimeout = (hosts) => {
    var _a, _b;
    for (let i = 0; i < hosts.length; i++) {
        const host = hosts[i];
        if (((_a = host.progress) === null || _a === void 0 ? void 0 : _a.stageTimedOut) !== undefined) {
            return (_b = host.progress) === null || _b === void 0 ? void 0 : _b.stageTimedOut;
        }
    }
    return false;
};
exports.getHostsWithTimeout = getHostsWithTimeout;
//# sourceMappingURL=utils.js.map