/*
 * Copyright (c) 2013 original authors
 *
 * 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.
 */
package org.eigengo.monitor.output.datadog;

import com.timgroup.statsd.NonBlockingStatsDClient;
import com.timgroup.statsd.StatsDClient;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import com.typesafe.config.ConfigParseOptions;
import com.typesafe.config.ConfigResolveOptions;
import org.eigengo.monitor.output.CounterInterface;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Submits the counters to the local statsd interface
 */
public class StatsdCounterInterface implements CounterInterface {
    private static final Config config = ConfigFactory.load("META-INF/monitor/output.conf",
            ConfigParseOptions.defaults().setAllowMissing(false),
            ConfigResolveOptions.defaults());
    private static final Config outputConfig = config.getConfig("org.eigengo.monitor.output");

    private static final String prefix = outputConfig.getString("prefix");
    private static final String remoteAddress = outputConfig.getString("remoteAddress");
    private static final int remotePort = outputConfig.getInt("remotePort");
    private static final int refresh = outputConfig.getInt("refresh");
    private static final int delay = outputConfig.getInt("initialDelay");
    private static final List<String> tagList = outputConfig.getStringList("constantTags");
    private static final String[] constantTags = tagList.toArray(new String[tagList.size()]);

    private static final StatsDClient statsd = new NonBlockingStatsDClient(prefix, remoteAddress, remotePort, constantTags);
    private static final ConcurrentHashMap<String, Metric> GAUGE_VALUES = new ConcurrentHashMap<>();

    public StatsdCounterInterface() {
        final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        //Send an update to DataDog every 5 seconds
        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                for (Metric metric : GAUGE_VALUES.values()) {
                    statsd.recordGaugeValue(metric.aspect, metric.value, metric.tags);
                }
            }
        }, delay, refresh, TimeUnit.SECONDS);
        //Shutdown scheduler
        Runtime.getRuntime().addShutdownHook(new Thread(){
            @Override
            public void run() {
                scheduledExecutorService.shutdown();
            }
        });
    }

    @Override
    public void incrementCounter(String aspect, String... tags) {
        statsd.incrementCounter(aspect, tags);
    }

    @Override
    public void incrementCounter(String aspect, int delta, String... tags) {
        statsd.count(aspect, delta, tags);
    }

    @Override
    public void decrementCounter(String aspect, String... tags) {
        statsd.decrementCounter(aspect, tags);
    }

    @Override
    public void recordGaugeValue(String aspect, int value, String... tags) {
        statsd.recordGaugeValue(aspect, value, tags);
        GAUGE_VALUES.put(aspect + joinTags(tags), new Metric(aspect, value, tags));
    }

    @Override
    public void recordExecutionTime(String aspect, int duration, String... tags) {
        statsd.recordExecutionTime(aspect, duration, tags);
    }

    private static String joinTags(String[] tags) {
        StringBuilder builder = new StringBuilder(256);
        for (String tag : tags) {
            builder.append(tag).append('|');
        }
        return builder.toString();
    }

    private static class Metric {
        private String aspect;
        private int value;
        private String[] tags;

        private Metric(String aspect, int value, String[] tags) {
            this.aspect = aspect;
            this.value = value;
            this.tags = tags;
        }

        @Override
        public String toString() {
            return "Metric{aspect='" + aspect + '\'' + ", value=" + value + ", tags=" + Arrays.toString(tags) + '}';
        }
    }


}

@@ -1041,156 +1041,9 @@
 ent.
-ConcurrentHashMap;%0Aimport java.util.concurrent.Executors;%0Aimport java.util.concurrent.ScheduledExecutorService;%0Aimport java.util.concurrent.TimeUnit
+*
 ;%0A%0A/
@@ -2407,16 +2407,605 @@
 adPool(1
+, new ThreadFactory() %7B%0A            private final ThreadFactory delegate = Executors.defaultThreadFactory();%0A%0A            /**%0A             * Set thread as daemon and with minimal priority using a delegate ThreadFactory%0A             * @param r Runnable%0A             * @return daemon thread%0A             */%0A            @Override%0A            public Thread newThread(Runnable r) %7B%0A                final Thread thread = delegate.newThread(r);%0A                thread.setDaemon(true);%0A                thread.setPriority(Thread.MIN_PRIORITY);%0A                return thread;%0A            %7D%0A        %7D
 );%0A     
@@ -3041,17 +3041,17 @@
 g every 
-5
+X
  seconds
@@ -3431,16 +3431,16 @@
 heduler%0A
-
         
@@ -3488,16 +3488,17 @@
 Thread()
+ 
 %7B%0A      

Use a minimal priority daemon thread