aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-core/src/main/java
diff options
context:
space:
mode:
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>2023-09-08 15:31:08 +0200
committersonartech <sonartech@sonarsource.com>2023-09-14 20:02:39 +0000
commit1bab5b5239cb35892d20cde1de1110d6cce30340 (patch)
tree2002203322bd2286ccb96def0544c8b46a8685af /server/sonar-webserver-core/src/main/java
parented12d6b1053cfe1971bf9735f6d087dee94732f9 (diff)
downloadsonarqube-1bab5b5239cb35892d20cde1de1110d6cce30340.tar.gz
sonarqube-1bab5b5239cb35892d20cde1de1110d6cce30340.zip
SONAR-20359 Add quality profiles to telemetry data
Diffstat (limited to 'server/sonar-webserver-core/src/main/java')
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/QualityProfileDataProvider.java95
-rw-r--r--server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java8
2 files changed, 102 insertions, 1 deletions
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/QualityProfileDataProvider.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/QualityProfileDataProvider.java
new file mode 100644
index 00000000000..040c3a5d97c
--- /dev/null
+++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/QualityProfileDataProvider.java
@@ -0,0 +1,95 @@
+/*
+ * 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.server.telemetry;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.qualityprofile.QProfileDto;
+import org.sonar.server.qualityprofile.QProfileComparison;
+
+import static java.util.stream.Collectors.toMap;
+
+public class QualityProfileDataProvider {
+
+ private final DbClient dbClient;
+ private final QProfileComparison qProfileComparison;
+
+ public QualityProfileDataProvider(DbClient dbClient, QProfileComparison qProfileComparison) {
+ this.dbClient = dbClient;
+ this.qProfileComparison = qProfileComparison;
+ }
+
+ public List<TelemetryData.QualityProfile> retrieveQualityProfilesData() {
+ try (DbSession dbSession = dbClient.openSession(false)) {
+
+ Set<String> defaultProfileUuids = dbClient.qualityProfileDao().selectAllDefaultProfiles(dbSession)
+ .stream().map(QProfileDto::getKee)
+ .collect(Collectors.toSet());
+
+ Map<String, QProfileDto> allProfileDtosByUuid = dbClient.qualityProfileDao().selectAll(dbSession)
+ .stream()
+ .collect(toMap(QProfileDto::getKee, p -> p));
+
+ return allProfileDtosByUuid.entrySet().stream()
+ .map(p -> mapQualityProfile(p.getValue(), allProfileDtosByUuid, defaultProfileUuids.contains(p.getKey()), dbSession))
+ .toList();
+ }
+ }
+
+ private TelemetryData.QualityProfile mapQualityProfile(QProfileDto profile, Map<String, QProfileDto> allProfileDtos, boolean isDefault, DbSession dbSession) {
+ QProfileDto rootProfile = getRootProfile(profile.getKee(), allProfileDtos);
+ Boolean isBuiltInRootParent;
+ if (profile.isBuiltIn()) {
+ isBuiltInRootParent = null;
+ } else {
+ isBuiltInRootParent = rootProfile.isBuiltIn() && !rootProfile.getKee().equals(profile.getKee());
+ }
+
+ Optional<QProfileComparison.QProfileComparisonResult> rulesComparison = Optional.of(profile)
+ .filter(p -> isBuiltInRootParent != null && isBuiltInRootParent)
+ .map(p -> qProfileComparison.compare(dbSession, rootProfile, profile));
+
+ return new TelemetryData.QualityProfile(profile.getKee(),
+ profile.getParentKee(),
+ profile.getLanguage(),
+ isDefault,
+ profile.isBuiltIn(),
+ isBuiltInRootParent,
+ rulesComparison.map(c -> c.modified().size()).orElse(null),
+ rulesComparison.map(c -> c.inRight().size()).orElse(null),
+ rulesComparison.map(c -> c.inLeft().size()).orElse(null)
+ );
+ }
+
+ public QProfileDto getRootProfile(String kee, Map<String, QProfileDto> allProfileDtos) {
+ QProfileDto qProfileDto = allProfileDtos.get(kee);
+ String parentKee = qProfileDto.getParentKee();
+ if (parentKee != null) {
+ return getRootProfile(parentKee, allProfileDtos);
+ } else {
+ return allProfileDtos.get(kee);
+ }
+ }
+}
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java
index 27009b23682..6382bef3174 100644
--- a/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java
+++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/telemetry/TelemetryDataLoaderImpl.java
@@ -112,6 +112,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader {
private final QualityGateFinder qualityGateFinder;
private final ManagedInstanceService managedInstanceService;
private final CloudUsageDataProvider cloudUsageDataProvider;
+ private final QualityProfileDataProvider qualityProfileDataProvider;
private final Set<NewCodeDefinition> newCodeDefinitions = new HashSet<>();
private final Map<String, NewCodeDefinition> ncdByProject = new HashMap<>();
private final Map<String, NewCodeDefinition> ncdByBranch = new HashMap<>();
@@ -121,7 +122,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader {
public TelemetryDataLoaderImpl(Server server, DbClient dbClient, PluginRepository pluginRepository,
PlatformEditionProvider editionProvider, InternalProperties internalProperties, Configuration configuration,
ContainerSupport containerSupport, QualityGateCaycChecker qualityGateCaycChecker, QualityGateFinder qualityGateFinder,
- ManagedInstanceService managedInstanceService, CloudUsageDataProvider cloudUsageDataProvider) {
+ ManagedInstanceService managedInstanceService, CloudUsageDataProvider cloudUsageDataProvider, QualityProfileDataProvider qualityProfileDataProvider) {
this.server = server;
this.dbClient = dbClient;
this.pluginRepository = pluginRepository;
@@ -133,6 +134,7 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader {
this.qualityGateFinder = qualityGateFinder;
this.managedInstanceService = managedInstanceService;
this.cloudUsageDataProvider = cloudUsageDataProvider;
+ this.qualityProfileDataProvider = qualityProfileDataProvider;
}
private static Database loadDatabaseMetadata(DbSession dbSession) {
@@ -174,6 +176,8 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader {
resolveUsers(data, dbSession);
}
+ data.setQualityProfiles(qualityProfileDataProvider.retrieveQualityProfilesData());
+
setSecurityCustomConfigIfPresent(data);
Optional<String> installationDateProperty = internalProperties.read(InternalProperties.INSTALLATION_DATE);
@@ -365,6 +369,8 @@ public class TelemetryDataLoaderImpl implements TelemetryDataLoader {
data.setQualityGates(qualityGates);
}
+
+
private void resolveUsers(TelemetryData.Builder data, DbSession dbSession) {
data.setUsers(dbClient.userDao().selectUsersForTelemetry(dbSession));
}