diff options
author | Eric Giffon <eric.giffon@sonarsource.com> | 2024-09-17 16:25:13 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-10-09 20:02:46 +0000 |
commit | 82225b439672af6d35d463b48001452321459ff4 (patch) | |
tree | 3036731e7a7adee7901856a0f453d2d942a5dce4 /server/sonar-telemetry/src | |
parent | 0ff9b7313c17474a867f744aa63afd94795a134b (diff) | |
download | sonarqube-82225b439672af6d35d463b48001452321459ff4.tar.gz sonarqube-82225b439672af6d35d463b48001452321459ff4.zip |
SONAR-22873 Read JSON measures (#11683)
Co-authored-by: Claire Villard <claire.villard@sonarsource.com>
Diffstat (limited to 'server/sonar-telemetry/src')
3 files changed, 132 insertions, 59 deletions
diff --git a/server/sonar-telemetry/src/it/java/org/sonar/telemetry/legacy/TelemetryDataLoaderImplIT.java b/server/sonar-telemetry/src/it/java/org/sonar/telemetry/legacy/TelemetryDataLoaderImplIT.java index 42af73dcce8..a523f348f1e 100644 --- a/server/sonar-telemetry/src/it/java/org/sonar/telemetry/legacy/TelemetryDataLoaderImplIT.java +++ b/server/sonar-telemetry/src/it/java/org/sonar/telemetry/legacy/TelemetryDataLoaderImplIT.java @@ -123,15 +123,18 @@ class TelemetryDataLoaderImplIT { private final QualityGateFinder qualityGateFinder = new QualityGateFinder(db.getDbClient()); private final QualityProfileDataProvider qualityProfileDataProvider = new QualityProfileDataProvider(db.getDbClient(), new QProfileComparison(db.getDbClient())); + private final ProjectLocDistributionDataProvider projectLocDistributionDataProvider = new ProjectLocDistributionDataProvider(db.getDbClient()); private final InternalProperties internalProperties = spy(new MapInternalProperties()); private final ManagedInstanceService managedInstanceService = mock(ManagedInstanceService.class); private final CloudUsageDataProvider cloudUsageDataProvider = mock(CloudUsageDataProvider.class); private final AiCodeAssuranceVerifier aiCodeAssuranceVerifier = mock(AiCodeAssuranceVerifier.class); private final TelemetryDataLoader communityUnderTest = new TelemetryDataLoaderImpl(server, db.getDbClient(), pluginRepository, editionProvider, - internalProperties, configuration, containerSupport, qualityGateCaycChecker, qualityGateFinder, managedInstanceService, cloudUsageDataProvider, qualityProfileDataProvider, aiCodeAssuranceVerifier); + internalProperties, configuration, containerSupport, qualityGateCaycChecker, qualityGateFinder, managedInstanceService, cloudUsageDataProvider, qualityProfileDataProvider, aiCodeAssuranceVerifier, + projectLocDistributionDataProvider); private final TelemetryDataLoader commercialUnderTest = new TelemetryDataLoaderImpl(server, db.getDbClient(), pluginRepository, editionProvider, - internalProperties, configuration, containerSupport, qualityGateCaycChecker, qualityGateFinder, managedInstanceService, cloudUsageDataProvider, qualityProfileDataProvider, aiCodeAssuranceVerifier); + internalProperties, configuration, containerSupport, qualityGateCaycChecker, qualityGateFinder, managedInstanceService, cloudUsageDataProvider, qualityProfileDataProvider, aiCodeAssuranceVerifier, + projectLocDistributionDataProvider); private QualityGateDto builtInDefaultQualityGate; private MetricDto bugsDto; @@ -184,27 +187,27 @@ class TelemetryDataLoaderImplIT { ComponentDto mainBranch1 = projectData1.getMainBranchComponent(); var branch1 = db.components().insertProjectBranch(mainBranch1, branchDto -> branchDto.setKey("reference")); var branch2 = db.components().insertProjectBranch(mainBranch1, branchDto -> branchDto.setKey("custom")); - db.measures().insertLiveMeasure(mainBranch1, lines, m -> m.setValue(110d)); - db.measures().insertLiveMeasure(mainBranch1, ncloc, m -> m.setValue(110d)); - db.measures().insertLiveMeasure(mainBranch1, coverage, m -> m.setValue(80d)); - db.measures().insertLiveMeasure(mainBranch1, nclocDistrib, m -> m.setValue(null).setData("java=70;js=30;kotlin=10")); - db.measures().insertLiveMeasure(mainBranch1, bugsDto, m -> m.setValue(1d)); - db.measures().insertLiveMeasure(mainBranch1, vulnerabilitiesDto, m -> m.setValue(1d).setData((String) null)); - db.measures().insertLiveMeasure(mainBranch1, securityHotspotsDto, m -> m.setValue(1d).setData((String) null)); - db.measures().insertLiveMeasure(mainBranch1, developmentCostDto, m -> m.setData("50").setValue(null)); - db.measures().insertLiveMeasure(mainBranch1, technicalDebtDto, m -> m.setValue(5d).setData((String) null)); + db.measures().insertMeasure(mainBranch1, m -> m.addValue(lines.getKey(), 110d)); + db.measures().insertMeasure(mainBranch1, m -> m.addValue(ncloc.getKey(), 110d)); + db.measures().insertMeasure(mainBranch1, m -> m.addValue(coverage.getKey(), 80d)); + db.measures().insertMeasure(mainBranch1, m -> m.addValue(nclocDistrib.getKey(), "java=70;js=30;kotlin=10")); + db.measures().insertMeasure(mainBranch1, m -> m.addValue(bugsDto.getKey(), 1d)); + db.measures().insertMeasure(mainBranch1, m -> m.addValue(vulnerabilitiesDto.getKey(), 1d)); + db.measures().insertMeasure(mainBranch1, m -> m.addValue(securityHotspotsDto.getKey(), 1d)); + db.measures().insertMeasure(mainBranch1, m -> m.addValue(developmentCostDto.getKey(), "50")); + db.measures().insertMeasure(mainBranch1, m -> m.addValue(technicalDebtDto.getKey(), 5d)); // Measures on other branches - db.measures().insertLiveMeasure(branch1, technicalDebtDto, m -> m.setValue(6d).setData((String) null)); - db.measures().insertLiveMeasure(branch2, technicalDebtDto, m -> m.setValue(7d).setData((String) null)); + db.measures().insertMeasure(branch1, m -> m.addValue(technicalDebtDto.getKey(), 6d)); + db.measures().insertMeasure(branch2, m -> m.addValue(technicalDebtDto.getKey(), 7d)); ProjectData projectData2 = db.components().insertPrivateProject(ComponentDbTester.defaults(), projectDto -> projectDto.setAiCodeAssurance(false)); when(aiCodeAssuranceVerifier.isAiCodeAssured(projectData2.getProjectDto().getAiCodeAssurance())).thenReturn(false); ComponentDto mainBranch2 = projectData2.getMainBranchComponent(); - db.measures().insertLiveMeasure(mainBranch2, lines, m -> m.setValue(200d)); - db.measures().insertLiveMeasure(mainBranch2, ncloc, m -> m.setValue(200d)); - db.measures().insertLiveMeasure(mainBranch2, coverage, m -> m.setValue(80d)); - db.measures().insertLiveMeasure(mainBranch2, nclocDistrib, m -> m.setValue(null).setData("java=180;js=20")); + db.measures().insertMeasure(mainBranch2, m -> m.addValue(lines.getKey(), 200d)); + db.measures().insertMeasure(mainBranch2, m -> m.addValue(ncloc.getKey(), 200d)); + db.measures().insertMeasure(mainBranch2, m -> m.addValue(coverage.getKey(), 80d)); + db.measures().insertMeasure(mainBranch2, m -> m.addValue(nclocDistrib.getKey(), "java=180;js=20")); SnapshotDto project1Analysis = db.components().insertSnapshot(mainBranch1, t -> t.setLast(true).setAnalysisDate(analysisDate)); SnapshotDto project2Analysis = db.components().insertSnapshot(mainBranch2, t -> t.setLast(true).setAnalysisDate(analysisDate)); @@ -417,16 +420,16 @@ class TelemetryDataLoaderImplIT { db.qualityProfiles().associateWithProject(projectData.getProjectDto(), javaQP, kotlinQP, jsQP); ComponentDto mainBranch = projectData.getMainBranchComponent(); - db.measures().insertLiveMeasure(mainBranch, lines, m -> m.setValue(110d)); - db.measures().insertLiveMeasure(mainBranch, ncloc, m -> m.setValue(110d)); - db.measures().insertLiveMeasure(mainBranch, coverage, m -> m.setValue(80d)); - db.measures().insertLiveMeasure(mainBranch, nclocDistrib, m -> m.setValue(null).setData("java=70;js=30;kotlin=10")); + db.measures().insertMeasure(mainBranch, m -> m.addValue(lines.getKey(), 110d)); + db.measures().insertMeasure(mainBranch, m -> m.addValue(ncloc.getKey(), 110d)); + db.measures().insertMeasure(mainBranch, m -> m.addValue(coverage.getKey(), 80d)); + db.measures().insertMeasure(mainBranch, m -> m.addValue(nclocDistrib.getKey(), "java=70;js=30;kotlin=10")); ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setBranchType(BRANCH)); - db.measures().insertLiveMeasure(branch, lines, m -> m.setValue(180d)); - db.measures().insertLiveMeasure(branch, ncloc, m -> m.setValue(180d)); - db.measures().insertLiveMeasure(branch, coverage, m -> m.setValue(80d)); - db.measures().insertLiveMeasure(branch, nclocDistrib, m -> m.setValue(null).setData("java=100;js=50;kotlin=30")); + db.measures().insertMeasure(branch, m -> m.addValue(lines.getKey(), 180d)); + db.measures().insertMeasure(branch, m -> m.addValue(ncloc.getKey(), 180d)); + db.measures().insertMeasure(branch, m -> m.addValue(coverage.getKey(), 80d)); + db.measures().insertMeasure(branch, m -> m.addValue(nclocDistrib.getKey(), "java=100;js=50;kotlin=30")); SnapshotDto project1Analysis = db.components().insertSnapshot(mainBranch, t -> t.setLast(true)); SnapshotDto project2Analysis = db.components().insertSnapshot(branch, t -> t.setLast(true)); @@ -464,12 +467,12 @@ class TelemetryDataLoaderImplIT { db.qualityProfiles().associateWithProject(projectData.getProjectDto(), jsQP); ComponentDto mainBranch = projectData.getMainBranchComponent(); - db.measures().insertLiveMeasure(mainBranch, ncloc, m -> m.setValue(110d)); - db.measures().insertLiveMeasure(mainBranch, nclocDistrib, m -> m.setValue(null).setData("java=70;js=30;kotlin=10")); + db.measures().insertMeasure(mainBranch, m -> m.addValue(ncloc.getKey(), 110d)); + db.measures().insertMeasure(mainBranch, m -> m.addValue(nclocDistrib.getKey(), "java=70;js=30;kotlin=10")); ComponentDto branch = db.components().insertProjectBranch(mainBranch, b -> b.setBranchType(BRANCH)); - db.measures().insertLiveMeasure(branch, ncloc, m -> m.setValue(180d)); - db.measures().insertLiveMeasure(branch, nclocDistrib, m -> m.setValue(null).setData("java=100;js=50;kotlin=30")); + db.measures().insertMeasure(branch, m -> m.addValue(ncloc.getKey(), 180d)); + db.measures().insertMeasure(branch, m -> m.addValue(nclocDistrib.getKey(), "java=100;js=50;kotlin=30")); SnapshotDto project1Analysis = db.components().insertSnapshot(mainBranch, t -> t.setLast(true)); SnapshotDto project2Analysis = db.components().insertSnapshot(branch, t -> t.setLast(true)); @@ -614,9 +617,9 @@ class TelemetryDataLoaderImplIT { MetricDto unanalyzedCpp = db.measures().insertMetric(m -> m.setKey(UNANALYZED_CPP_KEY)); ComponentDto project1 = db.components().insertPublicProject().getMainBranchComponent(); ComponentDto project2 = db.components().insertPublicProject().getMainBranchComponent(); - db.measures().insertLiveMeasure(project1, unanalyzedC); - db.measures().insertLiveMeasure(project2, unanalyzedC); - db.measures().insertLiveMeasure(project2, unanalyzedCpp); + db.measures().insertMeasure(project1, m -> m.addValue(unanalyzedC.getKey(), 1)); + db.measures().insertMeasure(project2, m -> m.addValue(unanalyzedC.getKey(), 1)); + db.measures().insertMeasure(project2, m -> m.addValue(unanalyzedCpp.getKey(), 1)); TelemetryData data = communityUnderTest.load(); @@ -631,8 +634,8 @@ class TelemetryDataLoaderImplIT { MetricDto unanalyzedCpp = db.measures().insertMetric(m -> m.setKey(UNANALYZED_CPP_KEY)); ComponentDto project1 = db.components().insertPublicProject().getMainBranchComponent(); ComponentDto project2 = db.components().insertPublicProject().getMainBranchComponent(); - db.measures().insertLiveMeasure(project1, unanalyzedC); - db.measures().insertLiveMeasure(project2, unanalyzedCpp); + db.measures().insertMeasure(project1, m -> m.addValue(unanalyzedC.getKey(), 1)); + db.measures().insertMeasure(project2, m -> m.addValue(unanalyzedCpp.getKey(), 1)); TelemetryData data = communityUnderTest.load(); diff --git a/server/sonar-telemetry/src/main/java/org/sonar/telemetry/legacy/ProjectLocDistributionDataProvider.java b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/legacy/ProjectLocDistributionDataProvider.java new file mode 100644 index 00000000000..03e01d9a951 --- /dev/null +++ b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/legacy/ProjectLocDistributionDataProvider.java @@ -0,0 +1,68 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.telemetry.legacy; + +import java.util.ArrayList; +import java.util.List; +import org.sonar.db.DbClient; +import org.sonar.db.DbSession; +import org.sonar.db.measure.MeasureDto; +import org.sonar.db.measure.ProjectLocDistributionDto; +import org.sonar.db.project.ProjectDto; + +import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY; +import static org.sonar.api.measures.CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION_KEY; + +public class ProjectLocDistributionDataProvider { + + private final DbClient dbClient; + + public ProjectLocDistributionDataProvider(DbClient dbClient) { + this.dbClient = dbClient; + } + + /** + * Returns the loc distribution of the largest branch of each project. + */ + public List<ProjectLocDistributionDto> getProjectLocDistribution(DbSession dbSession) { + List<ProjectLocDistributionDto> branchesWithLargestNcloc = new ArrayList<>(); + List<ProjectDto> projects = dbClient.projectDao().selectProjects(dbSession); + for (ProjectDto project : projects) { + List<MeasureDto> branchMeasures = dbClient.measureDao().selectBranchMeasuresForProject(dbSession, project.getUuid()); + + long maxncloc = 0; + String largestBranchUuid = null; + String locDistribution = null; + for (MeasureDto measure : branchMeasures) { + Long branchNcloc = measure.getLong(NCLOC_KEY); + if (branchNcloc != null && branchNcloc >= maxncloc) { + maxncloc = branchNcloc; + largestBranchUuid = measure.getBranchUuid(); + locDistribution = measure.getString(NCLOC_LANGUAGE_DISTRIBUTION_KEY); + } + } + + if (locDistribution != null) { + branchesWithLargestNcloc.add(new ProjectLocDistributionDto(project.getUuid(), largestBranchUuid, locDistribution)); + } + } + return branchesWithLargestNcloc; + } +} diff --git a/server/sonar-telemetry/src/main/java/org/sonar/telemetry/legacy/TelemetryDataLoaderImpl.java b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/legacy/TelemetryDataLoaderImpl.java index b1eac7b4e52..9bfffe45fc0 100644 --- a/server/sonar-telemetry/src/main/java/org/sonar/telemetry/legacy/TelemetryDataLoaderImpl.java +++ b/server/sonar-telemetry/src/main/java/org/sonar/telemetry/legacy/TelemetryDataLoaderImpl.java @@ -49,8 +49,9 @@ import org.sonar.db.component.AnalysisPropertyValuePerProject; import org.sonar.db.component.BranchMeasuresDto; import org.sonar.db.component.PrBranchAnalyzedLanguageCountByProjectDto; import org.sonar.db.component.SnapshotDto; +import org.sonar.db.measure.MeasureDto; import org.sonar.db.measure.ProjectLocDistributionDto; -import org.sonar.db.measure.ProjectMainBranchLiveMeasureDto; +import org.sonar.db.measure.ProjectMainBranchMeasureDto; import org.sonar.db.metric.MetricDto; import org.sonar.db.newcodeperiod.NewCodePeriodDto; import org.sonar.db.project.ProjectDto; @@ -71,7 +72,6 @@ import org.sonar.telemetry.legacy.TelemetryData.NewCodeDefinition; import static java.util.Arrays.asList; import static java.util.Optional.ofNullable; -import static java.util.stream.Collectors.groupingBy; import static java.util.stream.Collectors.toMap; import static org.apache.commons.lang3.StringUtils.startsWithIgnoreCase; import static org.sonar.api.measures.CoreMetrics.BUGS_KEY; @@ -116,6 +116,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { private final CloudUsageDataProvider cloudUsageDataProvider; private final QualityProfileDataProvider qualityProfileDataProvider; private final AiCodeAssuranceVerifier aiCodeAssuranceVerifier; + private final ProjectLocDistributionDataProvider projectLocDistributionDataProvider; private final Set<NewCodeDefinition> newCodeDefinitions = new HashSet<>(); private final Map<String, NewCodeDefinition> ncdByProject = new HashMap<>(); private final Map<String, NewCodeDefinition> ncdByBranch = new HashMap<>(); @@ -128,7 +129,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { PlatformEditionProvider editionProvider, InternalProperties internalProperties, Configuration configuration, ContainerSupport containerSupport, QualityGateCaycChecker qualityGateCaycChecker, QualityGateFinder qualityGateFinder, ManagedInstanceService managedInstanceService, CloudUsageDataProvider cloudUsageDataProvider, QualityProfileDataProvider qualityProfileDataProvider, - AiCodeAssuranceVerifier aiCodeAssuranceVerifier) { + AiCodeAssuranceVerifier aiCodeAssuranceVerifier, ProjectLocDistributionDataProvider projectLocDistributionDataProvider) { this.server = server; this.dbClient = dbClient; this.pluginRepository = pluginRepository; @@ -142,6 +143,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { this.cloudUsageDataProvider = cloudUsageDataProvider; this.qualityProfileDataProvider = qualityProfileDataProvider; this.aiCodeAssuranceVerifier = aiCodeAssuranceVerifier; + this.projectLocDistributionDataProvider = projectLocDistributionDataProvider; } private static Database loadDatabaseMetadata(DbSession dbSession) { @@ -278,16 +280,24 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { } private void resolveUnanalyzedLanguageCode(TelemetryData.Builder data, DbSession dbSession) { - long numberOfUnanalyzedCMeasures = dbClient.liveMeasureDao().countProjectsHavingMeasure(dbSession, UNANALYZED_C_KEY); - long numberOfUnanalyzedCppMeasures = dbClient.liveMeasureDao().countProjectsHavingMeasure(dbSession, UNANALYZED_CPP_KEY); editionProvider.get() .filter(edition -> edition.equals(COMMUNITY)) .ifPresent(edition -> { + List<MeasureDto> measureDtos = dbClient.measureDao().selectAllForMainBranches(dbSession); + long numberOfUnanalyzedCMeasures = countProjectsHavingMeasure(measureDtos, UNANALYZED_C_KEY); + long numberOfUnanalyzedCppMeasures = countProjectsHavingMeasure(measureDtos, UNANALYZED_CPP_KEY); + data.setHasUnanalyzedC(numberOfUnanalyzedCMeasures > 0); data.setHasUnanalyzedCpp(numberOfUnanalyzedCppMeasures > 0); }); } + private static long countProjectsHavingMeasure(List<MeasureDto> measureDtos, String metricKey) { + return measureDtos.stream() + .filter(m -> m.getMetricValues().containsKey(metricKey)) + .count(); + } + private Long retrieveCurrentMessageSequenceNumber() { return internalProperties.read(I_PROP_MESSAGE_SEQUENCE).map(Long::parseLong).orElse(0L); } @@ -299,9 +309,8 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { Map<String, PrBranchAnalyzedLanguageCountByProjectDto> prAndBranchCountByProject = dbClient.branchDao().countPrBranchAnalyzedLanguageByProjectUuid(dbSession) .stream().collect(toMap(PrBranchAnalyzedLanguageCountByProjectDto::getProjectUuid, Function.identity())); Map<String, String> qgatesByProject = getProjectQgatesMap(dbSession); - Map<String, Map<String, Number>> metricsByProject = getProjectMetricsByMetricKeys(dbSession, TECHNICAL_DEBT_KEY, DEVELOPMENT_COST_KEY, SECURITY_HOTSPOTS_KEY, - VULNERABILITIES_KEY, - BUGS_KEY); + Map<String, Map<String, Number>> metricsByProject = getProjectMetricsByMetricKeys(dbSession, List.of(TECHNICAL_DEBT_KEY, + DEVELOPMENT_COST_KEY, SECURITY_HOTSPOTS_KEY, VULNERABILITIES_KEY, BUGS_KEY)); Map<String, Long> securityReportExportedAtByProjectUuid = getSecurityReportExportedAtDateByProjectUuid(dbSession); List<TelemetryData.ProjectStatistics> projectStatistics = new ArrayList<>(); @@ -356,10 +365,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { } private void resolveProjects(TelemetryData.Builder data, DbSession dbSession) { - Map<String, String> metricUuidMap = getNclocMetricUuidMap(dbSession); - String nclocUuid = metricUuidMap.get(NCLOC_KEY); - String nclocDistributionUuid = metricUuidMap.get(NCLOC_LANGUAGE_DISTRIBUTION_KEY); - List<ProjectLocDistributionDto> branchesWithLargestNcloc = dbClient.liveMeasureDao().selectLargestBranchesLocDistribution(dbSession, nclocUuid, nclocDistributionUuid); + List<ProjectLocDistributionDto> branchesWithLargestNcloc = projectLocDistributionDataProvider.getProjectLocDistribution(dbSession); List<String> branchUuids = branchesWithLargestNcloc.stream().map(ProjectLocDistributionDto::branchUuid).toList(); Map<String, Long> latestSnapshotMap = dbClient.snapshotDao().selectLastAnalysesByRootComponentUuids(dbSession, branchUuids) .stream() @@ -488,22 +494,18 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader { .collect(toMap(ProjectQgateAssociationDto::getUuid, p -> Optional.ofNullable(p.getGateUuid()).orElse(""))); } - private Map<String, Map<String, Number>> getProjectMetricsByMetricKeys(DbSession dbSession, String... metricKeys) { - Map<String, String> metricNamesByUuid = dbClient.metricDao().selectByKeys(dbSession, asList(metricKeys)) - .stream() - .collect(toMap(MetricDto::getUuid, MetricDto::getKey)); + private Map<String, Map<String, Number>> getProjectMetricsByMetricKeys(DbSession dbSession, List<String> metricKeys) { + Map<String, Map<String, Number>> measuresByProject = new HashMap<>(); + List<ProjectMainBranchMeasureDto> projectMainBranchMeasureDtos = dbClient.measureDao().selectAllForProjectMainBranches(dbSession); - // metrics can be empty for un-analyzed projects - if (metricNamesByUuid.isEmpty()) { - return Collections.emptyMap(); + for (ProjectMainBranchMeasureDto projectMainBranchMeasureDto : projectMainBranchMeasureDtos) { + Map<String, Number> measures = projectMainBranchMeasureDto.getMetricValues().entrySet().stream() + .filter(e -> metricKeys.contains(e.getKey())) + .collect(toMap(Map.Entry::getKey, e -> Double.parseDouble(e.getValue().toString()))); + measuresByProject.put(projectMainBranchMeasureDto.getProjectUuid(), measures); } - return dbClient.liveMeasureDao().selectForProjectMainBranchesByMetricUuids(dbSession, metricNamesByUuid.keySet()) - .stream() - .collect(groupingBy(ProjectMainBranchLiveMeasureDto::getProjectUuid, - toMap(lmDto -> metricNamesByUuid.get(lmDto.getMetricUuid()), - lmDto -> Optional.ofNullable(lmDto.getValue()).orElseGet(() -> Double.valueOf(lmDto.getTextValue())), - (oldValue, newValue) -> newValue, HashMap::new))); + return measuresByProject; } private static boolean checkIfCloudAlm(String almRaw, String alm, String url, String cloudUrl) { |