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.telemetry.project;
22 import java.util.HashMap;
23 import java.util.List;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.project.ProjectDto;
29 import org.sonar.db.property.PropertyDto;
30 import org.sonar.db.property.PropertyQuery;
31 import org.sonar.telemetry.Dimension;
32 import org.sonar.telemetry.Granularity;
33 import org.sonar.telemetry.TelemetryDataType;
34 import org.sonar.telemetry.TelemetryDataProvider;
36 public class ProjectCppAutoconfigTelemetryProvider implements TelemetryDataProvider<String> {
38 private final DbClient dbClient;
40 public ProjectCppAutoconfigTelemetryProvider(DbClient dbClient) {
41 this.dbClient = dbClient;
45 public String getMetricKey() {
46 return "project_cpp_config_type";
50 public Dimension getDimension() {
51 return Dimension.PROJECT;
55 public Granularity getGranularity() {
56 return Granularity.WEEKLY;
60 public TelemetryDataType getType() {
61 return TelemetryDataType.STRING;
65 public Map<String, String> getUuidValues() {
66 Map<String, String> cppConfigTypePerProjectUuid = new HashMap<>();
67 try (DbSession dbSession = dbClient.openSession(true)) {
68 //TODO in the feature ideally languages should be defined in the codebase as enums, using strings is error-prone
69 List<ProjectDto> cppProjects = dbClient.projectDao().selectProjectsByLanguage(dbSession, Set.of("cpp", "c"));
70 for (ProjectDto cppProject : cppProjects) {
71 CppConfigType cppConfigType = getCppConfigType(cppProject, dbSession);
72 cppConfigTypePerProjectUuid.put(cppProject.getUuid(), cppConfigType.name());
75 return cppConfigTypePerProjectUuid;
78 private CppConfigType getCppConfigType(ProjectDto project, DbSession dbSession) {
79 List<PropertyDto> propertyDtos = dbClient.propertiesDao().selectByQuery(PropertyQuery
81 .setEntityUuid(project.getUuid())
83 for (PropertyDto propertyDto : propertyDtos) {
84 if (propertyDto.getKey().equals("sonar.cfamily.build-wrapper-output")) {
85 return CppConfigType.BW_DEPRECATED;
87 if (propertyDto.getKey().equals("sonar.cfamily.compile-commands")) {
88 return CppConfigType.COMPDB;
91 return CppConfigType.AUTOCONFIG;
95 BW_DEPRECATED, COMPDB, AUTOCONFIG