package com.sequenceiq.cloudbreak.service.metrics;

import org.springframework.stereotype.Service;

import com.sequenceiq.cloudbreak.common.metrics.AbstractMetricService;
import com.sequenceiq.cloudbreak.common.metrics.type.MetricTag;
import com.sequenceiq.cloudbreak.domain.stack.Stack;
import com.sequenceiq.cloudbreak.domain.view.StackView;

@Service("MetricService")
public class CloudbreakMetricService extends AbstractMetricService {

    private static final String METRIC_PREFIX = "cloudbreak";

    public void incrementMetricCounter(MetricType metric, Stack stack) {
        incrementMetricCounter(metric,
                CloudbreakMetricTag.STACK_TYPE.name(), stack.getType().name(),
                MetricTag.CLOUD_PROVIDER.name(), stack.cloudPlatform(),
                MetricTag.TUNNEL_TYPE.name(), stack.getTunnel().name());
    }

    public void incrementMetricCounter(MetricType metric, Stack stack, Exception e) {
        incrementMetricCounter(metric,
                MetricTag.EXCEPTION_TYPE.name(), e.getClass().getName(),
                CloudbreakMetricTag.STACK_TYPE.name(), stack.getType().name(),
                MetricTag.CLOUD_PROVIDER.name(), stack.cloudPlatform(),
                MetricTag.TUNNEL_TYPE.name(), stack.getTunnel().name());
    }

    public void incrementMetricCounter(MetricType metric, StackView stack) {
        incrementMetricCounter(metric,
                CloudbreakMetricTag.STACK_TYPE.name(), stack.getType().name(),
                MetricTag.CLOUD_PROVIDER.name(), stack.cloudPlatform(),
                MetricTag.TUNNEL_TYPE.name(), stack.getTunnel().name());
    }

    public void incrementMetricCounter(MetricType metric, StackView stack, Exception e) {
        incrementMetricCounter(metric,
                MetricTag.EXCEPTION_TYPE.name(), e.getClass().getName(),
                CloudbreakMetricTag.STACK_TYPE.name(), stack.getType().name(),
                MetricTag.CLOUD_PROVIDER.name(), stack.cloudPlatform(),
                MetricTag.TUNNEL_TYPE.name(), stack.getTunnel().name());
    }

    @Override
    protected String getMetricPrefix() {
        return METRIC_PREFIX;
    }

    public void recordImageCopyTime(Stack stack, Runnable checkImage) {
        recordLongTaskTimer(MetricType.STACK_IMAGE_COPY, checkImage,
                MetricTag.TENANT.name(), stack.getTenant().getName(),
                MetricTag.CLOUD_PROVIDER.name(), stack.cloudPlatform(),
                MetricTag.REGION.name(), stack.getRegion());
    }
}

@@ -338,16 +338,45 @@
 kView;%0A%0A
+import java.util.ArrayList;%0A%0A
 @Service
@@ -611,38 +611,42 @@
     
-incrementMetricCounter(metric,
+String%5B%5D tags = nullableValueTags(
 %0A   
@@ -858,32 +858,78 @@
 nnel().name());%0A
+        incrementMetricCounter(metric, tags);%0A
     %7D%0A%0A    publi
@@ -1017,38 +1017,42 @@
     
-incrementMetricCounter(metric,
+String%5B%5D tags = nullableValueTags(
 %0A   
@@ -1337,32 +1337,78 @@
 nnel().name());%0A
+        incrementMetricCounter(metric, tags);%0A
     %7D%0A%0A    publi
@@ -1487,38 +1487,42 @@
     
-incrementMetricCounter(metric,
+String%5B%5D tags = nullableValueTags(
 %0A   
@@ -1734,32 +1734,78 @@
 nnel().name());%0A
+        incrementMetricCounter(metric, tags);%0A
     %7D%0A%0A    publi
@@ -1897,38 +1897,42 @@
     
-incrementMetricCounter(metric,
+String%5B%5D tags = nullableValueTags(
 %0A   
@@ -2221,24 +2221,70 @@
 ().name());%0A
+        incrementMetricCounter(metric, tags);%0A
     %7D%0A%0A    @
@@ -2454,68 +2454,42 @@
     
-recordLongTaskTimer(MetricType.STACK_IMAGE_COPY, checkImage,
+String%5B%5D tags = nullableValueTags(
 %0A   
@@ -2619,32 +2619,32 @@
 loudPlatform(),%0A
-
                 
@@ -2684,20 +2684,777 @@
 tRegion());%0A
+        recordLongTaskTimer(MetricType.STACK_IMAGE_COPY, checkImage, tags);%0A    %7D%0A%0A    private String%5B%5D nullableValueTags(String... tags) %7B%0A        if (tags.length %25 2 == 1) %7B%0A            throw new IllegalArgumentException(%22tags size must be even, it is a set of key=value pairs of tags%22);%0A        %7D%0A        ArrayList%3CString%3E accumulatedTags = new ArrayList%3C%3E();%0A        for (int i = 0; i %3C tags.length; i += 2) %7B%0A            if (tags%5Bi + 1%5D != null) %7B%0A                accumulatedTags.add(tags%5Bi%5D);%0A                accumulatedTags.add(tags%5Bi + 1%5D);%0A            %7D%0A        %7D%0A        String %5B%5D arrAccumulatedTags = new String%5BaccumulatedTags.size()%5D;%0A        arrAccumulatedTags = accumulatedTags.toArray(arrAccumulatedTags);%0A        return arrAccumulatedTags;%0A
     %7D%0A%7D%0A

Fix NullPointerException in ycloud metric for region