"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getClustersAvailability = getClustersAvailability;
const axios_1 = __importDefault(require("axios"));
const map_1 = __importDefault(require("lodash/map"));
const utils_1 = require("../components/utils");
const utils_2 = require("../utils");
function getClusterAvailability(clusterConfig, odinPath) {
    const ODIN_PERIOD = 5;
    const ODIN_METRIC = 'master';
    const TIMEOUT = 15 * 1000;
    return axios_1.default
        .request({
        url: odinPath + '/availability_stat/' + clusterConfig.id,
        params: {
            period: ODIN_PERIOD,
            target: ODIN_METRIC,
        },
        method: 'GET',
        timeout: TIMEOUT,
        responseType: 'json',
    })
        .then((response) => {
        return response.data;
    });
}
function getAvailability(req, clusters) {
    const odinBaseUrl = req.ctx.config.odinBaseUrl;
    const isMultiOdinBaseUrl = 'string' !== typeof odinBaseUrl;
    const makeOdinIsAliveUrl = (odinBaseUrl) => `${odinBaseUrl}/is_alive`;
    const commonIsAlive = isMultiOdinBaseUrl
        ? Promise.resolve({ data: true })
        : axios_1.default.request({ url: makeOdinIsAliveUrl(odinBaseUrl) });
    return commonIsAlive
        .then(({ data: commonAlive }) => {
        if (!commonAlive) {
            return [];
        }
        return Promise.all((0, map_1.default)(clusters, (clusterConfig) => {
            const odinPath = isMultiOdinBaseUrl
                ? odinBaseUrl?.[clusterConfig.id]
                : odinBaseUrl;
            if (!odinPath) {
                return {};
            }
            const alive = isMultiOdinBaseUrl
                ? axios_1.default.get(makeOdinIsAliveUrl(odinPath)).catch((e) => {
                    req.ctx.logError(`Error of getting ${odinPath}`, e);
                    return { data: false };
                })
                : Promise.resolve({ data: true });
            return alive.then(({ data }) => {
                if (data != true) {
                    return {};
                }
                return getClusterAvailability(clusterConfig, odinPath)
                    .then((availability) => ({ id: clusterConfig.id, availability }))
                    .catch((error) => {
                    req.ctx.logError('getAvailability error', error);
                    return {};
                });
            });
        }));
    })
        .catch((e) => {
        req.ctx.logError(`Error of getting ${odinBaseUrl}`, e);
        return [];
    });
}
async function getClustersAvailability(req, res) {
    const odinBaseUrl = req.ctx.config.odinBaseUrl;
    if (!odinBaseUrl) {
        (0, utils_2.sendAndLogError)(req.ctx, res, 500, new Error('odin base url is not configured'));
        return;
    }
    const clusters = (0, utils_1.getClustersFromConfig)();
    await getAvailability(req, clusters)
        .then((data) => (0, utils_2.sendResponse)(res, data))
        .catch((error) => (0, utils_2.sendError)(res, error));
}
