3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.telemetry;
22 import java.util.List;
24 import java.util.Optional;
26 import java.util.stream.Collectors;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.qualityprofile.QProfileDto;
30 import org.sonar.server.qualityprofile.QProfileComparison;
32 import static java.util.stream.Collectors.toMap;
34 public class QualityProfileDataProvider {
36 private final DbClient dbClient;
37 private final QProfileComparison qProfileComparison;
39 public QualityProfileDataProvider(DbClient dbClient, QProfileComparison qProfileComparison) {
40 this.dbClient = dbClient;
41 this.qProfileComparison = qProfileComparison;
44 public List<TelemetryData.QualityProfile> retrieveQualityProfilesData() {
45 try (DbSession dbSession = dbClient.openSession(false)) {
47 Set<String> defaultProfileUuids = dbClient.qualityProfileDao().selectAllDefaultProfiles(dbSession)
48 .stream().map(QProfileDto::getKee)
49 .collect(Collectors.toSet());
51 Map<String, QProfileDto> allProfileDtosByUuid = dbClient.qualityProfileDao().selectAll(dbSession)
53 .collect(toMap(QProfileDto::getKee, p -> p));
55 return allProfileDtosByUuid.entrySet().stream()
56 .map(p -> mapQualityProfile(p.getValue(), allProfileDtosByUuid, defaultProfileUuids.contains(p.getKey()), dbSession))
61 private TelemetryData.QualityProfile mapQualityProfile(QProfileDto profile, Map<String, QProfileDto> allProfileDtos, boolean isDefault, DbSession dbSession) {
62 QProfileDto rootProfile = getRootProfile(profile.getKee(), allProfileDtos);
63 Boolean isBuiltInRootParent;
64 if (profile.isBuiltIn()) {
65 isBuiltInRootParent = null;
67 isBuiltInRootParent = rootProfile.isBuiltIn() && !rootProfile.getKee().equals(profile.getKee());
70 Optional<QProfileComparison.QProfileComparisonResult> rulesComparison = Optional.of(profile)
71 .filter(p -> isBuiltInRootParent != null && isBuiltInRootParent)
72 .map(p -> qProfileComparison.compare(dbSession, rootProfile, profile));
74 return new TelemetryData.QualityProfile(profile.getKee(),
75 profile.getParentKee(),
76 profile.getLanguage(),
80 rulesComparison.map(c -> c.modified().size()).orElse(null),
81 rulesComparison.map(c -> c.inRight().size()).orElse(null),
82 rulesComparison.map(c -> c.inLeft().size()).orElse(null)
86 public QProfileDto getRootProfile(String kee, Map<String, QProfileDto> allProfileDtos) {
87 QProfileDto qProfileDto = allProfileDtos.get(kee);
88 String parentKee = qProfileDto.getParentKee();
89 if (parentKee != null) {
90 return getRootProfile(parentKee, allProfileDtos);
92 return allProfileDtos.get(kee);