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.Arrays;
23 import java.util.HashMap;
24 import java.util.List;
26 import org.sonar.db.DbClient;
27 import org.sonar.db.DbSession;
28 import org.sonar.db.measure.ProjectLocDistributionDto;
29 import org.sonar.db.metric.MetricDto;
30 import org.sonar.telemetry.core.Dimension;
31 import org.sonar.telemetry.core.Granularity;
32 import org.sonar.telemetry.core.TelemetryDataProvider;
33 import org.sonar.telemetry.core.TelemetryDataType;
35 import static java.util.Arrays.asList;
36 import static java.util.stream.Collectors.toMap;
37 import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
38 import static org.sonar.api.measures.CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION_KEY;
40 public class TelemetryNclocProvider implements TelemetryDataProvider<Long> {
42 public static final String METRIC_KEY = "ncloc_per_language";
44 private final DbClient dbClient;
46 public TelemetryNclocProvider(DbClient dbClient) {
47 this.dbClient = dbClient;
51 public String getMetricKey() {
56 public Dimension getDimension() {
57 return Dimension.LANGUAGE;
61 public Granularity getGranularity() {
62 return Granularity.DAILY;
66 public TelemetryDataType getType() {
67 return TelemetryDataType.INTEGER;
71 public Map<String, Long> getUuidValues() {
72 try (DbSession dbSession = dbClient.openSession(false)) {
73 return getNclocDistribution(dbSession);
77 private Map<String, Long> getNclocDistribution(DbSession dbSession) {
78 Map<String, String> metricUuidMap = getNclocMetricUuidMap(dbSession);
79 String nclocUuid = metricUuidMap.get(NCLOC_KEY);
80 String nclocDistributionUuid = metricUuidMap.get(NCLOC_LANGUAGE_DISTRIBUTION_KEY);
81 List<ProjectLocDistributionDto> branchesWithLargestNcloc = dbClient.liveMeasureDao().selectLargestBranchesLocDistribution(dbSession, nclocUuid, nclocDistributionUuid);
82 List<LanguageDistribution> languageDistributions = getLanguageDistributionList(branchesWithLargestNcloc);
83 return getNclocDistributionPerLanguage(languageDistributions);
86 private Map<String, String> getNclocMetricUuidMap(DbSession dbSession) {
87 return dbClient.metricDao().selectByKeys(dbSession, asList(NCLOC_KEY, NCLOC_LANGUAGE_DISTRIBUTION_KEY))
89 .collect(toMap(MetricDto::getKey, MetricDto::getUuid));
92 private static List<LanguageDistribution> getLanguageDistributionList(List<ProjectLocDistributionDto> branchesWithLargestNcloc) {
93 return branchesWithLargestNcloc.stream()
94 .flatMap(measure -> Arrays.stream(measure.locDistribution().split(";"))
95 .map(languageAndLoc -> languageAndLoc.split("="))
96 .map(languageAndLoc -> new LanguageDistribution(
98 Long.parseLong(languageAndLoc[1]))))
102 private static Map<String, Long> getNclocDistributionPerLanguage(List<LanguageDistribution> languageDistributions) {
103 // a Map<String, Integer> that contains the sum of ncloc per language
104 Map<String, Long> nclocPerLanguage = new HashMap<>();
105 languageDistributions.forEach(languageDistribution -> nclocPerLanguage.merge(languageDistribution.language, languageDistribution.ncloc, Long::sum));
106 return nclocPerLanguage;
109 private record LanguageDistribution(String language, long ncloc) {