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.ce.task.projectanalysis.qualitymodel;
22 import java.util.Optional;
23 import org.sonar.api.measures.CoreMetrics;
24 import org.sonar.ce.task.projectanalysis.component.Component;
25 import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
26 import org.sonar.ce.task.projectanalysis.component.PathAwareVisitorAdapter;
27 import org.sonar.ce.task.projectanalysis.formula.counter.RatingValue;
28 import org.sonar.ce.task.projectanalysis.measure.Measure;
29 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
30 import org.sonar.ce.task.projectanalysis.measure.RatingMeasures;
31 import org.sonar.ce.task.projectanalysis.metric.Metric;
32 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
33 import org.sonar.server.measure.Rating;
35 import static org.sonar.api.measures.CoreMetrics.DEVELOPMENT_COST_KEY;
36 import static org.sonar.api.measures.CoreMetrics.EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_KEY;
37 import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
38 import static org.sonar.api.measures.CoreMetrics.SQALE_DEBT_RATIO_KEY;
39 import static org.sonar.api.measures.CoreMetrics.SQALE_RATING_KEY;
40 import static org.sonar.api.measures.CoreMetrics.TECHNICAL_DEBT_KEY;
41 import static org.sonar.ce.task.projectanalysis.measure.Measure.newMeasureBuilder;
44 * Compute measures related to maintainability for projects and descendants :
45 * {@link CoreMetrics#DEVELOPMENT_COST_KEY}
46 * {@link CoreMetrics#SQALE_DEBT_RATIO_KEY}
47 * {@link CoreMetrics#SQALE_RATING_KEY}
48 * {@link CoreMetrics#EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_KEY}
50 public class MaintainabilityMeasuresVisitor extends PathAwareVisitorAdapter<MaintainabilityMeasuresVisitor.Counter> {
51 private final MeasureRepository measureRepository;
52 private final RatingSettings ratingSettings;
54 private final Metric nclocMetric;
55 private final Metric developmentCostMetric;
57 private final Metric maintainabilityRemediationEffortMetric;
58 private final Metric debtRatioMetric;
59 private final Metric maintainabilityRatingMetric;
60 private final Metric effortToMaintainabilityRatingAMetric;
62 public MaintainabilityMeasuresVisitor(MetricRepository metricRepository, MeasureRepository measureRepository, RatingSettings ratingSettings) {
63 super(CrawlerDepthLimit.FILE, Order.POST_ORDER, CounterFactory.INSTANCE);
64 this.measureRepository = measureRepository;
65 this.ratingSettings = ratingSettings;
68 this.nclocMetric = metricRepository.getByKey(NCLOC_KEY);
69 this.maintainabilityRemediationEffortMetric = metricRepository.getByKey(TECHNICAL_DEBT_KEY);
72 this.developmentCostMetric = metricRepository.getByKey(DEVELOPMENT_COST_KEY);
73 this.debtRatioMetric = metricRepository.getByKey(SQALE_DEBT_RATIO_KEY);
74 this.maintainabilityRatingMetric = metricRepository.getByKey(SQALE_RATING_KEY);
75 this.effortToMaintainabilityRatingAMetric = metricRepository.getByKey(EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_KEY);
79 public void visitProject(Component project, Path<Counter> path) {
80 computeAndSaveMeasures(project, path);
84 public void visitDirectory(Component directory, Path<Counter> path) {
85 computeAndSaveMeasures(directory, path);
89 public void visitFile(Component file, Path<Counter> path) {
90 path.current().addDevCosts(computeDevelopmentCost(file));
91 computeAndSaveMeasures(file, path);
94 private long computeDevelopmentCost(Component file) {
95 Optional<Measure> measure = measureRepository.getRawMeasure(file, nclocMetric);
96 long ncloc = measure.map(Measure::getIntValue).orElse(0);
97 return ncloc * ratingSettings.getDevCost();
100 private void computeAndSaveMeasures(Component component, Path<Counter> path) {
101 addDevelopmentCostMeasure(component, path.current());
103 double density = computeDensity(component, path.current());
104 addDebtRatioMeasure(component, density);
105 addMaintainabilityRatingMeasure(component, density);
106 addEffortToMaintainabilityRatingAMeasure(component, path);
111 private double computeDensity(Component component, Counter developmentCost) {
112 Optional<Measure> measure = measureRepository.getRawMeasure(component, maintainabilityRemediationEffortMetric);
113 double maintainabilityRemediationEffort = measure.isPresent() ? measure.get().getLongValue() : 0L;
114 if (Double.doubleToRawLongBits(developmentCost.devCosts) != 0L) {
115 return maintainabilityRemediationEffort / developmentCost.devCosts;
120 private void addDevelopmentCostMeasure(Component component, Counter developmentCost) {
121 // the value of this measure is stored as a string because it can exceed the size limit of number storage on some DB
122 measureRepository.add(component, developmentCostMetric, newMeasureBuilder().create(Long.toString(developmentCost.devCosts)));
125 private void addDebtRatioMeasure(Component component, double density) {
126 measureRepository.add(component, debtRatioMetric, newMeasureBuilder().create(100.0 * density, debtRatioMetric.getDecimalScale()));
129 private void addMaintainabilityRatingMeasure(Component component, double density) {
130 Rating rating = ratingSettings.getDebtRatingGrid().getRatingForDensity(density);
131 measureRepository.add(component, maintainabilityRatingMetric, RatingMeasures.get(rating));
134 private void addEffortToMaintainabilityRatingAMeasure(Component component, Path<Counter> path) {
135 long developmentCostValue = path.current().devCosts;
136 Optional<Measure> effortMeasure = measureRepository.getRawMeasure(component, maintainabilityRemediationEffortMetric);
137 long effort = effortMeasure.isPresent() ? effortMeasure.get().getLongValue() : 0L;
138 long upperGradeCost = ((Double) (ratingSettings.getDebtRatingGrid().getGradeLowerBound(Rating.B) * developmentCostValue)).longValue();
139 long effortToRatingA = upperGradeCost < effort ? (effort - upperGradeCost) : 0L;
140 measureRepository.add(component, effortToMaintainabilityRatingAMetric, Measure.newMeasureBuilder().create(effortToRatingA));
143 private static void addToParent(Path<Counter> path) {
144 if (!path.isRoot()) {
145 path.parent().add(path.current());
149 public static final class Counter {
150 private long devCosts = 0;
151 private RatingValue reliabilityRating = new RatingValue();
152 private RatingValue securityRating = new RatingValue();
155 // prevents instantiation
158 void add(Counter otherCounter) {
159 addDevCosts(otherCounter.devCosts);
160 reliabilityRating.increment(otherCounter.reliabilityRating);
161 securityRating.increment(otherCounter.securityRating);
164 void addDevCosts(long developmentCosts) {
165 this.devCosts += developmentCosts;
169 private static final class CounterFactory extends SimpleStackElementFactory<Counter> {
170 public static final CounterFactory INSTANCE = new CounterFactory();
172 private CounterFactory() {
173 // prevents instantiation
177 public Counter createForAny(Component component) {
178 return new Counter();