From 6bf45ac19fcf831e7bb3cbfb7975c09f109f7647 Mon Sep 17 00:00:00 2001 From: Zipeng WU Date: Mon, 5 Jun 2023 15:39:42 +0200 Subject: [PATCH] SONAR-19483 Collect daily counts of analysis and green Quality Gate in Telemetry for branches Co-authored-by: Zipeng WU Co-authored-by: Nolwenn Cadic --- .../org/sonar/db/component/BranchDaoIT.java | 37 +++++++++-- .../org/sonar/db/component/BranchDao.java | 7 ++- .../org/sonar/db/component/BranchMapper.java | 3 +- .../sonar/db/component/BranchMeasuresDto.java | 63 +++++++++++++++++++ .../org/sonar/db/component/BranchMapper.xml | 24 ++++++- .../sonar/server/telemetry/TelemetryData.java | 2 +- .../telemetry/TelemetryDataJsonWriter.java | 3 + .../TelemetryDataJsonWriterTest.java | 28 +++++---- .../telemetry/TelemetryDataLoaderImpl.java | 23 ++++--- .../TelemetryDataLoaderImplTest.java | 48 +++++++++++++- 10 files changed, 202 insertions(+), 36 deletions(-) create mode 100644 server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMeasuresDto.java diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/component/BranchDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/component/BranchDaoIT.java index b0bb9df6e60..4a6859125c2 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/component/BranchDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/component/BranchDaoIT.java @@ -51,6 +51,7 @@ import static org.apache.commons.lang.StringUtils.repeat; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.entry; import static org.assertj.core.api.Assertions.tuple; +import static org.sonar.api.measures.CoreMetrics.ALERT_STATUS_KEY; import static org.sonar.db.component.BranchType.BRANCH; import static org.sonar.db.component.BranchType.PULL_REQUEST; @@ -120,6 +121,30 @@ public class BranchDaoIT { assertThat(loaded.isMain()).isTrue(); } + @Test + public void selectBranchMeasuresForTelemetry() { + BranchDto dto = new BranchDto(); + dto.setProjectUuid("U1"); + dto.setUuid("U1"); + dto.setBranchType(BranchType.BRANCH); + dto.setKey("feature"); + dto.setIsMain(true); + dto.setExcludeFromPurge(false); + underTest.insert(dbSession, dto); + + MetricDto qg = db.measures().insertMetric(m -> m.setKey(ALERT_STATUS_KEY)); + SnapshotDto analysis = db.components().insertSnapshot(dto); + db.measures().insertMeasure(dto, analysis, qg, pm -> pm.setData("OK")); + + var branchMeasures = underTest.selectBranchMeasuresWithCaycMetric(dbSession); + + assertThat(branchMeasures) + .hasSize(1) + .extracting(BranchMeasuresDto::getBranchUuid, BranchMeasuresDto::getBranchKey, BranchMeasuresDto::getProjectUuid, + BranchMeasuresDto::getAnalysisCount, BranchMeasuresDto::getGreenQualityGateCount, BranchMeasuresDto::getExcludeFromPurge) + .containsExactly(tuple("U1", "feature", "U1", 1, 1, false)); + } + @Test public void updateExcludeFromPurge() { BranchDto dto = new BranchDto(); @@ -139,7 +164,7 @@ public class BranchDaoIT { @DataProvider public static Object[][] nullOrEmpty() { - return new Object[][]{ + return new Object[][] { {null}, {""} }; @@ -149,7 +174,7 @@ public class BranchDaoIT { public static Object[][] oldAndNewValuesCombinations() { String value1 = randomAlphabetic(10); String value2 = randomAlphabetic(20); - return new Object[][]{ + return new Object[][] { {null, value1}, {"", value1}, {value1, null}, @@ -454,7 +479,8 @@ public class BranchDaoIT { assertThat(branches).extracting(BranchDto::getUuid, BranchDto::getKey, BranchDto::isMain, BranchDto::getProjectUuid, BranchDto::getBranchType, BranchDto::getMergeBranchUuid) .containsOnly(tuple(mainBranch.getUuid(), mainBranch.getKey(), mainBranch.isMain(), mainBranch.getProjectUuid(), mainBranch.getBranchType(), mainBranch.getMergeBranchUuid()), - tuple(featureBranch.getUuid(), featureBranch.getKey(), featureBranch.isMain(), featureBranch.getProjectUuid(), featureBranch.getBranchType(), featureBranch.getMergeBranchUuid())); + tuple(featureBranch.getUuid(), featureBranch.getKey(), featureBranch.isMain(), featureBranch.getProjectUuid(), featureBranch.getBranchType(), + featureBranch.getMergeBranchUuid())); } @Test @@ -597,7 +623,8 @@ public class BranchDaoIT { db.measures().insertLiveMeasure(project3, unanalyzedC); assertThat(underTest.countPrBranchAnalyzedLanguageByProjectUuid(db.getSession())) - .extracting(PrBranchAnalyzedLanguageCountByProjectDto::getProjectUuid, PrBranchAnalyzedLanguageCountByProjectDto::getBranch, PrBranchAnalyzedLanguageCountByProjectDto::getPullRequest) + .extracting(PrBranchAnalyzedLanguageCountByProjectDto::getProjectUuid, PrBranchAnalyzedLanguageCountByProjectDto::getBranch, + PrBranchAnalyzedLanguageCountByProjectDto::getPullRequest) .containsExactlyInAnyOrder( tuple(project1.uuid(), 3L, 3L), tuple(project2.uuid(), 1L, 1L), @@ -805,7 +832,7 @@ public class BranchDaoIT { @DataProvider public static Object[][] booleanValues() { - return new Object[][]{ + return new Object[][] { {true}, {false} }; diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java index 609944cdb6d..6a639579905 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchDao.java @@ -19,6 +19,8 @@ */ package org.sonar.db.component; +import java.time.ZoneId; +import java.time.ZonedDateTime; import java.util.Collection; import java.util.LinkedList; import java.util.List; @@ -199,7 +201,8 @@ public class BranchDao implements Dao { .orElse(false); } - public List selectAllBranches(DbSession dbSession) { - return mapper(dbSession).selectAllBranches(); + public List selectBranchMeasuresWithCaycMetric(DbSession dbSession) { + long yesterday = ZonedDateTime.now(ZoneId.systemDefault()).minusDays(1).toInstant().toEpochMilli(); + return mapper(dbSession).selectBranchMeasuresWithCaycMetric(yesterday); } } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java index 86489d34b07..4aa4b32281f 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMapper.java @@ -23,7 +23,6 @@ import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Optional; -import java.util.Set; import org.apache.ibatis.annotations.Param; public interface BranchMapper { @@ -77,5 +76,5 @@ public interface BranchMapper { List selectMainBranchesByProjectUuids(@Param("projectUuids") Collection projectUuids); - List selectAllBranches(); + List selectBranchMeasuresWithCaycMetric(long yesterday); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMeasuresDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMeasuresDto.java new file mode 100644 index 00000000000..f5d19e1362e --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/component/BranchMeasuresDto.java @@ -0,0 +1,63 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 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.db.component; + +public class BranchMeasuresDto { + private String branchUuid; + private String projectUuid; + private String branchKey; + private boolean excludeFromPurge; + private int greenQualityGateCount; + private int analysisCount; + + public BranchMeasuresDto(String branchUuid, String projectUuid, String branchKey, boolean excludeFromPurge, int greenQualityGateCount, int analysisCount) { + this.branchUuid = branchUuid; + this.projectUuid = projectUuid; + this.branchKey = branchKey; + this.excludeFromPurge = excludeFromPurge; + this.greenQualityGateCount = greenQualityGateCount; + this.analysisCount = analysisCount; + } + + public String getBranchUuid() { + return branchUuid; + } + + public String getProjectUuid() { + return projectUuid; + } + + public boolean getExcludeFromPurge() { + return excludeFromPurge; + } + + public int getGreenQualityGateCount() { + return greenQualityGateCount; + } + + public int getAnalysisCount() { + return analysisCount; + } + + public String getBranchKey() { + return branchKey; + } + +} diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml index 140e3ecfd2c..d38e6645f53 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/component/BranchMapper.xml @@ -14,6 +14,15 @@ pb.is_main as isMain + + pb.uuid as branchUuid, + pb.project_uuid as projectUuid, + pb.kee as branchKey, + pb.exclude_from_purge as excludeFromPurge, + coalesce(ag.greenQualityGateCount, 0) as greenQualityGateCount, + coalesce(ag.analysisCount, 0) as analysisCount + + insert into project_branches ( uuid, @@ -100,11 +109,20 @@ - select - + from project_branches pb - where branch_type='BRANCH' + left join ( + select pm.component_uuid as branchUuid, count(case when pm.text_value ='OK' then 1 end) as greenQualityGateCount, count(1) as analysisCount + from project_measures pm + inner join metrics m on m.uuid = pm.metric_uuid + inner join snapshots s on s.uuid = pm.analysis_uuid + where m.name = 'alert_status' and s.created_at >= #{yesterday, jdbcType=BIGINT} + group by pm.component_uuid + ) ag + on ag.branchUuid = pb.uuid + where pb.branch_type='BRANCH'