"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.DeepstreamIOTelemetry = void 0;
const types_1 = require("@deepstream/types");
const ds_info_1 = require("../../config/ds-info");
const uuid_1 = require("uuid");
const utils_1 = require("../../utils/utils");
const needle_1 = require("needle");
const TELEMETRY_URL = process.env.TELEMETRY_URL || 'http://telemetry.deepstream.io:8080/api/v1/startup';
const DEFAULT_UUID = '00000000-0000-0000-0000-000000000000';
class DeepstreamIOTelemetry extends types_1.DeepstreamPlugin {
    constructor(pluginOptions, services, config) {
        super();
        this.pluginOptions = pluginOptions;
        this.services = services;
        this.config = config;
        this.description = 'Deepstream Telemetry';
        this.logger = this.services.logger.getNameSpace('TELEMETRY');
    }
    init() {
        if (this.pluginOptions.enabled === false) {
            this.logger.info(types_1.EVENT.INFO, 'Telemetry disabled, please enable in order to support development of this project. Further info found at https://deepstream.io/blog/20200512-5.1-telemetry/');
            return;
        }
        if (this.pluginOptions.deploymentId === undefined || !utils_1.validateUUID(this.pluginOptions.deploymentId)) {
            this.logger.error(types_1.EVENT.ERROR, `Invalid deployment id, must be uuid format. Feel free to use this one "${uuid_1.v4()}"`);
            this.pluginOptions.deploymentId = DEFAULT_UUID;
        }
    }
    async whenReady() {
        if (this.pluginOptions.enabled === false) {
            return;
        }
        const info = ds_info_1.getDSInfo();
        const enabledFeatures = this.config.enabledFeatures;
        const config = this.config;
        const services = Object.keys(this.config).reduce((result, key) => {
            if (!config[key]) {
                return result;
            }
            if (config[key].type) {
                result[key] = config[key].type;
            }
            else if (config[key].name) {
                result[key] = {
                    name: config[key].name
                };
            }
            else if (config[key].path) {
                result[key] = 'custom';
            }
            return result;
        }, {});
        const analytics = {
            deploymentId: this.pluginOptions.deploymentId,
            ...info,
            enabledFeatures,
            services
        };
        if (this.pluginOptions.debug) {
            this.logger.info(types_1.EVENT.TELEMETRY_DEBUG, `We would have sent the following: ${JSON.stringify(analytics)}`);
        }
        else {
            this.sendReport(analytics);
        }
    }
    async close() {
    }
    sendReport(data) {
        needle_1.post(TELEMETRY_URL, data, { content_type: 'application/json' }, (error) => {
            if (error) {
                if (error.code === 'ECONNREFUSED') {
                    this.logger.warn(types_1.EVENT.TELEMETRY_UNREACHABLE, "Can't reach telemetry endpoint");
                }
                else {
                    console.log(error);
                    this.logger.error(types_1.EVENT.ERROR, `Telemetry error: ${error}`);
                }
            }
        });
    }
}
exports.DeepstreamIOTelemetry = DeepstreamIOTelemetry;
//# sourceMappingURL=deepstreamio-telemetry.js.map