3 * Copyright (C) 2009-2024 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.rules.RuleType;
30 import org.sonar.ce.task.projectanalysis.component.Component;
31 import org.sonar.ce.task.projectanalysis.measure.Measure;
32 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
33 import org.sonar.ce.task.projectanalysis.metric.Metric;
34 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
35 import org.sonar.core.issue.DefaultIssue;
36 import org.sonar.core.issue.status.IssueStatus;
38 import static org.sonar.api.issue.Issue.STATUS_CONFIRMED;
39 import static org.sonar.api.issue.Issue.STATUS_OPEN;
40 import static org.sonar.api.issue.Issue.STATUS_REOPENED;
41 import static org.sonar.api.measures.CoreMetrics.ACCEPTED_ISSUES_KEY;
42 import static org.sonar.api.measures.CoreMetrics.BLOCKER_VIOLATIONS_KEY;
43 import static org.sonar.api.measures.CoreMetrics.BUGS_KEY;
44 import static org.sonar.api.measures.CoreMetrics.CODE_SMELLS_KEY;
45 import static org.sonar.api.measures.CoreMetrics.CONFIRMED_ISSUES_KEY;
46 import static org.sonar.api.measures.CoreMetrics.CRITICAL_VIOLATIONS_KEY;
47 import static org.sonar.api.measures.CoreMetrics.FALSE_POSITIVE_ISSUES_KEY;
48 import static org.sonar.api.measures.CoreMetrics.INFO_VIOLATIONS_KEY;
49 import static org.sonar.api.measures.CoreMetrics.MAJOR_VIOLATIONS_KEY;
50 import static org.sonar.api.measures.CoreMetrics.MINOR_VIOLATIONS_KEY;
51 import static org.sonar.api.measures.CoreMetrics.NEW_BLOCKER_VIOLATIONS_KEY;
52 import static org.sonar.api.measures.CoreMetrics.NEW_BUGS_KEY;
53 import static org.sonar.api.measures.CoreMetrics.NEW_CODE_SMELLS_KEY;
54 import static org.sonar.api.measures.CoreMetrics.NEW_CRITICAL_VIOLATIONS_KEY;
55 import static org.sonar.api.measures.CoreMetrics.NEW_INFO_VIOLATIONS_KEY;
56 import static org.sonar.api.measures.CoreMetrics.NEW_MAJOR_VIOLATIONS_KEY;
57 import static org.sonar.api.measures.CoreMetrics.NEW_MINOR_VIOLATIONS_KEY;
58 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_KEY;
59 import static org.sonar.api.measures.CoreMetrics.NEW_VIOLATIONS_KEY;
60 import static org.sonar.api.measures.CoreMetrics.NEW_VULNERABILITIES_KEY;
61 import static org.sonar.api.measures.CoreMetrics.OPEN_ISSUES_KEY;
62 import static org.sonar.api.measures.CoreMetrics.REOPENED_ISSUES_KEY;
63 import static org.sonar.api.measures.CoreMetrics.SECURITY_HOTSPOTS_KEY;
64 import static org.sonar.api.measures.CoreMetrics.VIOLATIONS_KEY;
65 import static org.sonar.api.measures.CoreMetrics.VULNERABILITIES_KEY;
66 import static org.sonar.api.rule.Severity.BLOCKER;
67 import static org.sonar.api.rule.Severity.CRITICAL;
68 import static org.sonar.api.rule.Severity.INFO;
69 import static org.sonar.api.rule.Severity.MAJOR;
70 import static org.sonar.api.rule.Severity.MINOR;
71 import static org.sonar.api.rules.RuleType.BUG;
72 import static org.sonar.api.rules.RuleType.CODE_SMELL;
73 import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT;
74 import static org.sonar.api.rules.RuleType.VULNERABILITY;
77 * For each component, computes the measures related to number of issues:
79 * <li>issues per status (open, reopen, confirmed)</li>
80 * <li>issues per resolution (unresolved, false-positives, won't fix)</li>
81 * <li>issues per severity (from info to blocker)</li>
82 * <li>issues per type (code smell, bug, vulnerability, security hotspots)</li>
84 * For each value, the variation on configured periods is also computed.
86 public class IssueCounter extends IssueVisitor {
88 private static final Map<String, String> SEVERITY_TO_METRIC_KEY = ImmutableMap.of(
89 BLOCKER, BLOCKER_VIOLATIONS_KEY,
90 CRITICAL, CRITICAL_VIOLATIONS_KEY,
91 MAJOR, MAJOR_VIOLATIONS_KEY,
92 MINOR, MINOR_VIOLATIONS_KEY,
93 INFO, INFO_VIOLATIONS_KEY);
95 private static final Map<String, String> SEVERITY_TO_NEW_METRIC_KEY = ImmutableMap.of(
96 BLOCKER, NEW_BLOCKER_VIOLATIONS_KEY,
97 CRITICAL, NEW_CRITICAL_VIOLATIONS_KEY,
98 MAJOR, NEW_MAJOR_VIOLATIONS_KEY,
99 MINOR, NEW_MINOR_VIOLATIONS_KEY,
100 INFO, NEW_INFO_VIOLATIONS_KEY);
102 private static final Map<RuleType, String> TYPE_TO_METRIC_KEY = ImmutableMap.<RuleType, String>builder()
103 .put(CODE_SMELL, CODE_SMELLS_KEY)
105 .put(VULNERABILITY, VULNERABILITIES_KEY)
106 .put(SECURITY_HOTSPOT, SECURITY_HOTSPOTS_KEY)
108 private static final Map<RuleType, String> TYPE_TO_NEW_METRIC_KEY = ImmutableMap.<RuleType, String>builder()
109 .put(CODE_SMELL, NEW_CODE_SMELLS_KEY)
110 .put(BUG, NEW_BUGS_KEY)
111 .put(VULNERABILITY, NEW_VULNERABILITIES_KEY)
112 .put(SECURITY_HOTSPOT, NEW_SECURITY_HOTSPOTS_KEY)
115 private final MetricRepository metricRepository;
116 private final MeasureRepository measureRepository;
117 private final NewIssueClassifier newIssueClassifier;
118 private final Map<String, Counters> countersByComponentUuid = new HashMap<>();
120 private Counters currentCounters;
122 public IssueCounter(MetricRepository metricRepository, MeasureRepository measureRepository, NewIssueClassifier newIssueClassifier) {
123 this.metricRepository = metricRepository;
124 this.measureRepository = measureRepository;
125 this.newIssueClassifier = newIssueClassifier;
129 public void beforeComponent(Component component) {
130 currentCounters = new Counters();
131 countersByComponentUuid.put(component.getUuid(), currentCounters);
133 // aggregate children counters
134 for (Component child : component.getChildren()) {
135 Counters childCounters = countersByComponentUuid.remove(child.getUuid());
136 currentCounters.add(childCounters);
141 public void onIssue(Component component, DefaultIssue issue) {
142 currentCounters.add(issue);
143 if (newIssueClassifier.isNew(component, issue)) {
144 currentCounters.addOnPeriod(issue);
149 public void afterComponent(Component component) {
150 addMeasuresBySeverity(component);
151 addMeasuresByStatus(component);
152 addMeasuresByType(component);
153 addNewMeasures(component);
154 currentCounters = null;
157 private void addMeasuresBySeverity(Component component) {
158 for (Map.Entry<String, String> entry : SEVERITY_TO_METRIC_KEY.entrySet()) {
159 String severity = entry.getKey();
160 String metricKey = entry.getValue();
161 addMeasure(component, metricKey, currentCounters.counter().severityBag.count(severity));
165 private void addMeasuresByStatus(Component component) {
166 addMeasure(component, VIOLATIONS_KEY, currentCounters.counter().unresolved);
167 addMeasure(component, OPEN_ISSUES_KEY, currentCounters.counter().open);
168 addMeasure(component, REOPENED_ISSUES_KEY, currentCounters.counter().reopened);
169 addMeasure(component, CONFIRMED_ISSUES_KEY, currentCounters.counter().confirmed);
170 addMeasure(component, FALSE_POSITIVE_ISSUES_KEY, currentCounters.counter().falsePositives);
171 addMeasure(component, ACCEPTED_ISSUES_KEY, currentCounters.counter().accepted);
174 private void addMeasuresByType(Component component) {
175 for (Map.Entry<RuleType, String> entry : TYPE_TO_METRIC_KEY.entrySet()) {
176 addMeasure(component, entry.getValue(), currentCounters.counter().typeBag.count(entry.getKey()));
180 private void addMeasure(Component component, String metricKey, int value) {
181 Metric metric = metricRepository.getByKey(metricKey);
182 measureRepository.add(component, metric, Measure.newMeasureBuilder().create(value));
185 private void addNewMeasures(Component component) {
186 if (!newIssueClassifier.isEnabled()) {
189 int unresolved = currentCounters.counterForPeriod().unresolved;
190 measureRepository.add(component, metricRepository.getByKey(NEW_VIOLATIONS_KEY), Measure.newMeasureBuilder()
191 .create(unresolved));
193 for (Map.Entry<String, String> entry : SEVERITY_TO_NEW_METRIC_KEY.entrySet()) {
194 String severity = entry.getKey();
195 String metricKey = entry.getValue();
196 Multiset<String> bag = currentCounters.counterForPeriod().severityBag;
197 Metric metric = metricRepository.getByKey(metricKey);
198 measureRepository.add(component, metric, Measure.newMeasureBuilder()
199 .create(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 .create(bag.count(type)));
215 * Count issues by status, resolutions, rules and severities
217 private static class Counter {
218 private int unresolved = 0;
219 private int open = 0;
220 private int reopened = 0;
221 private int confirmed = 0;
222 private int falsePositives = 0;
223 private int accepted = 0;
224 private final Multiset<String> severityBag = HashMultiset.create();
225 private final EnumMultiset<RuleType> typeBag = EnumMultiset.create(RuleType.class);
227 void add(Counter counter) {
228 unresolved += counter.unresolved;
229 open += counter.open;
230 reopened += counter.reopened;
231 confirmed += counter.confirmed;
232 falsePositives += counter.falsePositives;
233 accepted += counter.accepted;
234 severityBag.addAll(counter.severityBag);
235 typeBag.addAll(counter.typeBag);
238 void add(DefaultIssue issue) {
239 if (issue.type() == SECURITY_HOTSPOT) {
240 if (issue.resolution() == null) {
241 typeBag.add(SECURITY_HOTSPOT);
245 if (issue.resolution() == null) {
247 typeBag.add(issue.type());
248 severityBag.add(issue.severity());
249 } else if (IssueStatus.FALSE_POSITIVE.equals(issue.getIssueStatus())) {
251 } else if (IssueStatus.ACCEPTED.equals(issue.getIssueStatus())) {
254 switch (issue.status()) {
258 case STATUS_REOPENED:
261 case STATUS_CONFIRMED:
265 // Other statuses are ignored
271 * List of {@link Counter} for regular value and period.
273 private static class Counters {
274 private final Counter counter = new Counter();
275 private final Counter counterForPeriod = new Counter();
277 void add(@Nullable Counters other) {
279 counter.add(other.counter);
280 counterForPeriod.add(other.counterForPeriod);
284 void addOnPeriod(DefaultIssue issue) {
285 counterForPeriod.add(issue);
288 void add(DefaultIssue issue) {
296 Counter counterForPeriod() {
297 return counterForPeriod;