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.ce.task.projectanalysis.step;
22 import java.util.ArrayList;
23 import java.util.List;
25 import java.util.Optional;
27 import java.util.function.Predicate;
28 import javax.annotation.Nonnull;
29 import org.sonar.ce.task.projectanalysis.component.Component;
30 import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
31 import org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
32 import org.sonar.ce.task.projectanalysis.component.FileStatuses;
33 import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
34 import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
35 import org.sonar.ce.task.projectanalysis.measure.BestValueOptimization;
36 import org.sonar.ce.task.projectanalysis.measure.Measure;
37 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
38 import org.sonar.ce.task.projectanalysis.measure.MeasureToMeasureDto;
39 import org.sonar.ce.task.projectanalysis.metric.Metric;
40 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
41 import org.sonar.ce.task.step.ComputationStep;
42 import org.sonar.db.DbClient;
43 import org.sonar.db.DbSession;
44 import org.sonar.db.measure.LiveMeasureDto;
46 import static org.sonar.api.measures.CoreMetrics.ACCEPTED_ISSUES_KEY;
47 import static org.sonar.api.measures.CoreMetrics.BLOCKER_VIOLATIONS_KEY;
48 import static org.sonar.api.measures.CoreMetrics.BUGS_KEY;
49 import static org.sonar.api.measures.CoreMetrics.CLASSES_KEY;
50 import static org.sonar.api.measures.CoreMetrics.CLASS_COMPLEXITY_KEY;
51 import static org.sonar.api.measures.CoreMetrics.CODE_SMELLS_KEY;
52 import static org.sonar.api.measures.CoreMetrics.COGNITIVE_COMPLEXITY_KEY;
53 import static org.sonar.api.measures.CoreMetrics.COMMENT_LINES_DENSITY_KEY;
54 import static org.sonar.api.measures.CoreMetrics.COMMENT_LINES_KEY;
55 import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_CLASSES_KEY;
56 import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_FUNCTIONS_KEY;
57 import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_KEY;
58 import static org.sonar.api.measures.CoreMetrics.CONFIRMED_ISSUES_KEY;
59 import static org.sonar.api.measures.CoreMetrics.CRITICAL_VIOLATIONS_KEY;
60 import static org.sonar.api.measures.CoreMetrics.DEVELOPMENT_COST_KEY;
61 import static org.sonar.api.measures.CoreMetrics.EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_KEY;
62 import static org.sonar.api.measures.CoreMetrics.FALSE_POSITIVE_ISSUES_KEY;
63 import static org.sonar.api.measures.CoreMetrics.FILES_KEY;
64 import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION_KEY;
65 import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY_KEY;
66 import static org.sonar.api.measures.CoreMetrics.FUNCTIONS_KEY;
67 import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY;
68 import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_KEY;
69 import static org.sonar.api.measures.CoreMetrics.GENERATED_LINES_KEY;
70 import static org.sonar.api.measures.CoreMetrics.GENERATED_NCLOC_KEY;
71 import static org.sonar.api.measures.CoreMetrics.INFO_VIOLATIONS_KEY;
72 import static org.sonar.api.measures.CoreMetrics.LINES_KEY;
73 import static org.sonar.api.measures.CoreMetrics.MAJOR_VIOLATIONS_KEY;
74 import static org.sonar.api.measures.CoreMetrics.MINOR_VIOLATIONS_KEY;
75 import static org.sonar.api.measures.CoreMetrics.NCLOC_DATA_KEY;
76 import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
77 import static org.sonar.api.measures.CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION_KEY;
78 import static org.sonar.api.measures.CoreMetrics.OPEN_ISSUES_KEY;
79 import static org.sonar.api.measures.CoreMetrics.RELIABILITY_RATING_KEY;
80 import static org.sonar.api.measures.CoreMetrics.RELIABILITY_REMEDIATION_EFFORT_KEY;
81 import static org.sonar.api.measures.CoreMetrics.REOPENED_ISSUES_KEY;
82 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_KEY;
83 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_REVIEWED_KEY;
84 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_REVIEWED_STATUS_KEY;
85 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_TO_REVIEW_STATUS_KEY;
86 import static org.sonar.api.measures.CoreMetrics.SECURITY_RATING_KEY;
87 import static org.sonar.api.measures.CoreMetrics.SECURITY_REMEDIATION_EFFORT_KEY;
88 import static org.sonar.api.measures.CoreMetrics.SECURITY_REVIEW_RATING_KEY;
89 import static org.sonar.api.measures.CoreMetrics.SQALE_DEBT_RATIO_KEY;
90 import static org.sonar.api.measures.CoreMetrics.SQALE_RATING_KEY;
91 import static org.sonar.api.measures.CoreMetrics.STATEMENTS_KEY;
92 import static org.sonar.api.measures.CoreMetrics.TECHNICAL_DEBT_KEY;
93 import static org.sonar.api.measures.CoreMetrics.VIOLATIONS_KEY;
94 import static org.sonar.api.measures.CoreMetrics.VULNERABILITIES_KEY;
95 import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
97 public class PersistLiveMeasuresStep implements ComputationStep {
100 * List of metrics that should not be persisted on file measure.
102 private static final Set<String> NOT_TO_PERSIST_ON_FILE_METRIC_KEYS = Set.of(FILE_COMPLEXITY_DISTRIBUTION_KEY, FUNCTION_COMPLEXITY_DISTRIBUTION_KEY);
103 private static final Set<String> NOT_TO_PERSIST_ON_UNCHANGED_FILES = Set.of(
104 BLOCKER_VIOLATIONS_KEY, BUGS_KEY, CLASS_COMPLEXITY_KEY, CLASSES_KEY, CODE_SMELLS_KEY, COGNITIVE_COMPLEXITY_KEY, COMMENT_LINES_KEY, COMMENT_LINES_DENSITY_KEY,
105 COMPLEXITY_KEY, COMPLEXITY_IN_CLASSES_KEY, COMPLEXITY_IN_FUNCTIONS_KEY, CONFIRMED_ISSUES_KEY, CRITICAL_VIOLATIONS_KEY, DEVELOPMENT_COST_KEY,
106 EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_KEY, FALSE_POSITIVE_ISSUES_KEY, FILE_COMPLEXITY_KEY, FILE_COMPLEXITY_DISTRIBUTION_KEY, FILES_KEY, FUNCTION_COMPLEXITY_KEY,
107 FUNCTION_COMPLEXITY_DISTRIBUTION_KEY, FUNCTIONS_KEY, GENERATED_LINES_KEY, GENERATED_NCLOC_KEY, INFO_VIOLATIONS_KEY, LINES_KEY,
108 MAJOR_VIOLATIONS_KEY, MINOR_VIOLATIONS_KEY, NCLOC_KEY, NCLOC_DATA_KEY, NCLOC_LANGUAGE_DISTRIBUTION_KEY, OPEN_ISSUES_KEY, RELIABILITY_RATING_KEY,
109 RELIABILITY_REMEDIATION_EFFORT_KEY, REOPENED_ISSUES_KEY, SECURITY_HOTSPOTS_KEY, SECURITY_HOTSPOTS_REVIEWED_KEY, SECURITY_HOTSPOTS_REVIEWED_STATUS_KEY,
110 SECURITY_HOTSPOTS_TO_REVIEW_STATUS_KEY, SECURITY_RATING_KEY, SECURITY_REMEDIATION_EFFORT_KEY, SECURITY_REVIEW_RATING_KEY, SQALE_DEBT_RATIO_KEY, TECHNICAL_DEBT_KEY,
111 SQALE_RATING_KEY, STATEMENTS_KEY, VIOLATIONS_KEY, VULNERABILITIES_KEY, ACCEPTED_ISSUES_KEY
113 private final DbClient dbClient;
114 private final MetricRepository metricRepository;
115 private final MeasureToMeasureDto measureToMeasureDto;
116 private final TreeRootHolder treeRootHolder;
117 private final MeasureRepository measureRepository;
118 private final Optional<FileStatuses> fileStatuses;
120 public PersistLiveMeasuresStep(DbClient dbClient, MetricRepository metricRepository, MeasureToMeasureDto measureToMeasureDto,
121 TreeRootHolder treeRootHolder, MeasureRepository measureRepository, Optional<FileStatuses> fileStatuses) {
122 this.dbClient = dbClient;
123 this.metricRepository = metricRepository;
124 this.measureToMeasureDto = measureToMeasureDto;
125 this.treeRootHolder = treeRootHolder;
126 this.measureRepository = measureRepository;
127 this.fileStatuses = fileStatuses;
131 public String getDescription() {
132 return "Persist live measures";
136 public void execute(ComputationStep.Context context) {
137 boolean supportUpsert = dbClient.getDatabase().getDialect().supportsUpsert();
138 try (DbSession dbSession = dbClient.openSession(true)) {
139 Component root = treeRootHolder.getRoot();
140 MeasureVisitor visitor = new MeasureVisitor(dbSession, supportUpsert);
141 new DepthTraversalTypeAwareCrawler(visitor).visit(root);
143 context.getStatistics()
144 .add("insertsOrUpdates", visitor.insertsOrUpdates);
148 private class MeasureVisitor extends TypeAwareVisitorAdapter {
149 private final DbSession dbSession;
150 private final boolean supportUpsert;
151 private int insertsOrUpdates = 0;
153 private MeasureVisitor(DbSession dbSession, boolean supportUpsert) {
154 super(CrawlerDepthLimit.LEAVES, PRE_ORDER);
155 this.supportUpsert = supportUpsert;
156 this.dbSession = dbSession;
160 public void visitAny(Component component) {
161 List<String> metricUuids = new ArrayList<>();
162 List<String> keptMetricUuids = new ArrayList<>();
163 Map<String, Measure> measures = measureRepository.getRawMeasures(component);
164 List<LiveMeasureDto> dtos = new ArrayList<>();
165 for (Map.Entry<String, Measure> measuresByMetricKey : measures.entrySet()) {
166 String metricKey = measuresByMetricKey.getKey();
167 if (NOT_TO_PERSIST_ON_FILE_METRIC_KEYS.contains(metricKey) ) {
170 Metric metric = metricRepository.getByKey(metricKey);
171 Predicate<Measure> notBestValueOptimized = BestValueOptimization.from(metric, component).negate();
172 Measure m = measuresByMetricKey.getValue();
173 if (!NonEmptyMeasure.INSTANCE.test(m) || !notBestValueOptimized.test(m)) {
176 metricUuids.add(metric.getUuid());
177 if (shouldSkipMetricOnUnchangedFile(component, metricKey)) {
178 keptMetricUuids.add(metric.getUuid());
182 LiveMeasureDto lm = measureToMeasureDto.toLiveMeasureDto(m, metric, component);
186 List<String> excludedMetricUuids = supportUpsert ? metricUuids : keptMetricUuids;
187 deleteNonexistentMeasures(dbSession, component.getUuid(), excludedMetricUuids);
188 dtos.forEach(dto -> insertMeasureOptimally(dbSession, dto));
191 insertsOrUpdates += dtos.size();
194 private void deleteNonexistentMeasures(DbSession dbSession, String componentUuid, List<String> excludedMetricUuids) {
195 // The measures that no longer exist on the component must be deleted, for example
196 // when the coverage on a file goes to the "best value" 100%.
197 // The measures on deleted components are deleted by the step PurgeDatastoresStep
198 dbClient.liveMeasureDao().deleteByComponentUuidExcludingMetricUuids(dbSession, componentUuid, excludedMetricUuids);
201 private void insertMeasureOptimally(DbSession dbSession, LiveMeasureDto dto) {
203 dbClient.liveMeasureDao().upsert(dbSession, dto);
205 dbClient.liveMeasureDao().insert(dbSession, dto);
209 private boolean shouldSkipMetricOnUnchangedFile(Component component, String metricKey) {
210 return component.getType() == Component.Type.FILE && fileStatuses.isPresent() &&
211 fileStatuses.get().isDataUnchanged(component) && NOT_TO_PERSIST_ON_UNCHANGED_FILES.contains(metricKey);
215 private enum NonEmptyMeasure implements Predicate<Measure> {
219 public boolean test(@Nonnull Measure input) {
220 return input.getValueType() != Measure.ValueType.NO_VALUE || input.getData() != null;