]> source.dussan.org Git - sonarqube.git/blob
c491feafdabdf519e1043d4e842476f329838c31
[sonarqube.git] /
1 /*
2  * Sonar, open source software quality management tool.
3  * Copyright (C) 2009 SonarSource SA
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.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;
35
36 import java.util.*;
37
38 @DependsUpon(classes = ViolationPersisterDecorator.class)
39 public class NewViolationsDecorator implements Decorator {
40
41   private TimeMachineConfiguration timeMachineConfiguration;
42
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();
47
48   public NewViolationsDecorator(TimeMachineConfiguration timeMachineConfiguration) {
49     this.timeMachineConfiguration = timeMachineConfiguration;
50   }
51
52   public boolean shouldExecuteOnProject(Project project) {
53     return project.isLatestAnalysis();
54   }
55
56   @DependedUpon
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);
60   }
61
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);
68       clearCache();
69     }
70   }
71
72   private boolean shouldDecorateResource(Resource resource, DecoratorContext context) {
73     return !ResourceUtils.isUnitTestClass(resource) && context.getMeasure(CoreMetrics.NEW_VIOLATIONS) == null;
74   }
75
76
77   private void clearCache() {
78     ruleToLevel.clear();
79     violationsBySeverity.clear();
80     violationsByRule.clear();
81   }
82
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());
88     }
89   }
90
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);
99     }
100     context.saveMeasure(measure);
101   }
102
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);
113       }
114       context.saveMeasure(measure);
115     }
116   }
117
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();
124       if (rule != null) {
125         childrenByRule.put(rule, childMeasure);
126         ruleToLevel.put(childRuleMeasure.getRule(), childRuleMeasure.getRulePriority());
127       }
128     }
129
130     Set<Rule> rules = Sets.newHashSet(violationsByRule.keys());
131     rules.addAll(childrenByRule.keys());
132
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);
141       }
142       context.saveMeasure(measure);
143     }
144   }
145
146   int sumChildren(int variationIndex, Collection<Measure> measures) {
147     int sum = 0;
148     for (Measure measure : measures) {
149       Double var = measure.getVariation(variationIndex);
150       if (var != null) {
151         sum += var.intValue();
152       }
153     }
154     return sum;
155   }
156
157   int countViolations(Collection<Violation> violations, Date targetDate) {
158     if (violations == null) {
159       return 0;
160     }
161     int count = 0;
162     for (Violation violation : violations) {
163       if (isAfter(violation, targetDate)) {
164         count++;
165       }
166     }
167     return count;
168   }
169
170   private boolean isAfter(Violation violation, Date date) {
171     return violation.getCreatedAt()!= null && violation.getCreatedAt().after(date);
172   }
173
174   private Metric getMetricForSeverity(RulePriority severity) {
175     Metric metric;
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;
186     } else {
187       throw new IllegalArgumentException("Not supported severity: " + severity);
188     }
189     return metric;
190   }
191
192   @Override
193   public String toString() {
194     return getClass().getSimpleName();
195   }
196 }