2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2009 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
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.
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.
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
20 package org.sonar.plugins.core.timemachine;
22 import com.google.common.collect.*;
23 import org.apache.commons.lang.time.DateUtils;
24 import org.sonar.api.batch.Decorator;
25 import org.sonar.api.batch.DecoratorContext;
26 import org.sonar.api.batch.DependedUpon;
27 import org.sonar.api.batch.DependsUpon;
28 import org.sonar.api.measures.*;
29 import org.sonar.api.resources.Project;
30 import org.sonar.api.resources.Resource;
31 import org.sonar.api.resources.ResourceUtils;
32 import org.sonar.api.rules.Rule;
33 import org.sonar.api.rules.RulePriority;
34 import org.sonar.api.rules.Violation;
38 @DependsUpon(classes = ViolationPersisterDecorator.class)
39 public class NewViolationsDecorator implements Decorator {
41 private TimeMachineConfiguration timeMachineConfiguration;
43 // temporary data for current resource
44 private Map<Rule, RulePriority> ruleToLevel = Maps.newHashMap();
45 private Multimap<RulePriority, Violation> violationsBySeverity = ArrayListMultimap.create();
46 private Multimap<Rule, Violation> violationsByRule = ArrayListMultimap.create();
48 public NewViolationsDecorator(TimeMachineConfiguration timeMachineConfiguration) {
49 this.timeMachineConfiguration = timeMachineConfiguration;
52 public boolean shouldExecuteOnProject(Project project) {
53 return project.isLatestAnalysis();
57 public List<Metric> generatesMetric() {
58 return Arrays.asList(CoreMetrics.NEW_VIOLATIONS,
59 CoreMetrics.NEW_BLOCKER_VIOLATIONS, CoreMetrics.NEW_CRITICAL_VIOLATIONS, CoreMetrics.NEW_MAJOR_VIOLATIONS, CoreMetrics.NEW_MINOR_VIOLATIONS, CoreMetrics.NEW_INFO_VIOLATIONS);
62 public void decorate(Resource resource, DecoratorContext context) {
63 if (shouldDecorateResource(resource, context)) {
64 prepareCurrentResourceViolations(context);
65 saveNewViolations(context);
66 saveNewViolationsBySeverity(context);
67 saveNewViolationsByRule(context);
72 private boolean shouldDecorateResource(Resource resource, DecoratorContext context) {
73 return !ResourceUtils.isUnitTestClass(resource) && context.getMeasure(CoreMetrics.NEW_VIOLATIONS) == null;
77 private void clearCache() {
79 violationsBySeverity.clear();
80 violationsByRule.clear();
83 private void prepareCurrentResourceViolations(DecoratorContext context) {
84 for (Violation violation : context.getViolations()) {
85 violationsBySeverity.put(violation.getSeverity(), violation);
86 violationsByRule.put(violation.getRule(), violation);
87 ruleToLevel.put(violation.getRule(), violation.getSeverity());
91 private void saveNewViolations(DecoratorContext context) {
92 Measure measure = new Measure(CoreMetrics.NEW_VIOLATIONS);
93 for (PastSnapshot pastSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) {
94 int variationIndex = pastSnapshot.getIndex();
95 Collection<Measure> children = context.getChildrenMeasures(CoreMetrics.NEW_VIOLATIONS);
96 int count = countViolations(context.getViolations(), pastSnapshot.getTargetDate());
97 double sum = sumChildren(variationIndex, children) + count;
98 measure.setVariation(variationIndex, sum);
100 context.saveMeasure(measure);
103 private void saveNewViolationsBySeverity(DecoratorContext context) {
104 for (RulePriority priority : RulePriority.values()) {
105 Metric metric = getMetricForSeverity(priority);
106 Measure measure = new Measure(metric);
107 for (PastSnapshot pastSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) {
108 int variationIndex = pastSnapshot.getIndex();
109 int count = countViolations(violationsBySeverity.get(priority), pastSnapshot.getTargetDate());
110 Collection<Measure> children = context.getChildrenMeasures(MeasuresFilters.metric(metric));
111 double sum = sumChildren(variationIndex, children) + count;
112 measure.setVariation(variationIndex, sum);
114 context.saveMeasure(measure);
118 private void saveNewViolationsByRule(DecoratorContext context) {
119 ListMultimap<Rule, Measure> childrenByRule = ArrayListMultimap.create();
120 Collection<Measure> children = context.getChildrenMeasures(MeasuresFilters.rules(CoreMetrics.NEW_VIOLATIONS));
121 for (Measure childMeasure : children) {
122 RuleMeasure childRuleMeasure = (RuleMeasure) childMeasure;
123 Rule rule = childRuleMeasure.getRule();
125 childrenByRule.put(rule, childMeasure);
126 ruleToLevel.put(childRuleMeasure.getRule(), childRuleMeasure.getRulePriority());
130 Set<Rule> rules = Sets.newHashSet(violationsByRule.keys());
131 rules.addAll(childrenByRule.keys());
133 for (Rule rule : rules) {
134 RuleMeasure measure = RuleMeasure.createForRule(CoreMetrics.NEW_VIOLATIONS, rule, null);
135 measure.setRulePriority(ruleToLevel.get(rule));
136 for (PastSnapshot pastSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) {
137 int variationIndex = pastSnapshot.getIndex();
138 int count = countViolations(violationsByRule.get(rule), pastSnapshot.getTargetDate());
139 double sum = sumChildren(variationIndex, childrenByRule.get(rule)) + count;
140 measure.setVariation(variationIndex, sum);
142 context.saveMeasure(measure);
146 int sumChildren(int variationIndex, Collection<Measure> measures) {
148 for (Measure measure : measures) {
149 Double var = measure.getVariation(variationIndex);
151 sum += var.intValue();
157 int countViolations(Collection<Violation> violations, Date targetDate) {
158 if (violations == null) {
162 for (Violation violation : violations) {
163 if (isAfter(violation, targetDate)) {
170 private boolean isAfter(Violation violation, Date date) {
171 return violation.getCreatedAt()!= null && violation.getCreatedAt().after(date);
174 private Metric getMetricForSeverity(RulePriority severity) {
176 if (severity.equals(RulePriority.BLOCKER)) {
177 metric = CoreMetrics.NEW_BLOCKER_VIOLATIONS;
178 } else if (severity.equals(RulePriority.CRITICAL)) {
179 metric = CoreMetrics.NEW_CRITICAL_VIOLATIONS;
180 } else if (severity.equals(RulePriority.MAJOR)) {
181 metric = CoreMetrics.NEW_MAJOR_VIOLATIONS;
182 } else if (severity.equals(RulePriority.MINOR)) {
183 metric = CoreMetrics.NEW_MINOR_VIOLATIONS;
184 } else if (severity.equals(RulePriority.INFO)) {
185 metric = CoreMetrics.NEW_INFO_VIOLATIONS;
187 throw new IllegalArgumentException("Not supported severity: " + severity);
193 public String toString() {
194 return getClass().getSimpleName();