]> source.dussan.org Git - sonarqube.git/blob
7ec45800832c979cf5832bbd88a3ae3dab9213e0
[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.core.metric.SoftwareQualitiesMetrics;
43 import org.sonar.db.DbClient;
44 import org.sonar.db.DbSession;
45 import org.sonar.db.measure.LiveMeasureDto;
46
47 import static org.sonar.api.measures.CoreMetrics.ACCEPTED_ISSUES_KEY;
48 import static org.sonar.api.measures.CoreMetrics.BLOCKER_VIOLATIONS_KEY;
49 import static org.sonar.api.measures.CoreMetrics.BUGS_KEY;
50 import static org.sonar.api.measures.CoreMetrics.CLASSES_KEY;
51 import static org.sonar.api.measures.CoreMetrics.CLASS_COMPLEXITY_KEY;
52 import static org.sonar.api.measures.CoreMetrics.CODE_SMELLS_KEY;
53 import static org.sonar.api.measures.CoreMetrics.COGNITIVE_COMPLEXITY_KEY;
54 import static org.sonar.api.measures.CoreMetrics.COMMENT_LINES_DENSITY_KEY;
55 import static org.sonar.api.measures.CoreMetrics.COMMENT_LINES_KEY;
56 import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_CLASSES_KEY;
57 import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_IN_FUNCTIONS_KEY;
58 import static org.sonar.api.measures.CoreMetrics.COMPLEXITY_KEY;
59 import static org.sonar.api.measures.CoreMetrics.CONFIRMED_ISSUES_KEY;
60 import static org.sonar.api.measures.CoreMetrics.CRITICAL_VIOLATIONS_KEY;
61 import static org.sonar.api.measures.CoreMetrics.DEVELOPMENT_COST_KEY;
62 import static org.sonar.api.measures.CoreMetrics.EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_KEY;
63 import static org.sonar.api.measures.CoreMetrics.FALSE_POSITIVE_ISSUES_KEY;
64 import static org.sonar.api.measures.CoreMetrics.FILES_KEY;
65 import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION_KEY;
66 import static org.sonar.api.measures.CoreMetrics.FILE_COMPLEXITY_KEY;
67 import static org.sonar.api.measures.CoreMetrics.FUNCTIONS_KEY;
68 import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY;
69 import static org.sonar.api.measures.CoreMetrics.FUNCTION_COMPLEXITY_KEY;
70 import static org.sonar.api.measures.CoreMetrics.GENERATED_LINES_KEY;
71 import static org.sonar.api.measures.CoreMetrics.GENERATED_NCLOC_KEY;
72 import static org.sonar.api.measures.CoreMetrics.INFO_VIOLATIONS_KEY;
73 import static org.sonar.api.measures.CoreMetrics.LINES_KEY;
74 import static org.sonar.api.measures.CoreMetrics.MAJOR_VIOLATIONS_KEY;
75 import static org.sonar.api.measures.CoreMetrics.MINOR_VIOLATIONS_KEY;
76 import static org.sonar.api.measures.CoreMetrics.NCLOC_DATA_KEY;
77 import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
78 import static org.sonar.api.measures.CoreMetrics.NCLOC_LANGUAGE_DISTRIBUTION_KEY;
79 import static org.sonar.api.measures.CoreMetrics.OPEN_ISSUES_KEY;
80 import static org.sonar.api.measures.CoreMetrics.RELIABILITY_RATING_KEY;
81 import static org.sonar.api.measures.CoreMetrics.RELIABILITY_REMEDIATION_EFFORT_KEY;
82 import static org.sonar.api.measures.CoreMetrics.REOPENED_ISSUES_KEY;
83 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_KEY;
84 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_REVIEWED_KEY;
85 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_REVIEWED_STATUS_KEY;
86 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_TO_REVIEW_STATUS_KEY;
87 import static org.sonar.api.measures.CoreMetrics.SECURITY_RATING_KEY;
88 import static org.sonar.api.measures.CoreMetrics.SECURITY_REMEDIATION_EFFORT_KEY;
89 import static org.sonar.api.measures.CoreMetrics.SECURITY_REVIEW_RATING_KEY;
90 import static org.sonar.api.measures.CoreMetrics.SQALE_DEBT_RATIO_KEY;
91 import static org.sonar.api.measures.CoreMetrics.SQALE_RATING_KEY;
92 import static org.sonar.api.measures.CoreMetrics.STATEMENTS_KEY;
93 import static org.sonar.api.measures.CoreMetrics.TECHNICAL_DEBT_KEY;
94 import static org.sonar.api.measures.CoreMetrics.VIOLATIONS_KEY;
95 import static org.sonar.api.measures.CoreMetrics.VULNERABILITIES_KEY;
96 import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
97
98 public class PersistLiveMeasuresStep implements ComputationStep {
99
100   /**
101    * List of metrics that should not be persisted on file measure.
102    */
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,
113
114     SoftwareQualitiesMetrics.SOFTWARE_QUALITY_MAINTAINABILITY_RATING_KEY,
115     SoftwareQualitiesMetrics.SOFTWARE_QUALITY_RELIABILITY_RATING_KEY,
116     SoftwareQualitiesMetrics.SOFTWARE_QUALITY_SECURITY_RATING_KEY,
117     SoftwareQualitiesMetrics.SOFTWARE_QUALITY_SECURITY_REVIEW_RATING_KEY,
118     SoftwareQualitiesMetrics.EFFORT_TO_REACH_SOFTWARE_QUALITY_MAINTAINABILITY_RATING_A_KEY,
119     SoftwareQualitiesMetrics.SOFTWARE_QUALITY_MAINTAINABILITY_REMEDIATION_EFFORT_KEY,
120     SoftwareQualitiesMetrics.SOFTWARE_QUALITY_SECURITY_REMEDIATION_EFFORT_KEY,
121     SoftwareQualitiesMetrics.SOFTWARE_QUALITY_RELIABILITY_REMEDIATION_EFFORT_KEY,
122     SoftwareQualitiesMetrics.SOFTWARE_QUALITY_MAINTAINABILITY_DEBT_RATIO_KEY
123   );
124   private final DbClient dbClient;
125   private final MetricRepository metricRepository;
126   private final MeasureToMeasureDto measureToMeasureDto;
127   private final TreeRootHolder treeRootHolder;
128   private final MeasureRepository measureRepository;
129   private final Optional<FileStatuses> fileStatuses;
130
131   public PersistLiveMeasuresStep(DbClient dbClient, MetricRepository metricRepository, MeasureToMeasureDto measureToMeasureDto,
132     TreeRootHolder treeRootHolder, MeasureRepository measureRepository, Optional<FileStatuses> fileStatuses) {
133     this.dbClient = dbClient;
134     this.metricRepository = metricRepository;
135     this.measureToMeasureDto = measureToMeasureDto;
136     this.treeRootHolder = treeRootHolder;
137     this.measureRepository = measureRepository;
138     this.fileStatuses = fileStatuses;
139   }
140
141   @Override
142   public String getDescription() {
143     return "Persist live measures";
144   }
145
146   @Override
147   public void execute(ComputationStep.Context context) {
148     boolean supportUpsert = dbClient.getDatabase().getDialect().supportsUpsert();
149     try (DbSession dbSession = dbClient.openSession(true)) {
150       Component root = treeRootHolder.getRoot();
151       MeasureVisitor visitor = new MeasureVisitor(dbSession, supportUpsert);
152       new DepthTraversalTypeAwareCrawler(visitor).visit(root);
153       dbSession.commit();
154       context.getStatistics()
155         .add("insertsOrUpdates", visitor.insertsOrUpdates);
156     }
157   }
158
159   private class MeasureVisitor extends TypeAwareVisitorAdapter {
160     private final DbSession dbSession;
161     private final boolean supportUpsert;
162     private int insertsOrUpdates = 0;
163
164     private MeasureVisitor(DbSession dbSession, boolean supportUpsert) {
165       super(CrawlerDepthLimit.LEAVES, PRE_ORDER);
166       this.supportUpsert = supportUpsert;
167       this.dbSession = dbSession;
168     }
169
170     @Override
171     public void visitAny(Component component) {
172       List<String> metricUuids = new ArrayList<>();
173       List<String> keptMetricUuids = new ArrayList<>();
174       Map<String, Measure> measures = measureRepository.getRawMeasures(component);
175       List<LiveMeasureDto> dtos = new ArrayList<>();
176       for (Map.Entry<String, Measure> measuresByMetricKey : measures.entrySet()) {
177         String metricKey = measuresByMetricKey.getKey();
178         if (NOT_TO_PERSIST_ON_FILE_METRIC_KEYS.contains(metricKey) ) {
179           continue;
180         }
181         Metric metric = metricRepository.getByKey(metricKey);
182         Predicate<Measure> notBestValueOptimized = BestValueOptimization.from(metric, component).negate();
183         Measure m = measuresByMetricKey.getValue();
184         if (!NonEmptyMeasure.INSTANCE.test(m) || !notBestValueOptimized.test(m)) {
185           continue;
186         }
187         metricUuids.add(metric.getUuid());
188         if (shouldSkipMetricOnUnchangedFile(component, metricKey)) {
189           keptMetricUuids.add(metric.getUuid());
190           continue;
191         }
192
193         LiveMeasureDto lm = measureToMeasureDto.toLiveMeasureDto(m, metric, component);
194         dtos.add(lm);
195       }
196
197       List<String> excludedMetricUuids = supportUpsert ? metricUuids : keptMetricUuids;
198       deleteNonexistentMeasures(dbSession, component.getUuid(), excludedMetricUuids);
199       dtos.forEach(dto -> insertMeasureOptimally(dbSession, dto));
200
201       dbSession.commit();
202       insertsOrUpdates += dtos.size();
203     }
204
205     private void deleteNonexistentMeasures(DbSession dbSession, String componentUuid, List<String> excludedMetricUuids) {
206       // The measures that no longer exist on the component must be deleted, for example
207       // when the coverage on a file goes to the "best value" 100%.
208       // The measures on deleted components are deleted by the step PurgeDatastoresStep
209       dbClient.liveMeasureDao().deleteByComponentUuidExcludingMetricUuids(dbSession, componentUuid, excludedMetricUuids);
210     }
211
212     private void insertMeasureOptimally(DbSession dbSession, LiveMeasureDto dto) {
213       if (supportUpsert) {
214         dbClient.liveMeasureDao().upsert(dbSession, dto);
215       } else {
216         dbClient.liveMeasureDao().insert(dbSession, dto);
217       }
218     }
219
220     private boolean shouldSkipMetricOnUnchangedFile(Component component, String metricKey) {
221       return component.getType() == Component.Type.FILE && fileStatuses.isPresent() &&
222         fileStatuses.get().isDataUnchanged(component) && NOT_TO_PERSIST_ON_UNCHANGED_FILES.contains(metricKey);
223     }
224   }
225
226   private enum NonEmptyMeasure implements Predicate<Measure> {
227     INSTANCE;
228
229     @Override
230     public boolean test(@Nonnull Measure input) {
231       return input.getValueType() != Measure.ValueType.NO_VALUE || input.getData() != null;
232     }
233   }
234 }