/*
* This program is copyright (c) 2007 Hortis-GRC SA.
*
* This file is part of Sonar.
* Sonar is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Sonar 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package ch.hortis.sonar.core.service;
import ch.hortis.sonar.core.TendencyAnalyser;
import ch.hortis.sonar.model.Metric;
import ch.hortis.sonar.model.Metrics;
import ch.hortis.sonar.model.ProjectMeasure;
import ch.hortis.sonar.service.MeasureKey;
import ch.hortis.sonar.service.ProjectMeasureService;
import org.jboss.util.collection.ReverseListIterator;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class TendencyCalculator extends AbstractService {
public static final int MAX_DAYS = 3;
public Metrics[] metrics;
public TendencyCalculator(Metrics... metrics) {
this.metrics = metrics;
}
public void execute(Module module, List<Module> directSubmodules) {
Map<MeasureKey, List<ProjectMeasure>> measuresByKey = getDatabaseService(ProjectMeasureService.class)
.getHistory(module.getMavenProject(), metrics);
for (Object o : measuresByKey.entrySet()) {
Map.Entry entry = (Map.Entry) o;
MeasureKey key = (MeasureKey) entry.getKey();
List<ProjectMeasure> measures = (List<ProjectMeasure>) entry.getValue();
List<Double> values = getValues(measures);
TendencyAnalyser analyser = new TendencyAnalyser(values, MAX_DAYS);
Number[] slopeAndLevel = getSlopeAndLevel(analyser, key.getMetric());
if (slopeAndLevel[0] != null && slopeAndLevel[1]!=null) {
module.createTendency(((MeasureKey) key.clone()), (Double) slopeAndLevel[0], (Integer) slopeAndLevel[1], MAX_DAYS);
}
}
}
protected Number[] getSlopeAndLevel(TendencyAnalyser analyser, Metric metric) {
Number[] slopeAndLevel = new Number[2];
Double slope = analyser.getSlope();
if (slope != null) {
Integer level = analyser.getLevel();
if (metric.getDirection() < 0) {
slope = -slope;
if (level != null) {
level = -level;
}
}
slopeAndLevel[0] = slope;
slopeAndLevel[1] = level;
}
return slopeAndLevel;
}
protected List<Double> getValues(List<ProjectMeasure> measures) {
List<Double> result = new ArrayList<Double>();
Calendar lastDay = null;
for (Iterator it = new ReverseListIterator(measures); it.hasNext();) {
ProjectMeasure measure = (ProjectMeasure) it.next();
Calendar day = getDay(measure.getSnapshot().getCreatedAt());
if (lastDay == null || day.before(lastDay)) {
result.add(measure.getValue());
}
lastDay = day;
}
Collections.reverse(result);
return result;
}
private Calendar getDay(Date date) {
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
return cal;
}
}