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 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.core.issue.DefaultIssue;
32 import org.sonar.server.metric.SoftwareQualitiesMetrics;
34 import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_REMEDIATION_EFFORT_KEY;
35 import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_REMEDIATION_EFFORT_KEY;
36 import static org.sonar.api.measures.CoreMetrics.NEW_TECHNICAL_DEBT_KEY;
37 import static org.sonar.server.metric.SoftwareQualitiesMetrics.NEW_SOFTWARE_QUALITY_MAINTAINABILITY_REMEDIATION_EFFORT_KEY;
38 import static org.sonar.server.metric.SoftwareQualitiesMetrics.NEW_SOFTWARE_QUALITY_RELIABILITY_REMEDIATION_EFFORT_KEY;
39 import static org.sonar.server.metric.SoftwareQualitiesMetrics.NEW_SOFTWARE_QUALITY_SECURITY_REMEDIATION_EFFORT_KEY;
42 * Compute new effort related measures :
43 * {@link CoreMetrics#NEW_TECHNICAL_DEBT_KEY}
44 * {@link CoreMetrics#NEW_RELIABILITY_REMEDIATION_EFFORT_KEY}
45 * {@link CoreMetrics#NEW_SECURITY_REMEDIATION_EFFORT_KEY}
46 * {@link SoftwareQualitiesMetrics#NEW_SOFTWARE_QUALITY_MAINTAINABILITY_REMEDIATION_EFFORT_KEY}
47 * {@link SoftwareQualitiesMetrics#NEW_SOFTWARE_QUALITY_RELIABILITY_REMEDIATION_EFFORT_KEY}
48 * {@link SoftwareQualitiesMetrics#NEW_SOFTWARE_QUALITY_SECURITY_REMEDIATION_EFFORT_KEY}
50 public class NewEffortAggregator extends IssueVisitor {
51 private final Map<String, NewEffortCounter> counterByComponentUuid = new HashMap<>();
52 private final MeasureRepository measureRepository;
54 private final Metric newMaintainabilityEffortMetric;
55 private final Metric newReliabilityEffortMetric;
56 private final Metric newSecurityEffortMetric;
57 private final NewIssueClassifier newIssueClassifier;
58 private final Metric newSoftwareQualityMaintainabilityEffortMetric;
59 private final Metric newSoftwareQualityReliabilityEffortMetric;
60 private final Metric newSoftwareQualitySecurityEffortMetric;
62 private NewEffortCounter counter = null;
64 public NewEffortAggregator(MetricRepository metricRepository, MeasureRepository measureRepository, NewIssueClassifier newIssueClassifier) {
65 this.measureRepository = measureRepository;
67 // Based on issue Type and Severity
68 this.newMaintainabilityEffortMetric = metricRepository.getByKey(NEW_TECHNICAL_DEBT_KEY);
69 this.newReliabilityEffortMetric = metricRepository.getByKey(NEW_RELIABILITY_REMEDIATION_EFFORT_KEY);
70 this.newSecurityEffortMetric = metricRepository.getByKey(NEW_SECURITY_REMEDIATION_EFFORT_KEY);
72 // Based on software qualities
73 this.newSoftwareQualityMaintainabilityEffortMetric = metricRepository.getByKey(NEW_SOFTWARE_QUALITY_MAINTAINABILITY_REMEDIATION_EFFORT_KEY);
74 this.newSoftwareQualityReliabilityEffortMetric = metricRepository.getByKey(NEW_SOFTWARE_QUALITY_RELIABILITY_REMEDIATION_EFFORT_KEY);
75 this.newSoftwareQualitySecurityEffortMetric = metricRepository.getByKey(NEW_SOFTWARE_QUALITY_SECURITY_REMEDIATION_EFFORT_KEY);
77 this.newIssueClassifier = newIssueClassifier;
81 public void beforeComponent(Component component) {
82 counter = new NewEffortCounter();
83 counterByComponentUuid.put(component.getUuid(), counter);
84 for (Component child : component.getChildren()) {
85 NewEffortCounter childSum = counterByComponentUuid.remove(child.getUuid());
86 if (childSum != null) {
87 counter.add(childSum);
93 public void onIssue(Component component, DefaultIssue issue) {
94 if (issue.resolution() == null && issue.effortInMinutes() != null) {
95 counter.add(component, issue);
100 public void afterComponent(Component component) {
101 if (newIssueClassifier.isEnabled()) {
102 computeMeasure(component, newMaintainabilityEffortMetric, counter.maintainabilitySum);
103 computeMeasure(component, newReliabilityEffortMetric, counter.reliabilitySum);
104 computeMeasure(component, newSecurityEffortMetric, counter.securitySum);
106 computeMeasure(component, newSoftwareQualityMaintainabilityEffortMetric, counter.softwareQualityMaintainabilitySum);
107 computeMeasure(component, newSoftwareQualityReliabilityEffortMetric, counter.softwareQualityReliabilitySum);
108 computeMeasure(component, newSoftwareQualitySecurityEffortMetric, counter.softwareQualitySecuritySum);
113 private void computeMeasure(Component component, Metric metric, EffortSum effortSum) {
114 long value = effortSum.isEmpty ? 0 : effortSum.newEffort;
115 measureRepository.add(component, metric, Measure.newMeasureBuilder().create(value));
118 private class NewEffortCounter {
119 private final EffortSum maintainabilitySum = new EffortSum();
120 private final EffortSum reliabilitySum = new EffortSum();
121 private final EffortSum securitySum = new EffortSum();
123 private final EffortSum softwareQualityMaintainabilitySum = new EffortSum();
124 private final EffortSum softwareQualityReliabilitySum = new EffortSum();
125 private final EffortSum softwareQualitySecuritySum = new EffortSum();
127 void add(NewEffortCounter otherCounter) {
128 maintainabilitySum.add(otherCounter.maintainabilitySum);
129 reliabilitySum.add(otherCounter.reliabilitySum);
130 securitySum.add(otherCounter.securitySum);
132 softwareQualityMaintainabilitySum.add(otherCounter.softwareQualityMaintainabilitySum);
133 softwareQualityReliabilitySum.add(otherCounter.softwareQualityReliabilitySum);
134 softwareQualitySecuritySum.add(otherCounter.softwareQualitySecuritySum);
137 void add(Component component, DefaultIssue issue) {
138 long newEffort = calculate(component, issue);
139 computeTypeEffort(issue, newEffort);
140 computeSoftwareQualityEffort(issue, newEffort);
143 private void computeSoftwareQualityEffort(DefaultIssue issue, long newEffort) {
144 issue.impacts().forEach((sq, severity) -> {
146 case MAINTAINABILITY:
147 softwareQualityMaintainabilitySum.add(newEffort);
150 softwareQualityReliabilitySum.add(newEffort);
153 softwareQualitySecuritySum.add(newEffort);
156 throw new IllegalStateException(String.format("Unknown software quality '%s'", sq));
161 private void computeTypeEffort(DefaultIssue issue, long newEffort) {
162 switch (issue.type()) {
164 maintainabilitySum.add(newEffort);
167 reliabilitySum.add(newEffort);
170 securitySum.add(newEffort);
172 case SECURITY_HOTSPOT:
176 throw new IllegalStateException(String.format("Unknown type '%s'", issue.type()));
180 long calculate(Component component, DefaultIssue issue) {
181 if (newIssueClassifier.isNew(component, issue)) {
182 return MoreObjects.firstNonNull(issue.effortInMinutes(), 0L);
189 private static class EffortSum {
190 private Long newEffort;
191 private boolean isEmpty = true;
193 void add(long newEffort) {
194 long previous = MoreObjects.firstNonNull(this.newEffort, 0L);
195 this.newEffort = previous + newEffort;
199 void add(EffortSum other) {
200 Long otherValue = other.newEffort;
201 if (otherValue != null) {