/*
* 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.model.Metrics;
import ch.hortis.sonar.model.ProjectMeasure;
import ch.hortis.sonar.model.Metric;
import ch.hortis.sonar.service.MeasureKey;
import ch.hortis.sonar.service.ProjectMeasureService;
import ch.hortis.sonar.core.TendencyAnalyser;
import java.util.*;
public class TendencyCalculator extends AbstractService {
public static final int MAX_DAYS = 3;
public Metrics[] targetTendencies;
public TendencyCalculator( Metrics... targetTendencies ) {
this.targetTendencies = targetTendencies;
}
public void execute(Module module, List<Module> directSubmodules) {
// load the measures history
if (log.isDebugEnabled()) {
log.debug("snapshot {} - starting...", module.getId());
}
long now = System.currentTimeMillis();
Date from = getStartDate(now);
Date to = getEndDate(now);
Map<MeasureKey, SortedMap<Date, ProjectMeasure>> historyByKey = getDatabaseService(ProjectMeasureService.class)
.getHistory(from, to, module.getMavenProject(), Arrays.asList( targetTendencies ) );
if (log.isDebugEnabled()) {
log.debug("snapshot {} - calculating tendencies on {} metrics", module.getId(), historyByKey.size());
}
for (MeasureKey key : module.getMeasureKeys()) {
SortedMap<Date, ProjectMeasure> measuresByDate = historyByKey.get(key);
if (measuresByDate != null) {
List<Double> values = getValuesWithoutGaps(measuresByDate, from, to);
TendencyAnalyser analyser = new TendencyAnalyser(values, MAX_DAYS);
Number[] slopeAndLevel = getSlopeAndLevel(analyser, key.getMetric());
if (slopeAndLevel[0] != null) {
module.createTendency(((MeasureKey) key.clone()), (Double) slopeAndLevel[0], (Integer) slopeAndLevel[1], MAX_DAYS);
}
}
}
log.info("snapshot {} - finished in {} ms", module.getId(), System.currentTimeMillis() - now);
}
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 Date getStartDate(long now) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(now);
cal.roll(Calendar.DATE, 1 - MAX_DAYS);
resetTime(cal);
return cal.getTime();
}
protected Date getEndDate(long now) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(now);
cal.roll(Calendar.DATE, 1);
resetTime(cal);
return cal.getTime();
}
private void resetTime(Calendar cal) {
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
}
protected List<Double> getValuesWithoutGaps(SortedMap<Date, ProjectMeasure> measuresByDate, Date from, Date to) {
List<Double> values = new ArrayList<Double>();
Calendar cal = Calendar.getInstance();
cal.setTime(from);
Calendar toCal = Calendar.getInstance();
toCal.setTime(to);
while (!cal.after(toCal)) {
ProjectMeasure measure = measuresByDate.get(cal.getTime());
if (measure == null) {
values.add(null);
} else {
values.add(measure.getValue());
}
cal.roll(Calendar.DATE, 1);
}
return values;
}
}