From: Sébastien Lesaint Date: Wed, 2 Dec 2015 14:49:02 +0000 (+0100) Subject: SONAR-7027 do not try and compute variations of developer measures X-Git-Tag: 5.3-RC1~92 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=45c9896c61d28f064952573dff0e30bee5e08d2f;p=sonarqube.git SONAR-7027 do not try and compute variations of developer measures --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputeMeasureVariationsStep.java b/server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputeMeasureVariationsStep.java index 768628ba67f..4da6dd4190f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputeMeasureVariationsStep.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/step/ComputeMeasureVariationsStep.java @@ -47,6 +47,7 @@ import org.sonar.server.computation.metric.MetricRepository; import org.sonar.server.computation.period.Period; import org.sonar.server.computation.period.PeriodsHolder; +import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.collect.FluentIterable.from; import static org.sonar.server.computation.component.Component.Type.DIRECTORY; import static org.sonar.server.computation.component.Component.Type.SUBVIEW; @@ -143,7 +144,7 @@ public class ComputeMeasureVariationsStep implements ComputationStep { private void setVariationMeasures(Component component, List pastMeasures, int period, MeasuresWithVariationRepository measuresWithVariationRepository) { Map pastMeasuresByMeasureKey = from(pastMeasures).uniqueIndex(pastMeasureToMeasureKey); - for (Map.Entry entry : measureRepository.getRawMeasures(component).entries()) { + for (Map.Entry entry : from(measureRepository.getRawMeasures(component).entries()).filter(NotDeveloperMeasure.INSTANCE)) { String metricKey = entry.getKey(); Measure measure = entry.getValue(); PastMeasureDto pastMeasure = pastMeasuresByMeasureKey.get(new MeasureKey(metricKey, measure.getCharacteristicId(), measure.getRuleId(), null)); @@ -175,6 +176,7 @@ public class ComputeMeasureVariationsStep implements ComputationStep { private final Map measuresWithVariations = new HashMap<>(); public void add(Metric metric, final Measure measure, int variationIndex, double variationValue) { + checkArgument(measure.getDeveloper() == null, "%s does not support computing variations of Measures for Developer", getClass().getSimpleName()); MeasureKey measureKey = new MeasureKey(metric.getKey(), measure.getCharacteristicId(), measure.getRuleId(), null); MeasureWithVariations measureWithVariations = measuresWithVariations.get(measureKey); if (measureWithVariations == null) { @@ -250,6 +252,15 @@ public class ComputeMeasureVariationsStep implements ComputationStep { } } + private enum NotDeveloperMeasure implements Predicate> { + INSTANCE; + + @Override + public boolean apply(@Nonnull Map.Entry input) { + return input.getValue().getDeveloper() == null; + } + } + @Override public String getDescription() { return "Compute measure variations"; diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportComputeMeasureVariationsStepTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportComputeMeasureVariationsStepTest.java index 1b74b2c70ca..07f848adc1f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportComputeMeasureVariationsStepTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/step/ReportComputeMeasureVariationsStepTest.java @@ -35,6 +35,7 @@ import org.sonar.db.measure.MeasureDto; import org.sonar.server.computation.batch.BatchReportReaderRule; import org.sonar.server.computation.batch.TreeRootHolderRule; import org.sonar.server.computation.component.Component; +import org.sonar.server.computation.component.DumbDeveloper; import org.sonar.server.computation.component.ReportComponent; import org.sonar.server.computation.measure.Measure; import org.sonar.server.computation.measure.MeasureRepositoryRule; @@ -48,6 +49,7 @@ import org.sonar.test.DbTests; import static org.assertj.core.api.Assertions.assertThat; import static org.sonar.db.component.SnapshotTesting.createForComponent; import static org.sonar.db.component.SnapshotTesting.newSnapshotForProject; +import static org.sonar.server.computation.measure.Measure.newMeasureBuilder; @Category(DbTests.class) public class ReportComputeMeasureVariationsStepTest { @@ -59,27 +61,23 @@ public class ReportComputeMeasureVariationsStepTest { static final ComponentDto PROJECT_DTO = ComponentTesting.newProjectDto(); - static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_DTO.uuid()).build(); + static final int PROJECT_REF = 1; + static final Component PROJECT = ReportComponent.builder(Component.Type.PROJECT, PROJECT_REF).setUuid(PROJECT_DTO.uuid()).build(); @Rule public DbTester dbTester = DbTester.create(System2.INSTANCE); - @Rule public BatchReportReaderRule reportReader = new BatchReportReaderRule(); - @Rule public PeriodsHolderRule periodsHolder = new PeriodsHolderRule(); - @Rule public TreeRootHolderRule treeRootHolder = new TreeRootHolderRule(); - @Rule public MetricRepositoryRule metricRepository = new MetricRepositoryRule() .add(ISSUES_METRIC) .add(DEBT_METRIC) .add(FILE_COMPLEXITY_METRIC) .add(BUILD_BREAKER_METRIC); - @Rule public MeasureRepositoryRule measureRepository = MeasureRepositoryRule.create(treeRootHolder, metricRepository); @@ -146,8 +144,8 @@ public class ReportComputeMeasureVariationsStepTest { Component project = ReportComponent.builder(Component.Type.PROJECT, 1).setUuid(PROJECT_DTO.uuid()).addChildren(directory).build(); treeRootHolder.setRoot(project); - addRawMeasure(project, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null)); - addRawMeasure(directory, ISSUES_METRIC, Measure.newMeasureBuilder().create(20, null)); + addRawMeasure(project, ISSUES_METRIC, newMeasureBuilder().create(80, null)); + addRawMeasure(directory, ISSUES_METRIC, newMeasureBuilder().create(20, null)); underTest.execute(); @@ -180,7 +178,7 @@ public class ReportComputeMeasureVariationsStepTest { treeRootHolder.setRoot(PROJECT); - addRawMeasure(PROJECT, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null)); + addRawMeasure(PROJECT, ISSUES_METRIC, newMeasureBuilder().create(80, null)); underTest.execute(); @@ -204,17 +202,17 @@ public class ReportComputeMeasureVariationsStepTest { newMeasureDto(DEBT_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 10d), newMeasureDto(FILE_COMPLEXITY_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 2d), newMeasureDto(BUILD_BREAKER_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 1d) - ); + ); session.commit(); periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot)); treeRootHolder.setRoot(PROJECT); - addRawMeasure(PROJECT, ISSUES_METRIC, Measure.newMeasureBuilder().create(80, null)); - addRawMeasure(PROJECT, DEBT_METRIC, Measure.newMeasureBuilder().create(5L, null)); - addRawMeasure(PROJECT, FILE_COMPLEXITY_METRIC, Measure.newMeasureBuilder().create(3d, null)); - addRawMeasure(PROJECT, BUILD_BREAKER_METRIC, Measure.newMeasureBuilder().create(false, null)); + addRawMeasure(PROJECT, ISSUES_METRIC, newMeasureBuilder().create(80, null)); + addRawMeasure(PROJECT, DEBT_METRIC, newMeasureBuilder().create(5L, null)); + addRawMeasure(PROJECT, FILE_COMPLEXITY_METRIC, newMeasureBuilder().create(3d, null)); + addRawMeasure(PROJECT, BUILD_BREAKER_METRIC, newMeasureBuilder().create(false, null)); underTest.execute(); @@ -226,6 +224,29 @@ public class ReportComputeMeasureVariationsStepTest { assertThat(measureRepository.getRawMeasure(PROJECT, BUILD_BREAKER_METRIC).get().getVariations().getVariation1()).isEqualTo(-1d); } + @Test + public void do_not_set_variations_on_numeric_metric_for_developer() { + SnapshotDto period1ProjectSnapshot = newSnapshotForProject(PROJECT_DTO); + dbClient.snapshotDao().insert(session, period1ProjectSnapshot); + dbClient.measureDao().insert(session, + newMeasureDto(ISSUES_METRIC.getId(), PROJECT_DTO.getId(), period1ProjectSnapshot.getId(), 60d) + ); + session.commit(); + + periodsHolder.setPeriods(newPeriod(1, period1ProjectSnapshot)); + + treeRootHolder.setRoot(PROJECT); + + DumbDeveloper developer = new DumbDeveloper("a"); + measureRepository.addRawMeasure(PROJECT_REF, ISSUES_METRIC.getKey(), newMeasureBuilder().forDeveloper(developer).create(80, null)); + + underTest.execute(); + + assertThat(measureRepository.getRawMeasures(PROJECT).keys()).hasSize(1); + + assertThat(measureRepository.getRawMeasure(PROJECT, ISSUES_METRIC, developer).get().hasVariations()).isFalse(); + } + private static MeasureDto newMeasureDto(int metricId, long projectId, long snapshotId, double value) { return new MeasureDto().setMetricId(metricId).setComponentId(projectId).setSnapshotId(snapshotId).setValue(value); }