aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-pushapi
diff options
context:
space:
mode:
authorEric Giffon <eric.giffon@sonarsource.com>2024-09-24 13:48:03 +0200
committersonartech <sonartech@sonarsource.com>2024-10-09 20:02:46 +0000
commitf32f5bdeb0d2105eab86d71e1cb75e1fd6e8331b (patch)
tree903a199311ccb874d78c7b5948ab0429baaa9ec3 /server/sonar-webserver-pushapi
parentc94654fc488cdd8d8f875631f0534d75d36630ab (diff)
downloadsonarqube-f32f5bdeb0d2105eab86d71e1cb75e1fd6e8331b.tar.gz
sonarqube-f32f5bdeb0d2105eab86d71e1cb75e1fd6e8331b.zip
SONAR-22873 Remove project queries using joins on live_measures
Diffstat (limited to 'server/sonar-webserver-pushapi')
-rw-r--r--server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/qualityprofile/QualityProfileChangeEventServiceImpl.java18
-rw-r--r--server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/qualityprofile/QualityProfileChangeEventServiceImplTest.java30
2 files changed, 18 insertions, 30 deletions
diff --git a/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/qualityprofile/QualityProfileChangeEventServiceImpl.java b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/qualityprofile/QualityProfileChangeEventServiceImpl.java
index baf693c63be..ddffddaab99 100644
--- a/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/qualityprofile/QualityProfileChangeEventServiceImpl.java
+++ b/server/sonar-webserver-pushapi/src/main/java/org/sonar/server/pushapi/qualityprofile/QualityProfileChangeEventServiceImpl.java
@@ -35,6 +35,7 @@ import java.util.stream.Stream;
import javax.annotation.Nullable;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
+import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.ServerSide;
import org.sonar.core.util.ParamChange;
@@ -42,6 +43,7 @@ import org.sonar.core.util.rule.RuleChange;
import org.sonar.core.util.rule.RuleSetChangedEvent;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.measure.ProjectMainBranchMeasureDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.db.pushevent.PushEventDto;
import org.sonar.db.qualityprofile.ActiveRuleDto;
@@ -271,10 +273,24 @@ public class QualityProfileChangeEventServiceImpl implements QualityProfileChang
}
private List<ProjectDto> getDefaultQualityProfileAssociatedProjects(DbSession dbSession, String language) {
- Set<String> associatedProjectUuids = dbClient.projectDao().selectProjectUuidsAssociatedToDefaultQualityProfileByLanguage(dbSession, language);
+ Set<String> associatedProjectUuids = new HashSet<>();
+
+ List<ProjectMainBranchMeasureDto> measureDtos =
+ dbClient.measureDao().selectAllForProjectMainBranchesAssociatedToDefaultQualityProfile(dbSession);
+ for (ProjectMainBranchMeasureDto measureDto : measureDtos) {
+ String distribution = (String) measureDto.getMetricValues().get(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION_KEY);
+ if (distribution != null && distributionContainsLanguage(distribution, language)) {
+ associatedProjectUuids.add(measureDto.getProjectUuid());
+ }
+ }
+
return dbClient.projectDao().selectByUuids(dbSession, associatedProjectUuids);
}
+ private static boolean distributionContainsLanguage(String distribution, String language) {
+ return distribution.startsWith(language + "=") || distribution.contains(";" + language + "=");
+ }
+
private List<ProjectDto> getManuallyAssociatedQualityProfileProjects(DbSession dbSession, List<QProfileDto> profiles) {
return profiles
.stream()
diff --git a/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/qualityprofile/QualityProfileChangeEventServiceImplTest.java b/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/qualityprofile/QualityProfileChangeEventServiceImplTest.java
index cea9b9855cb..8d629900c8f 100644
--- a/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/qualityprofile/QualityProfileChangeEventServiceImplTest.java
+++ b/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/qualityprofile/QualityProfileChangeEventServiceImplTest.java
@@ -20,12 +20,10 @@
package org.sonar.server.pushapi.qualityprofile;
import java.nio.charset.StandardCharsets;
-import java.security.SecureRandom;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
-import java.util.Random;
import java.util.Set;
import java.util.function.Consumer;
import org.junit.Rule;
@@ -34,8 +32,6 @@ import org.sonar.api.rule.RuleKey;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.ProjectData;
-import org.sonar.db.measure.LiveMeasureDto;
-import org.sonar.db.metric.MetricDto;
import org.sonar.db.project.ProjectDto;
import org.sonar.db.pushevent.PushEventDto;
import org.sonar.db.qualityprofile.ActiveRuleDto;
@@ -51,15 +47,12 @@ import static java.util.List.of;
import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.measures.CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION_KEY;
-import static org.sonar.api.measures.Metric.ValueType.STRING;
import static org.sonar.db.rule.RuleTesting.newCustomRule;
import static org.sonar.db.rule.RuleTesting.newTemplateRule;
import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED;
public class QualityProfileChangeEventServiceImplTest {
- private final Random random = new SecureRandom();
-
@Rule
public DbTester db = DbTester.create();
@@ -114,7 +107,7 @@ public class QualityProfileChangeEventServiceImplTest {
QProfileDto defaultQualityProfile = insertDefaultQualityProfile(language);
RuleDto rule = insertCustomRule(templateRule, language, "<div>line1\nline2</div>");
ActiveRuleChange activeRuleChange = changeActiveRule(defaultQualityProfile, rule, "paramChangeKey", "paramChangeValue");
- insertQualityProfileLiveMeasure(mainBranch, projectData.getProjectDto(), language, NCLOC_LANGUAGE_DISTRIBUTION_KEY);
+ db.measures().insertMeasure(mainBranch, m -> m.addValue(NCLOC_LANGUAGE_DISTRIBUTION_KEY, language + "=100"));
db.getSession().commit();
@@ -221,25 +214,4 @@ public class QualityProfileChangeEventServiceImplTest {
"\"params\":[{\"key\":\"" + activeRuleParam1.getKey() + "\",\"value\":\"" + activeRuleParam1.getValue() + "\"}]}]," +
"\"deactivatedRules\":[\"repo2:ruleKey2\"]");
}
-
- private void insertQualityProfileLiveMeasure(ComponentDto branch, ProjectDto projectDto, String language, String metricKey) {
- MetricDto metric = insertMetric(metricKey);
-
- Consumer<LiveMeasureDto> configureLiveMeasure = liveMeasure -> liveMeasure
- .setMetricUuid(metric.getUuid())
- .setComponentUuid(branch.uuid())
- .setProjectUuid(projectDto.getUuid())
- .setData(language + "=" + random.nextInt(10));
-
- db.measures().insertLiveMeasure(branch, metric, configureLiveMeasure);
- }
-
- private MetricDto insertMetric(String metricKey) {
- Consumer<MetricDto> configureMetric = metric -> metric
- .setUuid("uuid")
- .setValueType(STRING.name())
- .setKey(metricKey);
-
- return db.measures().insertMetric(configureMetric);
- }
}