]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7135 WS api/measures/component_tree better best value algorithm
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 15 Jan 2016 16:19:07 +0000 (17:19 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 15 Jan 2016 17:05:43 +0000 (18:05 +0100)
server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java
server/sonar-server/src/main/java/org/sonar/server/measure/ws/MeasureDtoToWsMeasure.java
server/sonar-server/src/main/resources/org/sonar/server/measure/ws/component_tree-example.json
server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java
sonar-ws/src/main/protobuf/ws-measures.proto

index e05441ac0ab93cadbd07a5dac6aa12a2234f73f8..16154108dfa3415e8903cc0d6b44de4512c4a29b 100644 (file)
@@ -211,7 +211,6 @@ public class ComponentTreeDataLoader {
    */
   private static void addBestValuesToMeasures(Table<String, MetricDto, MeasureDto> measuresByComponentUuidAndMetric, List<ComponentDtoWithSnapshotId> components,
     List<MetricDto> metrics, List<WsMeasures.Period> periods) {
-    List<ComponentDtoWithSnapshotId> componentsEligibleForBestValue = from(components).filter(IsFileComponent.INSTANCE).toList();
     List<MetricDtoWithBestValue> metricDtosWithBestValueMeasure = from(metrics)
       .filter(IsMetricOptimizedForBestValue.INSTANCE)
       .transform(new MetricDtoToMetricDtoWithBestValue(periods))
@@ -220,6 +219,7 @@ public class ComponentTreeDataLoader {
       return;
     }
 
+    List<ComponentDtoWithSnapshotId> componentsEligibleForBestValue = from(components).filter(IsFileComponent.INSTANCE).toList();
     for (ComponentDtoWithSnapshotId component : componentsEligibleForBestValue) {
       for (MetricDtoWithBestValue metricWithBestValue : metricDtosWithBestValueMeasure) {
         if (measuresByComponentUuidAndMetric.get(component.uuid(), metricWithBestValue.metric) == null) {
@@ -381,6 +381,7 @@ public class ComponentTreeDataLoader {
   }
 
   private static class MetricDtoWithBestValue {
+    private static final String LOWER_CASE_NEW_METRIC_PREFIX = "new_";
     private final MetricDto metric;
 
     private final MeasureDto bestValue;
@@ -389,11 +390,16 @@ public class ComponentTreeDataLoader {
       this.metric = metric;
       MeasureDto measure = new MeasureDto()
         .setMetricId(metric.getId())
-        .setMetricKey(metric.getKey())
-        .setValue(metric.getBestValue());
-      for (Integer periodIndex : periodIndexes) {
-        measure.setVariation(periodIndex, 0.0d);
+        .setMetricKey(metric.getKey());
+      boolean isNewTypeMetric = metric.getKey().toLowerCase().startsWith(LOWER_CASE_NEW_METRIC_PREFIX);
+      if (isNewTypeMetric) {
+        for (Integer periodIndex : periodIndexes) {
+          measure.setVariation(periodIndex, 0.0d);
+        }
+      } else {
+        measure.setValue(metric.getBestValue());
       }
+
       this.bestValue = measure;
     }
   }
@@ -407,15 +413,6 @@ public class ComponentTreeDataLoader {
     }
   }
 
-  private enum ComponentDtoWithSnapshotIdToSnapshotIdFunction implements Function<ComponentDtoWithSnapshotId, Long> {
-    INSTANCE;
-
-    @Override
-    public Long apply(@Nonnull ComponentDtoWithSnapshotId input) {
-      return input.getSnapshotId();
-    }
-  }
-
   private enum IsNotMetricSort implements Predicate<String> {
     INSTANCE;
 
index a4070fe6bda94203ce0ec13e5c582aec70c4779b..bf9156fc46fe1704ae25f11c68a378c519f8251b 100644 (file)
@@ -44,7 +44,7 @@ class MeasureDtoToWsMeasure {
     WsMeasures.PeriodValue.Builder periodBuilder = WsMeasures.PeriodValue.newBuilder();
     for (int i = 1; i <= 5; i++) {
       if (measureDto.getVariation(i) != null) {
-        measure.addPeriods(periodBuilder
+        measure.getPeriodsBuilder().addPeriodsValue(periodBuilder
           .clear()
           .setIndex(i)
           .setValue(formatNumericalValue(measureDto.getVariation(i), metricDto)));
index 9c22d862ac9533e2a5545bd45471e158631bf5f0..c3f33333d7dbd149d2233d95912c1d1225be93f8 100644 (file)
       {
         "metric": "complexity",
         "value": "42",
-        "periods": []
       },
       {
         "metric": "ncloc",
         "value": "1984",
-        "periods": []
       }
     ]
   },
         {
           "metric": "complexity",
           "value": "12",
-          "periods": []
         },
         {
           "metric": "ncloc",
           "value": "114",
-          "periods": []
         }
       ]
     },
       "higherValuesAreBetter": false,
       "qualitative": true,
       "hidden": false,
-      "custom": false
+      "custom": false,
+      "bestValue": "0"
     }
   ],
   "periods": [
index b666b72f79af075b7d77e49ecf972ae7e009ee52..51ffab6228cd7aad3f93be8c9d6b73aa74824c01 100644 (file)
@@ -172,14 +172,18 @@ public class ComponentTreeActionTest {
     ComponentDto directoryDto = newDirectory(projectDto, "directory-uuid", "path/to/directory").setName("directory-1");
     SnapshotDto directorySnapshot = componentDb.insertComponentAndSnapshot(directoryDto, projectSnapshot);
     SnapshotDto fileSnapshot = componentDb.insertComponentAndSnapshot(newFileDto(directoryDto, "file-uuid").setName("file-1"), directorySnapshot);
-    MetricDto ncloc = newMetricDto()
+    MetricDto coverage = insertCoverageMetric();
+    dbClient.metricDao().insert(dbSession, newMetricDto()
       .setKey("ncloc")
       .setValueType(ValueType.INT.name())
       .setOptimizedBestValue(true)
       .setBestValue(100d)
-      .setWorstValue(1_000d);
-    dbClient.metricDao().insert(dbSession, ncloc);
-    MetricDto coverage = insertCoverageMetric();
+      .setWorstValue(1000d));
+    dbClient.metricDao().insert(dbSession, newMetricDtoWithoutOptimization()
+      .setKey("new_violations")
+      .setOptimizedBestValue(true)
+      .setBestValue(1984.0d)
+      .setValueType(ValueType.INT.name()));
     dbClient.measureDao().insert(dbSession,
       newMeasureDto(coverage, fileSnapshot.getId()).setValue(15.5d),
       newMeasureDto(coverage, directorySnapshot.getId()).setValue(42.0d));
@@ -187,17 +191,18 @@ public class ComponentTreeActionTest {
 
     ComponentTreeWsResponse response = call(ws.newRequest()
       .setParam(PARAM_BASE_COMPONENT_ID, "project-uuid")
-      .setParam(PARAM_METRIC_KEYS, "ncloc,coverage")
+      .setParam(PARAM_METRIC_KEYS, "ncloc,coverage,new_violations")
       .setParam(PARAM_ADDITIONAL_FIELDS, "metrics"));
 
     // directory measures
     assertThat(response.getComponentsList().get(0).getMeasures().getMeasuresList()).extracting("metric").containsOnly("coverage");
     // file measures
-    assertThat(response.getComponentsList().get(1).getMeasures().getMeasuresList()).extracting("metric").containsOnly("ncloc", "coverage");
-    assertThat(response.getComponentsList().get(1).getMeasures().getMeasuresList()).extracting("value").containsOnly("100", "15.5");
+    List<WsMeasures.Measure> fileMeasures = response.getComponentsList().get(1).getMeasures().getMeasuresList();
+    assertThat(fileMeasures).extracting("metric").containsOnly("ncloc", "coverage", "new_violations");
+    assertThat(fileMeasures).extracting("value").containsOnly("100", "15.5", "");
 
     List<Common.Metric> metrics = response.getMetrics().getMetricsList();
-    assertThat(metrics).extracting("bestValue").contains("100");
+    assertThat(metrics).extracting("bestValue").contains("100", "");
     assertThat(metrics).extracting("worstValue").contains("1000");
   }
 
@@ -386,7 +391,7 @@ public class ComponentTreeActionTest {
       .setName("ElementImpl.java")
       .setQualifier(Qualifiers.FILE)
       .setPath("src/main/java/com/sonarsource/markdown/impl/ElementImpl.java"), projectSnapshot);
-    SnapshotDto file2Snapshot = componentDb.insertComponentAndSnapshot(newFileDto(project)
+    componentDb.insertComponentAndSnapshot(newFileDto(project)
       .setUuid("AVIwDXE_bJbJqrw6wFwJ")
       .setKey("com.sonarsource:java-markdown:src/test/java/com/sonarsource/markdown/impl/ElementImplTest.java")
       .setName("ElementImplTest.java")
@@ -423,10 +428,6 @@ public class ComponentTreeActionTest {
         .setVariation(1, 25.0d)
         .setVariation(2, 0.0d)
         .setVariation(3, 25.0d),
-      newMeasureDto(newViolations, file2Snapshot.getId())
-        .setVariation(1, 0.0d)
-        .setVariation(2, 0.0d)
-        .setVariation(3, 0.0d),
       newMeasureDto(newViolations, directorySnapshot.getId())
         .setVariation(1, 25.0d)
         .setVariation(2, 0.0d)
@@ -449,7 +450,9 @@ public class ComponentTreeActionTest {
       .setDirection(-1)
       .setQualitative(true)
       .setHidden(false)
-      .setUserManaged(false));
+      .setUserManaged(false)
+      .setOptimizedBestValue(true)
+      .setBestValue(0.0d));
     db.commit();
     return metric;
   }
index 5217936392c5b4408b841bbb58a386fe42a7fccd..5960ea78943fcca6d2d3e3ae938e57b39aafea9e 100644 (file)
@@ -70,7 +70,11 @@ message Measures {
 message Measure {
   optional string metric = 1;
   optional string value = 2;
-  repeated PeriodValue periods = 3;
+  optional PeriodsValue periods = 3;
+}
+
+message PeriodsValue {
+  repeated PeriodValue periodsValue = 1;
 }
 
 message PeriodValue {