]> source.dussan.org Git - sonarqube.git/blob
22efd4cd1bc65d0a94509c69ee69bd5a62ad5334
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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 java.util.HashMap;
23 import java.util.Map;
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;
31
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;
35
36 /**
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}
41  */
42 public class EffortAggregator extends IssueVisitor {
43
44   private final MeasureRepository measureRepository;
45   private final Map<String, EffortCounter> effortsByComponentUuid = new HashMap<>();
46
47   private final Metric maintainabilityEffortMetric;
48   private final Metric reliabilityEffortMetric;
49   private final Metric securityEffortMetric;
50
51   private EffortCounter effortCounter;
52
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);
58   }
59
60   @Override
61   public void beforeComponent(Component component) {
62     effortCounter = new EffortCounter();
63     effortsByComponentUuid.put(component.getUuid(), effortCounter);
64
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);
71       }
72     }
73   }
74
75   @Override
76   public void onIssue(Component component, DefaultIssue issue) {
77     if (issue.resolution() == null) {
78       effortCounter.add(issue);
79     }
80   }
81
82   @Override
83   public void afterComponent(Component component) {
84     computeMaintainabilityEffortMeasure(component);
85     computeReliabilityEffortMeasure(component);
86     computeSecurityEffortMeasure(component);
87
88     this.effortCounter = null;
89   }
90
91   private void computeMaintainabilityEffortMeasure(Component component) {
92     measureRepository.add(component, maintainabilityEffortMetric, Measure.newMeasureBuilder().create(effortCounter.maintainabilityEffort));
93   }
94
95   private void computeReliabilityEffortMeasure(Component component) {
96     measureRepository.add(component, reliabilityEffortMetric, Measure.newMeasureBuilder().create(effortCounter.reliabilityEffort));
97   }
98
99   private void computeSecurityEffortMeasure(Component component) {
100     measureRepository.add(component, securityEffortMetric, Measure.newMeasureBuilder().create(effortCounter.securityEffort));
101   }
102
103   private static class EffortCounter {
104     private long maintainabilityEffort = 0L;
105     private long reliabilityEffort = 0L;
106     private long securityEffort = 0L;
107
108     void add(DefaultIssue issue) {
109       Long issueEffort = issue.effortInMinutes();
110       if (issueEffort != null && issueEffort != 0L) {
111         switch (issue.type()) {
112           case CODE_SMELL:
113             maintainabilityEffort += issueEffort;
114             break;
115           case BUG:
116             reliabilityEffort += issueEffort;
117             break;
118           case VULNERABILITY:
119             securityEffort += issueEffort;
120             break;
121           case SECURITY_HOTSPOT:
122             // Not counted
123             break;
124           default:
125             throw new IllegalStateException(String.format("Unknown type '%s'", issue.type()));
126         }
127       }
128     }
129
130     public void add(EffortCounter effortCounter) {
131       maintainabilityEffort += effortCounter.maintainabilityEffort;
132       reliabilityEffort += effortCounter.reliabilityEffort;
133       securityEffort += effortCounter.securityEffort;
134     }
135   }
136 }