3 * Copyright (C) 2009-2024 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 java.util.HashMap;
24 import org.sonar.api.measures.CoreMetrics;
25 import org.sonar.ce.task.projectanalysis.component.Component;
26 import org.sonar.ce.task.projectanalysis.measure.Measure;
27 import org.sonar.ce.task.projectanalysis.measure.MeasureRepository;
28 import org.sonar.ce.task.projectanalysis.metric.Metric;
29 import org.sonar.ce.task.projectanalysis.metric.MetricRepository;
30 import org.sonar.core.issue.DefaultIssue;
32 import static org.sonar.api.measures.CoreMetrics.RELIABILITY_REMEDIATION_EFFORT_KEY;
33 import static org.sonar.api.measures.CoreMetrics.SECURITY_REMEDIATION_EFFORT_KEY;
34 import static org.sonar.api.measures.CoreMetrics.TECHNICAL_DEBT_KEY;
37 * Compute effort related measures :
38 * {@link CoreMetrics#TECHNICAL_DEBT_KEY}
39 * {@link CoreMetrics#RELIABILITY_REMEDIATION_EFFORT_KEY}
40 * {@link CoreMetrics#SECURITY_REMEDIATION_EFFORT_KEY}
42 public class EffortAggregator extends IssueVisitor {
44 private final MeasureRepository measureRepository;
45 private final Map<String, EffortCounter> effortsByComponentUuid = new HashMap<>();
47 private final Metric maintainabilityEffortMetric;
48 private final Metric reliabilityEffortMetric;
49 private final Metric securityEffortMetric;
51 private EffortCounter effortCounter;
53 public EffortAggregator(MetricRepository metricRepository, MeasureRepository measureRepository) {
54 this.measureRepository = measureRepository;
55 this.maintainabilityEffortMetric = metricRepository.getByKey(TECHNICAL_DEBT_KEY);
56 this.reliabilityEffortMetric = metricRepository.getByKey(RELIABILITY_REMEDIATION_EFFORT_KEY);
57 this.securityEffortMetric = metricRepository.getByKey(SECURITY_REMEDIATION_EFFORT_KEY);
61 public void beforeComponent(Component component) {
62 effortCounter = new EffortCounter();
63 effortsByComponentUuid.put(component.getUuid(), effortCounter);
65 // aggregate children counters
66 for (Component child : component.getChildren()) {
67 // no need to keep the children in memory. They can be garbage-collected.
68 EffortCounter childEffortCounter = effortsByComponentUuid.remove(child.getUuid());
69 if (childEffortCounter != null) {
70 effortCounter.add(childEffortCounter);
76 public void onIssue(Component component, DefaultIssue issue) {
77 if (issue.resolution() == null) {
78 effortCounter.add(issue);
83 public void afterComponent(Component component) {
84 computeMaintainabilityEffortMeasure(component);
85 computeReliabilityEffortMeasure(component);
86 computeSecurityEffortMeasure(component);
88 this.effortCounter = null;
91 private void computeMaintainabilityEffortMeasure(Component component) {
92 measureRepository.add(component, maintainabilityEffortMetric, Measure.newMeasureBuilder().create(effortCounter.maintainabilityEffort));
95 private void computeReliabilityEffortMeasure(Component component) {
96 measureRepository.add(component, reliabilityEffortMetric, Measure.newMeasureBuilder().create(effortCounter.reliabilityEffort));
99 private void computeSecurityEffortMeasure(Component component) {
100 measureRepository.add(component, securityEffortMetric, Measure.newMeasureBuilder().create(effortCounter.securityEffort));
103 private static class EffortCounter {
104 private long maintainabilityEffort = 0L;
105 private long reliabilityEffort = 0L;
106 private long securityEffort = 0L;
108 void add(DefaultIssue issue) {
109 Long issueEffort = issue.effortInMinutes();
110 if (issueEffort != null && issueEffort != 0L) {
111 switch (issue.type()) {
113 maintainabilityEffort += issueEffort;
116 reliabilityEffort += issueEffort;
119 securityEffort += issueEffort;
121 case SECURITY_HOTSPOT:
125 throw new IllegalStateException(String.format("Unknown type '%s'", issue.type()));
130 public void add(EffortCounter effortCounter) {
131 maintainabilityEffort += effortCounter.maintainabilityEffort;
132 reliabilityEffort += effortCounter.reliabilityEffort;
133 securityEffort += effortCounter.securityEffort;