package com.skcraft.plume.common.module.metrics;

import com.codahale.metrics.JmxReporter;
import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.graphite.Graphite;
import com.codahale.metrics.graphite.GraphiteReporter;
import com.google.inject.Inject;
import com.sk89q.worldedit.util.eventbus.Subscribe;
import com.skcraft.plume.common.event.lifecycle.InitializationEvent;
import com.skcraft.plume.common.util.Environment;
import com.skcraft.plume.common.util.config.Config;
import com.skcraft.plume.common.util.config.InjectConfig;
import com.skcraft.plume.common.util.module.Module;
import lombok.extern.java.Log;
import ninja.leaping.configurate.objectmapping.Setting;
import ninja.leaping.configurate.objectmapping.serialize.ConfigSerializable;

import java.net.InetSocketAddress;
import java.util.concurrent.TimeUnit;

@Module(name = "metrics-reporter", desc = "Make metrics available outside the server", enabled = true)
@Log
public class MetricsReporter {

    @Inject private MetricRegistry metricRegistry;
    @Inject private Environment environment;
    @InjectConfig("metrics_reporter") private Config<ReporterConfig> config;

    @Subscribe
    public void onInitialization(InitializationEvent event) {
        if (config.get().graphite.enabled) {
            log.info("Registering Graphite metrics destination...");
            Graphite graphite = new Graphite(new InetSocketAddress(config.get().graphite.address, config.get().graphite.port));
            GraphiteReporter reporter = GraphiteReporter.forRegistry(metricRegistry)
                    .prefixedWith("plume.servers." + environment.getServerId().replace(".", "_"))
                    .convertRatesTo(TimeUnit.SECONDS)
                    .convertDurationsTo(TimeUnit.MILLISECONDS)
                    .filter(MetricFilter.ALL)
                    .build(graphite);
            reporter.start(config.get().graphite.interval, TimeUnit.SECONDS);
        }

        if (config.get().jmx.enabled) {
            log.info("Registering metrics via JMX...");
            JmxReporter reporter = JmxReporter.forRegistry(metricRegistry).build();
            reporter.start();
        }
    }

    private static class ReporterConfig {
        @Setting(comment = "Enable reporting via JMX")
        private JMXConfig jmx = new JMXConfig();

        @Setting(comment = "Enable reporting to a Graphite instance")
        private GraphiteConfig graphite = new GraphiteConfig();
    }

    @ConfigSerializable
    private static class GraphiteConfig {
        @Setting
        private boolean enabled = false;
        @Setting
        private String address = "127.0.0.1";
        @Setting
        private int port = 22003;
        @Setting(comment = "The interval in seconds to report metrics at")
        private int interval = 10;
    }

    @ConfigSerializable
    private static class JMXConfig {
        @Setting
        private boolean enabled = false;
    }

}

@@ -43,16 +43,61 @@
 trics;%0A%0A
+import com.codahale.metrics.ConsoleReporter;%0A
 import c
@@ -121,32 +121,32 @@
 cs.JmxReporter;%0A
-
 import com.codah
@@ -1309,24 +1309,451 @@
 nt event) %7B%0A
+        if (config.get().console.enabled) %7B%0A            log.info(%22Registering console metrics printing...%22);%0A            ConsoleReporter reporter = ConsoleReporter.forRegistry(metricRegistry)%0A                    .convertRatesTo(TimeUnit.SECONDS)%0A                    .convertDurationsTo(TimeUnit.MILLISECONDS)%0A                    .build();%0A            reporter.start(config.get().console.interval, TimeUnit.SECONDS);%0A        %7D%0A%0A
         if (
@@ -2716,32 +2716,153 @@
 eporterConfig %7B%0A
+        @Setting(comment = %22Enable reporting via console%22)%0A        private ConsoleConfig console = new ConsoleConfig();%0A%0A
         @Setting
@@ -3016,24 +3016,24 @@
  instance%22)%0A
-
         priv
@@ -3083,32 +3083,272 @@
 onfig();%0A    %7D%0A%0A
+    @ConfigSerializable%0A    private static class ConsoleConfig %7B%0A        @Setting%0A        private boolean enabled = false;%0A        @Setting(comment = %22The interval in seconds to report metrics at%22)%0A        private int interval = 10;%0A    %7D%0A%0A
     @ConfigSeria

Add support for printing metrics to console.