diff options
Diffstat (limited to 'sonar-db/src')
7 files changed, 61 insertions, 21 deletions
diff --git a/sonar-db/src/main/java/org/sonar/db/component/SnapshotDto.java b/sonar-db/src/main/java/org/sonar/db/component/SnapshotDto.java index c84b1176211..635b9118496 100644 --- a/sonar-db/src/main/java/org/sonar/db/component/SnapshotDto.java +++ b/sonar-db/src/main/java/org/sonar/db/component/SnapshotDto.java @@ -265,6 +265,9 @@ public final class SnapshotDto { return this; } + /** + * @return analysis date + */ public Long getCreatedAt() { return createdAt; } diff --git a/sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java b/sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java index 0daf18d49b1..5539c79a73d 100644 --- a/sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java +++ b/sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java @@ -22,7 +22,6 @@ package org.sonar.db.measure; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.Optional; import org.apache.ibatis.session.ResultHandler; @@ -30,6 +29,7 @@ import org.sonar.db.Dao; import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; +import static java.util.Collections.emptyList; import static org.sonar.db.DatabaseUtils.executeLargeInputs; import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput; @@ -58,7 +58,7 @@ public class MeasureDao implements Dao { */ public List<MeasureDto> selectByQuery(DbSession dbSession, MeasureQuery query) { if (query.returnsEmpty()) { - return Collections.emptyList(); + return emptyList(); } if (query.isOnComponents()) { return executeLargeInputs( @@ -106,18 +106,28 @@ public class MeasureDao implements Dao { public List<MeasureDto> selectTreeByQuery(DbSession dbSession, ComponentDto baseComponent, MeasureTreeQuery query) { if (query.returnsEmpty()) { - return Collections.emptyList(); + return emptyList(); } return mapper(dbSession).selectTreeByQuery(query, baseComponent.uuid(), query.getUuidPath(baseComponent)); } - public List<PastMeasureDto> selectPastMeasures(DbSession dbSession, - String componentUuid, - String analysisUuid, - Collection<Integer> metricIds) { + public List<PastMeasureDto> selectPastMeasures(DbSession dbSession, String componentUuid, String analysisUuid, Collection<Integer> metricIds) { + if (metricIds.isEmpty()) { + return emptyList(); + } return executeLargeInputs( metricIds, - ids -> mapper(dbSession).selectPastMeasures(componentUuid, analysisUuid, ids)); + ids -> mapper(dbSession).selectPastMeasuresOnSingleAnalysis(componentUuid, analysisUuid, ids)); + } + + public List<MeasureDto> selectPastMeasures(DbSession dbSession, String componentUuid, List<String> analysisUuids, List<Integer> metricIds) { + if (analysisUuids.isEmpty() || metricIds.isEmpty()) { + return emptyList(); + } + + return executeLargeInputs( + analysisUuids, + analyses -> mapper(dbSession).selectPastMeasuresOnSeveralAnalyses(componentUuid, analyses, metricIds)); } /** diff --git a/sonar-db/src/main/java/org/sonar/db/measure/MeasureDto.java b/sonar-db/src/main/java/org/sonar/db/measure/MeasureDto.java index ff4bd806603..9e41defc058 100644 --- a/sonar-db/src/main/java/org/sonar/db/measure/MeasureDto.java +++ b/sonar-db/src/main/java/org/sonar/db/measure/MeasureDto.java @@ -86,6 +86,9 @@ public class MeasureDto { return this; } + /** + * @param index starts at 1 + */ @CheckForNull public Double getVariation(int index) { switch (index) { diff --git a/sonar-db/src/main/java/org/sonar/db/measure/MeasureMapper.java b/sonar-db/src/main/java/org/sonar/db/measure/MeasureMapper.java index 978b8b0c47c..eb282770edc 100644 --- a/sonar-db/src/main/java/org/sonar/db/measure/MeasureMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/measure/MeasureMapper.java @@ -40,7 +40,11 @@ public interface MeasureMapper { List<MeasureDto> selectTreeByQuery(@Param("query") MeasureTreeQuery measureQuery, @Param("baseUuid") String baseUuid, @Param("baseUuidPath") String baseUuidPath); - List<PastMeasureDto> selectPastMeasures(@Param("componentUuid") String componentUuid, @Param("analysisUuid") String analysisUuid, @Param("metricIds") List<Integer> metricIds); + List<PastMeasureDto> selectPastMeasuresOnSingleAnalysis(@Param("componentUuid") String componentUuid, @Param("analysisUuid") String analysisUuid, + @Param("metricIds") List<Integer> metricIds); + + List<MeasureDto> selectPastMeasuresOnSeveralAnalyses(@Param("componentUuid") String componentUuid, @Param("analysisUuids") Collection<String> analysisUuid, + @Param("metricIds") Collection<Integer> metricIds); List<MeasureDto> selectProjectMeasuresOfDeveloper(@Param("developerId") long developerId, @Param("metricIds") Collection<Integer> metricIds); diff --git a/sonar-db/src/main/java/org/sonar/db/metric/MetricDtoFunctions.java b/sonar-db/src/main/java/org/sonar/db/metric/MetricDtoFunctions.java index f19fed07105..d0a4fbd84d0 100644 --- a/sonar-db/src/main/java/org/sonar/db/metric/MetricDtoFunctions.java +++ b/sonar-db/src/main/java/org/sonar/db/metric/MetricDtoFunctions.java @@ -20,7 +20,6 @@ package org.sonar.db.metric; import java.util.function.Predicate; -import javax.annotation.Nonnull; /** * Common functions on MetricDto @@ -32,15 +31,6 @@ public class MetricDtoFunctions { public static Predicate<MetricDto> isOptimizedForBestValue() { - return IsMetricOptimizedForBestValue.INSTANCE; - } - - private enum IsMetricOptimizedForBestValue implements Predicate<MetricDto> { - INSTANCE; - - @Override - public boolean test(@Nonnull MetricDto input) { - return input.isOptimizedBestValue() && input.getBestValue() != null; - } + return m -> m != null && m.isOptimizedBestValue() && m.getBestValue() != null; } } diff --git a/sonar-db/src/main/resources/org/sonar/db/measure/MeasureMapper.xml b/sonar-db/src/main/resources/org/sonar/db/measure/MeasureMapper.xml index 9b5900bba35..c56d91b0cb5 100644 --- a/sonar-db/src/main/resources/org/sonar/db/measure/MeasureMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/measure/MeasureMapper.xml @@ -128,7 +128,7 @@ <include refid="org.sonar.db.component.ComponentMapper.selectDescendantsFilters"/> </sql> - <select id="selectPastMeasures" parameterType="map" resultType="org.sonar.db.measure.PastMeasureDto"> + <select id="selectPastMeasuresOnSingleAnalysis" parameterType="map" resultType="org.sonar.db.measure.PastMeasureDto"> select pm.id as id, pm.metric_id as metricId, pm.person_id as personId, pm.value as value from project_measures pm inner join snapshots analysis on analysis.uuid = pm.analysis_uuid @@ -139,6 +139,17 @@ and pm.person_id is null </select> + <select id="selectPastMeasuresOnSeveralAnalyses" parameterType="map" resultType="Measure"> + select <include refid="measureColumns"/> + from project_measures pm + inner join snapshots analysis on analysis.uuid = pm.analysis_uuid + where + pm.component_uuid = #{componentUuid} + and analysis.uuid in <foreach item="analysisUuid" collection="analysisUuids" open="(" separator="," close=")">#{analysisUuid}</foreach> + and pm.metric_id in <foreach item="metricId" collection="metricIds" open="(" separator="," close=")">#{metricId}</foreach> + and pm.person_id is null + </select> + <select id="selectProjectMeasuresOfDeveloper" parameterType="map" resultType="Measure"> SELECT <include refid="measureColumns"/> diff --git a/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java b/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java index 4fb8fb3b8d3..0ac06eccd8f 100644 --- a/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java +++ b/sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java @@ -35,6 +35,7 @@ import org.sonar.db.component.ComponentDto; import org.sonar.db.component.SnapshotTesting; import org.sonar.db.organization.OrganizationDto; +import static com.google.common.collect.Lists.newArrayList; import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; @@ -449,6 +450,24 @@ public class MeasureDaoTest { verifyMeasures(file1, MeasureTreeQuery.builder().setStrategy(LEAVES), "M2", "M3"); } + @Test + public void select_past_measures_with_several_analyses() { + ComponentDto project = db.components().insertProject(); + insertAnalysis(LAST_ANALYSIS_UUID, project.uuid(), true); + insertAnalysis(OTHER_ANALYSIS_UUID, project.uuid(), false); + db.components().indexAllComponents(); + + // project + insertMeasure("PROJECT_M1", LAST_ANALYSIS_UUID, project.uuid(), NCLOC_METRIC_ID); + insertMeasure("PROJECT_M2", OTHER_ANALYSIS_UUID, project.uuid(), NCLOC_METRIC_ID); + db.commit(); + + // Children measures of project + List<MeasureDto> result = underTest.selectPastMeasures(db.getSession(), project.uuid(), newArrayList(LAST_ANALYSIS_UUID, OTHER_ANALYSIS_UUID), singletonList(NCLOC_METRIC_ID)); + + assertThat(result).hasSize(2).extracting(MeasureDto::getData).containsOnly("PROJECT_M1", "PROJECT_M2"); + } + private Optional<MeasureDto> selectSingle(MeasureQuery.Builder query) { return underTest.selectSingle(db.getSession(), query.build()); } |