3 * Copyright (C) 2009-2019 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.ce.task.projectanalysis.issue;
22 import com.google.common.base.MoreObjects;
23 import java.util.HashMap;
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;
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;
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}
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;
51 private final Metric newMaintainabilityEffortMetric;
52 private final Metric newReliabilityEffortMetric;
53 private final Metric newSecurityEffortMetric;
55 private NewEffortCounter counter = null;
57 public NewEffortAggregator(PeriodHolder periodHolder, MetricRepository metricRepository, MeasureRepository measureRepository) {
58 this.periodHolder = periodHolder;
59 this.measureRepository = measureRepository;
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);
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);
79 public void onIssue(Component component, DefaultIssue issue) {
80 if (issue.resolution() == null && issue.effortInMinutes() != null && periodHolder.hasPeriod()) {
81 counter.add(issue, periodHolder.getPeriod());
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);
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());
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();
105 void add(NewEffortCounter otherCounter) {
106 maintainabilitySum.add(otherCounter.maintainabilitySum);
107 reliabilitySum.add(otherCounter.reliabilitySum);
108 securitySum.add(otherCounter.securitySum);
111 void add(DefaultIssue issue, Period period) {
112 long newEffort = calculate(issue, period);
113 switch (issue.type()) {
115 maintainabilitySum.add(newEffort);
118 reliabilitySum.add(newEffort);
121 securitySum.add(newEffort);
123 case SECURITY_HOTSPOT:
127 throw new IllegalStateException(String.format("Unknown type '%s'", issue.type()));
131 long calculate(DefaultIssue issue, Period period) {
132 if (issue.creationDate().getTime() > truncateToSeconds(period.getSnapshotDate())) {
133 return MoreObjects.firstNonNull(issue.effortInMinutes(), 0L);
139 private static class EffortSum {
140 private Double newEffort;
141 private boolean isEmpty = true;
143 void add(long newEffort) {
144 double previous = MoreObjects.firstNonNull(this.newEffort, 0d);
145 this.newEffort = previous + newEffort;
149 void add(EffortSum other) {
150 Double otherValue = other.newEffort;
151 if (otherValue != null) {
152 add(otherValue.longValue());