'use strict';
Object.defineProperty(exports, "__esModule", { value: true });
exports.clusterStatus = clusterStatus;
const worker_threads_1 = require("worker_threads");
const manageThreads_js_1 = require("../core/server/threads/manageThreads.js");
const nodeName_ts_1 = require("../core/server/nodeName.js");
const subscriptionManager_ts_1 = require("./subscriptionManager.js");
const knownNodes_ts_1 = require("./knownNodes.js");
const replicationConnection_ts_1 = require("./replicationConnection.js");
require("../core/server/serverHelpers/serverUtilities.js");
let clusterStatusResolve;
(0, manageThreads_js_1.onMessageByType)('cluster-status', async (message) => {
    clusterStatusResolve(message);
});
/**
 * Function will msg all the remote nodes in the hdbNodes table. From the replies
 * it gets back from each node and the details in the hdbNodes table it will
 * generate a status object. All the status objects are pushed to an array and returned.
 * @returns {Promise<{is_enabled: *, node_name: *, connections: *[]}>}
 */
async function clusterStatus() {
    let response;
    if (worker_threads_1.parentPort) {
        worker_threads_1.parentPort.postMessage({ type: 'request-cluster-status' });
        response = await new Promise((resolve) => {
            clusterStatusResolve = resolve;
        });
    }
    else {
        response = (0, subscriptionManager_ts_1.requestClusterStatus)();
    }
    // Augment the response with replication status information
    for (let connection of response.connections) {
        const remoteNodeName = connection.name;
        for (let socket of connection.database_sockets) {
            const databaseName = socket.database;
            let auditStore;
            for (let table of Object.values(databases[databaseName] || {})) {
                auditStore = table.auditStore;
                if (auditStore)
                    break;
            }
            if (!auditStore)
                continue;
            let replicationSharedStatus = (0, knownNodes_ts_1.getReplicationSharedStatus)(auditStore, databaseName, remoteNodeName);
            socket.lastCommitConfirmed = asDate(replicationSharedStatus[replicationConnection_ts_1.CONFIRMATION_STATUS_POSITION]);
            socket.lastReceivedRemoteTime = asDate(replicationSharedStatus[replicationConnection_ts_1.RECEIVED_VERSION_POSITION]);
            socket.lastReceivedLocalTime = asDate(replicationSharedStatus[replicationConnection_ts_1.RECEIVED_TIME_POSITION]);
            // Raw version timestamp for precise sync comparison (preserves float64 precision)
            socket.lastReceivedVersion = replicationSharedStatus[replicationConnection_ts_1.RECEIVED_VERSION_POSITION];
            socket.sendingMessage = asDate(replicationSharedStatus[replicationConnection_ts_1.SENDING_TIME_POSITION]);
            socket.backPressurePercent = replicationSharedStatus[replicationConnection_ts_1.BACK_PRESSURE_RATIO_POSITION] * 100;
            socket.lastReceivedStatus =
                replicationSharedStatus[replicationConnection_ts_1.RECEIVING_STATUS_POSITION] === replicationConnection_ts_1.RECEIVING_STATUS_RECEIVING ? 'Receiving' : 'Waiting';
        }
    }
    // Add node name and shard/url info for this node
    response.node_name = (0, nodeName_ts_1.getThisNodeName)();
    // If it doesn't exist and or needs to be updated.
    const thisNode = (0, knownNodes_ts_1.getHDBNodeTable)().primaryStore.get(response.node_name);
    if (thisNode?.shard)
        response.shard = thisNode.shard;
    if (thisNode?.url)
        response.url = thisNode.url;
    response.is_enabled = true; // if we have replication, replication is enabled
    return response;
}
function asDate(date) {
    return date ? (date === 1 ? 'Copying' : new Date(date).toUTCString()) : undefined;
}
server.registerOperation?.({
    name: 'cluster_status',
    execute: clusterStatus,
    httpMethod: 'GET',
});
//# sourceMappingURL=clusterStatus.js.map