]> source.dussan.org Git - sonarqube.git/blob
6be67d9d68e510ed4640cb3bd6c64547a661fb9b
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.server.computation.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.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;
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
68 /**
69  * For each component, computes the measures related to number of issues:
70  * <ul>
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>
75  * </ul>
76  * For each value, the variation on configured periods is also computed.
77  */
78 public class IssueCounter extends IssueVisitor {
79
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);
86
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);
93
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)
98     .build();
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)
103     .build();
104
105   private final PeriodHolder periodHolder;
106   private final MetricRepository metricRepository;
107   private final MeasureRepository measureRepository;
108
109   private final Map<Integer, Counters> countersByComponentRef = new HashMap<>();
110   private Counters currentCounters;
111
112   public IssueCounter(PeriodHolder periodHolder,
113     MetricRepository metricRepository, MeasureRepository measureRepository) {
114     this.periodHolder = periodHolder;
115     this.metricRepository = metricRepository;
116     this.measureRepository = measureRepository;
117   }
118
119   @Override
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);
124
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);
130     }
131   }
132
133   @Override
134   public void onIssue(Component component, DefaultIssue issue) {
135     currentCounters.add(issue);
136     if (!periodHolder.hasPeriod()) {
137       return;
138     }
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);
143     }
144   }
145
146   @Override
147   public void afterComponent(Component component) {
148     addMeasuresBySeverity(component);
149     addMeasuresByStatus(component);
150     addMeasuresByType(component);
151     addMeasuresByPeriod(component);
152     currentCounters = null;
153   }
154
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));
160     }
161   }
162
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);
170   }
171
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()));
175     }
176   }
177
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));
181   }
182
183   private void addMeasuresByPeriod(Component component) {
184     if (!periodHolder.hasPeriod()) {
185       return;
186     }
187     Double unresolvedVariations = (double) currentCounters.counterForPeriod().unresolved;
188     measureRepository.add(component, metricRepository.getByKey(NEW_VIOLATIONS_KEY), Measure.newMeasureBuilder()
189       .setVariation(unresolvedVariations)
190       .createNoValue());
191
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))
199         .createNoValue());
200     }
201
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))
211         .createNoValue());
212     }
213   }
214
215   /**
216    * Count issues by status, resolutions, rules and severities
217    */
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);
227
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);
237     }
238
239     void add(DefaultIssue issue) {
240       if (issue.resolution() == null) {
241         unresolved++;
242         typeBag.add(issue.type());
243         severityBag.add(issue.severity());
244       } else if (RESOLUTION_FALSE_POSITIVE.equals(issue.resolution())) {
245         falsePositives++;
246       } else if (RESOLUTION_WONT_FIX.equals(issue.resolution())) {
247         wontFix++;
248       }
249       switch (issue.status()) {
250         case STATUS_OPEN:
251           open++;
252           break;
253         case STATUS_REOPENED:
254           reopened++;
255           break;
256         case STATUS_CONFIRMED:
257           confirmed++;
258           break;
259         default:
260           // Other statuses are ignored
261       }
262     }
263   }
264
265   /**
266    * List of {@link Counter} for regular value and period.
267    */
268   private static class Counters {
269     private final Counter counter = new Counter();
270     private final Counter counterForPeriod = new Counter();
271
272     void add(@Nullable Counters other) {
273       if (other != null) {
274         counter.add(other.counter);
275         counterForPeriod.add(other.counterForPeriod);
276       }
277     }
278
279     void addOnPeriod(DefaultIssue issue) {
280       counterForPeriod.add(issue);
281     }
282
283     void add(DefaultIssue issue) {
284       counter.add(issue);
285     }
286
287     Counter counter() {
288       return counter;
289     }
290
291     Counter counterForPeriod() {
292       return counterForPeriod;
293     }
294   }
295 }