3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.issue;
22 import com.google.common.base.Function;
23 import com.google.common.base.MoreObjects;
24 import com.google.common.base.Predicate;
25 import com.google.common.collect.Lists;
26 import com.google.common.collect.Ordering;
27 import java.util.Collection;
28 import java.util.Comparator;
29 import java.util.Date;
30 import java.util.Iterator;
31 import java.util.List;
32 import javax.annotation.CheckForNull;
33 import javax.annotation.Nonnull;
34 import javax.annotation.Nullable;
35 import org.sonar.core.issue.DefaultIssue;
36 import org.sonar.core.issue.FieldDiffs;
37 import org.sonar.db.issue.IssueChangeDto;
38 import org.sonar.server.computation.task.projectanalysis.period.Period;
39 import org.sonar.server.issue.IssueFieldsSetter;
41 import static com.google.common.collect.FluentIterable.from;
42 import static org.sonar.api.utils.DateUtils.truncateToSeconds;
45 * Gets the issue debt that was introduced on a period. The algorithm
46 * is based on the issue changelog.
48 public class NewEffortCalculator {
51 * Changelog have to be sorted from newest to oldest.
52 * Null date should be the first as this happen when technical debt has changed since previous analysis.
54 private static final Comparator<FieldDiffs> CHANGE_ORDERING = Ordering.natural().reverse().nullsFirst().onResultOf((Function<FieldDiffs, Date>) dto -> dto.creationDate());
56 public long calculate(DefaultIssue issue, Collection<IssueChangeDto> debtChangelog, Period period) {
57 if (issue.creationDate().getTime() > truncateToSeconds(period.getSnapshotDate())) {
58 return MoreObjects.firstNonNull(issue.effortInMinutes(), 0L);
60 return calculateFromChangelog(issue, debtChangelog, period.getSnapshotDate());
63 private static long calculateFromChangelog(DefaultIssue issue, Collection<IssueChangeDto> debtChangelog, long periodDate) {
64 List<FieldDiffs> debtDiffs = from(debtChangelog).transform(ToFieldDiffs.INSTANCE).filter(HasDebtChange.INSTANCE).toSortedList(CHANGE_ORDERING);
65 FieldDiffs currentChange = issue.currentChange();
66 if (currentChange != null && HasDebtChange.INSTANCE.apply(currentChange)) {
67 debtDiffs = Lists.newArrayList(debtDiffs);
68 debtDiffs.add(currentChange);
70 long newDebt = issue.effortInMinutes();
72 for (Iterator<FieldDiffs> it = debtDiffs.iterator(); it.hasNext();) {
73 FieldDiffs diffs = it.next();
74 Date date = diffs.creationDate();
76 if (isBeforeOrEqual(date, new Date(periodDate))) {
77 // return new value from the change that is just before the period date
78 return subtract(newDebt, debtDiff(diffs).newValueLong());
81 // return old value from the change that is just after the period date when there's no more element in changelog
82 return subtract(newDebt, debtDiff(diffs).oldValueLong());
93 private static long subtract(long newDebt, @Nullable Long with) {
95 return Math.max(0L, newDebt - with);
100 private static boolean isBeforeOrEqual(@Nullable Date changeDate, Date periodDate) {
101 return (changeDate != null) && (truncateToSeconds(changeDate.getTime()) <= truncateToSeconds(periodDate.getTime()));
104 private static FieldDiffs.Diff debtDiff(FieldDiffs diffs) {
105 return diffs.diffs().get(IssueFieldsSetter.TECHNICAL_DEBT);
108 private enum ToFieldDiffs implements Function<IssueChangeDto, FieldDiffs> {
111 public FieldDiffs apply(@Nonnull IssueChangeDto dto) {
112 return dto.toFieldDiffs();
116 private enum HasDebtChange implements Predicate<FieldDiffs> {
119 public boolean apply(@Nonnull FieldDiffs diffs) {
120 return diffs.diffs().containsKey(IssueFieldsSetter.TECHNICAL_DEBT);