"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const logger_1 = require("./logger");
const redis_1 = require("./redis");
const cassandra_1 = require("./cassandra");
const elastic_1 = require("./elastic");
const mysql_1 = require("./mysql");
const mailer_1 = require("./mailer");
const metrics_1 = require("./metrics");
const multipleInstanceManager_1 = require("./multipleInstanceManager");
const crashreporter_1 = require("./crashreporter");
const consul_1 = require("./consul");
const promBundle = require("express-prom-bundle");
class Global {
    constructor() {
        this.personalStatus = function () { return null; };
        this.personalIsFunctional = function () { return true; };
        this.loggers = null;
        this.redises = null;
        this.cassandras = null;
        this.elastics = null;
        this.mysqls = null;
        this.mailers = null;
        this.metrics = null;
        this.crashreporters = null;
        this.consuls = null;
    }
    static get Instance() {
        if (this.instance === null || this.instance === undefined) {
            this.instance = new this();
        }
        return this.instance;
    }
    get Loggers() {
        if (this.loggers == null) {
            this.loggers = new multipleInstanceManager_1.default(logger_1.default);
        }
        return this.loggers;
    }
    get Redis() {
        if (this.redises == null) {
            this.redises = new multipleInstanceManager_1.default(redis_1.default);
        }
        return this.redises;
    }
    get Cassandras() {
        if (this.cassandras == null) {
            this.cassandras = new multipleInstanceManager_1.default(cassandra_1.default);
        }
        return this.cassandras;
    }
    get Elastics() {
        if (this.elastics == null) {
            this.elastics = new multipleInstanceManager_1.default(elastic_1.default);
        }
        return this.elastics;
    }
    get Mysql() {
        if (this.mysqls == null) {
            this.mysqls = new multipleInstanceManager_1.default(mysql_1.default);
        }
        return this.mysqls;
    }
    get Mailers() {
        if (this.mailers == null) {
            this.mailers = new multipleInstanceManager_1.default(mailer_1.default);
        }
        return this.mailers;
    }
    get Metrics() {
        if (this.metrics == null) {
            this.metrics = new multipleInstanceManager_1.default(metrics_1.default);
        }
        return this.metrics;
    }
    get Crashreporters() {
        if (this.crashreporters == null) {
            this.crashreporters = new multipleInstanceManager_1.default(crashreporter_1.default);
        }
        return this.crashreporters;
    }
    get Consuls() {
        if (this.consuls == null) {
            this.consuls = new multipleInstanceManager_1.default(consul_1.default);
        }
        return this.consuls;
    }
    /**
     *
     */
    getMergedMetrics() {
        // TODO : register already reports all metrics, the merge is redundant or even dangerous
        if (this.metrics != null) {
            let values = this.metrics.getValues();
            let registry = [];
            values.forEach(element => {
                registry.push(element.getRegistry());
            });
            return promBundle.promClient.Registry.merge(registry);
        }
        else {
            return promBundle.promClient.register;
        }
    }
    /**
     * pure visual method, return the stastus of the connections in more human readable fashion
     */
    getStatus() {
        /**
         * default values
         */
        let response = {
            redis: "inactive",
            cassandra: "inactive",
            elastic: "inactive",
            mailer: "inactive",
            mysql: "inactive",
            consul: "inactive"
        };
        /**
         * put the status of the connection if we are using that connection
         */
        this.redises != null ? response["redis"] = this.redises.getStatus() : "";
        this.cassandras != null ? response["cassandra"] = this.cassandras.getStatus() : "";
        this.elastics != null ? response["elastic"] = this.elastics.getStatus() : "";
        this.mailers != null ? response["mailer"] = this.mailers.getStatus() : "";
        this.mysqls != null ? response["mysql"] = this.mysqls.getStatus() : "";
        this.consuls != null ? response["consul"] = this.consuls.getStatus() : "";
        /**
         * add extra custom visual checks
         */
        let personal = this.personalStatus();
        if (personal != null) {
            return {
                base: response,
                personal: personal
            };
        }
        //return the composed response
        return response;
    }
    /**
     * check the status of the main components
     *
     * @return true|false
     */
    isFunctional() {
        let redis = this.redises != null ? this.redises.isFunctional() : true;
        let cassandra = this.cassandras != null ? this.cassandras.isFunctional() : true;
        let elastic = this.elastics != null ? this.elastics.isFunctional() : true;
        let mysql = this.mysqls != null ? this.mysqls.isFunctional() : true;
        return redis && cassandra && elastic && mysql && this.personalIsFunctional();
    }
}
exports.default = Global;
//# sourceMappingURL=global.js.map