2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2013 SonarSource
4 * mailto:contact AT sonarsource DOT com
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.
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.
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.plugins.core.issue;
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;
39 import javax.annotation.Nullable;
44 * Computes metrics related to number of issues.
48 @DependsUpon(DecoratorBarriers.ISSUES_TRACKED)
49 public class CountUnresolvedIssuesDecorator implements Decorator {
51 private final ResourcePerspectives perspectives;
52 private final RuleFinder rulefinder;
53 private final TimeMachineConfiguration timeMachineConfiguration;
55 public CountUnresolvedIssuesDecorator(ResourcePerspectives perspectives, RuleFinder rulefinder, TimeMachineConfiguration timeMachineConfiguration) {
56 this.perspectives = perspectives;
57 this.rulefinder = rulefinder;
58 this.timeMachineConfiguration = timeMachineConfiguration;
61 public boolean shouldExecuteOnProject(Project project) {
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
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);
92 Multiset<RulePriority> severityBag = HashMultiset.create();
93 Map<RulePriority, Multiset<Rule>> rulesPerSeverity = Maps.newHashMap();
94 ListMultimap<RulePriority, Issue> issuesPerSeverity = ArrayListMultimap.create();
96 int countReopened = 0;
97 int countConfirmed = 0;
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);
105 if (Issue.STATUS_OPEN.equals(issue.status())) {
108 if (Issue.STATUS_REOPENED.equals(issue.status())) {
111 if (Issue.STATUS_CONFIRMED.equals(issue.status())) {
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);
123 saveTotalIssues(context, issues);
124 saveNewIssues(context, issues, shouldSaveNewMetrics);
126 saveMeasure(context, CoreMetrics.OPEN_ISSUES, countOpen);
127 saveMeasure(context, CoreMetrics.REOPENED_ISSUES, countReopened);
128 saveMeasure(context, CoreMetrics.CONFIRMED_ISSUES, countConfirmed);
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());
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);
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);
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));
164 private void saveIssuesPerRules(DecoratorContext context, RulePriority severity, Map<RulePriority, Multiset<Rule>> rulesPerSeverity) {
165 Metric metric = SeverityUtils.severityToIssueMetric(severity);
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());
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);
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();
194 Collection<Measure> children = context.getChildrenMeasures(MeasuresFilters.rules(metric));
195 for (Measure child : children) {
196 RuleMeasure childRuleMeasure = (RuleMeasure) child;
197 Rule rule = childRuleMeasure.getRule();
199 childMeasuresPerRule.put(rule, childRuleMeasure);
204 for (Issue issue : issues) {
205 if (RulePriority.valueOf(issue.severity()).equals(severity)) {
206 Rule rule = rulefinder.findByKey(issue.ruleKey().repository(), issue.ruleKey().rule());
208 issuesPerRule.put(rule, issue);
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);
220 context.saveMeasure(measure);
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);
232 context.saveMeasure(measure);
235 private void saveMeasure(DecoratorContext context, Metric metric, int value) {
236 context.saveMeasure(metric, (double) (value + sumChildren(context, metric)));
239 private int sumChildren(DecoratorContext context, Metric metric) {
241 if (!ResourceUtils.isFile(context.getResource())) {
242 sum = MeasureUtils.sum(true, context.getChildrenMeasures(metric)).intValue();
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);
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);
263 int countIssuesAfterDate(Collection<Issue> issues, @Nullable Date targetDate) {
264 if (issues == null) {
268 for (Issue issue : issues) {
269 if (isAfter(issue, targetDate)) {
276 private boolean isAfter(Issue issue, @Nullable Date date) {
277 return date == null || (issue.creationDate() != null && DateUtils.truncatedCompareTo(issue.creationDate(), date, Calendar.SECOND) > 0);
280 private boolean shouldSaveNewMetrics(DecoratorContext context) {
281 return context.getMeasure(CoreMetrics.NEW_VIOLATIONS) == null;
285 public String toString() {
286 return getClass().getSimpleName();