]> source.dussan.org Git - sonarqube.git/blob
6860cc81b473c4dc4da09adcc616ab312d6db1c6
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2016 SonarSource SA
4  * mailto:contact 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.computation.qualitymodel;
21
22 import com.google.common.base.Optional;
23 import org.sonar.api.ce.measure.Issue;
24 import org.sonar.api.measures.CoreMetrics;
25 import org.sonar.api.rule.Severity;
26 import org.sonar.api.utils.Duration;
27 import org.sonar.api.utils.log.Logger;
28 import org.sonar.api.utils.log.Loggers;
29 import org.sonar.server.computation.component.Component;
30 import org.sonar.server.computation.component.CrawlerDepthLimit;
31 import org.sonar.server.computation.component.PathAwareVisitorAdapter;
32 import org.sonar.server.computation.issue.ComponentIssuesRepository;
33 import org.sonar.server.computation.measure.Measure;
34 import org.sonar.server.computation.measure.MeasureRepository;
35 import org.sonar.server.computation.metric.Metric;
36 import org.sonar.server.computation.metric.MetricRepository;
37 import org.sonar.server.computation.qualitymodel.RatingGrid.Rating;
38
39 import static org.sonar.api.measures.CoreMetrics.DEVELOPMENT_COST_KEY;
40 import static org.sonar.api.measures.CoreMetrics.EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_KEY;
41 import static org.sonar.api.measures.CoreMetrics.EFFORT_TO_REACH_RELIABILITY_RATING_A_KEY;
42 import static org.sonar.api.measures.CoreMetrics.EFFORT_TO_REACH_SECURITY_RATING_A_KEY;
43 import static org.sonar.api.measures.CoreMetrics.NCLOC_KEY;
44 import static org.sonar.api.measures.CoreMetrics.RELIABILITY_RATING_KEY;
45 import static org.sonar.api.measures.CoreMetrics.SECURITY_RATING_KEY;
46 import static org.sonar.api.measures.CoreMetrics.SQALE_DEBT_RATIO_KEY;
47 import static org.sonar.api.measures.CoreMetrics.SQALE_RATING_KEY;
48 import static org.sonar.api.measures.CoreMetrics.TECHNICAL_DEBT_KEY;
49 import static org.sonar.api.rule.Severity.BLOCKER;
50 import static org.sonar.api.rule.Severity.CRITICAL;
51 import static org.sonar.api.rule.Severity.MAJOR;
52 import static org.sonar.api.rule.Severity.MINOR;
53 import static org.sonar.api.rules.RuleType.BUG;
54 import static org.sonar.api.rules.RuleType.VULNERABILITY;
55 import static org.sonar.server.computation.component.ComponentVisitor.Order.POST_ORDER;
56 import static org.sonar.server.computation.measure.Measure.newMeasureBuilder;
57
58 /**
59  * Compute following measures :
60  * {@link CoreMetrics#DEVELOPMENT_COST_KEY}
61  * {@link CoreMetrics#SQALE_DEBT_RATIO_KEY}
62  * {@link CoreMetrics#SQALE_RATING_KEY}
63  * {@link CoreMetrics#EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_KEY}
64  * {@link CoreMetrics#RELIABILITY_RATING_KEY}
65  * {@link CoreMetrics#EFFORT_TO_REACH_RELIABILITY_RATING_A_KEY}
66  * {@link CoreMetrics#SECURITY_RATING_KEY}
67  * {@link CoreMetrics#EFFORT_TO_REACH_SECURITY_RATING_A_KEY}
68  */
69 public class QualityModelMeasuresVisitor extends PathAwareVisitorAdapter<QualityModelMeasuresVisitor.QualityModelCounter> {
70   private static final Logger LOG = Loggers.get(QualityModelMeasuresVisitor.class);
71
72   private final MeasureRepository measureRepository;
73   private final ComponentIssuesRepository componentIssuesRepository;
74   private final RatingSettings ratingSettings;
75   private final RatingGrid ratingGrid;
76
77   private final Metric nclocMetric;
78   private final Metric developmentCostMetric;
79
80   private final Metric maintainabilityRemediationEffortMetric;
81   private final Metric debtRatioMetric;
82   private final Metric maintainabilityRatingMetric;
83   private final Metric effortToMaintainabilityRatingAMetric;
84   private final Metric reliabilityRatingMetric;
85   private final Metric effortToReliabilityRatingAMetric;
86   private final Metric securityRatingMetric;
87   private final Metric effortToSecurityRatingAMetric;
88
89   public QualityModelMeasuresVisitor(MetricRepository metricRepository, MeasureRepository measureRepository, RatingSettings ratingSettings,
90     ComponentIssuesRepository componentIssuesRepository) {
91     super(CrawlerDepthLimit.LEAVES, POST_ORDER, DevelopmentCostCounterFactory.INSTANCE);
92     this.measureRepository = measureRepository;
93     this.componentIssuesRepository = componentIssuesRepository;
94     this.ratingSettings = ratingSettings;
95     this.ratingGrid = ratingSettings.getRatingGrid();
96
97     // Input metrics
98     this.nclocMetric = metricRepository.getByKey(NCLOC_KEY);
99     this.maintainabilityRemediationEffortMetric = metricRepository.getByKey(TECHNICAL_DEBT_KEY);
100
101     // Output metrics
102     this.developmentCostMetric = metricRepository.getByKey(DEVELOPMENT_COST_KEY);
103     this.debtRatioMetric = metricRepository.getByKey(SQALE_DEBT_RATIO_KEY);
104     this.maintainabilityRatingMetric = metricRepository.getByKey(SQALE_RATING_KEY);
105     this.reliabilityRatingMetric = metricRepository.getByKey(RELIABILITY_RATING_KEY);
106     this.securityRatingMetric = metricRepository.getByKey(SECURITY_RATING_KEY);
107     this.effortToMaintainabilityRatingAMetric = metricRepository.getByKey(EFFORT_TO_REACH_MAINTAINABILITY_RATING_A_KEY);
108     this.effortToReliabilityRatingAMetric = metricRepository.getByKey(EFFORT_TO_REACH_RELIABILITY_RATING_A_KEY);
109     this.effortToSecurityRatingAMetric = metricRepository.getByKey(EFFORT_TO_REACH_SECURITY_RATING_A_KEY);
110   }
111
112   @Override
113   public void visitProject(Component project, Path<QualityModelCounter> path) {
114     computeAndSaveMeasures(project, path);
115   }
116
117   @Override
118   public void visitDirectory(Component directory, Path<QualityModelCounter> path) {
119     computeAndSaveMeasures(directory, path);
120   }
121
122   @Override
123   public void visitModule(Component module, Path<QualityModelCounter> path) {
124     computeAndSaveMeasures(module, path);
125   }
126
127   @Override
128   public void visitFile(Component file, Path<QualityModelCounter> path) {
129     if (!file.getFileAttributes().isUnitTest()) {
130       path.current().addDevCosts(computeDevelopmentCost(file));
131       computeAndSaveMeasures(file, path);
132     }
133   }
134
135   private long computeDevelopmentCost(Component file) {
136     Optional<Measure> measure = measureRepository.getRawMeasure(file, nclocMetric);
137     long ncloc = measure.isPresent() ? measure.get().getIntValue() : 0;
138     return ncloc * ratingSettings.getDevCost(file.getFileAttributes().getLanguageKey());
139   }
140
141   @Override
142   public void visitView(Component view, Path<QualityModelCounter> path) {
143     computeAndSaveMeasures(view, path);
144   }
145
146   @Override
147   public void visitSubView(Component subView, Path<QualityModelCounter> path) {
148     computeAndSaveMeasures(subView, path);
149   }
150
151   @Override
152   public void visitProjectView(Component projectView, Path<QualityModelCounter> path) {
153     Optional<Measure> developmentCostMeasure = measureRepository.getRawMeasure(projectView, developmentCostMetric);
154     if (developmentCostMeasure.isPresent()) {
155       try {
156         path.parent().addDevCosts(Long.valueOf(developmentCostMeasure.get().getStringValue()));
157       } catch (NumberFormatException e) {
158         LOG.trace("Failed to parse value of metric {} for component {}", developmentCostMetric.getName(), projectView.getKey());
159       }
160     }
161     Optional<Measure> reliabilityRatingMeasure = measureRepository.getRawMeasure(projectView, reliabilityRatingMetric);
162     if (reliabilityRatingMeasure.isPresent()) {
163       path.parent().addReliabilityRating(Rating.valueOf(reliabilityRatingMeasure.get().getData()));
164     }
165     Optional<Measure> securityRatingMeasure = measureRepository.getRawMeasure(projectView, securityRatingMetric);
166     if (securityRatingMeasure.isPresent()) {
167       path.parent().addSecurityRating(Rating.valueOf(securityRatingMeasure.get().getData()));
168     }
169     Optional<Measure> effortToReliabilityRatingAMeasure = measureRepository.getRawMeasure(projectView, effortToReliabilityRatingAMetric);
170     if (effortToReliabilityRatingAMeasure.isPresent()) {
171       path.parent().addEffortToReachReliabilityRatingA(effortToReliabilityRatingAMeasure.get().getLongValue());
172     }
173     Optional<Measure> effortToSecurityRatingAMeasure = measureRepository.getRawMeasure(projectView, effortToSecurityRatingAMetric);
174     if (effortToSecurityRatingAMeasure.isPresent()) {
175       path.parent().addEffortToReachSecurityRatingA(effortToSecurityRatingAMeasure.get().getLongValue());
176     }
177   }
178
179   private void computeAndSaveMeasures(Component component, Path<QualityModelCounter> path) {
180     addIssues(component, path);
181     addDevelopmentCostMeasure(component, path.current());
182
183     double density = computeDensity(component, path.current());
184     addDebtRatioMeasure(component, density);
185     addMaintainabilityRatingMeasure(component, density);
186     addEffortToMaintainabilityRatingAMeasure(component, path);
187     addReliabilityRatingMeasure(component, path);
188     addEffortToReliabilityRatingAMeasure(component, path);
189     addSecurityRatingMeasure(component, path);
190     addEffortToSecurityRatingAMeasure(component, path);
191
192     addToParent(path);
193   }
194
195   private void addIssues(Component component, Path<QualityModelCounter> path){
196     for (Issue issue : componentIssuesRepository.getIssues(component)) {
197       if (issue.resolution() == null) {
198         path.current().addIssue(issue);
199       }
200     }
201   }
202
203   private double computeDensity(Component component, QualityModelCounter developmentCost) {
204     Optional<Measure> measure = measureRepository.getRawMeasure(component, maintainabilityRemediationEffortMetric);
205     double maintainabilityRemediationEffort = measure.isPresent() ? measure.get().getLongValue() : 0L;
206     if (Double.doubleToRawLongBits(developmentCost.devCosts) != 0L) {
207       return maintainabilityRemediationEffort / (double) developmentCost.devCosts;
208     }
209     return 0d;
210   }
211
212   private void addDevelopmentCostMeasure(Component component, QualityModelCounter developmentCost) {
213     // the value of this measure is stored as a string because it can exceed the size limit of number storage on some DB
214     measureRepository.add(component, developmentCostMetric, newMeasureBuilder().create(Long.toString(developmentCost.devCosts)));
215   }
216
217   private void addDebtRatioMeasure(Component component, double density) {
218     measureRepository.add(component, debtRatioMetric, newMeasureBuilder().create(100.0 * density, debtRatioMetric.getDecimalScale()));
219   }
220
221   private void addMaintainabilityRatingMeasure(Component component, double density) {
222     Rating rating = ratingGrid.getRatingForDensity(density);
223     measureRepository.add(component, maintainabilityRatingMetric, newMeasureBuilder().create(rating.getIndex(), rating.name()));
224   }
225
226   private void addReliabilityRatingMeasure(Component component, Path<QualityModelCounter> path) {
227     Rating rating = path.current().reliabilityRating;
228     measureRepository.add(component, reliabilityRatingMetric, newMeasureBuilder().create(rating.getIndex(), rating.name()));
229   }
230
231   private void addSecurityRatingMeasure(Component component, Path<QualityModelCounter> path) {
232     Rating rating = path.current().securityRating;
233     measureRepository.add(component, securityRatingMetric, newMeasureBuilder().create(rating.getIndex(), rating.name()));
234   }
235
236   private void addEffortToMaintainabilityRatingAMeasure(Component component, Path<QualityModelCounter> path) {
237     long developmentCostValue = path.current().devCosts;
238     Optional<Measure> effortMeasure = measureRepository.getRawMeasure(component, maintainabilityRemediationEffortMetric);
239     long effort = effortMeasure.isPresent() ? effortMeasure.get().getLongValue() : 0L;
240     long upperGradeCost = ((Double) (ratingGrid.getGradeLowerBound(Rating.B) * developmentCostValue)).longValue();
241     long effortToRatingA = upperGradeCost < effort ? (effort - upperGradeCost) : 0L;
242     measureRepository.add(component, effortToMaintainabilityRatingAMetric, Measure.newMeasureBuilder().create(effortToRatingA));
243   }
244
245   private void addEffortToReliabilityRatingAMeasure(Component component, Path<QualityModelCounter> path) {
246     measureRepository.add(component, effortToReliabilityRatingAMetric, Measure.newMeasureBuilder().create(path.current().effortToReachReliabilityRatingA));
247   }
248
249   private void addEffortToSecurityRatingAMeasure(Component component, Path<QualityModelCounter> path) {
250     measureRepository.add(component, effortToSecurityRatingAMetric, Measure.newMeasureBuilder().create(path.current().effortToReachSecurityRatingA));
251   }
252
253   private static void addToParent(Path<QualityModelCounter> path) {
254     if (!path.isRoot()) {
255       path.parent().add(path.current());
256     }
257   }
258
259   public static final class QualityModelCounter {
260     private long devCosts = 0;
261     private Rating reliabilityRating = Rating.A;
262     private long effortToReachReliabilityRatingA = 0L;
263     private Rating securityRating = Rating.A;
264     private long effortToReachSecurityRatingA = 0L;
265
266     private QualityModelCounter() {
267       // prevents instantiation
268     }
269
270     public void add(QualityModelCounter otherCounter) {
271       addDevCosts(otherCounter.devCosts);
272       addReliabilityRating(otherCounter.reliabilityRating);
273       addSecurityRating(otherCounter.securityRating);
274       addEffortToReachReliabilityRatingA(otherCounter.effortToReachReliabilityRatingA);
275       addEffortToReachSecurityRatingA(otherCounter.effortToReachSecurityRatingA);
276     }
277
278     public void addDevCosts(long developmentCosts) {
279       this.devCosts += developmentCosts;
280     }
281
282     public void addIssue(Issue issue) {
283       Rating rating = getRatingFromSeverity(issue.severity());
284       if (issue.type().equals(BUG)) {
285         addReliabilityRating(rating);
286         addEffortToReachReliabilityRatingA(getEffortForNotMinorIssue(issue));
287       } else if (issue.type().equals(VULNERABILITY)) {
288         addSecurityRating(rating);
289         addEffortToReachSecurityRatingA(getEffortForNotMinorIssue(issue));
290       }
291     }
292
293     private void addReliabilityRating(Rating rating) {
294       if (reliabilityRating.compareTo(rating) > 0) {
295         reliabilityRating = rating;
296       }
297     }
298
299     private void addSecurityRating(Rating rating) {
300       if (securityRating.compareTo(rating) > 0) {
301         securityRating = rating;
302       }
303     }
304
305     public void addEffortToReachReliabilityRatingA(long reliabilityEffort) {
306       this.effortToReachReliabilityRatingA += reliabilityEffort;
307     }
308
309     public void addEffortToReachSecurityRatingA(long securityEffort) {
310       this.effortToReachSecurityRatingA += securityEffort;
311     }
312
313     private static long getEffortForNotMinorIssue(Issue issue) {
314       Duration debt = issue.debt();
315       if (!issue.severity().equals(Severity.INFO) && debt != null) {
316         return debt.toMinutes();
317       }
318       return 0L;
319     }
320
321     private static Rating getRatingFromSeverity(String severity) {
322       switch (severity) {
323         case BLOCKER:
324           return Rating.E;
325         case CRITICAL:
326           return Rating.D;
327         case MAJOR:
328           return Rating.C;
329         case MINOR:
330           return Rating.B;
331         default:
332           return Rating.A;
333       }
334     }
335   }
336
337   private static final class DevelopmentCostCounterFactory extends SimpleStackElementFactory<QualityModelCounter> {
338     public static final DevelopmentCostCounterFactory INSTANCE = new DevelopmentCostCounterFactory();
339
340     private DevelopmentCostCounterFactory() {
341       // prevents instantiation
342     }
343
344     @Override
345     public QualityModelCounter createForAny(Component component) {
346       return new QualityModelCounter();
347     }
348
349     /** Counter is not used at ProjectView level, saves on instantiating useless objects */
350     @Override
351     public QualityModelCounter createForProjectView(Component projectView) {
352       return null;
353     }
354   }
355 }