"use strict";
/* TODO auto-generated eslint ignore, please fix! */
/* eslint @typescript-eslint/no-inferrable-types: "off" */
/**
 * Metrics Collector
 *
 *  Copyright (C) 2010-2019 R-T Specialty, LLC.
 *
 *  This file is part of liza.
 *
 *  liza is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Collect Metrics for Prometheus
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetricsCollector = void 0;
var MetricsCollector = /** @class */ (function () {
    /**
     * Initialize delta logger
     *
     * @param _client   - A prometheus client
     * @param _factory  - A factory to create prometheus components
     * @param _conf     - Prometheus configuration
     * @param _emitter  - Event emitter
     * @param _timer    - A timer function to create a tuple timestamp
     */
    function MetricsCollector(_client, _factory, _conf, _emitter, _timer) {
        var _this = this;
        this._client = _client;
        this._factory = _factory;
        this._conf = _conf;
        this._emitter = _emitter;
        this._timer = _timer;
        this._process_time_name = 'liza_delta_process_time';
        this._process_time_help = 'Delta process time in ms';
        this._total_error_name = 'liza_delta_error';
        this._total_error_help = 'Total errors from delta processing';
        this._current_error_name = 'liza_delta_current_error';
        this._current_error_help = 'The current number of documents in an error state';
        this._total_processed_name = 'liza_delta_success';
        this._total_processed_help = 'Total deltas successfully processed';
        /** Timing map */
        this._timing_map = {};
        // Create metrics
        this._gateway = this._factory.createGateway(this._client, this._conf.hostname, this._conf.port);
        this._process_time = this._factory.createHistogram(this._client, this._process_time_name, this._process_time_help, this._conf.buckets_start, this._conf.buckets_width, this._conf.buckets_count);
        this._total_error = this._factory.createCounter(this._client, this._total_error_name, this._total_error_help);
        this._current_error = this._factory.createGauge(this._client, this._current_error_name, this._current_error_help);
        this._total_processed = this._factory.createCounter(this._client, this._total_processed_name, this._total_processed_help);
        // Create grouping key
        var groupings = {
            env: this._conf.env,
            instance: this._conf.instance + '_' + this._conf.env,
            service: 'delta_processor',
        };
        // Push metrics on a specific interval
        this._push_interval = setInterval(function () {
            _this._gateway.pushAdd({
                jobName: 'liza_delta_metrics',
                groupings: groupings,
            }, _this.getPushCallback(_this));
        }, this._conf.push_interval_ms);
        // Subsribe metrics to events
        this.hookMetrics();
    }
    /**
     * Stop the push interval
     */
    MetricsCollector.prototype.stop = function () {
        clearInterval(this._push_interval);
    };
    /**
     * List to events to update metrics
     */
    MetricsCollector.prototype.hookMetrics = function () {
        var _this = this;
        this._emitter.on('delta-process-start', function (uid) {
            _this._timing_map[uid] = _this._timer();
        });
        this._emitter.on('delta-process-end', function (uid) {
            var start_time_ms = _this._timing_map[uid] || [-1, -1];
            var t = _this._timer(start_time_ms);
            var total_time_ms = t[0] * 1000 + t[1] / 1000000;
            _this._process_time.observe(total_time_ms);
            _this._total_processed.inc();
        });
        this._emitter.on('error', function (_) { return _this._total_error.inc(); });
    };
    /**
     * Handle push error
     *
     * @param self - Metrics Collector object
     *
     * @return a function to handle the pushAdd callback
     */
    MetricsCollector.prototype.getPushCallback = function (self) {
        return function (error, _response, _body) {
            if (error) {
                self._emitter.emit('error', error);
            }
        };
    };
    /**
     * Update metrics with current error count
     *
     * @param count - the number of errors found
     */
    MetricsCollector.prototype.updateErrorCount = function (count) {
        this._current_error.set(+count);
    };
    return MetricsCollector;
}());
exports.MetricsCollector = MetricsCollector;
//# sourceMappingURL=data:application/json;base64, 