3 * Copyright (C) 2009-2019 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.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.ce.task.projectanalysis.component.Component;
32 import org.sonar.ce.task.projectanalysis.measure.Measure;
33 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
34 import org.sonar.ce.task.projectanalysis.metric.Metric;
35 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
36 import org.sonar.ce.task.projectanalysis.period.Period;
37 import org.sonar.ce.task.projectanalysis.period.PeriodHolder;
38 import org.sonar.core.issue.DefaultIssue;
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;
67 import static org.sonar.api.utils.DateUtils.truncateToSeconds;
70 * For each component, computes the measures related to number of issues:
72 * <li>issues per status (open, reopen, confirmed)</li>
73 * <li>issues per resolution (unresolved, false-positives, won't fix)</li>
74 * <li>issues per severity (from info to blocker)</li>
75 * <li>issues per type (code smell, bug, vulnerability)</li>
77 * For each value, the variation on configured periods is also computed.
79 public class IssueCounter extends IssueVisitor {
81 private static final Map<String, String> SEVERITY_TO_METRIC_KEY = ImmutableMap.of(
82 BLOCKER, BLOCKER_VIOLATIONS_KEY,
83 CRITICAL, CRITICAL_VIOLATIONS_KEY,
84 MAJOR, MAJOR_VIOLATIONS_KEY,
85 MINOR, MINOR_VIOLATIONS_KEY,
86 INFO, INFO_VIOLATIONS_KEY);
88 private static final Map<String, String> SEVERITY_TO_NEW_METRIC_KEY = ImmutableMap.of(
89 BLOCKER, NEW_BLOCKER_VIOLATIONS_KEY,
90 CRITICAL, NEW_CRITICAL_VIOLATIONS_KEY,
91 MAJOR, NEW_MAJOR_VIOLATIONS_KEY,
92 MINOR, NEW_MINOR_VIOLATIONS_KEY,
93 INFO, NEW_INFO_VIOLATIONS_KEY);
95 private static final Map<RuleType, String> TYPE_TO_METRIC_KEY = ImmutableMap.<RuleType, String>builder()
96 .put(RuleType.CODE_SMELL, CoreMetrics.CODE_SMELLS_KEY)
97 .put(RuleType.BUG, CoreMetrics.BUGS_KEY)
98 .put(RuleType.VULNERABILITY, CoreMetrics.VULNERABILITIES_KEY)
100 private static final Map<RuleType, String> TYPE_TO_NEW_METRIC_KEY = ImmutableMap.<RuleType, String>builder()
101 .put(RuleType.CODE_SMELL, CoreMetrics.NEW_CODE_SMELLS_KEY)
102 .put(RuleType.BUG, CoreMetrics.NEW_BUGS_KEY)
103 .put(RuleType.VULNERABILITY, CoreMetrics.NEW_VULNERABILITIES_KEY)
106 private final PeriodHolder periodHolder;
107 private final MetricRepository metricRepository;
108 private final MeasureRepository measureRepository;
109 private final Map<String, Counters> countersByComponentUuid = new HashMap<>();
111 private Counters currentCounters;
113 public IssueCounter(PeriodHolder periodHolder,
114 MetricRepository metricRepository, MeasureRepository measureRepository) {
115 this.periodHolder = periodHolder;
116 this.metricRepository = metricRepository;
117 this.measureRepository = measureRepository;
121 public void beforeComponent(Component component) {
122 currentCounters = new Counters();
123 countersByComponentUuid.put(component.getUuid(), 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 = countersByComponentUuid.remove(child.getUuid());
129 currentCounters.add(childCounters);
134 public void onIssue(Component component, DefaultIssue issue) {
135 if (issue.type() == RuleType.SECURITY_HOTSPOT) {
139 currentCounters.add(issue);
140 if (!periodHolder.hasPeriod()) {
143 Period period = periodHolder.getPeriod();
144 // Add one second to not take into account issues created during current analysis
145 if (issue.creationDate().getTime() > truncateToSeconds(period.getSnapshotDate())) {
146 currentCounters.addOnPeriod(issue);
151 public void afterComponent(Component component) {
152 addMeasuresBySeverity(component);
153 addMeasuresByStatus(component);
154 addMeasuresByType(component);
155 addMeasuresByPeriod(component);
156 currentCounters = null;
159 private void addMeasuresBySeverity(Component component) {
160 for (Map.Entry<String, String> entry : SEVERITY_TO_METRIC_KEY.entrySet()) {
161 String severity = entry.getKey();
162 String metricKey = entry.getValue();
163 addMeasure(component, metricKey, currentCounters.counter().severityBag.count(severity));
167 private void addMeasuresByStatus(Component component) {
168 addMeasure(component, VIOLATIONS_KEY, currentCounters.counter().unresolved);
169 addMeasure(component, OPEN_ISSUES_KEY, currentCounters.counter().open);
170 addMeasure(component, REOPENED_ISSUES_KEY, currentCounters.counter().reopened);
171 addMeasure(component, CONFIRMED_ISSUES_KEY, currentCounters.counter().confirmed);
172 addMeasure(component, FALSE_POSITIVE_ISSUES_KEY, currentCounters.counter().falsePositives);
173 addMeasure(component, WONT_FIX_ISSUES_KEY, currentCounters.counter().wontFix);
176 private void addMeasuresByType(Component component) {
177 for (Map.Entry<RuleType, String> entry : TYPE_TO_METRIC_KEY.entrySet()) {
178 addMeasure(component, entry.getValue(), currentCounters.counter().typeBag.count(entry.getKey()));
182 private void addMeasure(Component component, String metricKey, int value) {
183 Metric metric = metricRepository.getByKey(metricKey);
184 measureRepository.add(component, metric, Measure.newMeasureBuilder().create(value));
187 private void addMeasuresByPeriod(Component component) {
188 if (!periodHolder.hasPeriod()) {
191 double unresolvedVariations = (double) currentCounters.counterForPeriod().unresolved;
192 measureRepository.add(component, metricRepository.getByKey(NEW_VIOLATIONS_KEY), Measure.newMeasureBuilder()
193 .setVariation(unresolvedVariations)
196 for (Map.Entry<String, String> entry : SEVERITY_TO_NEW_METRIC_KEY.entrySet()) {
197 String severity = entry.getKey();
198 String metricKey = entry.getValue();
199 Multiset<String> bag = currentCounters.counterForPeriod().severityBag;
200 Metric metric = metricRepository.getByKey(metricKey);
201 measureRepository.add(component, metric, Measure.newMeasureBuilder()
202 .setVariation((double) bag.count(severity))
206 // waiting for Java 8 lambda in order to factor this loop with the previous one
207 // (see call currentCounters.counterForPeriod(period.getIndex()).xxx with xxx as severityBag or typeBag)
208 for (Map.Entry<RuleType, String> entry : TYPE_TO_NEW_METRIC_KEY.entrySet()) {
209 RuleType type = entry.getKey();
210 String metricKey = entry.getValue();
211 Multiset<RuleType> bag = currentCounters.counterForPeriod().typeBag;
212 Metric metric = metricRepository.getByKey(metricKey);
213 measureRepository.add(component, metric, Measure.newMeasureBuilder()
214 .setVariation((double) bag.count(type))
220 * Count issues by status, resolutions, rules and severities
222 private static class Counter {
223 private int unresolved = 0;
224 private int open = 0;
225 private int reopened = 0;
226 private int confirmed = 0;
227 private int falsePositives = 0;
228 private int wontFix = 0;
229 private final Multiset<String> severityBag = HashMultiset.create();
230 private final EnumMultiset<RuleType> typeBag = EnumMultiset.create(RuleType.class);
232 void add(Counter counter) {
233 unresolved += counter.unresolved;
234 open += counter.open;
235 reopened += counter.reopened;
236 confirmed += counter.confirmed;
237 falsePositives += counter.falsePositives;
238 wontFix += counter.wontFix;
239 severityBag.addAll(counter.severityBag);
240 typeBag.addAll(counter.typeBag);
243 void add(DefaultIssue issue) {
244 if (issue.resolution() == null) {
246 typeBag.add(issue.type());
247 severityBag.add(issue.severity());
248 } else if (RESOLUTION_FALSE_POSITIVE.equals(issue.resolution())) {
250 } else if (RESOLUTION_WONT_FIX.equals(issue.resolution())) {
253 switch (issue.status()) {
257 case STATUS_REOPENED:
260 case STATUS_CONFIRMED:
264 // Other statuses are ignored
270 * List of {@link Counter} for regular value and period.
272 private static class Counters {
273 private final Counter counter = new Counter();
274 private final Counter counterForPeriod = new Counter();
276 void add(@Nullable Counters other) {
278 counter.add(other.counter);
279 counterForPeriod.add(other.counterForPeriod);
283 void addOnPeriod(DefaultIssue issue) {
284 counterForPeriod.add(issue);
287 void add(DefaultIssue issue) {
295 Counter counterForPeriod() {
296 return counterForPeriod;