/*
 * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * or in the "license" file accompanying this file. This file 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.
 */

package com.amazon.opendistro.elasticsearch.performanceanalyzer.collectors;

import com.amazon.opendistro.elasticsearch.performanceanalyzer.ESResources;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.PerformanceAnalyzerApp;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.PerformanceAnalyzerController;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.config.overrides.ConfigOverridesWrapper;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.AllMetrics;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsConfiguration;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.MetricsProcessor;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.metrics.PerformanceAnalyzerMetrics;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.rca.framework.metrics.ExceptionsAndErrors;
import com.amazon.opendistro.elasticsearch.performanceanalyzer.rca.framework.metrics.WriterMetrics;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.annotations.VisibleForTesting;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.elasticsearch.cluster.service.ClusterApplierService;
import org.elasticsearch.cluster.service.MasterService;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * This class starts publishing latency and failure metrics for Publish Phase of Cluster state. These metrics are
 * emitted from Master node.
 */
public class MasterClusterStateUpdateStatsCollector extends PerformanceAnalyzerMetricsCollector implements
        MetricsProcessor {
    public static final int SAMPLING_TIME_INTERVAL =
            MetricsConfiguration.CONFIG_MAP.get(MasterClusterStateUpdateStatsCollector.class).samplingInterval;
    private static final int KEYS_PATH_LENGTH = 0;
    private static final Logger LOG = LogManager.getLogger(MasterClusterStateUpdateStatsCollector.class);
    public static final String MASTER_CLUSTER_UPDATE_STATS_CLASS_NAME = "org.elasticsearch.cluster.service.MasterClusterUpdateStats";
    private static final String GET_MASTER_CLUSTER_UPDATE_STATS_METHOD_NAME = "getStats";
    private static final ObjectMapper mapper;
    private volatile MasterClusterStateUpdateStats prevMasterClusterStateUpdateStats = new MasterClusterStateUpdateStats();
    private final StringBuilder value;
    private final PerformanceAnalyzerController controller;
    private final ConfigOverridesWrapper configOverridesWrapper;

    static {
        mapper = new ObjectMapper();
        mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public MasterClusterStateUpdateStatsCollector(PerformanceAnalyzerController controller,
                                                  ConfigOverridesWrapper configOverridesWrapper) {
        super(SAMPLING_TIME_INTERVAL, MasterClusterStateUpdateStatsCollector.class.getSimpleName());
        value = new StringBuilder();
        this.controller = controller;
        this.configOverridesWrapper = configOverridesWrapper;
    }

    @Override
    public void collectMetrics(long startTime) {
        if (!controller.isCollectorEnabled(configOverridesWrapper, getCollectorName())) {
            return;
        }
        try {
            long mCurrT = System.currentTimeMillis();
            if (ESResources.INSTANCE.getClusterService() == null
                    || ESResources.INSTANCE.getClusterService().getMasterService() == null) {
                return;
            }
            MasterClusterStateUpdateStats currentMasterClusterStateUpdateStats = mapper.readValue(
                    mapper.writeValueAsString(getMasterClusterStateUpdateStats()), MasterClusterStateUpdateStats.class);

            MasterClusterStateUpdateMetrics masterClusterStateUpdateMetrics = new MasterClusterStateUpdateMetrics(
                    computeLatency(currentMasterClusterStateUpdateStats), computeFailure(currentMasterClusterStateUpdateStats));
            prevMasterClusterStateUpdateStats = currentMasterClusterStateUpdateStats;

            value.setLength(0);
            value.append(PerformanceAnalyzerMetrics.getJsonCurrentMilliSeconds())
                    .append(PerformanceAnalyzerMetrics.sMetricNewLineDelimitor);
            value.append(masterClusterStateUpdateMetrics.serialize());
            saveMetricValues(value.toString(), startTime);

            PerformanceAnalyzerApp.WRITER_METRICS_AGGREGATOR.updateStat(
                    WriterMetrics.MASTER_CLUSTER_UPDATE_STATS_COLLECTOR_EXECUTION_TIME, "",
                    System.currentTimeMillis() - mCurrT);
        } catch (Exception ex) {
            PerformanceAnalyzerApp.ERRORS_AND_EXCEPTIONS_AGGREGATOR.updateStat(
                    ExceptionsAndErrors.MASTER_CLUSTER_UPDATE_STATS_COLLECTOR_ERROR, "", 1);
            LOG.debug("Exception in Collecting Master Cluster State Update Metrics: {} for startTime {}",
                    () -> ex.toString(), () -> startTime);
        }
    }

    @VisibleForTesting
    public Object getMasterClusterStateUpdateStats() throws InvocationTargetException, IllegalAccessException,
            NoSuchMethodException {
        Method method = MasterService.class.getMethod(GET_MASTER_CLUSTER_UPDATE_STATS_METHOD_NAME);
        return method.invoke(ESResources.INSTANCE.getClusterService().getMasterService());
    }

    /**
     * MasterClusterUpdateMetric in ES is a tracker for total time taken to publish cluster state and the
     * number of times it has failed. To calculate point in time metric,
     * we will have to store its previous state and calculate the diff to get the point in time latency.
     * This might return as 0 if there is no publish request since last retrieval.
     *
     * @param currentMetrics Current Publish Cluster update stats in ES from master perspective
     * @return point in time latency.
     */
    private double computeLatency(final MasterClusterStateUpdateStats currentMetrics) {
        final double rate = computeRate(currentMetrics.publishTotalCount);
        if (rate <= 0) {
            return 0D;
        }
        return (currentMetrics.publishTimeTakenInMillis - prevMasterClusterStateUpdateStats.publishTimeTakenInMillis) / rate;
    }

    private double computeRate(final double currentTotalCount) {
        return currentTotalCount - prevMasterClusterStateUpdateStats.publishTotalCount;
    }

    /**
     * MasterClusterUpdateMetric in ES is a tracker for total time taken to publish cluster state and the
     * number of times it has failed. To calculate point in time metric,
     * we will have to store its previous state and calculate the diff to get the point in time latency.
     * This might return as 0 if there is no publish request since last retrieval.
     *
     * @param currentMetrics Current Publish Cluster update stats in ES from master perspective
     * @return point in time failure.
     */
    private double computeFailure(final MasterClusterStateUpdateStats currentMetrics) {
        double failed = currentMetrics.publishFailedCount - prevMasterClusterStateUpdateStats.publishFailedCount;
        if(failed < 0) {
            return 0D;
        }
        return failed;
    }

    @Override
    public String getMetricsPath(long startTime, String... keysPath) {
        if (keysPath.length != KEYS_PATH_LENGTH) {
            throw new RuntimeException("keys length should be " + KEYS_PATH_LENGTH);
        }
        return PerformanceAnalyzerMetrics.generatePath(startTime, PerformanceAnalyzerMetrics.sMasterClusterUpdate);
    }

    public static class MasterClusterStateUpdateStats {
        /**
         * The number of publish cluster state operations.
         */
        private long publishTotalCount;
        /**
         * The time publish cluster state operation takes in millis
         */
        private long publishTimeTakenInMillis;
        /**
         * The number of publish cluster state operations that have failed.
         */
        private long publishFailedCount;

        @VisibleForTesting
        public MasterClusterStateUpdateStats(long totalCount, long timeTakenInMillis, long failedCount) {
            this.publishTotalCount = totalCount;
            this.publishTimeTakenInMillis = timeTakenInMillis;
            this.publishFailedCount = failedCount;
        }

        public MasterClusterStateUpdateStats() {

        }
    }

    public static class MasterClusterStateUpdateMetrics extends MetricStatus {
        private double publishFailedCount;
        private double publishTimeTakenInMillis;

        public MasterClusterStateUpdateMetrics(double latency, double failedCount) {
            this.publishTimeTakenInMillis = latency;
            this.publishFailedCount = failedCount;
        }

        @JsonProperty(AllMetrics.MasterClusterUpdateStatsValue.Constants.PUBLISH_CLUSTER_STATE_LATENCY)
        public double getPublishTimeTakenInMillis() {
            return publishTimeTakenInMillis;
        }

        @JsonProperty(AllMetrics.MasterClusterUpdateStatsValue.Constants.PUBLISH_CLUSTER_STATE_FAILURE)
        public double getPublishFailedCount() {
            return publishFailedCount;
        }
    }
}
