package com.kryptnostic.rhizome.pods;

import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.health.HealthCheckRegistry;
import com.kryptnostic.rhizome.configuration.RhizomeConfiguration;
import com.ryantenney.metrics.spring.config.annotation.EnableMetrics;
import com.ryantenney.metrics.spring.config.annotation.MetricsConfigurer;
import io.prometheus.client.CollectorRegistry;
import io.prometheus.client.dropwizard.DropwizardExports;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import javax.inject.Inject;
import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * @author Matthew Tamayo-Rios
 */
@Configuration
@EnableMetrics(
        proxyTargetClass = true )
@Import( { AsyncPod.class, ConfigurationPod.class } )
public class MetricsPod implements MetricsConfigurer {
    private static final Logger              logger              = LoggerFactory.getLogger( MetricsPod.class );
    private static final MetricRegistry      metricRegistry      = new MetricRegistry();
    private static final HealthCheckRegistry healthCheckRegistry = new HealthCheckRegistry();

    @Inject
    private RhizomeConfiguration config;

    // @Bean
    // public GraphiteReporter serverGraphiteReporter() throws IOException {
    //     Graphite graphite = serverGraphite();
    //
    //     if ( graphite == null ) {
    //         return null;
    //     }
    //     return GraphiteReporter.forRegistry( metricRegistry )
    //             .prefixedWith( getHostName() )
    //             .convertDurationsTo( TimeUnit.MILLISECONDS )
    //             .convertRatesTo( TimeUnit.SECONDS )
    //             .build( graphite );
    // }

    // @Bean
    // public GraphiteReporter aggregateGraphiteReporter() throws IOException {
    //     Graphite graphite = serverGraphite();
    //
    //     if ( graphite == null ) {
    //         return null;
    //     }
    //
    //     return GraphiteReporter.forRegistry( metricRegistry )
    //             .convertDurationsTo( TimeUnit.MILLISECONDS )
    //             .convertRatesTo( TimeUnit.SECONDS )
    //             .build( graphite );
    // }

    // @Bean
    // public ConsoleReporter consoleGraphiteReporter() {
    //     if ( config.getGraphiteConfiguration().isPresent() ) {
    //         GraphiteConfiguration graphiteConfig = config.getGraphiteConfiguration().get();
    //         if ( graphiteConfig.isEnableConsole() ) {
    //             return ConsoleReporter.forRegistry( metricRegistry )
    //                     .convertDurationsTo( TimeUnit.MILLISECONDS )
    //                     .convertRatesTo( TimeUnit.SECONDS )
    //                     .build();
    //         }
    //     }
    //     return null;
    // }

    @Override
    public void configureReporters( MetricRegistry registry ) {

        // https://github.com/prometheus/client_java/issues/101#issuecomment-275143650
        CollectorRegistry.defaultRegistry.register( new DropwizardExports( registry ) );
    }

    @Override
    @Bean
    public HealthCheckRegistry getHealthCheckRegistry() {
        return healthCheckRegistry;
    }

    @Override
    @Bean
    public MetricRegistry getMetricRegistry() {
        return metricRegistry;
    }

    // @Bean
    // public Graphite serverGraphite() throws IOException {
    //     if ( config.getGraphiteConfiguration().isPresent() ) {
    //         GraphiteConfiguration graphiteConfig = config.getGraphiteConfiguration().get();
    //         logger.info(
    //                 "Initializing server graphite instance with at {}:{}",
    //                 graphiteConfig.getGraphiteHost(),
    //                 graphiteConfig.getGraphitePort() );
    //         Graphite graphite = new Graphite( new InetSocketAddress(
    //                 graphiteConfig.getGraphiteHost(),
    //                 graphiteConfig.getGraphitePort() ) );
    //         graphite.connect();
    //         return graphite;
    //     }
    //     return null;
    // }

    // @Autowired( required = false )
    // public void startGraphite( Set<ScheduledReporter> reporters ) {
    //     reporters.forEach( reporter -> {
    //         if ( reporter != null ) {
    //             reporter.start( 10, TimeUnit.SECONDS );
    //         }
    //     } );
    // }

    protected String getHostName() {
        try {
            return InetAddress.getLocalHost().getHostName();
        } catch ( UnknownHostException e ) {
            logger.warn( "Unable to determine hostname, default to Hazelcast UUID", e );
            return null;
        }
    }

    protected String getGlobalName() {
        if ( config.getGraphiteConfiguration().isPresent() ) {
            return config.getGraphiteConfiguration().get().getGraphiteGlobalPrefix();
        }
        return "global";
    }
}

