]> source.dussan.org Git - sonarqube.git/blob
f70214658aeb401f0e553bf20eb023f510a186e7
[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.server.measure.live;
21
22 import java.util.List;
23 import java.util.Optional;
24 import java.util.OptionalInt;
25 import java.util.Set;
26 import java.util.function.BiConsumer;
27 import org.sonar.api.issue.Issue;
28 import org.sonar.api.issue.impact.SoftwareQuality;
29 import org.sonar.api.measures.CoreMetrics;
30 import org.sonar.api.measures.Metric;
31 import org.sonar.api.rule.Severity;
32 import org.sonar.api.rules.RuleType;
33 import org.sonar.server.measure.ImpactMeasureBuilder;
34 import org.sonar.server.measure.Rating;
35
36 import static java.util.Arrays.asList;
37 import static org.sonar.api.measures.CoreMetrics.CODE_SMELLS;
38 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_REVIEWED;
39 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_REVIEWED_STATUS;
40 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_TO_REVIEW_STATUS;
41 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_REVIEWED;
42 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_REVIEWED_STATUS;
43 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_TO_REVIEW_STATUS;
44 import static org.sonar.server.measure.Rating.RATING_BY_SEVERITY;
45 import static org.sonar.server.security.SecurityReviewRating.computePercent;
46 import static org.sonar.server.security.SecurityReviewRating.computeRating;
47
48 public class MeasureUpdateFormulaFactoryImpl implements MeasureUpdateFormulaFactory {
49   private static final List<MeasureUpdateFormula> FORMULAS = asList(
50     new MeasureUpdateFormula(CODE_SMELLS, false, new AddChildren(),
51       (context, issues) -> context.setValue(issues.countUnresolvedByType(RuleType.CODE_SMELL, false))),
52
53     new MeasureUpdateFormula(CoreMetrics.BUGS, false, new AddChildren(),
54       (context, issues) -> context.setValue(issues.countUnresolvedByType(RuleType.BUG, false))),
55
56     new MeasureUpdateFormula(CoreMetrics.VULNERABILITIES, false, new AddChildren(),
57       (context, issues) -> context.setValue(issues.countUnresolvedByType(RuleType.VULNERABILITY, false))),
58
59     new MeasureUpdateFormula(CoreMetrics.SECURITY_HOTSPOTS, false, new AddChildren(),
60       (context, issues) -> context.setValue(issues.countUnresolvedByType(RuleType.SECURITY_HOTSPOT, false))),
61
62     new MeasureUpdateFormula(CoreMetrics.RELIABILITY_ISSUES, false, true, new ImpactAddChildren(),
63       (context, issues) -> context.setValue(issues.getBySoftwareQuality(SoftwareQuality.RELIABILITY))),
64
65     new MeasureUpdateFormula(CoreMetrics.MAINTAINABILITY_ISSUES, false, true, new ImpactAddChildren(),
66       (context, issues) -> context.setValue(issues.getBySoftwareQuality(SoftwareQuality.MAINTAINABILITY))),
67
68     new MeasureUpdateFormula(CoreMetrics.SECURITY_ISSUES, false, true, new ImpactAddChildren(),
69       (context, issues) -> context.setValue(issues.getBySoftwareQuality(SoftwareQuality.SECURITY))),
70
71     new MeasureUpdateFormula(CoreMetrics.VIOLATIONS, false, new AddChildren(),
72       (context, issues) -> context.setValue(issues.countUnresolved(false))),
73
74     new MeasureUpdateFormula(CoreMetrics.BLOCKER_VIOLATIONS, false, new AddChildren(),
75       (context, issues) -> context.setValue(issues.countUnresolvedBySeverity(Severity.BLOCKER, false))),
76
77     new MeasureUpdateFormula(CoreMetrics.CRITICAL_VIOLATIONS, false, new AddChildren(),
78       (context, issues) -> context.setValue(issues.countUnresolvedBySeverity(Severity.CRITICAL, false))),
79
80     new MeasureUpdateFormula(CoreMetrics.MAJOR_VIOLATIONS, false, new AddChildren(),
81       (context, issues) -> context.setValue(issues.countUnresolvedBySeverity(Severity.MAJOR, false))),
82
83     new MeasureUpdateFormula(CoreMetrics.MINOR_VIOLATIONS, false, new AddChildren(),
84       (context, issues) -> context.setValue(issues.countUnresolvedBySeverity(Severity.MINOR, false))),
85
86     new MeasureUpdateFormula(CoreMetrics.INFO_VIOLATIONS, false, new AddChildren(),
87       (context, issues) -> context.setValue(issues.countUnresolvedBySeverity(Severity.INFO, false))),
88
89     new MeasureUpdateFormula(CoreMetrics.FALSE_POSITIVE_ISSUES, false, new AddChildren(),
90       (context, issues) -> context.setValue(issues.countByResolution(Issue.RESOLUTION_FALSE_POSITIVE, false))),
91
92     new MeasureUpdateFormula(CoreMetrics.ACCEPTED_ISSUES, false, new AddChildren(),
93       (context, issues) -> context.setValue(issues.countByResolution(Issue.RESOLUTION_WONT_FIX, false))),
94
95     new MeasureUpdateFormula(CoreMetrics.HIGH_IMPACT_ACCEPTED_ISSUES, false, true, new AddChildren(),
96       (context, issues) -> context.setValue(issues.countHighImpactAccepted(false))),
97
98     new MeasureUpdateFormula(CoreMetrics.OPEN_ISSUES, false, new AddChildren(),
99       (context, issues) -> context.setValue(issues.countByStatus(Issue.STATUS_OPEN, false))),
100
101     new MeasureUpdateFormula(CoreMetrics.REOPENED_ISSUES, false, new AddChildren(),
102       (context, issues) -> context.setValue(issues.countByStatus(Issue.STATUS_REOPENED, false))),
103
104     new MeasureUpdateFormula(CoreMetrics.CONFIRMED_ISSUES, false, new AddChildren(),
105       (context, issues) -> context.setValue(issues.countByStatus(Issue.STATUS_CONFIRMED, false))),
106
107     new MeasureUpdateFormula(CoreMetrics.TECHNICAL_DEBT, false, new AddChildren(),
108       (context, issues) -> context.setValue(issues.sumEffortOfUnresolved(RuleType.CODE_SMELL, false))),
109
110     new MeasureUpdateFormula(CoreMetrics.RELIABILITY_REMEDIATION_EFFORT, false, new AddChildren(),
111       (context, issues) -> context.setValue(issues.sumEffortOfUnresolved(RuleType.BUG, false))),
112
113     new MeasureUpdateFormula(CoreMetrics.SECURITY_REMEDIATION_EFFORT, false, new AddChildren(),
114       (context, issues) -> context.setValue(issues.sumEffortOfUnresolved(RuleType.VULNERABILITY, false))),
115
116     new MeasureUpdateFormula(CoreMetrics.SQALE_DEBT_RATIO, false, false,
117       (context, formula) -> context.setValue(100.0 * debtDensity(context)),
118       (context, issues) -> context.setValue(100.0 * debtDensity(context)),
119       asList(CoreMetrics.TECHNICAL_DEBT, CoreMetrics.DEVELOPMENT_COST)),
120
121     new MeasureUpdateFormula(CoreMetrics.SQALE_RATING, false, false,
122       (context, issues) -> context.setValue(context.getDebtRatingGrid().getRatingForDensity(debtDensity(context))),
123       (context, issues) -> context.setValue(context.getDebtRatingGrid().getRatingForDensity(debtDensity(context))),
124       asList(CoreMetrics.TECHNICAL_DEBT, CoreMetrics.DEVELOPMENT_COST)),
125
126     new MeasureUpdateFormula(CoreMetrics.EFFORT_TO_REACH_MAINTAINABILITY_RATING_A, false, false,
127       (context, formula) -> context.setValue(effortToReachMaintainabilityRatingA(context)),
128       (context, issues) -> context.setValue(effortToReachMaintainabilityRatingA(context)), asList(CoreMetrics.TECHNICAL_DEBT, CoreMetrics.DEVELOPMENT_COST)),
129
130     new MeasureUpdateFormula(CoreMetrics.RELIABILITY_RATING, false, new MaxRatingChildren(),
131       (context, issues) -> context.setValue(RATING_BY_SEVERITY.get(issues.getHighestSeverityOfUnresolved(RuleType.BUG, false).orElse(Severity.INFO)))),
132
133     new MeasureUpdateFormula(CoreMetrics.SECURITY_RATING, false, new MaxRatingChildren(),
134       (context, issues) -> context.setValue(RATING_BY_SEVERITY.get(issues.getHighestSeverityOfUnresolved(RuleType.VULNERABILITY, false).orElse(Severity.INFO)))),
135
136     new MeasureUpdateFormula(SECURITY_HOTSPOTS_REVIEWED_STATUS, false,
137       (context, formula) -> context.setValue(context.getValue(SECURITY_HOTSPOTS_REVIEWED_STATUS).orElse(0D) + context.getChildrenHotspotsReviewed()),
138       (context, issues) -> context.setValue(issues.countHotspotsByStatus(Issue.STATUS_REVIEWED, false))),
139
140     new MeasureUpdateFormula(SECURITY_HOTSPOTS_TO_REVIEW_STATUS, false,
141       (context, formula) -> context.setValue(context.getValue(SECURITY_HOTSPOTS_TO_REVIEW_STATUS).orElse(0D) + context.getChildrenHotspotsToReview()),
142       (context, issues) -> context.setValue(issues.countHotspotsByStatus(Issue.STATUS_TO_REVIEW, false))),
143
144     new MeasureUpdateFormula(CoreMetrics.SECURITY_HOTSPOTS_REVIEWED, false,
145       (context, formula) -> {
146         Optional<Double> percent = computePercent(
147           context.getValue(SECURITY_HOTSPOTS_TO_REVIEW_STATUS).orElse(0D).longValue(),
148           context.getValue(SECURITY_HOTSPOTS_REVIEWED_STATUS).orElse(0D).longValue());
149         percent.ifPresent(context::setValue);
150       },
151       (context, issues) -> computePercent(issues.countHotspotsByStatus(Issue.STATUS_TO_REVIEW, false), issues.countHotspotsByStatus(Issue.STATUS_REVIEWED, false))
152         .ifPresent(context::setValue)),
153
154     new MeasureUpdateFormula(CoreMetrics.SECURITY_REVIEW_RATING, false,
155       (context, formula) -> context.setValue(computeRating(context.getValue(SECURITY_HOTSPOTS_REVIEWED).orElse(null))),
156       (context, issues) -> {
157         Optional<Double> percent = computePercent(issues.countHotspotsByStatus(Issue.STATUS_TO_REVIEW, false), issues.countHotspotsByStatus(Issue.STATUS_REVIEWED, false));
158         context.setValue(computeRating(percent.orElse(null)));
159       }),
160
161     new MeasureUpdateFormula(CoreMetrics.NEW_CODE_SMELLS, true, new AddChildren(),
162       (context, issues) -> context.setValue(issues.countUnresolvedByType(RuleType.CODE_SMELL, true))),
163
164     new MeasureUpdateFormula(CoreMetrics.NEW_BUGS, true, new AddChildren(),
165       (context, issues) -> context.setValue(issues.countUnresolvedByType(RuleType.BUG, true))),
166
167     new MeasureUpdateFormula(CoreMetrics.NEW_VULNERABILITIES, true, new AddChildren(),
168       (context, issues) -> context.setValue(issues.countUnresolvedByType(RuleType.VULNERABILITY, true))),
169
170     new MeasureUpdateFormula(CoreMetrics.NEW_SECURITY_HOTSPOTS, true, new AddChildren(),
171       (context, issues) -> context.setValue(issues.countUnresolvedByType(RuleType.SECURITY_HOTSPOT, true))),
172
173     new MeasureUpdateFormula(CoreMetrics.NEW_VIOLATIONS, true, new AddChildren(),
174       (context, issues) -> context.setValue(issues.countUnresolved(true))),
175
176     new MeasureUpdateFormula(CoreMetrics.NEW_BLOCKER_VIOLATIONS, true, new AddChildren(),
177       (context, issues) -> context.setValue(issues.countUnresolvedBySeverity(Severity.BLOCKER, true))),
178
179     new MeasureUpdateFormula(CoreMetrics.NEW_CRITICAL_VIOLATIONS, true, new AddChildren(),
180       (context, issues) -> context.setValue(issues.countUnresolvedBySeverity(Severity.CRITICAL, true))),
181
182     new MeasureUpdateFormula(CoreMetrics.NEW_MAJOR_VIOLATIONS, true, new AddChildren(),
183       (context, issues) -> context.setValue(issues.countUnresolvedBySeverity(Severity.MAJOR, true))),
184
185     new MeasureUpdateFormula(CoreMetrics.NEW_MINOR_VIOLATIONS, true, new AddChildren(),
186       (context, issues) -> context.setValue(issues.countUnresolvedBySeverity(Severity.MINOR, true))),
187
188     new MeasureUpdateFormula(CoreMetrics.NEW_INFO_VIOLATIONS, true, new AddChildren(),
189       (context, issues) -> context.setValue(issues.countUnresolvedBySeverity(Severity.INFO, true))),
190
191     new MeasureUpdateFormula(CoreMetrics.NEW_ACCEPTED_ISSUES, true, true, new AddChildren(),
192       (context, issues) -> context.setValue(issues.countByResolution(Issue.RESOLUTION_WONT_FIX, true))),
193
194     new MeasureUpdateFormula(CoreMetrics.NEW_TECHNICAL_DEBT, true, new AddChildren(),
195       (context, issues) -> context.setValue(issues.sumEffortOfUnresolved(RuleType.CODE_SMELL, true))),
196
197     new MeasureUpdateFormula(CoreMetrics.NEW_RELIABILITY_REMEDIATION_EFFORT, true, new AddChildren(),
198       (context, issues) -> context.setValue(issues.sumEffortOfUnresolved(RuleType.BUG, true))),
199
200     new MeasureUpdateFormula(CoreMetrics.NEW_SECURITY_REMEDIATION_EFFORT, true, new AddChildren(),
201       (context, issues) -> context.setValue(issues.sumEffortOfUnresolved(RuleType.VULNERABILITY, true))),
202
203     new MeasureUpdateFormula(CoreMetrics.NEW_RELIABILITY_RATING, true, new MaxRatingChildren(),
204       (context, issues) -> {
205         String highestSeverity = issues.getHighestSeverityOfUnresolved(RuleType.BUG, true).orElse(Severity.INFO);
206         context.setValue(RATING_BY_SEVERITY.get(highestSeverity));
207       }),
208
209     new MeasureUpdateFormula(CoreMetrics.NEW_SECURITY_RATING, true, new MaxRatingChildren(),
210       (context, issues) -> {
211         String highestSeverity = issues.getHighestSeverityOfUnresolved(RuleType.VULNERABILITY, true).orElse(Severity.INFO);
212         context.setValue(RATING_BY_SEVERITY.get(highestSeverity));
213       }),
214
215     new MeasureUpdateFormula(NEW_SECURITY_HOTSPOTS_REVIEWED_STATUS, true,
216       (context, formula) -> context.setValue(context.getValue(NEW_SECURITY_HOTSPOTS_REVIEWED_STATUS).orElse(0D) + context.getChildrenNewHotspotsReviewed()),
217       (context, issues) -> context.setValue(issues.countHotspotsByStatus(Issue.STATUS_REVIEWED, true))),
218
219     new MeasureUpdateFormula(NEW_SECURITY_HOTSPOTS_TO_REVIEW_STATUS, true,
220       (context, formula) -> context.setValue(context.getValue(NEW_SECURITY_HOTSPOTS_TO_REVIEW_STATUS).orElse(0D) + context.getChildrenNewHotspotsToReview()),
221       (context, issues) -> context.setValue(issues.countHotspotsByStatus(Issue.STATUS_TO_REVIEW, true))),
222
223     new MeasureUpdateFormula(NEW_SECURITY_HOTSPOTS_REVIEWED, true,
224       (context, formula) -> {
225         Optional<Double> percent = computePercent(
226           context.getValue(NEW_SECURITY_HOTSPOTS_TO_REVIEW_STATUS).orElse(0D).longValue(),
227           context.getValue(NEW_SECURITY_HOTSPOTS_REVIEWED_STATUS).orElse(0D).longValue());
228         percent.ifPresent(context::setValue);
229       },
230       (context, issues) -> computePercent(issues.countHotspotsByStatus(Issue.STATUS_TO_REVIEW, true), issues.countHotspotsByStatus(Issue.STATUS_REVIEWED, true))
231         .ifPresent(context::setValue)),
232
233     new MeasureUpdateFormula(CoreMetrics.NEW_SECURITY_REVIEW_RATING, true,
234       (context, formula) -> context.setValue(computeRating(context.getValue(NEW_SECURITY_HOTSPOTS_REVIEWED).orElse(null))),
235       (context, issues) -> {
236         Optional<Double> percent = computePercent(issues.countHotspotsByStatus(Issue.STATUS_TO_REVIEW, true), issues.countHotspotsByStatus(Issue.STATUS_REVIEWED, true));
237         context.setValue(computeRating(percent.orElse(null)));
238       }),
239
240     new MeasureUpdateFormula(CoreMetrics.NEW_SQALE_DEBT_RATIO, true, false,
241       (context, formula) -> context.setValue(100.0D * newDebtDensity(context)),
242       (context, issues) -> context.setValue(100.0D * newDebtDensity(context)),
243       asList(CoreMetrics.NEW_TECHNICAL_DEBT, CoreMetrics.NEW_DEVELOPMENT_COST)),
244
245     new MeasureUpdateFormula(CoreMetrics.NEW_MAINTAINABILITY_RATING, true, false,
246       (context, formula) -> context.setValue(context.getDebtRatingGrid().getRatingForDensity(newDebtDensity(context))),
247       (context, issues) -> context.setValue(context.getDebtRatingGrid().getRatingForDensity(newDebtDensity(context))),
248       asList(CoreMetrics.NEW_TECHNICAL_DEBT, CoreMetrics.NEW_DEVELOPMENT_COST)));
249
250   private static final Set<Metric> FORMULA_METRICS = MeasureUpdateFormulaFactory.extractMetrics(FORMULAS);
251
252   private static double debtDensity(MeasureUpdateFormula.Context context) {
253     double debt = Math.max(context.getValue(CoreMetrics.TECHNICAL_DEBT).orElse(0.0D), 0.0D);
254     Optional<Double> devCost = context.getText(CoreMetrics.DEVELOPMENT_COST).map(Double::parseDouble);
255     if (devCost.isPresent() && Double.doubleToRawLongBits(devCost.get()) > 0L) {
256       return debt / devCost.get();
257     }
258     return 0.0D;
259   }
260
261   private static double newDebtDensity(MeasureUpdateFormula.Context context) {
262     double debt = Math.max(context.getValue(CoreMetrics.NEW_TECHNICAL_DEBT).orElse(0.0D), 0.0D);
263     Optional<Double> devCost = context.getValue(CoreMetrics.NEW_DEVELOPMENT_COST);
264     if (devCost.isPresent() && Double.doubleToRawLongBits(devCost.get()) > 0L) {
265       return debt / devCost.get();
266     }
267     return 0.0D;
268   }
269
270   private static double effortToReachMaintainabilityRatingA(MeasureUpdateFormula.Context context) {
271     double developmentCost = context.getText(CoreMetrics.DEVELOPMENT_COST).map(Double::parseDouble).orElse(0.0D);
272     double effort = context.getValue(CoreMetrics.TECHNICAL_DEBT).orElse(0.0D);
273     double upperGradeCost = context.getDebtRatingGrid().getGradeLowerBound(Rating.B) * developmentCost;
274     return upperGradeCost < effort ? (effort - upperGradeCost) : 0.0D;
275   }
276
277   static class AddChildren implements BiConsumer<MeasureUpdateFormula.Context, MeasureUpdateFormula> {
278     @Override
279     public void accept(MeasureUpdateFormula.Context context, MeasureUpdateFormula formula) {
280       double sum = context.getChildrenValues().stream().mapToDouble(x -> x).sum();
281       context.setValue(context.getValue(formula.getMetric()).orElse(0D) + sum);
282     }
283   }
284
285   private static class MaxRatingChildren implements BiConsumer<MeasureUpdateFormula.Context, MeasureUpdateFormula> {
286     @Override
287     public void accept(MeasureUpdateFormula.Context context, MeasureUpdateFormula formula) {
288       OptionalInt max = context.getChildrenValues().stream().mapToInt(Double::intValue).max();
289       if (max.isPresent()) {
290         int currentRating = context.getValue(formula.getMetric()).map(Double::intValue).orElse(Rating.A.getIndex());
291         context.setValue(Rating.valueOf(Math.max(currentRating, max.getAsInt())));
292       }
293     }
294   }
295
296   private static class ImpactAddChildren implements BiConsumer<MeasureUpdateFormula.Context, MeasureUpdateFormula> {
297     @Override
298     public void accept(MeasureUpdateFormula.Context context, MeasureUpdateFormula formula) {
299       ImpactMeasureBuilder impactMeasureBuilder = ImpactMeasureBuilder.createEmpty();
300       context.getChildrenTextValues().stream()
301         .map(ImpactMeasureBuilder::fromString)
302         .forEach(impactMeasureBuilder::add);
303       context.getText(formula.getMetric()).ifPresent(value -> impactMeasureBuilder.add(ImpactMeasureBuilder.fromString(value)));
304       context.setValue(impactMeasureBuilder.buildAsString());
305     }
306   }
307
308   @Override
309   public List<MeasureUpdateFormula> getFormulas() {
310     return FORMULAS;
311   }
312
313   @Override
314   public Set<Metric> getFormulaMetrics() {
315     return FORMULA_METRICS;
316   }
317 }