]> source.dussan.org Git - sonarqube.git/blob
358c3394edc77a6d5c7e8ccb93849ebb9116a0e7
[sonarqube.git] /
1 /*
2  * Sonar, open source software quality management tool.
3  * Copyright (C) 2008-2011 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
6  * Sonar 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  * Sonar 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
17  * License along with Sonar; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
19  */
20 package org.sonar.plugins.core.timemachine;
21
22 import com.google.common.collect.*;
23 import org.apache.commons.lang.StringUtils;
24 import org.sonar.api.batch.*;
25 import org.sonar.api.measures.*;
26 import org.sonar.api.resources.Project;
27 import org.sonar.api.resources.Resource;
28 import org.sonar.api.resources.ResourceUtils;
29 import org.sonar.api.resources.Scopes;
30 import org.sonar.api.rules.Rule;
31 import org.sonar.api.rules.RulePriority;
32 import org.sonar.api.rules.Violation;
33 import org.sonar.batch.components.PastSnapshot;
34 import org.sonar.batch.components.TimeMachineConfiguration;
35 import org.sonar.core.NotDryRun;
36
37 import java.util.*;
38
39 @NotDryRun
40 @DependsUpon(DecoratorBarriers.END_OF_VIOLATION_TRACKING)
41 public class NewViolationsDecorator implements Decorator {
42
43   private TimeMachineConfiguration timeMachineConfiguration;
44
45   // temporary data for current resource
46   private Map<Rule, RulePriority> ruleToLevel = Maps.newHashMap();
47   private Multimap<RulePriority, Violation> violationsBySeverity = ArrayListMultimap.create();
48   private Multimap<Rule, Violation> violationsByRule = ArrayListMultimap.create();
49
50   public NewViolationsDecorator(TimeMachineConfiguration timeMachineConfiguration) {
51     this.timeMachineConfiguration = timeMachineConfiguration;
52   }
53
54   public boolean shouldExecuteOnProject(Project project) {
55     return project.isLatestAnalysis();
56   }
57
58   @DependedUpon
59   public List<Metric> generatesMetric() {
60     return Arrays.asList(
61         CoreMetrics.NEW_VIOLATIONS, CoreMetrics.NEW_BLOCKER_VIOLATIONS, CoreMetrics.NEW_CRITICAL_VIOLATIONS,
62         CoreMetrics.NEW_MAJOR_VIOLATIONS, CoreMetrics.NEW_MINOR_VIOLATIONS, CoreMetrics.NEW_INFO_VIOLATIONS);
63   }
64
65   public void decorate(Resource resource, DecoratorContext context) {
66     if (shouldDecorateResource(resource, context)) {
67       prepareCurrentResourceViolations(context);
68       saveNewViolations(context);
69       saveNewViolationsBySeverity(context);
70       saveNewViolationsByRule(context);
71       clearCache();
72     }
73   }
74
75   private boolean shouldDecorateResource(Resource resource, DecoratorContext context) {
76     return
77         (StringUtils.equals(Scopes.PROJECT, resource.getScope()) || StringUtils.equals(Scopes.DIRECTORY, resource.getScope()) || StringUtils.equals(Scopes.FILE, resource.getScope()))
78             && !ResourceUtils.isUnitTestClass(resource) && context.getMeasure(CoreMetrics.NEW_VIOLATIONS) == null;
79   }
80
81
82   private void clearCache() {
83     ruleToLevel.clear();
84     violationsBySeverity.clear();
85     violationsByRule.clear();
86   }
87
88   private void prepareCurrentResourceViolations(DecoratorContext context) {
89     for (Violation violation : context.getViolations()) {
90       violationsBySeverity.put(violation.getSeverity(), violation);
91       violationsByRule.put(violation.getRule(), violation);
92       ruleToLevel.put(violation.getRule(), violation.getSeverity());
93     }
94   }
95
96   private void saveNewViolations(DecoratorContext context) {
97     Measure measure = new Measure(CoreMetrics.NEW_VIOLATIONS);
98     for (PastSnapshot pastSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) {
99       int variationIndex = pastSnapshot.getIndex();
100       Collection<Measure> children = context.getChildrenMeasures(CoreMetrics.NEW_VIOLATIONS);
101       int count = countViolations(context.getViolations(), pastSnapshot.getTargetDate());
102       double sum = sumChildren(variationIndex, children) + count;
103       measure.setVariation(variationIndex, sum);
104     }
105     context.saveMeasure(measure);
106   }
107
108   private void saveNewViolationsBySeverity(DecoratorContext context) {
109     for (RulePriority priority : RulePriority.values()) {
110       Metric metric = getMetricForSeverity(priority);
111       Measure measure = new Measure(metric);
112       for (PastSnapshot pastSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) {
113         int variationIndex = pastSnapshot.getIndex();
114         int count = countViolations(violationsBySeverity.get(priority), pastSnapshot.getTargetDate());
115         Collection<Measure> children = context.getChildrenMeasures(MeasuresFilters.metric(metric));
116         double sum = sumChildren(variationIndex, children) + count;
117         measure.setVariation(variationIndex, sum);
118       }
119       context.saveMeasure(measure);
120     }
121   }
122
123   private void saveNewViolationsByRule(DecoratorContext context) {
124     ListMultimap<Rule, Measure> childrenByRule = ArrayListMultimap.create();
125     Collection<Measure> children = context.getChildrenMeasures(MeasuresFilters.rules(CoreMetrics.NEW_VIOLATIONS));
126     for (Measure childMeasure : children) {
127       RuleMeasure childRuleMeasure = (RuleMeasure) childMeasure;
128       Rule rule = childRuleMeasure.getRule();
129       if (rule != null) {
130         childrenByRule.put(rule, childMeasure);
131         ruleToLevel.put(childRuleMeasure.getRule(), childRuleMeasure.getRulePriority());
132       }
133     }
134
135     Set<Rule> rules = Sets.newHashSet(violationsByRule.keys());
136     rules.addAll(childrenByRule.keys());
137
138     for (Rule rule : rules) {
139       RuleMeasure measure = RuleMeasure.createForRule(CoreMetrics.NEW_VIOLATIONS, rule, null);
140       measure.setRulePriority(ruleToLevel.get(rule));
141       for (PastSnapshot pastSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) {
142         int variationIndex = pastSnapshot.getIndex();
143         int count = countViolations(violationsByRule.get(rule), pastSnapshot.getTargetDate());
144         double sum = sumChildren(variationIndex, childrenByRule.get(rule)) + count;
145         measure.setVariation(variationIndex, sum);
146       }
147       context.saveMeasure(measure);
148     }
149   }
150
151   int sumChildren(int variationIndex, Collection<Measure> measures) {
152     int sum = 0;
153     for (Measure measure : measures) {
154       Double var = measure.getVariation(variationIndex);
155       if (var != null) {
156         sum += var.intValue();
157       }
158     }
159     return sum;
160   }
161
162   int countViolations(Collection<Violation> violations, Date targetDate) {
163     if (violations == null) {
164       return 0;
165     }
166     int count = 0;
167     for (Violation violation : violations) {
168       if (isAfter(violation, targetDate)) {
169         count++;
170       }
171     }
172     return count;
173   }
174
175   private boolean isAfter(Violation violation, Date date) {
176     return violation.getCreatedAt()!= null && violation.getCreatedAt().after(date);
177   }
178
179   private Metric getMetricForSeverity(RulePriority severity) {
180     Metric metric;
181     if (severity.equals(RulePriority.BLOCKER)) {
182       metric = CoreMetrics.NEW_BLOCKER_VIOLATIONS;
183     } else if (severity.equals(RulePriority.CRITICAL)) {
184       metric = CoreMetrics.NEW_CRITICAL_VIOLATIONS;
185     } else if (severity.equals(RulePriority.MAJOR)) {
186       metric = CoreMetrics.NEW_MAJOR_VIOLATIONS;
187     } else if (severity.equals(RulePriority.MINOR)) {
188       metric = CoreMetrics.NEW_MINOR_VIOLATIONS;
189     } else if (severity.equals(RulePriority.INFO)) {
190       metric = CoreMetrics.NEW_INFO_VIOLATIONS;
191     } else {
192       throw new IllegalArgumentException("Not supported severity: " + severity);
193     }
194     return metric;
195   }
196
197   @Override
198   public String toString() {
199     return getClass().getSimpleName();
200   }
201 }