]> source.dussan.org Git - sonarqube.git/blob
30376875414a10bd661399c17caf1cb7f736555d
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.ce.task.projectanalysis.issue;
21
22 import com.google.common.base.MoreObjects;
23 import java.util.HashMap;
24 import java.util.Map;
25 import org.sonar.api.measures.CoreMetrics;
26 import org.sonar.ce.task.projectanalysis.component.Component;
27 import org.sonar.ce.task.projectanalysis.measure.Measure;
28 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
29 import org.sonar.ce.task.projectanalysis.metric.Metric;
30 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
31 import org.sonar.ce.task.projectanalysis.period.Period;
32 import org.sonar.ce.task.projectanalysis.period.PeriodHolder;
33 import org.sonar.core.issue.DefaultIssue;
34
35 import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_REMEDIATION_EFFORT_KEY;
36 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_REMEDIATION_EFFORT_KEY;
37 import static org.sonar.api.measures.CoreMetrics.NEW_TECHNICAL_DEBT_KEY;
38 import static org.sonar.api.utils.DateUtils.truncateToSeconds;
39
40 /**
41  * Compute new effort related measures :
42  * {@link CoreMetrics#NEW_TECHNICAL_DEBT_KEY}
43  * {@link CoreMetrics#NEW_RELIABILITY_REMEDIATION_EFFORT_KEY}
44  * {@link CoreMetrics#NEW_SECURITY_REMEDIATION_EFFORT_KEY}
45  */
46 public class NewEffortAggregator extends IssueVisitor {
47   private final Map<String, NewEffortCounter> counterByComponentUuid = new HashMap<>();
48   private final PeriodHolder periodHolder;
49   private final MeasureRepository measureRepository;
50
51   private final Metric newMaintainabilityEffortMetric;
52   private final Metric newReliabilityEffortMetric;
53   private final Metric newSecurityEffortMetric;
54
55   private NewEffortCounter counter = null;
56
57   public NewEffortAggregator(PeriodHolder periodHolder, MetricRepository metricRepository, MeasureRepository measureRepository) {
58     this.periodHolder = periodHolder;
59     this.measureRepository = measureRepository;
60
61     this.newMaintainabilityEffortMetric = metricRepository.getByKey(NEW_TECHNICAL_DEBT_KEY);
62     this.newReliabilityEffortMetric = metricRepository.getByKey(NEW_RELIABILITY_REMEDIATION_EFFORT_KEY);
63     this.newSecurityEffortMetric = metricRepository.getByKey(NEW_SECURITY_REMEDIATION_EFFORT_KEY);
64   }
65
66   @Override
67   public void beforeComponent(Component component) {
68     counter = new NewEffortCounter();
69     counterByComponentUuid.put(component.getUuid(), counter);
70     for (Component child : component.getChildren()) {
71       NewEffortCounter childSum = counterByComponentUuid.remove(child.getUuid());
72       if (childSum != null) {
73         counter.add(childSum);
74       }
75     }
76   }
77
78   @Override
79   public void onIssue(Component component, DefaultIssue issue) {
80     if (issue.resolution() == null && issue.effortInMinutes() != null && periodHolder.hasPeriod()) {
81       counter.add(issue, periodHolder.getPeriod());
82     }
83   }
84
85   @Override
86   public void afterComponent(Component component) {
87     if (periodHolder.hasPeriod()) {
88       computeMeasure(component, newMaintainabilityEffortMetric, counter.maintainabilitySum);
89       computeMeasure(component, newReliabilityEffortMetric, counter.reliabilitySum);
90       computeMeasure(component, newSecurityEffortMetric, counter.securitySum);
91     }
92     counter = null;
93   }
94
95   private void computeMeasure(Component component, Metric metric, EffortSum effortSum) {
96     double variation = effortSum.isEmpty ? 0.0 : effortSum.newEffort;
97     measureRepository.add(component, metric, Measure.newMeasureBuilder().setVariation(variation).createNoValue());
98   }
99
100   private static class NewEffortCounter {
101     private final EffortSum maintainabilitySum = new EffortSum();
102     private final EffortSum reliabilitySum = new EffortSum();
103     private final EffortSum securitySum = new EffortSum();
104
105     void add(NewEffortCounter otherCounter) {
106       maintainabilitySum.add(otherCounter.maintainabilitySum);
107       reliabilitySum.add(otherCounter.reliabilitySum);
108       securitySum.add(otherCounter.securitySum);
109     }
110
111     void add(DefaultIssue issue, Period period) {
112       long newEffort = calculate(issue, period);
113       switch (issue.type()) {
114         case CODE_SMELL:
115           maintainabilitySum.add(newEffort);
116           break;
117         case BUG:
118           reliabilitySum.add(newEffort);
119           break;
120         case VULNERABILITY:
121           securitySum.add(newEffort);
122           break;
123         case SECURITY_HOTSPOT:
124           // Not counted
125           break;
126         default:
127           throw new IllegalStateException(String.format("Unknown type '%s'", issue.type()));
128       }
129     }
130
131     long calculate(DefaultIssue issue, Period period) {
132       if (issue.creationDate().getTime() > truncateToSeconds(period.getSnapshotDate())) {
133         return MoreObjects.firstNonNull(issue.effortInMinutes(), 0L);
134       }
135       return 0L;
136     }
137   }
138
139   private static class EffortSum {
140     private Double newEffort;
141     private boolean isEmpty = true;
142
143     void add(long newEffort) {
144       double previous = MoreObjects.firstNonNull(this.newEffort, 0d);
145       this.newEffort = previous + newEffort;
146       isEmpty = false;
147     }
148
149     void add(EffortSum other) {
150       Double otherValue = other.newEffort;
151       if (otherValue != null) {
152         add(otherValue.longValue());
153       }
154     }
155   }
156 }