/*
 * Copyright (c) 2016, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * WSO2 Inc. licenses this file to you 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.
 */

package org.wso2.carbon.siddhi.metrics.core;

import org.wso2.carbon.metrics.manager.Level;
import org.wso2.carbon.metrics.manager.MetricManager;
import org.wso2.carbon.metrics.manager.Timer;
import org.wso2.carbon.siddhi.metrics.core.util.SiddhiMetricsConstants;
import org.wso2.siddhi.core.util.statistics.LatencyTracker;

public class SiddhiLatencyMetric implements LatencyTracker {
    // Using thread local variables to keep the timer track
    // the time of the same execution path by different threads.
    private ThreadLocal<Timer> execLatencyTimer;
    private ThreadLocal<Timer.Context> context;
    private String metricName;
    private boolean enabled;

    public SiddhiLatencyMetric(String name, boolean isEnabled) {
        enabled = isEnabled;
        if (enabled) {
            metricName = MetricManager.name(name, SiddhiMetricsConstants.METRIC_SUFFIX_LATENCY);
            execLatencyTimer = new ThreadLocal<Timer>() {
                protected Timer initialValue() {
                    return MetricManager.timer(metricName, Level.INFO);
                }
            };
            context = new ThreadLocal<Timer.Context>() {
                protected Timer.Context initialValue() {
                    return null;
                }
            };
        }
    }

    /**
     * This is called when the processing of the event is started. This is called at
     * ProcessStreamReceiver#receive before the event is passed into process chain.
     */
    public void markIn() {
        if (enabled) {
            if (context.get() != null) {
                throw new IllegalStateException("MarkIn consecutively called without calling markOut in " + metricName);
            }
            context.set(execLatencyTimer.get().start());
        }
    }

    /**
     * This is called to when the processing of an event is finished. This is called at two places,
     * 1. OutputRateLimiter#sendToCallBacks - When the event is processed and by the full chain and emitted out.
     * 2. ProcessStreamReceiver#receive - When event is not processed by full process chain(e.g. Filtered out by a filter).
     */
    @Override
    public void markOut() {
        if (enabled) {
            if (context.get() != null) {
                context.get().stop();
                context.set(null);
            }
        }
    }

    /**
     * @return Name of the latency tracker.
     */
    @Override
    public String getName() {
        if (enabled) {
            return metricName;
        } else {
            return null;
        }
    }
}
