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.telemetry.core.Dimension;
30 import org.sonar.telemetry.core.Granularity;
31 import org.sonar.telemetry.core.TelemetryDataProvider;
32 import org.sonar.telemetry.core.TelemetryDataType;
33 import org.sonar.telemetry.legacy.ProjectLocDistributionDataProvider;
35 public class TelemetryNclocProvider implements TelemetryDataProvider<Long> {
37 public static final String METRIC_KEY = "ncloc_per_language";
39 private final DbClient dbClient;
40 private final ProjectLocDistributionDataProvider projectLocDistributionDataProvider;
42 public TelemetryNclocProvider(DbClient dbClient, ProjectLocDistributionDataProvider projectLocDistributionDataProvider) {
43 this.dbClient = dbClient;
44 this.projectLocDistributionDataProvider = projectLocDistributionDataProvider;
48 public String getMetricKey() {
53 public Dimension getDimension() {
54 return Dimension.LANGUAGE;
58 public Granularity getGranularity() {
59 return Granularity.DAILY;
63 public TelemetryDataType getType() {
64 return TelemetryDataType.INTEGER;
68 public Map<String, Long> getValues() {
69 try (DbSession dbSession = dbClient.openSession(false)) {
70 return getNclocDistribution(dbSession);
74 private Map<String, Long> getNclocDistribution(DbSession dbSession) {
75 List<ProjectLocDistributionDto> branchesWithLargestNcloc = projectLocDistributionDataProvider.getProjectLocDistribution(dbSession);
76 List<LanguageDistribution> languageDistributions = getLanguageDistributionList(branchesWithLargestNcloc);
77 return getNclocDistributionPerLanguage(languageDistributions);
80 private static List<LanguageDistribution> getLanguageDistributionList(List<ProjectLocDistributionDto> branchesWithLargestNcloc) {
81 return branchesWithLargestNcloc.stream()
82 .flatMap(measure -> Arrays.stream(measure.locDistribution().split(";"))
83 .map(languageAndLoc -> languageAndLoc.split("="))
84 .map(languageAndLoc -> new LanguageDistribution(
86 Long.parseLong(languageAndLoc[1]))))
90 private static Map<String, Long> getNclocDistributionPerLanguage(List<LanguageDistribution> languageDistributions) {
91 // a Map<String, Integer> that contains the sum of ncloc per language
92 Map<String, Long> nclocPerLanguage = new HashMap<>();
93 languageDistributions.forEach(languageDistribution -> nclocPerLanguage.merge(languageDistribution.language, languageDistribution.ncloc, Long::sum));
94 return nclocPerLanguage;
97 private record LanguageDistribution(String language, long ncloc) {