package nl.jpoint.maven.vertx.utils;

import com.amazonaws.services.cloudwatch.AmazonCloudWatch;
import com.amazonaws.services.cloudwatch.AmazonCloudWatchClientBuilder;
import com.amazonaws.services.cloudwatch.model.*;
import nl.jpoint.maven.vertx.mojo.DeployConfiguration;
import org.apache.maven.plugin.logging.Log;

import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

public class AwsCloudWatchUtils {


    private static final String OK_DEPLOYS = "ok.deploys";
    private static final String FAILED_DEPLOYS = "failed.deploys";
    private static final String AS_GROUP_SIZE = "as.group.size";
    private final AmazonCloudWatch cloudWatch;
    private final int asGroupSize;
    private final DeployConfiguration activeConfiguration;
    private final Log log;

    private Long start = null;

    public AwsCloudWatchUtils(String region, int asGroupSize, DeployConfiguration activeConfiguration, Log log) {
        cloudWatch = AmazonCloudWatchClientBuilder.standard().withRegion(region).build();
        this.asGroupSize = asGroupSize;
        this.activeConfiguration = activeConfiguration;
        this.log = log;
    }

    public void startTimer() {
        this.start = System.currentTimeMillis();
    }

    public void logSuccess() {
        this.logMetric(OK_DEPLOYS);
    }

    public void logFailed() {
        this.logMetric(FAILED_DEPLOYS);
    }

    private void logMetric(String metricName) {
        Dimension dimension1 = new Dimension().withName("Application").withValue(activeConfiguration.getMetricsConfiguration().getApplication());
        Dimension dimension2 = new Dimension().withName("Environments").withValue(activeConfiguration.getMetricsConfiguration().getEnvironment());

        List<MetricDatum> datums = new ArrayList<>();


        datums.add(new MetricDatum()
                .withMetricName(AS_GROUP_SIZE)
                .withUnit(StandardUnit.None)
                .withValue((double) asGroupSize)
                .withDimensions(dimension1, dimension2));
        datums.add(new MetricDatum()
                .withMetricName(metricName)
                .withUnit(StandardUnit.None)
                .withValue(1.0)
                .withDimensions(dimension1, dimension2));

        if (start != null) {
            datums.add(new MetricDatum()
                    .withMetricName("deploy.duration")
                    .withUnit(StandardUnit.Seconds)
                    .withValue((double) Duration.of(System.currentTimeMillis() - start, ChronoUnit.MILLIS).getSeconds())
                    .withDimensions(dimension1, dimension2));
        }
        try {

            cloudWatch.putMetricData(new PutMetricDataRequest().withNamespace(activeConfiguration.getMetricsConfiguration().getNamespace()).withMetricData(datums));
        } catch (AmazonCloudWatchException e) {
            log.error("Unable to push Cloudwatch metrics.", e);
        }
    }


}

@@ -2005,67 +2005,11 @@
 ize)
-%0A                .withDimensions(dimension1, dimension2)
 );%0A
+
     
@@ -2165,64 +2165,8 @@
 1.0)
-%0A                .withDimensions(dimension1, dimension2)
 );%0A%0A
@@ -2466,68 +2466,8 @@
 s())
-%0A                    .withDimensions(dimension1, dimension2)
 );%0A 
@@ -2471,24 +2471,25 @@
 ;%0A        %7D%0A
+%0A
         try 
@@ -2490,16 +2490,346 @@
   try %7B%0A
+            datums.forEach(metricDatum -%3E metricDatum.withDimensions(dimension1));%0A            cloudWatch.putMetricData(new PutMetricDataRequest().withNamespace(activeConfiguration.getMetricsConfiguration().getNamespace()).withMetricData(datums));%0A            datums.forEach(metricDatum -%3E metricDatum.withDimensions(dimension2));
 %0A       

Add extra dimension