package org.apache.bookkeeper.stats.twitter.ostrich;

import com.twitter.ostrich.admin.CustomHttpHandler;
import com.twitter.ostrich.admin.RuntimeEnvironment;
import com.twitter.util.Duration;
import org.apache.bookkeeper.stats.StatsLogger;
import org.apache.bookkeeper.stats.StatsProvider;
import org.apache.commons.configuration.Configuration;
import scala.Some;
import scala.collection.immutable.$colon$colon;
import scala.collection.immutable.List;
import scala.collection.immutable.List$;
import scala.collection.immutable.Map;
import scala.collection.immutable.Map$;

import java.util.concurrent.TimeUnit;

public class OstrichProvider implements StatsProvider {

    protected final static String STATS_EXPORT = "statsExport";
    protected final static String STATS_HTTP_PORT = "statsHttpPort";

    private com.twitter.ostrich.admin.AdminHttpService statsExporter = null;
    private final String collectionName;

    private static <T> List<T> list(T ... ts) {
        List<T> result = List$.MODULE$.empty();
        for (int i = ts.length; i > 0; i--) {
            result = new $colon$colon<T>(ts[i-1], result);
        }
        return result;
    }

    private static <K, V> Map<K, V> emptyMap() {
        Map<K, V> result = Map$.MODULE$.empty();
        return result;
    }

    public OstrichProvider() {
        this("");
    }

    public OstrichProvider(String collectionName) {
        this.collectionName = collectionName;
    }

    @Override
    public void start(Configuration conf) {
        if (conf.getBoolean(STATS_EXPORT, false)) {
            statsExporter = new com.twitter.ostrich.admin.AdminServiceFactory(
                    conf.getInt(STATS_HTTP_PORT, 9002), 20, null, Some.apply(""), null,
                    OstrichProvider.<String, CustomHttpHandler>emptyMap(), list(Duration.apply(1, TimeUnit.MINUTES))
            ).apply(RuntimeEnvironment.apply(this, new String[0]));
        }
        if (null != statsExporter) {
            statsExporter.start();
        }
    }

    @Override
    public void stop() {
        if (null != statsExporter) {
            statsExporter.shutdown();
        }
    }

    @Override
    public StatsLogger getStatsLogger(String scope) {
        return new OstrichStatsLoggerImpl(scope, com.twitter.ostrich.stats.Stats.get(collectionName));
    }
}

@@ -152,16 +152,63 @@
 onment;%0A
+import com.twitter.ostrich.admin.StatsFactory;%0A
 import c
@@ -612,16 +612,50 @@
 le.Map$;
+%0Aimport scala.util.matching.Regex;
 %0A%0Aimport
@@ -1234,24 +1234,114 @@
 ult;%0A    %7D%0A%0A
+    private static %3CT%3E List%3CT%3E emptyList() %7B%0A        return List$.MODULE$.empty();%0A    %7D%0A%0A
     private 
@@ -1870,34 +1870,121 @@
 20, 
-null, Some.apply(%22%22), null
+OstrichProvider.%3CStatsFactory%3EemptyList(),%0A                    Some.apply(%22%22), OstrichProvider.%3CRegex%3EemptyList()
 ,%0A  
@@ -2180,90 +2180,8 @@
   %7D%0A
-        if (null != statsExporter) %7B%0A            statsExporter.start();%0A        %7D%0A
     

Fix ostrich stats provider