]> source.dussan.org Git - sonarqube.git/blob
196d647bf79a759acfd5f766a8a3c5decac11679
[sonarqube.git] /
1 /*
2  * SonarQube, open source software quality management tool.
3  * Copyright (C) 2008-2013 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
6  * SonarQube 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  * SonarQube 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.plugins.core.issue;
21
22 import com.google.common.annotations.VisibleForTesting;
23 import com.google.common.collect.*;
24 import org.apache.commons.lang.time.DateUtils;
25 import org.sonar.api.batch.*;
26 import org.sonar.api.component.ResourcePerspectives;
27 import org.sonar.api.issue.Issuable;
28 import org.sonar.api.issue.Issue;
29 import org.sonar.api.measures.*;
30 import org.sonar.api.resources.Project;
31 import org.sonar.api.resources.Resource;
32 import org.sonar.api.resources.ResourceUtils;
33 import org.sonar.api.rules.Rule;
34 import org.sonar.api.rules.RuleFinder;
35 import org.sonar.api.rules.RulePriority;
36 import org.sonar.batch.components.Period;
37 import org.sonar.batch.components.TimeMachineConfiguration;
38
39 import javax.annotation.Nullable;
40
41 import java.util.*;
42
43 /**
44  * Computes metrics related to number of issues.
45  *
46  * @since 3.6
47  */
48 @DependsUpon(DecoratorBarriers.ISSUES_TRACKED)
49 public class CountUnresolvedIssuesDecorator implements Decorator {
50
51   private final ResourcePerspectives perspectives;
52   private final RuleFinder rulefinder;
53   private final TimeMachineConfiguration timeMachineConfiguration;
54
55   public CountUnresolvedIssuesDecorator(ResourcePerspectives perspectives, RuleFinder rulefinder, TimeMachineConfiguration timeMachineConfiguration) {
56     this.perspectives = perspectives;
57     this.rulefinder = rulefinder;
58     this.timeMachineConfiguration = timeMachineConfiguration;
59   }
60
61   public boolean shouldExecuteOnProject(Project project) {
62     return true;
63   }
64
65   @DependedUpon
66   public List<Metric> generatesIssuesMetrics() {
67     return ImmutableList.of(
68       CoreMetrics.VIOLATIONS,
69       CoreMetrics.BLOCKER_VIOLATIONS,
70       CoreMetrics.CRITICAL_VIOLATIONS,
71       CoreMetrics.MAJOR_VIOLATIONS,
72       CoreMetrics.MINOR_VIOLATIONS,
73       CoreMetrics.INFO_VIOLATIONS,
74       CoreMetrics.NEW_VIOLATIONS,
75       CoreMetrics.NEW_BLOCKER_VIOLATIONS,
76       CoreMetrics.NEW_CRITICAL_VIOLATIONS,
77       CoreMetrics.NEW_MAJOR_VIOLATIONS,
78       CoreMetrics.NEW_MINOR_VIOLATIONS,
79       CoreMetrics.NEW_INFO_VIOLATIONS,
80       CoreMetrics.OPEN_ISSUES,
81       CoreMetrics.REOPENED_ISSUES,
82       CoreMetrics.CONFIRMED_ISSUES
83     );
84   }
85
86   public void decorate(Resource resource, DecoratorContext context) {
87     Issuable issuable = perspectives.as(Issuable.class, resource);
88     if (issuable != null) {
89       Collection<Issue> issues = issuable.issues();
90       boolean shouldSaveNewMetrics = shouldSaveNewMetrics(context);
91
92       Multiset<RulePriority> severityBag = HashMultiset.create();
93       Map<RulePriority, Multiset<Rule>> rulesPerSeverity = Maps.newHashMap();
94       ListMultimap<RulePriority, Issue> issuesPerSeverity = ArrayListMultimap.create();
95       int countOpen = 0;
96       int countReopened = 0;
97       int countConfirmed = 0;
98
99       for (Issue issue : issues) {
100         severityBag.add(RulePriority.valueOf(issue.severity()));
101         Multiset<Rule> rulesBag = initRules(rulesPerSeverity, RulePriority.valueOf(issue.severity()));
102         rulesBag.add(rulefinder.findByKey(issue.ruleKey().repository(), issue.ruleKey().rule()));
103         issuesPerSeverity.put(RulePriority.valueOf(issue.severity()), issue);
104
105         if (Issue.STATUS_OPEN.equals(issue.status())) {
106           countOpen++;
107         }
108         if (Issue.STATUS_REOPENED.equals(issue.status())) {
109           countReopened++;
110         }
111         if (Issue.STATUS_CONFIRMED.equals(issue.status())) {
112           countConfirmed++;
113         }
114       }
115
116       for (RulePriority ruleSeverity : RulePriority.values()) {
117         saveIssuesForSeverity(context, ruleSeverity, severityBag);
118         saveIssuesPerRules(context, ruleSeverity, rulesPerSeverity);
119         saveNewIssuesForSeverity(context, ruleSeverity, issuesPerSeverity, shouldSaveNewMetrics);
120         saveNewIssuesPerRule(context, ruleSeverity, issues, shouldSaveNewMetrics);
121       }
122
123       saveTotalIssues(context, issues);
124       saveNewIssues(context, issues, shouldSaveNewMetrics);
125
126       saveMeasure(context, CoreMetrics.OPEN_ISSUES, countOpen);
127       saveMeasure(context, CoreMetrics.REOPENED_ISSUES, countReopened);
128       saveMeasure(context, CoreMetrics.CONFIRMED_ISSUES, countConfirmed);
129     }
130   }
131
132   private void saveTotalIssues(DecoratorContext context, Collection<Issue> issues) {
133     if (context.getMeasure(CoreMetrics.VIOLATIONS) == null) {
134       Collection<Measure> childrenIssues = context.getChildrenMeasures(CoreMetrics.VIOLATIONS);
135       Double sum = MeasureUtils.sum(true, childrenIssues);
136       context.saveMeasure(CoreMetrics.VIOLATIONS, sum + issues.size());
137     }
138   }
139
140   private void saveNewIssues(DecoratorContext context, Collection<Issue> issues, boolean shouldSaveNewMetrics) {
141     if (shouldSaveNewMetrics) {
142       Measure measure = new Measure(CoreMetrics.NEW_VIOLATIONS);
143       saveNewIssues(context, measure, issues);
144     }
145   }
146
147   private void saveIssuesForSeverity(DecoratorContext context, RulePriority ruleSeverity, Multiset<RulePriority> severitiesBag) {
148     Metric metric = SeverityUtils.severityToIssueMetric(ruleSeverity);
149     if (context.getMeasure(metric) == null) {
150       Collection<Measure> children = context.getChildrenMeasures(MeasuresFilters.metric(metric));
151       int sum = MeasureUtils.sum(true, children).intValue() + severitiesBag.count(ruleSeverity);
152       context.saveMeasure(metric, (double) sum);
153     }
154   }
155
156   private void saveNewIssuesForSeverity(DecoratorContext context, RulePriority severity, ListMultimap<RulePriority, Issue> issuesPerSeverities, boolean shouldSaveNewMetrics) {
157     if (shouldSaveNewMetrics) {
158       Metric metric = SeverityUtils.severityToNewMetricIssue(severity);
159       Measure measure = new Measure(metric);
160       saveNewIssues(context, measure, issuesPerSeverities.get(severity));
161     }
162   }
163
164   private void saveIssuesPerRules(DecoratorContext context, RulePriority severity, Map<RulePriority, Multiset<Rule>> rulesPerSeverity) {
165     Metric metric = SeverityUtils.severityToIssueMetric(severity);
166
167     Collection<Measure> children = context.getChildrenMeasures(MeasuresFilters.rules(metric));
168     for (Measure child : children) {
169       RuleMeasure childRuleMeasure = (RuleMeasure) child;
170       Rule rule = childRuleMeasure.getRule();
171       if (rule != null && MeasureUtils.hasValue(childRuleMeasure)) {
172         Multiset<Rule> rulesBag = initRules(rulesPerSeverity, severity);
173         rulesBag.add(rule, childRuleMeasure.getIntValue());
174       }
175     }
176
177     Multiset<Rule> rulesBag = rulesPerSeverity.get(severity);
178     if (rulesBag != null) {
179       for (Multiset.Entry<Rule> entry : rulesBag.entrySet()) {
180         RuleMeasure measure = RuleMeasure.createForRule(metric, entry.getElement(), (double) entry.getCount());
181         measure.setSeverity(severity);
182         context.saveMeasure(measure);
183       }
184     }
185   }
186
187   private void saveNewIssuesPerRule(DecoratorContext context, RulePriority severity, Collection<Issue> issues, boolean shouldSaveNewMetrics) {
188     if (shouldSaveNewMetrics) {
189       Metric metric = SeverityUtils.severityToNewMetricIssue(severity);
190       ListMultimap<Rule, Measure> childMeasuresPerRule = ArrayListMultimap.create();
191       ListMultimap<Rule, Issue> issuesPerRule = ArrayListMultimap.create();
192       Set<Rule> rules = Sets.newHashSet();
193
194       Collection<Measure> children = context.getChildrenMeasures(MeasuresFilters.rules(metric));
195       for (Measure child : children) {
196         RuleMeasure childRuleMeasure = (RuleMeasure) child;
197         Rule rule = childRuleMeasure.getRule();
198         if (rule != null) {
199           childMeasuresPerRule.put(rule, childRuleMeasure);
200           rules.add(rule);
201         }
202       }
203
204       for (Issue issue : issues) {
205         if (RulePriority.valueOf(issue.severity()).equals(severity)) {
206           Rule rule = rulefinder.findByKey(issue.ruleKey().repository(), issue.ruleKey().rule());
207           rules.add(rule);
208           issuesPerRule.put(rule, issue);
209         }
210       }
211
212       for (Rule rule : rules) {
213         RuleMeasure measure = RuleMeasure.createForRule(metric, rule, null);
214         measure.setSeverity(severity);
215         for (Period period : timeMachineConfiguration.periods()) {
216           int variationIndex = period.getIndex();
217           double sum = MeasureUtils.sumOnVariation(true, variationIndex, childMeasuresPerRule.get(rule)) + countIssues(issuesPerRule.get(rule), period);
218           measure.setVariation(variationIndex, sum);
219         }
220         context.saveMeasure(measure);
221       }
222     }
223   }
224
225   private void saveNewIssues(DecoratorContext context, Measure measure, Collection<Issue> issues) {
226     for (Period period : timeMachineConfiguration.periods()) {
227       int variationIndex = period.getIndex();
228       Collection<Measure> children = context.getChildrenMeasures(measure.getMetric());
229       double sum = MeasureUtils.sumOnVariation(true, variationIndex, children) + countIssues(issues, period);
230       measure.setVariation(variationIndex, sum);
231     }
232     context.saveMeasure(measure);
233   }
234
235   private void saveMeasure(DecoratorContext context, Metric metric, int value) {
236     context.saveMeasure(metric, (double) (value + sumChildren(context, metric)));
237   }
238
239   private int sumChildren(DecoratorContext context, Metric metric) {
240     int sum = 0;
241     if (!ResourceUtils.isFile(context.getResource())) {
242       sum = MeasureUtils.sum(true, context.getChildrenMeasures(metric)).intValue();
243     }
244     return sum;
245   }
246
247   private Multiset<Rule> initRules(Map<RulePriority, Multiset<Rule>> rulesPerSeverity, RulePriority severity) {
248     Multiset<Rule> rulesBag = rulesPerSeverity.get(severity);
249     if (rulesBag == null) {
250       rulesBag = HashMultiset.create();
251       rulesPerSeverity.put(severity, rulesBag);
252     }
253     return rulesBag;
254   }
255
256   private int countIssues(Collection<Issue> issues, Period period) {
257     // SONAR-3647 Use real snapshot date and not target date in order to stay consistent with other measure variations
258     Date datePlusOneSecond = period.getDate() != null ? DateUtils.addSeconds(period.getDate(), 1) : null;
259     return countIssuesAfterDate(issues, datePlusOneSecond);
260   }
261
262   @VisibleForTesting
263   int countIssuesAfterDate(Collection<Issue> issues, @Nullable Date targetDate) {
264     if (issues == null) {
265       return 0;
266     }
267     int count = 0;
268     for (Issue issue : issues) {
269       if (isAfter(issue, targetDate)) {
270         count++;
271       }
272     }
273     return count;
274   }
275
276   private boolean isAfter(Issue issue, @Nullable Date date) {
277     return date == null || (issue.creationDate() != null && DateUtils.truncatedCompareTo(issue.creationDate(), date, Calendar.SECOND) > 0);
278   }
279
280   private boolean shouldSaveNewMetrics(DecoratorContext context) {
281     return context.getMeasure(CoreMetrics.NEW_VIOLATIONS) == null;
282   }
283
284   @Override
285   public String toString() {
286     return getClass().getSimpleName();
287   }
288 }