/
SqaleNewMeasuresVisitor.java
249 lines (214 loc) · 9.9 KB
/
SqaleNewMeasuresVisitor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.computation.sqale;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.utils.KeyValueFormat;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.component.CrawlerDepthLimit;
import org.sonar.server.computation.component.PathAwareVisitorAdapter;
import org.sonar.server.computation.formula.counter.LongVariationValue;
import org.sonar.server.computation.measure.Measure;
import org.sonar.server.computation.measure.MeasureRepository;
import org.sonar.server.computation.measure.MeasureVariations;
import org.sonar.server.computation.metric.Metric;
import org.sonar.server.computation.metric.MetricRepository;
import org.sonar.server.computation.period.Period;
import org.sonar.server.computation.period.PeriodsHolder;
import org.sonar.server.computation.scm.Changeset;
import org.sonar.server.computation.scm.ScmInfo;
import org.sonar.server.computation.scm.ScmInfoRepository;
import static com.google.common.collect.FluentIterable.from;
import static org.sonar.api.utils.KeyValueFormat.newIntegerConverter;
import static org.sonar.server.computation.component.ComponentVisitor.Order.POST_ORDER;
import static org.sonar.server.computation.measure.Measure.newMeasureBuilder;
import static org.sonar.server.computation.measure.MeasureVariations.newMeasureVariationsBuilder;
/**
* This visitor depends on {@link org.sonar.server.computation.issue.IntegrateIssuesVisitor} for the computation of
* metric {@link CoreMetrics#NEW_TECHNICAL_DEBT}.
*/
public class SqaleNewMeasuresVisitor extends PathAwareVisitorAdapter<SqaleNewMeasuresVisitor.NewDevelopmentCostCounter> {
private static final Logger LOG = Loggers.get(SqaleNewMeasuresVisitor.class);
private final ScmInfoRepository scmInfoRepository;
private final MeasureRepository measureRepository;
private final PeriodsHolder periodsHolder;
private final SqaleRatingSettings sqaleRatingSettings;
private final Metric newDebtMetric;
private final Metric nclocDataMetric;
private final Metric newDebtRatioMetric;
public SqaleNewMeasuresVisitor(MetricRepository metricRepository, MeasureRepository measureRepository, ScmInfoRepository scmInfoRepository, PeriodsHolder periodsHolder,
SqaleRatingSettings sqaleRatingSettings) {
super(CrawlerDepthLimit.FILE, POST_ORDER, NewDevelopmentCostCounterFactory.INSTANCE);
this.measureRepository = measureRepository;
this.scmInfoRepository = scmInfoRepository;
this.periodsHolder = periodsHolder;
this.sqaleRatingSettings = sqaleRatingSettings;
// computed by NewDebtAggregator which is executed by IntegrateIssuesVisitor
this.newDebtMetric = metricRepository.getByKey(CoreMetrics.NEW_TECHNICAL_DEBT_KEY);
// which line is ncloc and which isn't
this.nclocDataMetric = metricRepository.getByKey(CoreMetrics.NCLOC_DATA_KEY);
// output metric
this.newDebtRatioMetric = metricRepository.getByKey(CoreMetrics.NEW_SQALE_DEBT_RATIO_KEY);
}
@Override
public void visitProject(Component project, Path<NewDevelopmentCostCounter> path) {
computeAndSaveNewDebtRatioMeasure(project, path);
}
@Override
public void visitModule(Component module, Path<NewDevelopmentCostCounter> path) {
computeAndSaveNewDebtRatioMeasure(module, path);
increaseNewDevCostOfParent(path);
}
@Override
public void visitDirectory(Component directory, Path<NewDevelopmentCostCounter> path) {
computeAndSaveNewDebtRatioMeasure(directory, path);
increaseNewDevCostOfParent(path);
}
@Override
public void visitFile(Component file, Path<NewDevelopmentCostCounter> path) {
if (file.getFileAttributes().isUnitTest()) {
return;
}
initNewDebtRatioCounter(file, path);
computeAndSaveNewDebtRatioMeasure(file, path);
increaseNewDevCostOfParent(path);
}
private void computeAndSaveNewDebtRatioMeasure(Component component, Path<NewDevelopmentCostCounter> path) {
MeasureVariations.Builder builder = newMeasureVariationsBuilder();
for (Period period : periodsHolder.getPeriods()) {
long newDevCost = path.current().getValue(period).getValue();
double density = computeDensity(component, period, newDevCost);
builder.setVariation(period, 100.0 * density);
}
if (!builder.isEmpty()) {
Measure measure = newMeasureBuilder().setVariations(builder.build()).createNoValue();
measureRepository.add(component, this.newDebtRatioMetric, measure);
}
}
private double computeDensity(Component component, Period period, long developmentCost) {
double debt = getLongValue(measureRepository.getRawMeasure(component, this.newDebtMetric), period);
if (developmentCost != 0L) {
return debt / (double) developmentCost;
}
return 0d;
}
private static long getLongValue(Optional<Measure> measure, Period period) {
if (!measure.isPresent()) {
return 0L;
}
return getLongValue(measure.get(), period);
}
private static long getLongValue(Measure measure, Period period) {
if (measure.hasVariations() && measure.getVariations().hasVariation(period.getIndex())) {
return (long) measure.getVariations().getVariation(period.getIndex());
}
return 0L;
}
private void initNewDebtRatioCounter(Component file, Path<NewDevelopmentCostCounter> path) {
// first analysis, no period, no differential value to compute, save processing time and return now
if (periodsHolder.getPeriods().isEmpty()) {
return;
}
Optional<Measure> nclocDataMeasure = measureRepository.getRawMeasure(file, this.nclocDataMetric);
if (!nclocDataMeasure.isPresent()) {
return;
}
Optional<ScmInfo> scmInfoOptional = scmInfoRepository.getScmInfo(file);
if (!scmInfoOptional.isPresent()) {
LOG.trace(String.format("No changeset for file %s. Dev cost will be zero.", file.getKey()));
return;
}
ScmInfo scmInfo = scmInfoOptional.get();
initNewDebtRatioCounter(path.current(), file.getFileAttributes().getLanguageKey(), nclocDataMeasure.get(), scmInfo);
}
private void initNewDebtRatioCounter(NewDevelopmentCostCounter devCostCounter, String languageKey, Measure nclocDataMeasure, ScmInfo scmInfo) {
long lineDevCost = sqaleRatingSettings.getDevCost(languageKey);
for (Integer nclocLineIndex : nclocLineIndexes(nclocDataMeasure)) {
Changeset changeset = scmInfo.getChangesetForLine(nclocLineIndex);
for (Period period : periodsHolder.getPeriods()) {
if (isLineInPeriod(changeset.getDate(), period)) {
devCostCounter.increment(period, lineDevCost);
}
}
}
}
private static void increaseNewDevCostOfParent(Path<NewDevelopmentCostCounter> path) {
path.parent().add(path.current());
}
/**
* A line belongs to a Period if its date is older than the SNAPSHOT's date of the period.
*/
private static boolean isLineInPeriod(long lineDate, Period period) {
return lineDate > period.getSnapshotDate();
}
/**
* NCLOC_DATA contains Key-value pairs, where key - is a number of line, and value - is an indicator of whether line
* contains code (1) or not (0).
*
* This method parses the value of the NCLOC_DATA measure and return the line numbers of lines which contain code.
*/
private static Iterable<Integer> nclocLineIndexes(Measure nclocDataMeasure) {
Map<Integer, Integer> parsedNclocData = KeyValueFormat.parse(nclocDataMeasure.getData(), newIntegerConverter(), newIntegerConverter());
return from(parsedNclocData.entrySet())
.filter(NclocEntryNclocLine.INSTANCE)
.transform(MapEntryToKey.INSTANCE);
}
public static final class NewDevelopmentCostCounter {
private final LongVariationValue.Array devCosts = LongVariationValue.newArray();
public void add(NewDevelopmentCostCounter counter) {
this.devCosts.incrementAll(counter.devCosts);
}
public LongVariationValue.Array increment(Period period, long value) {
return devCosts.increment(period, value);
}
public LongVariationValue getValue(Period period) {
return this.devCosts.get(period);
}
}
private enum NclocEntryNclocLine implements Predicate<Map.Entry<Integer, Integer>> {
INSTANCE;
@Override
public boolean apply(@Nonnull Map.Entry<Integer, Integer> input) {
return input.getValue() == 1;
}
}
private enum MapEntryToKey implements Function<Map.Entry<Integer, Integer>, Integer> {
INSTANCE;
@Override
@Nullable
public Integer apply(@Nonnull Map.Entry<Integer, Integer> input) {
return input.getKey();
}
}
private static class NewDevelopmentCostCounterFactory extends SimpleStackElementFactory<NewDevelopmentCostCounter> {
public static final NewDevelopmentCostCounterFactory INSTANCE = new NewDevelopmentCostCounterFactory();
@Override
public NewDevelopmentCostCounter createForAny(Component component) {
return new NewDevelopmentCostCounter();
}
}
}