3 * Copyright (C) 2009-2023 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.api.measures.CoreMetrics.WONT_FIX_ISSUES_KEY;
96 import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
98 public class PersistLiveMeasuresStep implements ComputationStep {
101 * List of metrics that should not be persisted on file measure.
103 private static final Set<String> NOT_TO_PERSIST_ON_FILE_METRIC_KEYS = Set.of(FILE_COMPLEXITY_DISTRIBUTION_KEY, FUNCTION_COMPLEXITY_DISTRIBUTION_KEY);
104 private static final Set<String> NOT_TO_PERSIST_ON_UNCHANGED_FILES = Set.of(
105 BLOCKER_VIOLATIONS_KEY, BUGS_KEY, CLASS_COMPLEXITY_KEY, CLASSES_KEY, CODE_SMELLS_KEY, COGNITIVE_COMPLEXITY_KEY, COMMENT_LINES_KEY, COMMENT_LINES_DENSITY_KEY,
106 COMPLEXITY_KEY, COMPLEXITY_IN_CLASSES_KEY, COMPLEXITY_IN_FUNCTIONS_KEY, CONFIRMED_ISSUES_KEY, CRITICAL_VIOLATIONS_KEY, DEVELOPMENT_COST_KEY,
107 EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_KEY, FALSE_POSITIVE_ISSUES_KEY, FILE_COMPLEXITY_KEY, FILE_COMPLEXITY_DISTRIBUTION_KEY, FILES_KEY, FUNCTION_COMPLEXITY_KEY,
108 FUNCTION_COMPLEXITY_DISTRIBUTION_KEY, FUNCTIONS_KEY, GENERATED_LINES_KEY, GENERATED_NCLOC_KEY, INFO_VIOLATIONS_KEY, LINES_KEY,
109 MAJOR_VIOLATIONS_KEY, MINOR_VIOLATIONS_KEY, NCLOC_KEY, NCLOC_DATA_KEY, NCLOC_LANGUAGE_DISTRIBUTION_KEY, OPEN_ISSUES_KEY, RELIABILITY_RATING_KEY,
110 RELIABILITY_REMEDIATION_EFFORT_KEY, REOPENED_ISSUES_KEY, SECURITY_HOTSPOTS_KEY, SECURITY_HOTSPOTS_REVIEWED_KEY, SECURITY_HOTSPOTS_REVIEWED_STATUS_KEY,
111 SECURITY_HOTSPOTS_TO_REVIEW_STATUS_KEY, SECURITY_RATING_KEY, SECURITY_REMEDIATION_EFFORT_KEY, SECURITY_REVIEW_RATING_KEY, SQALE_DEBT_RATIO_KEY, TECHNICAL_DEBT_KEY,
112 SQALE_RATING_KEY, STATEMENTS_KEY, VIOLATIONS_KEY, VULNERABILITIES_KEY, ACCEPTED_ISSUES_KEY
114 private final DbClient dbClient;
115 private final MetricRepository metricRepository;
116 private final MeasureToMeasureDto measureToMeasureDto;
117 private final TreeRootHolder treeRootHolder;
118 private final MeasureRepository measureRepository;
119 private final Optional<FileStatuses> fileStatuses;
121 public PersistLiveMeasuresStep(DbClient dbClient, MetricRepository metricRepository, MeasureToMeasureDto measureToMeasureDto,
122 TreeRootHolder treeRootHolder, MeasureRepository measureRepository, Optional<FileStatuses> fileStatuses) {
123 this.dbClient = dbClient;
124 this.metricRepository = metricRepository;
125 this.measureToMeasureDto = measureToMeasureDto;
126 this.treeRootHolder = treeRootHolder;
127 this.measureRepository = measureRepository;
128 this.fileStatuses = fileStatuses;
132 public String getDescription() {
133 return "Persist live measures";
137 public void execute(ComputationStep.Context context) {
138 boolean supportUpsert = dbClient.getDatabase().getDialect().supportsUpsert();
139 try (DbSession dbSession = dbClient.openSession(true)) {
140 Component root = treeRootHolder.getRoot();
141 MeasureVisitor visitor = new MeasureVisitor(dbSession, supportUpsert);
142 new DepthTraversalTypeAwareCrawler(visitor).visit(root);
144 context.getStatistics()
145 .add("insertsOrUpdates", visitor.insertsOrUpdates);
149 private class MeasureVisitor extends TypeAwareVisitorAdapter {
150 private final DbSession dbSession;
151 private final boolean supportUpsert;
152 private int insertsOrUpdates = 0;
154 private MeasureVisitor(DbSession dbSession, boolean supportUpsert) {
155 super(CrawlerDepthLimit.LEAVES, PRE_ORDER);
156 this.supportUpsert = supportUpsert;
157 this.dbSession = dbSession;
161 public void visitAny(Component component) {
162 List<String> metricUuids = new ArrayList<>();
163 List<String> keptMetricUuids = new ArrayList<>();
164 Map<String, Measure> measures = measureRepository.getRawMeasures(component);
165 List<LiveMeasureDto> dtos = new ArrayList<>();
166 for (Map.Entry<String, Measure> measuresByMetricKey : measures.entrySet()) {
167 String metricKey = measuresByMetricKey.getKey();
168 if (NOT_TO_PERSIST_ON_FILE_METRIC_KEYS.contains(metricKey) ) {
171 Metric metric = metricRepository.getByKey(metricKey);
172 Predicate<Measure> notBestValueOptimized = BestValueOptimization.from(metric, component).negate();
173 Measure m = measuresByMetricKey.getValue();
174 if (!NonEmptyMeasure.INSTANCE.test(m) || !notBestValueOptimized.test(m)) {
177 metricUuids.add(metric.getUuid());
178 if (shouldSkipMetricOnUnchangedFile(component, metricKey)) {
179 keptMetricUuids.add(metric.getUuid());
183 LiveMeasureDto lm = measureToMeasureDto.toLiveMeasureDto(m, metric, component);
187 List<String> excludedMetricUuids = supportUpsert ? metricUuids : keptMetricUuids;
188 deleteNonexistentMeasures(dbSession, component.getUuid(), excludedMetricUuids);
189 dtos.forEach(dto -> insertMeasureOptimally(dbSession, dto));
192 insertsOrUpdates += dtos.size();
195 private void deleteNonexistentMeasures(DbSession dbSession, String componentUuid, List<String> excludedMetricUuids) {
196 // The measures that no longer exist on the component must be deleted, for example
197 // when the coverage on a file goes to the "best value" 100%.
198 // The measures on deleted components are deleted by the step PurgeDatastoresStep
199 dbClient.liveMeasureDao().deleteByComponentUuidExcludingMetricUuids(dbSession, componentUuid, excludedMetricUuids);
202 private void insertMeasureOptimally(DbSession dbSession, LiveMeasureDto dto) {
204 dbClient.liveMeasureDao().upsert(dbSession, dto);
206 dbClient.liveMeasureDao().insert(dbSession, dto);
210 private boolean shouldSkipMetricOnUnchangedFile(Component component, String metricKey) {
211 return component.getType() == Component.Type.FILE && fileStatuses.isPresent() &&
212 fileStatuses.get().isDataUnchanged(component) && NOT_TO_PERSIST_ON_UNCHANGED_FILES.contains(metricKey);
216 private enum NonEmptyMeasure implements Predicate<Measure> {
220 public boolean test(@Nonnull Measure input) {
221 return input.getValueType() != Measure.ValueType.NO_VALUE || input.getData() != null;