"use strict";
/*
 * Copyright © 2020 Atomist, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SdmGoalMetricReportingAutomationEventListener = void 0;
const AutomationEventListener_1 = require("@atomist/automation-client/lib/server/AutomationEventListener");
const statsdClient_1 = require("@atomist/automation-client/lib/spi/statsd/statsdClient");
const cluster = require("cluster");
const _ = require("lodash");
const validateGoal_1 = require("../delivery/goals/support/validateGoal");
/**
 * Automation listener that reports goal round trip metrics to StatsD.
 */
class SdmGoalMetricReportingAutomationEventListener extends AutomationEventListener_1.AutomationEventListenerSupport {
    async startupSuccessful(client) {
        if (cluster.isMaster && client.configuration.statsd.enabled) {
            this.statsd = client.configuration.statsd.client.factory.create(statsdClient_1.defaultStatsDClientOptions(client.configuration));
        }
    }
    eventIncoming(payload) {
        if (cluster.isMaster && !!this.statsd && process.env.ATOMIST_ISOLATED_GOAL !== "forked") {
            const goal = _.get(payload.data, "SdmGoal[0]");
            if (!!goal && validateGoal_1.shouldHandle(goal)) {
                this.statsd.increment(`counter.goal`, 1, 1, [`atomist_goal:${goal.name}`], () => {
                    /* intentionally left empty */
                });
                this.statsd.timing("timer.goal.round_trip", Date.now() - goal.ts, 1, {}, () => {
                    /* intentionally left empty */
                });
            }
        }
    }
}
exports.SdmGoalMetricReportingAutomationEventListener = SdmGoalMetricReportingAutomationEventListener;
//# sourceMappingURL=SdmGoalMetricReportingAutomationEventListener.js.map