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.platform.telemetry;
22 import java.util.HashMap;
23 import java.util.List;
26 import org.sonar.api.measures.CoreMetrics;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.measure.ProjectMainBranchMeasureDto;
30 import org.sonar.db.property.PropertyDto;
31 import org.sonar.db.property.PropertyQuery;
32 import org.sonar.telemetry.core.Dimension;
33 import org.sonar.telemetry.core.Granularity;
34 import org.sonar.telemetry.core.TelemetryDataProvider;
35 import org.sonar.telemetry.core.TelemetryDataType;
37 public class ProjectCppAutoconfigTelemetryProvider implements TelemetryDataProvider<String> {
39 private final DbClient dbClient;
41 public ProjectCppAutoconfigTelemetryProvider(DbClient dbClient) {
42 this.dbClient = dbClient;
46 public String getMetricKey() {
47 return "project_cpp_config_type";
51 public Dimension getDimension() {
52 return Dimension.PROJECT;
56 public Granularity getGranularity() {
57 return Granularity.WEEKLY;
61 public TelemetryDataType getType() {
62 return TelemetryDataType.STRING;
66 public Map<String, String> getValues() {
67 Map<String, String> cppConfigTypePerProjectUuid = new HashMap<>();
68 try (DbSession dbSession = dbClient.openSession(true)) {
69 List<ProjectMainBranchMeasureDto> measureDtos = dbClient.measureDao().selectAllForProjectMainBranches(dbSession);
70 for (ProjectMainBranchMeasureDto measureDto : measureDtos) {
71 String distribution = (String) measureDto.getMetricValues().get(CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION_KEY);
72 if (distribution != null && Set.of("cpp", "c").stream().anyMatch(language -> distributionContainsLanguage(distribution, language))) {
73 CppConfigType cppConfigType = getCppConfigType(measureDto.getProjectUuid(), dbSession);
74 cppConfigTypePerProjectUuid.put(measureDto.getProjectUuid(), cppConfigType.name());
78 return cppConfigTypePerProjectUuid;
81 private static boolean distributionContainsLanguage(String distribution, String language) {
82 return distribution.startsWith(language + "=") || distribution.contains(";" + language + "=");
85 private CppConfigType getCppConfigType(String entityUuid, DbSession dbSession) {
86 List<PropertyDto> propertyDtos = dbClient.propertiesDao().selectByQuery(PropertyQuery
88 .setEntityUuid(entityUuid)
90 for (PropertyDto propertyDto : propertyDtos) {
91 if (propertyDto.getKey().equals("sonar.cfamily.build-wrapper-output")) {
92 return CppConfigType.BW_DEPRECATED;
94 if (propertyDto.getKey().equals("sonar.cfamily.compile-commands")) {
95 return CppConfigType.COMPDB;
98 return CppConfigType.AUTOCONFIG;
102 BW_DEPRECATED, COMPDB, AUTOCONFIG