]> source.dussan.org Git - sonarqube.git/blob
9177eaca0bb47e28c9979d963a0db633f865f221
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.step;
21
22 import java.util.ArrayList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Optional;
26 import java.util.Set;
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;
45
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;
96
97 public class PersistLiveMeasuresStep implements ComputationStep {
98
99   /**
100    * List of metrics that should not be persisted on file measure.
101    */
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
112   );
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;
119
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;
128   }
129
130   @Override
131   public String getDescription() {
132     return "Persist live measures";
133   }
134
135   @Override
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);
142       dbSession.commit();
143       context.getStatistics()
144         .add("insertsOrUpdates", visitor.insertsOrUpdates);
145     }
146   }
147
148   private class MeasureVisitor extends TypeAwareVisitorAdapter {
149     private final DbSession dbSession;
150     private final boolean supportUpsert;
151     private int insertsOrUpdates = 0;
152
153     private MeasureVisitor(DbSession dbSession, boolean supportUpsert) {
154       super(CrawlerDepthLimit.LEAVES, PRE_ORDER);
155       this.supportUpsert = supportUpsert;
156       this.dbSession = dbSession;
157     }
158
159     @Override
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) ) {
168           continue;
169         }
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)) {
174           continue;
175         }
176         metricUuids.add(metric.getUuid());
177         if (shouldSkipMetricOnUnchangedFile(component, metricKey)) {
178           keptMetricUuids.add(metric.getUuid());
179           continue;
180         }
181
182         LiveMeasureDto lm = measureToMeasureDto.toLiveMeasureDto(m, metric, component);
183         dtos.add(lm);
184       }
185
186       List<String> excludedMetricUuids = supportUpsert ? metricUuids : keptMetricUuids;
187       deleteNonexistentMeasures(dbSession, component.getUuid(), excludedMetricUuids);
188       dtos.forEach(dto -> insertMeasureOptimally(dbSession, dto));
189
190       dbSession.commit();
191       insertsOrUpdates += dtos.size();
192     }
193
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);
199     }
200
201     private void insertMeasureOptimally(DbSession dbSession, LiveMeasureDto dto) {
202       if (supportUpsert) {
203         dbClient.liveMeasureDao().upsert(dbSession, dto);
204       } else {
205         dbClient.liveMeasureDao().insert(dbSession, dto);
206       }
207     }
208
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);
212     }
213   }
214
215   private enum NonEmptyMeasure implements Predicate<Measure> {
216     INSTANCE;
217
218     @Override
219     public boolean test(@Nonnull Measure input) {
220       return input.getValueType() != Measure.ValueType.NO_VALUE || input.getData() != null;
221     }
222   }
223 }