*/
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))
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) {
}
private static class MetricDtoWithBestValue {
+ private static final String LOWER_CASE_NEW_METRIC_PREFIX = "new_";
private final MetricDto metric;
private final MeasureDto bestValue;
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;
}
}
}
}
- 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;
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));
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");
}
.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")
.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)
.setDirection(-1)
.setQualitative(true)
.setHidden(false)
- .setUserManaged(false));
+ .setUserManaged(false)
+ .setOptimizedBestValue(true)
+ .setBestValue(0.0d));
db.commit();
return metric;
}