3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.issue;
22 import com.google.common.base.MoreObjects;
23 import com.google.common.collect.ArrayListMultimap;
24 import com.google.common.collect.ListMultimap;
25 import java.util.HashMap;
26 import java.util.List;
28 import org.sonar.api.measures.CoreMetrics;
29 import org.sonar.core.issue.DefaultIssue;
30 import org.sonar.db.DbClient;
31 import org.sonar.db.DbSession;
32 import org.sonar.db.issue.IssueChangeDto;
33 import org.sonar.server.computation.task.projectanalysis.component.Component;
34 import org.sonar.server.computation.task.projectanalysis.measure.Measure;
35 import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepository;
36 import org.sonar.server.computation.task.projectanalysis.metric.Metric;
37 import org.sonar.server.computation.task.projectanalysis.metric.MetricRepository;
38 import org.sonar.server.computation.task.projectanalysis.period.Period;
39 import org.sonar.server.computation.task.projectanalysis.period.PeriodHolder;
41 import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_REMEDIATION_EFFORT_KEY;
42 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_REMEDIATION_EFFORT_KEY;
43 import static org.sonar.api.measures.CoreMetrics.NEW_TECHNICAL_DEBT_KEY;
46 * Compute new effort related measures :
47 * {@link CoreMetrics#NEW_TECHNICAL_DEBT_KEY}
48 * {@link CoreMetrics#NEW_RELIABILITY_REMEDIATION_EFFORT_KEY}
49 * {@link CoreMetrics#NEW_SECURITY_REMEDIATION_EFFORT_KEY}
51 public class NewEffortAggregator extends IssueVisitor {
53 private final NewEffortCalculator calculator;
54 private final PeriodHolder periodHolder;
55 private final DbClient dbClient;
56 private final MeasureRepository measureRepository;
58 private final Metric newMaintainabilityEffortMetric;
59 private final Metric newReliabilityEffortMetric;
60 private final Metric newSecurityEffortMetric;
62 private ListMultimap<String, IssueChangeDto> changesByIssueUuid = ArrayListMultimap.create();
63 private Map<Integer, NewEffortCounter> counterByComponentRef = new HashMap<>();
64 private NewEffortCounter counter = null;
66 public NewEffortAggregator(NewEffortCalculator calculator, PeriodHolder periodHolder, DbClient dbClient,
67 MetricRepository metricRepository, MeasureRepository measureRepository) {
68 this.calculator = calculator;
69 this.periodHolder = periodHolder;
70 this.dbClient = dbClient;
71 this.measureRepository = measureRepository;
73 this.newMaintainabilityEffortMetric = metricRepository.getByKey(NEW_TECHNICAL_DEBT_KEY);
74 this.newReliabilityEffortMetric = metricRepository.getByKey(NEW_RELIABILITY_REMEDIATION_EFFORT_KEY);
75 this.newSecurityEffortMetric = metricRepository.getByKey(NEW_SECURITY_REMEDIATION_EFFORT_KEY);
79 public void beforeComponent(Component component) {
80 try (DbSession dbSession = dbClient.openSession(false)) {
81 List<IssueChangeDto> changes = dbClient.issueChangeDao().selectChangelogOfNonClosedIssuesByComponent(dbSession, component.getUuid());
82 for (IssueChangeDto change : changes) {
83 changesByIssueUuid.put(change.getIssueKey(), change);
87 counter = new NewEffortCounter(calculator);
88 counterByComponentRef.put(component.getReportAttributes().getRef(), counter);
89 for (Component child : component.getChildren()) {
90 NewEffortCounter childSum = counterByComponentRef.remove(child.getReportAttributes().getRef());
91 if (childSum != null) {
92 counter.add(childSum);
98 public void onIssue(Component component, DefaultIssue issue) {
99 if (issue.resolution() == null && issue.effortInMinutes() != null && periodHolder.hasPeriod()) {
100 List<IssueChangeDto> changelog = changesByIssueUuid.get(issue.key());
101 counter.add(issue, periodHolder.getPeriod(), changelog);
106 public void afterComponent(Component component) {
107 computeMeasure(component, newMaintainabilityEffortMetric, counter.maintainabilitySum);
108 computeMeasure(component, newReliabilityEffortMetric, counter.reliabilitySum);
109 computeMeasure(component, newSecurityEffortMetric, counter.securitySum);
110 changesByIssueUuid.clear();
114 private void computeMeasure(Component component, Metric metric, EffortSum effortSum) {
115 if (!effortSum.isEmpty) {
116 measureRepository.add(component, metric, Measure.newMeasureBuilder().setVariation(effortSum.newEffort).createNoValue());
120 private static class NewEffortCounter {
121 private final NewEffortCalculator calculator;
123 private final EffortSum maintainabilitySum = new EffortSum();
124 private final EffortSum reliabilitySum = new EffortSum();
125 private final EffortSum securitySum = new EffortSum();
127 public NewEffortCounter(NewEffortCalculator calculator) {
128 this.calculator = calculator;
131 void add(NewEffortCounter otherCounter) {
132 maintainabilitySum.add(otherCounter.maintainabilitySum);
133 reliabilitySum.add(otherCounter.reliabilitySum);
134 securitySum.add(otherCounter.securitySum);
137 void add(DefaultIssue issue, Period period, List<IssueChangeDto> changelog) {
138 long newEffort = calculator.calculate(issue, changelog, period);
139 switch (issue.type()) {
141 maintainabilitySum.add(newEffort);
144 reliabilitySum.add(newEffort);
147 securitySum.add(newEffort);
150 throw new IllegalStateException(String.format("Unknown type '%s'", issue.type()));
155 private static class EffortSum {
156 private Double newEffort;
157 private boolean isEmpty = true;
159 void add(long newEffort) {
160 double previous = MoreObjects.firstNonNull(this.newEffort, 0d);
161 this.newEffort = previous + newEffort;
165 void add(EffortSum other) {
166 Double otherValue = other.newEffort;
167 if (otherValue != null) {
168 add(otherValue.longValue());