]> source.dussan.org Git - sonarqube.git/blob
92e5c4bedb6c9b72606f7d87cd36c02a912c1d50
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2018 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
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.ce.task.projectanalysis.issue;
21
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;
27 import java.util.Map;
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.core.issue.DefaultIssue;
33 import org.sonar.ce.task.projectanalysis.measure.Measure;
34 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
35 import org.sonar.ce.task.projectanalysis.metric.Metric;
36 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
37 import org.sonar.ce.task.projectanalysis.period.Period;
38 import org.sonar.ce.task.projectanalysis.period.PeriodHolder;
39
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;
68
69 /**
70  * For each component, computes the measures related to number of issues:
71  * <ul>
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>
76  * </ul>
77  * For each value, the variation on configured periods is also computed.
78  */
79 public class IssueCounter extends IssueVisitor {
80
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);
87
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);
94
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)
99     .build();
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)
104     .build();
105
106   private final PeriodHolder periodHolder;
107   private final MetricRepository metricRepository;
108   private final MeasureRepository measureRepository;
109
110   private final Map<Integer, Counters> countersByComponentRef = new HashMap<>();
111   private Counters currentCounters;
112
113   public IssueCounter(PeriodHolder periodHolder,
114     MetricRepository metricRepository, MeasureRepository measureRepository) {
115     this.periodHolder = periodHolder;
116     this.metricRepository = metricRepository;
117     this.measureRepository = measureRepository;
118   }
119
120   @Override
121   public void beforeComponent(Component component) {
122     // TODO optimization no need to instantiate counter if no open issues
123     currentCounters = new Counters();
124     countersByComponentRef.put(component.getReportAttributes().getRef(), currentCounters);
125
126     // aggregate children counters
127     for (Component child : component.getChildren()) {
128       // no need to keep the children in memory. They can be garbage-collected.
129       Counters childCounters = countersByComponentRef.remove(child.getReportAttributes().getRef());
130       currentCounters.add(childCounters);
131     }
132   }
133
134   @Override
135   public void onIssue(Component component, DefaultIssue issue) {
136     currentCounters.add(issue);
137     if (!periodHolder.hasPeriod()) {
138       return;
139     }
140     Period period = periodHolder.getPeriod();
141     // Add one second to not take into account issues created during current analysis
142     if (issue.creationDate().getTime() > truncateToSeconds(period.getSnapshotDate())) {
143       currentCounters.addOnPeriod(issue);
144     }
145   }
146
147   @Override
148   public void afterComponent(Component component) {
149     addMeasuresBySeverity(component);
150     addMeasuresByStatus(component);
151     addMeasuresByType(component);
152     addMeasuresByPeriod(component);
153     currentCounters = null;
154   }
155
156   private void addMeasuresBySeverity(Component component) {
157     for (Map.Entry<String, String> entry : SEVERITY_TO_METRIC_KEY.entrySet()) {
158       String severity = entry.getKey();
159       String metricKey = entry.getValue();
160       addMeasure(component, metricKey, currentCounters.counter().severityBag.count(severity));
161     }
162   }
163
164   private void addMeasuresByStatus(Component component) {
165     addMeasure(component, VIOLATIONS_KEY, currentCounters.counter().unresolved);
166     addMeasure(component, OPEN_ISSUES_KEY, currentCounters.counter().open);
167     addMeasure(component, REOPENED_ISSUES_KEY, currentCounters.counter().reopened);
168     addMeasure(component, CONFIRMED_ISSUES_KEY, currentCounters.counter().confirmed);
169     addMeasure(component, FALSE_POSITIVE_ISSUES_KEY, currentCounters.counter().falsePositives);
170     addMeasure(component, WONT_FIX_ISSUES_KEY, currentCounters.counter().wontFix);
171   }
172
173   private void addMeasuresByType(Component component) {
174     for (Map.Entry<RuleType, String> entry : TYPE_TO_METRIC_KEY.entrySet()) {
175       addMeasure(component, entry.getValue(), currentCounters.counter().typeBag.count(entry.getKey()));
176     }
177   }
178
179   private void addMeasure(Component component, String metricKey, int value) {
180     Metric metric = metricRepository.getByKey(metricKey);
181     measureRepository.add(component, metric, Measure.newMeasureBuilder().create(value));
182   }
183
184   private void addMeasuresByPeriod(Component component) {
185     if (!periodHolder.hasPeriod()) {
186       return;
187     }
188     double unresolvedVariations = (double) currentCounters.counterForPeriod().unresolved;
189     measureRepository.add(component, metricRepository.getByKey(NEW_VIOLATIONS_KEY), Measure.newMeasureBuilder()
190       .setVariation(unresolvedVariations)
191       .createNoValue());
192
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         .setVariation((double) bag.count(severity))
200         .createNoValue());
201     }
202
203     // waiting for Java 8 lambda in order to factor this loop with the previous one
204     // (see call currentCounters.counterForPeriod(period.getIndex()).xxx with xxx as severityBag or typeBag)
205     for (Map.Entry<RuleType, String> entry : TYPE_TO_NEW_METRIC_KEY.entrySet()) {
206       RuleType type = entry.getKey();
207       String metricKey = entry.getValue();
208       Multiset<RuleType> bag = currentCounters.counterForPeriod().typeBag;
209       Metric metric = metricRepository.getByKey(metricKey);
210       measureRepository.add(component, metric, Measure.newMeasureBuilder()
211         .setVariation((double) bag.count(type))
212         .createNoValue());
213     }
214   }
215
216   /**
217    * Count issues by status, resolutions, rules and severities
218    */
219   private static class Counter {
220     private int unresolved = 0;
221     private int open = 0;
222     private int reopened = 0;
223     private int confirmed = 0;
224     private int falsePositives = 0;
225     private int wontFix = 0;
226     private final Multiset<String> severityBag = HashMultiset.create();
227     private final EnumMultiset<RuleType> typeBag = EnumMultiset.create(RuleType.class);
228
229     void add(Counter counter) {
230       unresolved += counter.unresolved;
231       open += counter.open;
232       reopened += counter.reopened;
233       confirmed += counter.confirmed;
234       falsePositives += counter.falsePositives;
235       wontFix += counter.wontFix;
236       severityBag.addAll(counter.severityBag);
237       typeBag.addAll(counter.typeBag);
238     }
239
240     void add(DefaultIssue issue) {
241       if (issue.resolution() == null) {
242         unresolved++;
243         typeBag.add(issue.type());
244         severityBag.add(issue.severity());
245       } else if (RESOLUTION_FALSE_POSITIVE.equals(issue.resolution())) {
246         falsePositives++;
247       } else if (RESOLUTION_WONT_FIX.equals(issue.resolution())) {
248         wontFix++;
249       }
250       switch (issue.status()) {
251         case STATUS_OPEN:
252           open++;
253           break;
254         case STATUS_REOPENED:
255           reopened++;
256           break;
257         case STATUS_CONFIRMED:
258           confirmed++;
259           break;
260         default:
261           // Other statuses are ignored
262       }
263     }
264   }
265
266   /**
267    * List of {@link Counter} for regular value and period.
268    */
269   private static class Counters {
270     private final Counter counter = new Counter();
271     private final Counter counterForPeriod = new Counter();
272
273     void add(@Nullable Counters other) {
274       if (other != null) {
275         counter.add(other.counter);
276         counterForPeriod.add(other.counterForPeriod);
277       }
278     }
279
280     void addOnPeriod(DefaultIssue issue) {
281       if (issue.type() != RuleType.SECURITY_HOTSPOT) {
282         counterForPeriod.add(issue);
283       }
284     }
285
286     void add(DefaultIssue issue) {
287       counter.add(issue);
288     }
289
290     Counter counter() {
291       return counter;
292     }
293
294     Counter counterForPeriod() {
295       return counterForPeriod;
296     }
297   }
298 }