You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NewDebtCalculator.java 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.issue;
  21. import com.google.common.base.Function;
  22. import com.google.common.base.Objects;
  23. import com.google.common.base.Predicate;
  24. import com.google.common.collect.Lists;
  25. import com.google.common.collect.Ordering;
  26. import java.util.Calendar;
  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.apache.commons.lang.time.DateUtils;
  36. import org.sonar.core.issue.DefaultIssue;
  37. import org.sonar.core.issue.FieldDiffs;
  38. import org.sonar.core.issue.IssueUpdater;
  39. import org.sonar.db.issue.IssueChangeDto;
  40. import org.sonar.server.computation.period.Period;
  41. import static com.google.common.collect.FluentIterable.from;
  42. /**
  43. * Gets the issue debt that was introduced on a period. The algorithm
  44. * is based on the issue changelog.
  45. */
  46. public class NewDebtCalculator {
  47. public long calculate(DefaultIssue issue, Collection<IssueChangeDto> debtChangelog, Period period) {
  48. if (issue.creationDate().getTime() > period.getSnapshotDate() + 1000L) {
  49. return Objects.firstNonNull(issue.debtInMinutes(), 0L);
  50. }
  51. return calculateFromChangelog(issue, debtChangelog, period.getSnapshotDate());
  52. }
  53. private long calculateFromChangelog(DefaultIssue issue, Collection<IssueChangeDto> debtChangelog, long periodDate) {
  54. List<FieldDiffs> debtDiffs = from(debtChangelog).transform(ToFieldDiffs.INSTANCE).filter(HasDebtChange.INSTANCE).toSortedList(CHANGE_ORDERING);
  55. FieldDiffs currentChange = issue.currentChange();
  56. if (currentChange != null && HasDebtChange.INSTANCE.apply(currentChange)) {
  57. debtDiffs = Lists.newArrayList(debtDiffs);
  58. debtDiffs.add(currentChange);
  59. }
  60. long newDebt = issue.debtInMinutes();
  61. for (Iterator<FieldDiffs> it = debtDiffs.iterator(); it.hasNext();) {
  62. FieldDiffs diffs = it.next();
  63. Date date = diffs.creationDate();
  64. // TODO use longs
  65. if (isBeforeOrEqual(date, new Date(periodDate))) {
  66. // return new value from the change that is just before the period date
  67. return subtract(newDebt, debtDiff(diffs).newValueLong());
  68. }
  69. if (!it.hasNext()) {
  70. // return old value from the change that is just after the period date when there's no more element in changelog
  71. return subtract(newDebt, debtDiff(diffs).oldValueLong());
  72. }
  73. }
  74. // no changelog
  75. return 0L;
  76. }
  77. /**
  78. * SONAR-5059
  79. */
  80. @CheckForNull
  81. private static long subtract(long newDebt, @Nullable Long with) {
  82. if (with != null) {
  83. return Math.max(0L, newDebt - with);
  84. }
  85. return newDebt;
  86. }
  87. private static boolean isBeforeOrEqual(@Nullable Date changeDate, Date periodDate) {
  88. return (changeDate != null) && (DateUtils.truncatedCompareTo(changeDate, periodDate, Calendar.SECOND) <= 0);
  89. }
  90. private static FieldDiffs.Diff debtDiff(FieldDiffs diffs) {
  91. return diffs.diffs().get(IssueUpdater.TECHNICAL_DEBT);
  92. }
  93. /**
  94. * Changelog have to be sorted from newest to oldest.
  95. * Null date should be the first as this happen when technical debt has changed since previous analysis.
  96. */
  97. private static final Comparator<FieldDiffs> CHANGE_ORDERING = Ordering.natural().reverse().nullsFirst().onResultOf(new Function<FieldDiffs, Date>() {
  98. @Override
  99. public Date apply(@Nonnull FieldDiffs dto) {
  100. return dto.creationDate();
  101. }
  102. });
  103. private enum ToFieldDiffs implements Function<IssueChangeDto, FieldDiffs> {
  104. INSTANCE;
  105. @Override
  106. public FieldDiffs apply(@Nonnull IssueChangeDto dto) {
  107. return dto.toFieldDiffs();
  108. }
  109. }
  110. private enum HasDebtChange implements Predicate<FieldDiffs> {
  111. INSTANCE;
  112. @Override
  113. public boolean apply(@Nonnull FieldDiffs diffs) {
  114. return diffs.diffs().containsKey(IssueUpdater.TECHNICAL_DEBT);
  115. }
  116. }
  117. }