
package main.java.ws;

import main.java.framework.api.metrics.MetricsRegister;
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.RequestHandler;
import org.sonar.api.server.ws.Response;
import org.sonar.api.utils.text.JsonWriter;

import java.util.HashMap;
import java.util.Map;

public class MetricsAction implements RequestHandler {

    private static final String METRICS = "metrics";
    public static final String NAME = "name";
    public static final String KEY = "key";

    @Override
    public void handle(Request request, Response response) throws Exception {


        Map<String, String> metrics = new HashMap<>();
        metrics.put(MetricsRegister.LOC_CLASS.getKey(), "Lines of code");
        metrics.put(MetricsRegister.NOA.getKey(), MetricsRegister.NOA.getDescription());
        metrics.put(MetricsRegister.NOM.getKey(), MetricsRegister.NOM.getDescription());
        metrics.put(MetricsRegister.CYCLO_AVERAGE.getKey(), MetricsRegister.CYCLO_AVERAGE.getDescription());
        metrics.put(MetricsRegister.CYCLO_MAXIMUM.getKey(), MetricsRegister.CYCLO_MAXIMUM.getDescription());
        metrics.put(MetricsRegister.CYCLO_TOTAL.getKey(), MetricsRegister.CYCLO_TOTAL.getDescription());



        try (JsonWriter json = response.newJsonWriter()) {

            json.beginObject();

            json.name(METRICS);
            json.beginArray();
            for (Map.Entry<String, String> metric: metrics.entrySet()) {
                json.beginObject();
                json.prop(KEY, metric.getKey());
                json.prop(NAME, metric.getValue());
                json.endObject();
            }
            json.endArray();

            json.endObject();
        }
    }
}

@@ -69,24 +69,62 @@
 csRegister;%0A
+import org.sonar.api.measures.Metric;%0A
 import org.s
@@ -533,16 +533,168 @@
 %22key%22;%0A%0A
+    private String formatMetricDescription(Metric metric) %7B%0A        return metric.getDescription() + %22 (%22 + metric.getKey().toUpperCase() + %22)%22;%0A    %7D%0A%0A
     @Ove
@@ -904,17 +904,77 @@
  of code
-%22
+ (%22 + MetricsRegister.LOC_CLASS.getKey().toUpperCase() + %22)%22 
 );%0A     
@@ -1010,32 +1010,56 @@
 r.NOA.getKey(), 
+formatMetricDescription(
 MetricsRegister.
@@ -1061,32 +1061,16 @@
 ster.NOA
-.getDescription(
 ));%0A    
@@ -1107,32 +1107,56 @@
 r.NOM.getKey(), 
+formatMetricDescription(
 MetricsRegister.
@@ -1158,32 +1158,16 @@
 ster.NOM
-.getDescription(
 ));%0A    
@@ -1214,32 +1214,56 @@
 ERAGE.getKey(), 
+formatMetricDescription(
 MetricsRegister.
@@ -1275,32 +1275,133 @@
 _AVERAGE
-.getDescription(
+));%0A        metrics.put(MetricsRegister.CYCLO_MAXIMUM.getKey(), formatMetricDescription(MetricsRegister.CYCLO_MAXIMUM
 ));%0A    
@@ -1430,39 +1430,37 @@
 sRegister.CYCLO_
-MAXIMUM
+TOTAL
 .getKey(), Metri
@@ -1458,158 +1458,364 @@
 (), 
-MetricsRegister.CYCLO_MAXIMUM.getDescription());%0A        metrics.put(MetricsRegister.CYCLO_TOTAL.getKey(), MetricsRegister.CYCLO_TOTAL.getDescription(
+formatMetricDescription(MetricsRegister.CYCLO_TOTAL));%0A        metrics.put(MetricsRegister.WMC.getKey(), formatMetricDescription(MetricsRegister.WMC));%0A        metrics.put(MetricsRegister.ATFD_CLASS.getKey(), formatMetricDescription(MetricsRegister.ATFD_CLASS));%0A        metrics.put(MetricsRegister.TCC.getKey(), formatMetricDescription(MetricsRegister.TCC
 ));%0A

add metric key to description in select boxes