package com.ctrip.xpipe.service.metric;

import com.ctrip.framework.clogging.agent.MessageManager;
import com.ctrip.framework.clogging.agent.metrics.aggregator.MetricsAggregator;
import com.ctrip.framework.clogging.agent.metrics.aggregator.MetricsAggregatorFactory;
import com.ctrip.xpipe.metric.MetricData;
import com.ctrip.xpipe.metric.MetricProxy;
import com.ctrip.xpipe.metric.MetricProxyException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * @author wenchao.meng
 *         <p>
 *         Aug 17, 2017
 */
public class DashBoardMetric implements MetricProxy{

    private Logger logger = LoggerFactory.getLogger(getClass());

    private static MetricsAggregator aggregator = MetricsAggregatorFactory.createAggregator("fx.xpipe.delay", "dc", "cluster", "shard", "ip", "port");

    public DashBoardMetric(){

        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            @Override
            public void run() {
                logger.info("[run][shutdown]");
                MessageManager.getInstance().shutdown();
            }
        }));
    }


    @Override
    public void writeBinMultiDataPoint(MetricData metricData) throws MetricProxyException {

        aggregator.add(metricData.getValue(),
                metricData.getDcName(),
                metricData.getClusterName(),
                metricData.getShardName(),
                metricData.getHostPort().getHost(),
                String.valueOf(metricData.getHostPort().getPort()));

    }

    @Override
    public int getOrder() {
        return 0;
    }
}

@@ -259,16 +259,123 @@
 actory;%0A
+import com.ctrip.framework.foundation.Foundation;%0Aimport com.ctrip.xpipe.api.foundation.FoundationService;%0A
 import c
@@ -408,16 +408,16 @@
 icData;%0A
-
 import c
@@ -885,16 +885,30 @@
 .delay%22,
+ %22console-dc%22,
  %22dc%22, %22
@@ -1347,16 +1347,16 @@
 tion %7B%0A%0A
-
         
@@ -1389,24 +1389,83 @@
 getValue(),%0A
+                FoundationService.DEFAULT.getDataCenter(),%0A
             

add console-dc for dashboard metric