]> source.dussan.org Git - sonarqube.git/blob
d0ede5918186cd52c5c730126a013e5924c9f539
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.issue;
21
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;
27 import java.util.Map;
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;
40
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;
44
45 /**
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}
50  */
51 public class NewEffortAggregator extends IssueVisitor {
52
53   private final NewEffortCalculator calculator;
54   private final PeriodHolder periodHolder;
55   private final DbClient dbClient;
56   private final MeasureRepository measureRepository;
57
58   private final Metric newMaintainabilityEffortMetric;
59   private final Metric newReliabilityEffortMetric;
60   private final Metric newSecurityEffortMetric;
61
62   private ListMultimap<String, IssueChangeDto> changesByIssueUuid = ArrayListMultimap.create();
63   private Map<Integer, NewEffortCounter> counterByComponentRef = new HashMap<>();
64   private NewEffortCounter counter = null;
65
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;
72
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);
76   }
77
78   @Override
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);
84       }
85     }
86
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);
93       }
94     }
95   }
96
97   @Override
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);
102     }
103   }
104
105   @Override
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();
111     counter = null;
112   }
113
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());
117     }
118   }
119
120   private static class NewEffortCounter {
121     private final NewEffortCalculator calculator;
122
123     private final EffortSum maintainabilitySum = new EffortSum();
124     private final EffortSum reliabilitySum = new EffortSum();
125     private final EffortSum securitySum = new EffortSum();
126
127     public NewEffortCounter(NewEffortCalculator calculator) {
128       this.calculator = calculator;
129     }
130
131     void add(NewEffortCounter otherCounter) {
132       maintainabilitySum.add(otherCounter.maintainabilitySum);
133       reliabilitySum.add(otherCounter.reliabilitySum);
134       securitySum.add(otherCounter.securitySum);
135     }
136
137     void add(DefaultIssue issue, Period period, List<IssueChangeDto> changelog) {
138       long newEffort = calculator.calculate(issue, changelog, period);
139       switch (issue.type()) {
140         case CODE_SMELL:
141           maintainabilitySum.add(newEffort);
142           break;
143         case BUG:
144           reliabilitySum.add(newEffort);
145           break;
146         case VULNERABILITY:
147           securitySum.add(newEffort);
148           break;
149         default:
150           throw new IllegalStateException(String.format("Unknown type '%s'", issue.type()));
151       }
152     }
153   }
154
155   private static class EffortSum {
156     private Double newEffort;
157     private boolean isEmpty = true;
158
159     void add(long newEffort) {
160       double previous = MoreObjects.firstNonNull(this.newEffort, 0d);
161       this.newEffort = previous + newEffort;
162       isEmpty = false;
163     }
164
165     void add(EffortSum other) {
166       Double otherValue = other.newEffort;
167       if (otherValue != null) {
168         add(otherValue.longValue());
169       }
170     }
171   }
172 }