001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * SonarQube is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    
021    package org.sonar.batch.debt;
022    
023    import com.google.common.collect.ImmutableList;
024    import org.sonar.api.batch.Decorator;
025    import org.sonar.api.batch.DecoratorBarriers;
026    import org.sonar.api.batch.DecoratorContext;
027    import org.sonar.api.batch.DependedUpon;
028    import org.sonar.api.batch.DependsUpon;
029    import org.sonar.api.component.ResourcePerspectives;
030    import org.sonar.api.issue.Issuable;
031    import org.sonar.api.issue.Issue;
032    import org.sonar.api.measures.CoreMetrics;
033    import org.sonar.api.measures.Measure;
034    import org.sonar.api.measures.MeasureUtils;
035    import org.sonar.api.measures.Metric;
036    import org.sonar.api.resources.Project;
037    import org.sonar.api.resources.Resource;
038    import org.sonar.batch.components.Period;
039    import org.sonar.batch.components.TimeMachineConfiguration;
040    
041    import javax.annotation.Nullable;
042    
043    import java.util.Collection;
044    import java.util.Date;
045    import java.util.List;
046    
047    import static com.google.common.collect.Lists.newArrayList;
048    
049    /**
050     * Decorator that computes the technical debt metric
051     */
052    @DependsUpon(DecoratorBarriers.ISSUES_TRACKED)
053    public final class NewDebtDecorator implements Decorator {
054    
055      private final ResourcePerspectives perspectives;
056      private final TimeMachineConfiguration timeMachineConfiguration;
057      private final IssueChangelogDebtCalculator issueChangelogDebtCalculator;
058    
059      public NewDebtDecorator(ResourcePerspectives perspectives, TimeMachineConfiguration timeMachineConfiguration,
060        IssueChangelogDebtCalculator issueChangelogDebtCalculator) {
061        this.perspectives = perspectives;
062        this.timeMachineConfiguration = timeMachineConfiguration;
063        this.issueChangelogDebtCalculator = issueChangelogDebtCalculator;
064      }
065    
066      public boolean shouldExecuteOnProject(Project project) {
067        return true;
068      }
069    
070      @DependedUpon
071      public List<Metric> generatesMetrics() {
072        return ImmutableList.<Metric>of(
073          CoreMetrics.NEW_TECHNICAL_DEBT
074          );
075      }
076    
077      public void decorate(Resource resource, DecoratorContext context) {
078        Issuable issuable = perspectives.as(Issuable.class, resource);
079        if (issuable != null && shouldSaveNewMetrics(context)) {
080          List<Issue> issues = newArrayList(issuable.issues());
081          saveMeasures(context, issues);
082        }
083      }
084    
085      private void saveMeasures(DecoratorContext context, Collection<Issue> issues) {
086        Measure measure = new Measure(CoreMetrics.NEW_TECHNICAL_DEBT);
087        for (Period period : timeMachineConfiguration.periods()) {
088          Date periodDate = period.getDate();
089          double value = calculateNewTechnicalDebtValue(issues, periodDate);
090          Collection<Measure> children = context.getChildrenMeasures(measure.getMetric());
091          double sum = MeasureUtils.sumOnVariation(true, period.getIndex(), children) + value;
092          measure.setVariation(period.getIndex(), sum);
093        }
094        context.saveMeasure(measure);
095      }
096    
097      private long calculateNewTechnicalDebtValue(Collection<Issue> issues, @Nullable Date periodDate) {
098        long result = 0;
099        for (Issue issue : issues) {
100          Long debt = issueChangelogDebtCalculator.calculateNewTechnicalDebt(issue, periodDate);
101          if (debt != null) {
102            result += debt;
103          }
104        }
105        return result;
106      }
107    
108      private boolean shouldSaveNewMetrics(DecoratorContext context) {
109        return context.getMeasure(CoreMetrics.NEW_TECHNICAL_DEBT) == null;
110      }
111    
112    }