"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPreloadedPrometheusDashboard = getPreloadedPrometheusDashboard;
const os_1 = __importDefault(require("os"));
const dashboard_type_1 = require("../../shared/prometheus/dashboard-type");
const utils_1 = require("../../shared/prometheus/utils");
const ServerFactory_1 = require("../ServerFactory");
const utils_2 = require("../utils");
const auto_updated_cache_1 = require("../utils/auto-updated-cache");
const requestsSetup_1 = require("./requestsSetup");
const yt = require('@ytsaurus/javascript-wrapper')();
function fetchPrometheusDashboard(key) {
    return (0, ServerFactory_1.getApp)().nodekit.ctx.call('fetchPrometheusDashboard', async (cx) => {
        const colonPos = key.indexOf(':');
        const dashboardType = key.substring(0, colonPos);
        const cluster = key.substring(colonPos + 1);
        if (colonPos === -1 || !cluster || !dashboardType) {
            throw new utils_2.ErrorWithCode(400, { message: `Unexpected key value`, attributes: { key } });
        }
        if (!cx.get('requestId')) {
            const requestId = crypto.randomUUID();
            cx.set('requestId', requestId);
            cx.setTag('request_id', requestId);
        }
        const timeStart = Date.now();
        const { setup: configSetup } = (0, requestsSetup_1.getRobotYTApiSetup)(cluster);
        const xYTCorrelationId = `${cx.get('requestId')}.fetchPrometheusDashboard`;
        function sendStats(responseStatus) {
            const timestamp = Date.now();
            cx.log('fetchClusterParams', { xYTCorrelationId, responseStatus });
            cx.stats?.('ytRequests', {
                responseStatus,
                headerContentLength: 0,
                timestamp,
                host: os_1.default.hostname(),
                service: cluster,
                requestTime: timestamp - timeStart,
                requestId: cx.get('requestId'),
                action: 'fetchPrometheusDashboard',
            });
        }
        return yt.v3.get({
            setup: configSetup,
            parameters: { path: (0, utils_1.getDashboardPath)(dashboardType) },
        }).then((res) => {
            sendStats(200);
            return res;
        }, (error) => {
            const statusCode = error.status ?? -500;
            sendStats(statusCode);
            return Promise.reject(error);
        });
    });
}
const getCached = (0, auto_updated_cache_1.createAutoUpdatedCache)(fetchPrometheusDashboard);
function getPreloadedPrometheusDashboard({ cluster, dashboardType = null, }) {
    if (!dashboardType || !dashboard_type_1.PROMETHEUS_DASHBOARD_TYPES.find((i) => i === dashboardType)) {
        throw new utils_2.ErrorWithCode(400, {
            message: `Unexpected value of dashboardType`,
            attributes: { dashboardType },
        });
    }
    return getCached(`${dashboardType}:${cluster}`);
}
