2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2011 SonarSource
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.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;
40 @DependsUpon(DecoratorBarriers.END_OF_VIOLATION_TRACKING)
41 public class NewViolationsDecorator implements Decorator {
43 private TimeMachineConfiguration timeMachineConfiguration;
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();
50 public NewViolationsDecorator(TimeMachineConfiguration timeMachineConfiguration) {
51 this.timeMachineConfiguration = timeMachineConfiguration;
54 public boolean shouldExecuteOnProject(Project project) {
55 return project.isLatestAnalysis();
59 public List<Metric> generatesMetric() {
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);
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);
75 private boolean shouldDecorateResource(Resource resource, DecoratorContext context) {
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;
82 private void clearCache() {
84 violationsBySeverity.clear();
85 violationsByRule.clear();
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());
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);
105 context.saveMeasure(measure);
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);
119 context.saveMeasure(measure);
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();
130 childrenByRule.put(rule, childMeasure);
131 ruleToLevel.put(childRuleMeasure.getRule(), childRuleMeasure.getRulePriority());
135 Set<Rule> rules = Sets.newHashSet(violationsByRule.keys());
136 rules.addAll(childrenByRule.keys());
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);
147 context.saveMeasure(measure);
151 int sumChildren(int variationIndex, Collection<Measure> measures) {
153 for (Measure measure : measures) {
154 Double var = measure.getVariation(variationIndex);
156 sum += var.intValue();
162 int countViolations(Collection<Violation> violations, Date targetDate) {
163 if (violations == null) {
167 for (Violation violation : violations) {
168 if (isAfter(violation, targetDate)) {
175 private boolean isAfter(Violation violation, Date date) {
176 return violation.getCreatedAt()!= null && violation.getCreatedAt().after(date);
179 private Metric getMetricForSeverity(RulePriority severity) {
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;
192 throw new IllegalArgumentException("Not supported severity: " + severity);
198 public String toString() {
199 return getClass().getSimpleName();