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.Calendar;
28 import java.util.Collection;
29 import java.util.Comparator;
30 import java.util.Date;
31 import java.util.Iterator;
32 import java.util.List;
33 import javax.annotation.CheckForNull;
34 import javax.annotation.Nonnull;
35 import javax.annotation.Nullable;
36 import org.apache.commons.lang.time.DateUtils;
37 import org.sonar.core.issue.DefaultIssue;
38 import org.sonar.core.issue.FieldDiffs;
39 import org.sonar.db.issue.IssueChangeDto;
40 import org.sonar.server.computation.task.projectanalysis.period.Period;
41 import org.sonar.server.issue.IssueFieldsSetter;
43 import static com.google.common.collect.FluentIterable.from;
46 * Gets the issue debt that was introduced on a period. The algorithm
47 * is based on the issue changelog.
49 public class NewEffortCalculator {
52 * Changelog have to be sorted from newest to oldest.
53 * Null date should be the first as this happen when technical debt has changed since previous analysis.
55 private static final Comparator<FieldDiffs> CHANGE_ORDERING = Ordering.natural().reverse().nullsFirst().onResultOf(new Function<FieldDiffs, Date>() {
57 public Date apply(@Nonnull FieldDiffs dto) {
58 return dto.creationDate();
62 public long calculate(DefaultIssue issue, Collection<IssueChangeDto> debtChangelog, Period period) {
63 if (issue.creationDate().getTime() > period.getSnapshotDate() + 1000L) {
64 return MoreObjects.firstNonNull(issue.effortInMinutes(), 0L);
66 return calculateFromChangelog(issue, debtChangelog, period.getSnapshotDate());
69 private static long calculateFromChangelog(DefaultIssue issue, Collection<IssueChangeDto> debtChangelog, long periodDate) {
70 List<FieldDiffs> debtDiffs = from(debtChangelog).transform(ToFieldDiffs.INSTANCE).filter(HasDebtChange.INSTANCE).toSortedList(CHANGE_ORDERING);
71 FieldDiffs currentChange = issue.currentChange();
72 if (currentChange != null && HasDebtChange.INSTANCE.apply(currentChange)) {
73 debtDiffs = Lists.newArrayList(debtDiffs);
74 debtDiffs.add(currentChange);
76 long newDebt = issue.effortInMinutes();
78 for (Iterator<FieldDiffs> it = debtDiffs.iterator(); it.hasNext();) {
79 FieldDiffs diffs = it.next();
80 Date date = diffs.creationDate();
82 if (isBeforeOrEqual(date, new Date(periodDate))) {
83 // return new value from the change that is just before the period date
84 return subtract(newDebt, debtDiff(diffs).newValueLong());
87 // return old value from the change that is just after the period date when there's no more element in changelog
88 return subtract(newDebt, debtDiff(diffs).oldValueLong());
99 private static long subtract(long newDebt, @Nullable Long with) {
101 return Math.max(0L, newDebt - with);
106 private static boolean isBeforeOrEqual(@Nullable Date changeDate, Date periodDate) {
107 return (changeDate != null) && (DateUtils.truncatedCompareTo(changeDate, periodDate, Calendar.SECOND) <= 0);
110 private static FieldDiffs.Diff debtDiff(FieldDiffs diffs) {
111 return diffs.diffs().get(IssueFieldsSetter.TECHNICAL_DEBT);
114 private enum ToFieldDiffs implements Function<IssueChangeDto, FieldDiffs> {
117 public FieldDiffs apply(@Nonnull IssueChangeDto dto) {
118 return dto.toFieldDiffs();
122 private enum HasDebtChange implements Predicate<FieldDiffs> {
125 public boolean apply(@Nonnull FieldDiffs diffs) {
126 return diffs.diffs().containsKey(IssueFieldsSetter.TECHNICAL_DEBT);