From: Teryk Bellahsene Date: Fri, 15 Jan 2016 16:19:07 +0000 (+0100) Subject: SONAR-7135 WS api/measures/component_tree better best value algorithm X-Git-Tag: 5.4-M8~18 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=2aeb5d7035dabbfef7aac0239c189e5561e3bd60;p=sonarqube.git SONAR-7135 WS api/measures/component_tree better best value algorithm --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java index e05441ac0ab..16154108dfa 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/ComponentTreeDataLoader.java @@ -211,7 +211,6 @@ public class ComponentTreeDataLoader { */ private static void addBestValuesToMeasures(Table measuresByComponentUuidAndMetric, List components, List metrics, List periods) { - List componentsEligibleForBestValue = from(components).filter(IsFileComponent.INSTANCE).toList(); List metricDtosWithBestValueMeasure = from(metrics) .filter(IsMetricOptimizedForBestValue.INSTANCE) .transform(new MetricDtoToMetricDtoWithBestValue(periods)) @@ -220,6 +219,7 @@ public class ComponentTreeDataLoader { return; } + List 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 { - INSTANCE; - - @Override - public Long apply(@Nonnull ComponentDtoWithSnapshotId input) { - return input.getSnapshotId(); - } - } - private enum IsNotMetricSort implements Predicate { INSTANCE; diff --git a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MeasureDtoToWsMeasure.java b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MeasureDtoToWsMeasure.java index a4070fe6bda..bf9156fc46f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MeasureDtoToWsMeasure.java +++ b/server/sonar-server/src/main/java/org/sonar/server/measure/ws/MeasureDtoToWsMeasure.java @@ -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))); diff --git a/server/sonar-server/src/main/resources/org/sonar/server/measure/ws/component_tree-example.json b/server/sonar-server/src/main/resources/org/sonar/server/measure/ws/component_tree-example.json index 9c22d862ac9..c3f33333d7d 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/measure/ws/component_tree-example.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/measure/ws/component_tree-example.json @@ -30,12 +30,10 @@ { "metric": "complexity", "value": "42", - "periods": [] }, { "metric": "ncloc", "value": "1984", - "periods": [] } ] }, @@ -67,12 +65,10 @@ { "metric": "complexity", "value": "12", - "periods": [] }, { "metric": "ncloc", "value": "114", - "periods": [] } ] }, @@ -181,7 +177,8 @@ "higherValuesAreBetter": false, "qualitative": true, "hidden": false, - "custom": false + "custom": false, + "bestValue": "0" } ], "periods": [ diff --git a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java index b666b72f79a..51ffab6228c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/measure/ws/ComponentTreeActionTest.java @@ -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 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 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; } diff --git a/sonar-ws/src/main/protobuf/ws-measures.proto b/sonar-ws/src/main/protobuf/ws-measures.proto index 5217936392c..5960ea78943 100644 --- a/sonar-ws/src/main/protobuf/ws-measures.proto +++ b/sonar-ws/src/main/protobuf/ws-measures.proto @@ -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 {