3 * Copyright (C) 2009-2017 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.computation.task.projectanalysis.issue;
22 import com.google.common.collect.EnumMultiset;
23 import com.google.common.collect.HashMultiset;
24 import com.google.common.collect.ImmutableMap;
25 import com.google.common.collect.Multiset;
26 import java.util.HashMap;
28 import javax.annotation.Nullable;
29 import org.sonar.api.measures.CoreMetrics;
30 import org.sonar.api.rules.RuleType;
31 import org.sonar.core.issue.DefaultIssue;
32 import org.sonar.server.computation.task.projectanalysis.component.Component;
33 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
34 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepository;
35 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
36 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
37 import org.sonar.server.computation.task.projectanalysis.period.Period;
38 import org.sonar.server.computation.task.projectanalysis.period.PeriodHolder;
40 import static org.sonar.api.issue.Issue.RESOLUTION_FALSE_POSITIVE;
41 import static org.sonar.api.issue.Issue.RESOLUTION_WONT_FIX;
42 import static org.sonar.api.issue.Issue.STATUS_CONFIRMED;
43 import static org.sonar.api.issue.Issue.STATUS_OPEN;
44 import static org.sonar.api.issue.Issue.STATUS_REOPENED;
45 import static org.sonar.api.measures.CoreMetrics.BLOCKER_VIOLATIONS_KEY;
46 import static org.sonar.api.measures.CoreMetrics.CONFIRMED_ISSUES_KEY;
47 import static org.sonar.api.measures.CoreMetrics.CRITICAL_VIOLATIONS_KEY;
48 import static org.sonar.api.measures.CoreMetrics.FALSE_POSITIVE_ISSUES_KEY;
49 import static org.sonar.api.measures.CoreMetrics.INFO_VIOLATIONS_KEY;
50 import static org.sonar.api.measures.CoreMetrics.MAJOR_VIOLATIONS_KEY;
51 import static org.sonar.api.measures.CoreMetrics.MINOR_VIOLATIONS_KEY;
52 import static org.sonar.api.measures.CoreMetrics.NEW_BLOCKER_VIOLATIONS_KEY;
53 import static org.sonar.api.measures.CoreMetrics.NEW_CRITICAL_VIOLATIONS_KEY;
54 import static org.sonar.api.measures.CoreMetrics.NEW_INFO_VIOLATIONS_KEY;
55 import static org.sonar.api.measures.CoreMetrics.NEW_MAJOR_VIOLATIONS_KEY;
56 import static org.sonar.api.measures.CoreMetrics.NEW_MINOR_VIOLATIONS_KEY;
57 import static org.sonar.api.measures.CoreMetrics.NEW_VIOLATIONS_KEY;
58 import static org.sonar.api.measures.CoreMetrics.OPEN_ISSUES_KEY;
59 import static org.sonar.api.measures.CoreMetrics.REOPENED_ISSUES_KEY;
60 import static org.sonar.api.measures.CoreMetrics.VIOLATIONS_KEY;
61 import static org.sonar.api.measures.CoreMetrics.WONT_FIX_ISSUES_KEY;
62 import static org.sonar.api.rule.Severity.BLOCKER;
63 import static org.sonar.api.rule.Severity.CRITICAL;
64 import static org.sonar.api.rule.Severity.INFO;
65 import static org.sonar.api.rule.Severity.MAJOR;
66 import static org.sonar.api.rule.Severity.MINOR;
69 * For each component, computes the measures related to number of issues:
71 * <li>issues per status (open, reopen, confirmed)</li>
72 * <li>issues per resolution (unresolved, false-positives, won't fix)</li>
73 * <li>issues per severity (from info to blocker)</li>
74 * <li>issues per type (code smell, bug, vulnerability)</li>
76 * For each value, the variation on configured periods is also computed.
78 public class IssueCounter extends IssueVisitor {
80 private static final Map<String, String> SEVERITY_TO_METRIC_KEY = ImmutableMap.of(
81 BLOCKER, BLOCKER_VIOLATIONS_KEY,
82 CRITICAL, CRITICAL_VIOLATIONS_KEY,
83 MAJOR, MAJOR_VIOLATIONS_KEY,
84 MINOR, MINOR_VIOLATIONS_KEY,
85 INFO, INFO_VIOLATIONS_KEY);
87 private static final Map<String, String> SEVERITY_TO_NEW_METRIC_KEY = ImmutableMap.of(
88 BLOCKER, NEW_BLOCKER_VIOLATIONS_KEY,
89 CRITICAL, NEW_CRITICAL_VIOLATIONS_KEY,
90 MAJOR, NEW_MAJOR_VIOLATIONS_KEY,
91 MINOR, NEW_MINOR_VIOLATIONS_KEY,
92 INFO, NEW_INFO_VIOLATIONS_KEY);
94 private static final Map<RuleType, String> TYPE_TO_METRIC_KEY = ImmutableMap.<RuleType, String>builder()
95 .put(RuleType.CODE_SMELL, CoreMetrics.CODE_SMELLS_KEY)
96 .put(RuleType.BUG, CoreMetrics.BUGS_KEY)
97 .put(RuleType.VULNERABILITY, CoreMetrics.VULNERABILITIES_KEY)
99 private static final Map<RuleType, String> TYPE_TO_NEW_METRIC_KEY = ImmutableMap.<RuleType, String>builder()
100 .put(RuleType.CODE_SMELL, CoreMetrics.NEW_CODE_SMELLS_KEY)
101 .put(RuleType.BUG, CoreMetrics.NEW_BUGS_KEY)
102 .put(RuleType.VULNERABILITY, CoreMetrics.NEW_VULNERABILITIES_KEY)
105 private final PeriodHolder periodHolder;
106 private final MetricRepository metricRepository;
107 private final MeasureRepository measureRepository;
109 private final Map<Integer, Counters> countersByComponentRef = new HashMap<>();
110 private Counters currentCounters;
112 public IssueCounter(PeriodHolder periodHolder,
113 MetricRepository metricRepository, MeasureRepository measureRepository) {
114 this.periodHolder = periodHolder;
115 this.metricRepository = metricRepository;
116 this.measureRepository = measureRepository;
120 public void beforeComponent(Component component) {
121 // TODO optimization no need to instantiate counter if no open issues
122 currentCounters = new Counters();
123 countersByComponentRef.put(component.getReportAttributes().getRef(), currentCounters);
125 // aggregate children counters
126 for (Component child : component.getChildren()) {
127 // no need to keep the children in memory. They can be garbage-collected.
128 Counters childCounters = countersByComponentRef.remove(child.getReportAttributes().getRef());
129 currentCounters.add(childCounters);
134 public void onIssue(Component component, DefaultIssue issue) {
135 currentCounters.add(issue);
136 if (!periodHolder.hasPeriod()) {
139 Period period = periodHolder.getPeriod();
140 // Add one second to not take into account issues created during current analysis
141 if (issue.creationDate().getTime() >= period.getSnapshotDate() + 1000L) {
142 currentCounters.addOnPeriod(issue);
147 public void afterComponent(Component component) {
148 addMeasuresBySeverity(component);
149 addMeasuresByStatus(component);
150 addMeasuresByType(component);
151 addMeasuresByPeriod(component);
152 currentCounters = null;
155 private void addMeasuresBySeverity(Component component) {
156 for (Map.Entry<String, String> entry : SEVERITY_TO_METRIC_KEY.entrySet()) {
157 String severity = entry.getKey();
158 String metricKey = entry.getValue();
159 addMeasure(component, metricKey, currentCounters.counter().severityBag.count(severity));
163 private void addMeasuresByStatus(Component component) {
164 addMeasure(component, VIOLATIONS_KEY, currentCounters.counter().unresolved);
165 addMeasure(component, OPEN_ISSUES_KEY, currentCounters.counter().open);
166 addMeasure(component, REOPENED_ISSUES_KEY, currentCounters.counter().reopened);
167 addMeasure(component, CONFIRMED_ISSUES_KEY, currentCounters.counter().confirmed);
168 addMeasure(component, FALSE_POSITIVE_ISSUES_KEY, currentCounters.counter().falsePositives);
169 addMeasure(component, WONT_FIX_ISSUES_KEY, currentCounters.counter().wontFix);
172 private void addMeasuresByType(Component component) {
173 for (Map.Entry<RuleType, String> entry : TYPE_TO_METRIC_KEY.entrySet()) {
174 addMeasure(component, entry.getValue(), currentCounters.counter().typeBag.count(entry.getKey()));
178 private void addMeasure(Component component, String metricKey, int value) {
179 Metric metric = metricRepository.getByKey(metricKey);
180 measureRepository.add(component, metric, Measure.newMeasureBuilder().create(value));
183 private void addMeasuresByPeriod(Component component) {
184 if (!periodHolder.hasPeriod()) {
187 Double unresolvedVariations = (double) currentCounters.counterForPeriod().unresolved;
188 measureRepository.add(component, metricRepository.getByKey(NEW_VIOLATIONS_KEY), Measure.newMeasureBuilder()
189 .setVariation(unresolvedVariations)
192 for (Map.Entry<String, String> entry : SEVERITY_TO_NEW_METRIC_KEY.entrySet()) {
193 String severity = entry.getKey();
194 String metricKey = entry.getValue();
195 Multiset<String> bag = currentCounters.counterForPeriod().severityBag;
196 Metric metric = metricRepository.getByKey(metricKey);
197 measureRepository.add(component, metric, Measure.newMeasureBuilder()
198 .setVariation((double) bag.count(severity))
202 // waiting for Java 8 lambda in order to factor this loop with the previous one
203 // (see call currentCounters.counterForPeriod(period.getIndex()).xxx with xxx as severityBag or typeBag)
204 for (Map.Entry<RuleType, String> entry : TYPE_TO_NEW_METRIC_KEY.entrySet()) {
205 RuleType type = entry.getKey();
206 String metricKey = entry.getValue();
207 Multiset<RuleType> bag = currentCounters.counterForPeriod().typeBag;
208 Metric metric = metricRepository.getByKey(metricKey);
209 measureRepository.add(component, metric, Measure.newMeasureBuilder()
210 .setVariation((double) bag.count(type))
216 * Count issues by status, resolutions, rules and severities
218 private static class Counter {
219 private int unresolved = 0;
220 private int open = 0;
221 private int reopened = 0;
222 private int confirmed = 0;
223 private int falsePositives = 0;
224 private int wontFix = 0;
225 private final Multiset<String> severityBag = HashMultiset.create();
226 private final EnumMultiset<RuleType> typeBag = EnumMultiset.create(RuleType.class);
228 void add(Counter counter) {
229 unresolved += counter.unresolved;
230 open += counter.open;
231 reopened += counter.reopened;
232 confirmed += counter.confirmed;
233 falsePositives += counter.falsePositives;
234 wontFix += counter.wontFix;
235 severityBag.addAll(counter.severityBag);
236 typeBag.addAll(counter.typeBag);
239 void add(DefaultIssue issue) {
240 if (issue.resolution() == null) {
242 typeBag.add(issue.type());
243 severityBag.add(issue.severity());
244 } else if (RESOLUTION_FALSE_POSITIVE.equals(issue.resolution())) {
246 } else if (RESOLUTION_WONT_FIX.equals(issue.resolution())) {
249 switch (issue.status()) {
253 case STATUS_REOPENED:
256 case STATUS_CONFIRMED:
260 // Other statuses are ignored
266 * List of {@link Counter} for regular value and period.
268 private static class Counters {
269 private final Counter counter = new Counter();
270 private final Counter counterForPeriod = new Counter();
272 void add(@Nullable Counters other) {
274 counter.add(other.counter);
275 counterForPeriod.add(other.counterForPeriod);
279 void addOnPeriod(DefaultIssue issue) {
280 counterForPeriod.add(issue);
283 void add(DefaultIssue issue) {
291 Counter counterForPeriod() {
292 return counterForPeriod;