@@ -838,24 +838,16 @@
 Metrics(
-%0A       
  proxyTa
@@ -974,16 +974,17 @@
 gurer %7B%0A
+%0A
     priv
@@ -1333,3125 +1333,497 @@
     
-// @Bean%0A    // public GraphiteReporter serverGraphiteReporter() throws IOException %7B%0A    //     Graphite graphite = serverGraphite();%0A    //%0A    //     if ( graphite == null ) %7B%0A    //         return null;%0A    //     %7D%0A    //     return GraphiteReporter.forRegistry( metricRegistry )%0A    //             .prefixedWith( getHostName() )%0A    //             .convertDurationsTo( TimeUnit.MILLISECONDS )%0A    //             .convertRatesTo( TimeUnit.SECONDS )%0A    //             .build( graphite );%0A    // %7D%0A%0A    // @Bean%0A    // public GraphiteReporter aggregateGraphiteReporter() throws IOException %7B%0A    //     Graphite graphite = serverGraphite();%0A    //%0A    //     if ( graphite == null ) %7B%0A    //         return null;%0A    //     %7D%0A    //%0A    //     return GraphiteReporter.forRegistry( metricRegistry )%0A    //             .convertDurationsTo( TimeUnit.MILLISECONDS )%0A    //             .convertRatesTo( TimeUnit.SECONDS )%0A    //             .build( graphite );%0A    // %7D%0A%0A    // @Bean%0A    // public ConsoleReporter consoleGraphiteReporter() %7B%0A    //     if ( config.getGraphiteConfiguration().isPresent() ) %7B%0A    //         GraphiteConfiguration graphiteConfig = config.getGraphiteConfiguration().get();%0A    //         if ( graphiteConfig.isEnableConsole() ) %7B%0A    //             return ConsoleReporter.forRegistry( metricRegistry )%0A    //                     .convertDurationsTo( TimeUnit.MILLISECONDS )%0A    //                     .convertRatesTo( TimeUnit.SECONDS )%0A    //                     .build();%0A    //         %7D%0A    //     %7D%0A    //     return null;%0A    // %7D%0A%0A    @Override%0A    public void configureReporters( MetricRegistry registry ) %7B%0A%0A        // https://github.com/prometheus/client_java/issues/101#issuecomment-275143650%0A        CollectorRegistry.defaultRegistry.register( new DropwizardExports( registry ) );%0A    %7D%0A%0A    @Override%0A    @Bean%0A    public HealthCheckRegistry getHealthCheckRegistry() %7B%0A        return healthCheckRegistry;%0A    %7D%0A%0A    @Override%0A    @Bean%0A    public MetricRegistry getMetricRegistry() %7B%0A        return metricRegistry;%0A    %7D%0A%0A    // @Bean%0A    // public Graphite serverGraphite() throws IOException %7B%0A    //     if ( config.getGraphiteConfiguration().isPresent() ) %7B%0A    //         GraphiteConfiguration graphiteConfig = config.getGraphiteConfiguration().get();%0A    //         logger.info(%0A    //                 %22Initializing server graphite instance with at %7B%7D:%7B%7D%22,%0A    //                 graphiteConfig.getGraphiteHost(),%0A    //                 graphiteConfig.getGraphitePort() );%0A    //         Graphite graphite = new Graphite( new InetSocketAddress(%0A    //                 graphiteConfig.getGraphiteHost(),%0A    //                 graphiteConfig.getGraphitePort() ) );%0A    //         graphite.connect();%0A    //         return graphite;%0A    //     %7D%0A    //     return null;%0A    // %7D%0A%0A    // @Autowired( required = false )%0A    // public void startGraphite( Set%3CScheduledReporter%3E reporters ) %7B%0A    //     reporters.forEach( reporter -%3E %7B%0A    //         if ( reporter != null ) %7B%0A    //             reporter.start( 10, TimeUnit.SECONDS );%0A    //         %7D%0A    //     %7D );%0A    //
+@Override%0A    public void configureReporters( MetricRegistry registry ) %7B%0A%0A        // https://github.com/prometheus/client_java/issues/101#issuecomment-275143650%0A        CollectorRegistry.defaultRegistry.register( new DropwizardExports( registry ) );%0A    %7D%0A%0A    @Override%0A    @Bean%0A    public HealthCheckRegistry getHealthCheckRegistry() %7B%0A        return healthCheckRegistry;%0A    %7D%0A%0A    @Override%0A    @Bean%0A    public MetricRegistry getMetricRegistry() %7B%0A        return metricRegistry;%0A   
  %7D%0A%0A

remove code that is commented out