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.server.measure.live;
22 import java.util.List;
23 import java.util.Objects;
24 import java.util.Optional;
25 import org.sonar.api.config.Configuration;
26 import org.sonar.api.measures.Metric;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbSession;
29 import org.sonar.db.component.BranchDto;
30 import org.sonar.db.component.BranchType;
31 import org.sonar.db.component.ComponentDto;
32 import org.sonar.db.component.SnapshotDto;
33 import org.sonar.db.measure.LiveMeasureDto;
34 import org.sonar.server.measure.DebtRatingGrid;
35 import org.sonar.server.measure.Rating;
37 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_KEY;
38 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_REVIEWED_KEY;
39 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_REVIEWED_STATUS_KEY;
40 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_TO_REVIEW_STATUS_KEY;
41 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_KEY;
42 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_REVIEWED_KEY;
43 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_REVIEWED_STATUS_KEY;
44 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_TO_REVIEW_STATUS_KEY;
45 import static org.sonar.db.newcodeperiod.NewCodePeriodType.REFERENCE_BRANCH;
47 public class LiveMeasureTreeUpdaterImpl implements LiveMeasureTreeUpdater {
48 private final DbClient dbClient;
49 private final MeasureUpdateFormulaFactory formulaFactory;
51 public LiveMeasureTreeUpdaterImpl(DbClient dbClient, MeasureUpdateFormulaFactory formulaFactory) {
52 this.dbClient = dbClient;
53 this.formulaFactory = formulaFactory;
57 public void update(DbSession dbSession, SnapshotDto lastAnalysis, Configuration config, ComponentIndex components, BranchDto branch, MeasureMatrix measures) {
58 long beginningOfLeak = getBeginningOfLeakPeriod(lastAnalysis, branch);
59 boolean shouldUseLeakFormulas = shouldUseLeakFormulas(lastAnalysis, branch);
61 // 1. set new measure from issues to each component from touched components to the root
62 updateMatrixWithIssues(dbSession, measures, components, config, shouldUseLeakFormulas, beginningOfLeak);
64 // 2. aggregate new measures up the component tree
65 updateMatrixWithHierarchy(measures, components, config, shouldUseLeakFormulas);
68 private void updateMatrixWithHierarchy(MeasureMatrix matrix, ComponentIndex components, Configuration config, boolean useLeakFormulas) {
69 DebtRatingGrid debtRatingGrid = new DebtRatingGrid(config);
70 FormulaContextImpl context = new FormulaContextImpl(matrix, components, debtRatingGrid);
71 components.getSortedTree().forEach(c -> {
72 for (MeasureUpdateFormula formula : formulaFactory.getFormulas()) {
73 if (shouldComputeMetric(formula, useLeakFormulas, components.getBranch(), matrix)) {
74 context.change(c, formula);
76 formula.computeHierarchy(context);
77 } catch (RuntimeException e) {
78 throw new IllegalStateException("Fail to compute " + formula.getMetric().getKey() + " on "
79 + context.getComponent().getKey() + " (uuid: " + context.getComponent().uuid() + ")", e);
86 private void updateMatrixWithIssues(DbSession dbSession, MeasureMatrix matrix, ComponentIndex components, Configuration config, boolean useLeakFormulas, long beginningOfLeak) {
87 DebtRatingGrid debtRatingGrid = new DebtRatingGrid(config);
88 FormulaContextImpl context = new FormulaContextImpl(matrix, components, debtRatingGrid);
90 components.getSortedTree().forEach(c -> {
91 IssueCounter issueCounter = new IssueCounter(dbClient.issueDao().selectIssueGroupsByComponent(dbSession, c, beginningOfLeak),
92 dbClient.issueDao().selectIssueImpactGroupsByComponent(dbSession, c));
93 for (MeasureUpdateFormula formula : formulaFactory.getFormulas()) {
94 if (shouldComputeMetric(formula, useLeakFormulas, components.getBranch(), matrix)) {
95 context.change(c, formula);
97 formula.compute(context, issueCounter);
98 } catch (RuntimeException e) {
99 throw new IllegalStateException("Fail to compute " + formula.getMetric().getKey() + " on "
100 + context.getComponent().getKey() + " (uuid: " + context.getComponent().uuid() + ")", e);
107 private static boolean shouldComputeMetric(MeasureUpdateFormula formula, boolean useLeakFormulas, ComponentDto branchComponent,
108 MeasureMatrix matrix) {
109 // Use formula when the leak period is defined, it's a PR, or the formula is not about the leak period
110 return (useLeakFormulas || !formula.isOnLeak())
111 // Some metrics should only be computed if the metric has been computed on the branch before (during analysis).
112 // Otherwise, the computed measure would only apply to the touched components and be incomplete.
113 && (!formula.isOnlyIfComputedOnBranch() || matrix.getMeasure(branchComponent, formula.getMetric().getKey()).isPresent());
116 private static long getBeginningOfLeakPeriod(SnapshotDto lastAnalysis, BranchDto branch) {
119 } else if (REFERENCE_BRANCH.name().equals(lastAnalysis.getPeriodMode())) {
122 return Optional.ofNullable(lastAnalysis.getPeriodDate()).orElse(Long.MAX_VALUE);
126 private static boolean isPR(BranchDto branch) {
127 return branch.getBranchType() == BranchType.PULL_REQUEST;
130 private static boolean shouldUseLeakFormulas(SnapshotDto lastAnalysis, BranchDto branch) {
131 return lastAnalysis.getPeriodDate() != null || isPR(branch) || REFERENCE_BRANCH.name().equals(lastAnalysis.getPeriodMode());
134 public static class FormulaContextImpl implements MeasureUpdateFormula.Context {
135 private final MeasureMatrix matrix;
136 private final ComponentIndex componentIndex;
137 private final DebtRatingGrid debtRatingGrid;
138 private ComponentDto currentComponent;
139 private MeasureUpdateFormula currentFormula;
141 public FormulaContextImpl(MeasureMatrix matrix, ComponentIndex componentIndex, DebtRatingGrid debtRatingGrid) {
142 this.matrix = matrix;
143 this.componentIndex = componentIndex;
144 this.debtRatingGrid = debtRatingGrid;
147 void change(ComponentDto component, MeasureUpdateFormula formula) {
148 this.currentComponent = component;
149 this.currentFormula = formula;
152 public List<Double> getChildrenValues() {
153 List<ComponentDto> children = componentIndex.getChildren(currentComponent);
154 return children.stream()
155 .flatMap(c -> matrix.getMeasure(c, currentFormula.getMetric().getKey()).stream())
156 .map(LiveMeasureDto::getValue)
157 .filter(Objects::nonNull)
161 public List<String> getChildrenTextValues() {
162 List<ComponentDto> children = componentIndex.getChildren(currentComponent);
163 return children.stream()
164 .flatMap(c -> matrix.getMeasure(c, currentFormula.getMetric().getKey()).stream())
165 .map(LiveMeasureDto::getTextValue)
166 .filter(Objects::nonNull)
171 * Some child components may not have the measures 'SECURITY_HOTSPOTS_TO_REVIEW_STATUS' and 'SECURITY_HOTSPOTS_REVIEWED_STATUS' saved for them,
172 * so we may need to calculate them based on 'SECURITY_HOTSPOTS_REVIEWED' and 'SECURITY_HOTSPOTS'.
175 public long getChildrenHotspotsReviewed() {
176 return getChildrenHotspotsReviewed(SECURITY_HOTSPOTS_REVIEWED_STATUS_KEY, SECURITY_HOTSPOTS_REVIEWED_KEY, SECURITY_HOTSPOTS_KEY);
180 * Some child components may not have the measure 'SECURITY_HOTSPOTS_TO_REVIEW_STATUS_KEY'. We assume that 'SECURITY_HOTSPOTS_KEY' has the same value.
183 public long getChildrenHotspotsToReview() {
184 return componentIndex.getChildren(currentComponent)
186 .map(c -> matrix.getMeasure(c, SECURITY_HOTSPOTS_TO_REVIEW_STATUS_KEY).or(() -> matrix.getMeasure(c, SECURITY_HOTSPOTS_KEY)))
187 .mapToLong(lmOpt -> lmOpt.flatMap(lm -> Optional.ofNullable(lm.getValue())).orElse(0D).longValue())
192 public long getChildrenNewHotspotsReviewed() {
193 return getChildrenHotspotsReviewed(NEW_SECURITY_HOTSPOTS_REVIEWED_STATUS_KEY, NEW_SECURITY_HOTSPOTS_REVIEWED_KEY, NEW_SECURITY_HOTSPOTS_KEY);
197 * Some child components may not have the measure 'NEW_SECURITY_HOTSPOTS_TO_REVIEW_STATUS_KEY'. We assume that 'NEW_SECURITY_HOTSPOTS_KEY' has the same value.
200 public long getChildrenNewHotspotsToReview() {
201 return componentIndex.getChildren(currentComponent)
203 .map(c -> matrix.getMeasure(c, NEW_SECURITY_HOTSPOTS_TO_REVIEW_STATUS_KEY).or(() -> matrix.getMeasure(c, NEW_SECURITY_HOTSPOTS_KEY)))
204 .mapToLong(lmOpt -> lmOpt.flatMap(lm -> Optional.ofNullable(lm.getValue())).orElse(0D).longValue())
208 private long getChildrenHotspotsReviewed(String metricKey, String percMetricKey, String hotspotsMetricKey) {
209 return componentIndex.getChildren(currentComponent)
211 .mapToLong(c -> getHotspotsReviewed(c, metricKey, percMetricKey, hotspotsMetricKey))
215 private long getHotspotsReviewed(ComponentDto c, String metricKey, String percMetricKey, String hotspotsMetricKey) {
216 Optional<LiveMeasureDto> measure = matrix.getMeasure(c, metricKey);
217 return measure.map(lm -> Optional.ofNullable(lm.getValue()).orElse(0D).longValue())
218 .orElseGet(() -> matrix.getMeasure(c, percMetricKey)
219 .flatMap(percentage -> matrix.getMeasure(c, hotspotsMetricKey)
221 double perc = Optional.ofNullable(percentage.getValue()).orElse(0D) / 100D;
222 double toReview = Optional.ofNullable(hotspots.getValue()).orElse(0D);
223 double reviewed = (toReview * perc) / (1D - perc);
224 return Math.round(reviewed);
232 public ComponentDto getComponent() {
233 return currentComponent;
237 public DebtRatingGrid getDebtRatingGrid() {
238 return debtRatingGrid;
242 public Optional<Double> getValue(Metric metric) {
243 Optional<LiveMeasureDto> measure = matrix.getMeasure(currentComponent, metric.getKey());
244 return measure.map(LiveMeasureDto::getValue);
248 public Optional<String> getText(Metric metric) {
249 Optional<LiveMeasureDto> measure = matrix.getMeasure(currentComponent, metric.getKey());
250 return measure.map(LiveMeasureDto::getTextValue);
254 public void setValue(double value) {
255 String metricKey = currentFormula.getMetric().getKey();
256 matrix.setValue(currentComponent, metricKey, value);
260 public void setValue(Rating value) {
261 String metricKey = currentFormula.getMetric().getKey();
262 matrix.setValue(currentComponent, metricKey, value);
266 public void setValue(String value) {
267 String metricKey = currentFormula.getMetric().getKey();
268 matrix.setValue(currentComponent, metricKey, value);