From 01e310b419f1ed34ecda6bbe45674ca8e0e2a422 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Tue, 7 Dec 2010 16:58:58 +0000 Subject: SONAR-1450 log comparison dates at batch startup + improve the algorithm of periods in number of days --- .../core/timemachine/NewViolationsDecorator.java | 2 +- .../core/timemachine/PastSnapshotFinderByDays.java | 37 +++-------- .../core/timemachine/TimeMachineConfiguration.java | 2 + .../timemachine/PastSnapshotFinderByDaysTest.java | 69 +++++--------------- .../timemachine/TimeMachineConfigurationTest.java | 16 ++++- .../PastSnapshotFinderByDaysTest/shared.xml | 76 ++++++++++++++++++++++ .../shouldLoadSnapshotsFromDatabase.xml | 76 ---------------------- 7 files changed, 117 insertions(+), 161 deletions(-) create mode 100644 plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest/shared.xml delete mode 100644 plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest/shouldLoadSnapshotsFromDatabase.xml (limited to 'plugins') diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java index 717f3b6734f..a51d5640247 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java @@ -105,8 +105,8 @@ public class NewViolationsDecorator implements Decorator { Measure measure = new Measure(metric); for (PastSnapshot variationSnapshot : timeMachineConfiguration.getProjectPastSnapshots()) { int variationIndex = variationSnapshot.getIndex(); - Collection children = context.getChildrenMeasures(MeasuresFilters.metric(metric)); int count = countViolations(violationsBySeverity.get(priority), variationSnapshot.getDate()); + Collection children = context.getChildrenMeasures(MeasuresFilters.metric(metric)); double sum = sumChildren(variationIndex, children) + count; measure.setVariation(variationIndex, sum); } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDays.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDays.java index b3235bb8ae3..9a49b236402 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDays.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDays.java @@ -27,7 +27,7 @@ import org.sonar.api.database.model.Snapshot; import java.util.Date; import java.util.List; -public class PastSnapshotFinderByDays implements BatchExtension{ +public class PastSnapshotFinderByDays implements BatchExtension { private Snapshot projectSnapshot; // TODO replace by PersistenceManager private DatabaseSession session; @@ -37,37 +37,16 @@ public class PastSnapshotFinderByDays implements BatchExtension{ } Snapshot findInDays(int days) { - List snapshots = loadSnapshotsFromDatabase(); - return getNearestToTarget(snapshots, projectSnapshot.getCreatedAt(), days); - } - - List loadSnapshotsFromDatabase() { - String hql = "from " + Snapshot.class.getSimpleName() + " where resourceId=:resourceId AND status=:status order by createdAt"; - return session.createQuery(hql) + Date targetDate = DateUtils.addDays(projectSnapshot.getCreatedAt(), -days); + String hql = "from " + Snapshot.class.getSimpleName() + " where resourceId=:resourceId AND status=:status AND createdAt>=:from AND createdAt<:to order by createdAt asc"; + List snapshots = session.createQuery(hql) + .setParameter("from", targetDate) + .setParameter("to", projectSnapshot.getCreatedAt()) .setParameter("resourceId", projectSnapshot.getResourceId()) .setParameter("status", Snapshot.STATUS_PROCESSED) + .setMaxResults(1) .getResultList(); - } - - static Snapshot getNearestToTarget(List snapshots, Date currentDate, int distanceInDays) { - Date targetDate = DateUtils.addDays(currentDate, -distanceInDays); - return getNearestToTarget(snapshots, targetDate); - } - - static Snapshot getNearestToTarget(List snapshots, Date targetDate) { - long bestDistance = Long.MAX_VALUE; - Snapshot nearest = null; - for (Snapshot snapshot : snapshots) { - long distance = distance(snapshot.getCreatedAt(), targetDate); - if (distance <= bestDistance) { - bestDistance = distance; - nearest = snapshot; - } - } - return nearest; - } - static long distance(Date d1, Date d2) { - return Math.abs(d1.getTime() - d2.getTime()); + return snapshots.isEmpty() ? null : snapshots.get(0); } } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfiguration.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfiguration.java index a3fe6efd7c8..1658381f0d4 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfiguration.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfiguration.java @@ -23,6 +23,7 @@ import com.google.common.collect.Lists; import org.apache.commons.configuration.Configuration; import org.sonar.api.BatchExtension; import org.sonar.api.CoreProperties; +import org.sonar.api.utils.Logs; import java.util.Collections; import java.util.List; @@ -44,6 +45,7 @@ public class TimeMachineConfiguration implements BatchExtension { for (int index = 1; index <= NUMBER_OF_VARIATION_SNAPSHOTS; index++) { PastSnapshot variationSnapshot = variationSnapshotFinder.find(configuration, index); if (variationSnapshot != null) { + Logs.INFO.info("Comparison date: " + variationSnapshot.getDate()); projectPastSnapshots.add(variationSnapshot); } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest.java index d4f70c535ae..a09efeda4dc 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest.java @@ -19,81 +19,46 @@ */ package org.sonar.plugins.core.timemachine; -import com.google.common.collect.Lists; +import org.hamcrest.core.IsNull; import org.junit.Test; import org.sonar.api.database.model.Snapshot; import org.sonar.jpa.test.AbstractDbUnitTestCase; import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.Collections; -import java.util.Date; -import java.util.List; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.core.IsNull.nullValue; import static org.junit.Assert.assertThat; -import static org.junit.internal.matchers.IsCollectionContaining.hasItems; public class PastSnapshotFinderByDaysTest extends AbstractDbUnitTestCase { - private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); - @Test - public void shouldLoadSnapshotsFromDatabase() { - setupData("shouldLoadSnapshotsFromDatabase"); + public void shouldGetNextSnapshot() throws ParseException { + setupData("shared"); - Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1009); + Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1009); // 2008-11-16 PastSnapshotFinderByDays finder = new PastSnapshotFinderByDays(projectSnapshot, getSession()); - List snapshots = finder.loadSnapshotsFromDatabase(); - List snapshotIds = Lists.newLinkedList(); - for (Snapshot snapshot : snapshots) { - snapshotIds.add(snapshot.getId()); - } - assertThat(snapshotIds.size(), is(2)); - assertThat(snapshotIds, hasItems(1000, 1003)); // project snapshots + + assertThat(finder.findInDays(50).getId(), is(1000)); } @Test - public void shouldGetNearestSnapshotBefore() throws ParseException { - Date current = dateFormat.parse("2010-10-20"); - // distance: 15 => target is 2010-10-05 + public void shouldIgnoreUnprocessedSnapshots() throws ParseException { + setupData("shared"); + + Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1009); // 2008-11-16 + PastSnapshotFinderByDays finder = new PastSnapshotFinderByDays(projectSnapshot, getSession()); - List snapshots = Arrays.asList( - newSnapshot(1, "2010-09-30"), - newSnapshot(2, "2010-10-03"),// -2 days - newSnapshot(3, "2010-10-08"),// +3 days - newSnapshot(4, "2010-10-12") // + 7 days - ); - assertThat(PastSnapshotFinderByDays.getNearestToTarget(snapshots, current, 15).getId(), is(2)); + assertThat(finder.findInDays(7).getId(), is(1006)); } @Test - public void shouldgetNearestSnapshotAfter() throws ParseException { - Date current = dateFormat.parse("2010-10-20"); - // distance: 15 => target is 2010-10-05 + public void shouldNotFindSelf() throws ParseException { + setupData("shared"); - List snapshots = Arrays.asList( - newSnapshot(1, "2010-09-30"), - newSnapshot(2, "2010-10-01"),// -4 days - newSnapshot(3, "2010-10-08"),// +3 days - newSnapshot(4, "2010-10-12") // + 7 days - ); - assertThat(PastSnapshotFinderByDays.getNearestToTarget(snapshots, current, 15).getId(), is(3)); - } + Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1009); // 2008-11-16 + PastSnapshotFinderByDays finder = new PastSnapshotFinderByDays(projectSnapshot, getSession()); - @Test - public void shouldReturnNullIfNoSnapshots() throws ParseException { - Date current = dateFormat.parse("2010-10-20"); - List snapshots = Collections.emptyList(); - assertThat(PastSnapshotFinderByDays.getNearestToTarget(snapshots, current, 15), nullValue()); + assertThat(finder.findInDays(1), IsNull.nullValue()); } - private Snapshot newSnapshot(int id, String date) throws ParseException { - Snapshot snapshot = new Snapshot(); - snapshot.setId(id); - snapshot.setCreatedAt(dateFormat.parse(date)); - return snapshot; - } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest.java index 67b8f7ea1b0..2070bc6b0d3 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationTest.java @@ -22,8 +22,13 @@ package org.sonar.plugins.core.timemachine; import org.apache.commons.configuration.PropertiesConfiguration; import org.junit.Test; import org.sonar.api.CoreProperties; +import org.sonar.api.database.model.Snapshot; import org.sonar.jpa.test.AbstractDbUnitTestCase; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.mockito.Mockito.*; @@ -44,11 +49,11 @@ public class TimeMachineConfigurationTest extends AbstractDbUnitTestCase { } @Test - public void shouldInitSnapshotReferences() { + public void shouldInitVariationSnapshots() throws ParseException { PropertiesConfiguration conf = new PropertiesConfiguration(); PastSnapshotFinder snapshotReferenceFinder = mock(PastSnapshotFinder.class); - when(snapshotReferenceFinder.find(conf, 1)).thenReturn(new PastSnapshot(1, "days", null)); - when(snapshotReferenceFinder.find(conf, 3)).thenReturn(new PastSnapshot(3, "days", null)); + when(snapshotReferenceFinder.find(conf, 1)).thenReturn(new PastSnapshot(1, "days", newSnapshot("2010-10-15"))); + when(snapshotReferenceFinder.find(conf, 3)).thenReturn(new PastSnapshot(3, "days", newSnapshot("2010-10-13"))); TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration(conf, snapshotReferenceFinder); @@ -59,5 +64,10 @@ public class TimeMachineConfigurationTest extends AbstractDbUnitTestCase { assertThat(timeMachineConfiguration.getProjectPastSnapshots().size(), is(2)); } + private Snapshot newSnapshot(String date) throws ParseException { + Date createdAt = new SimpleDateFormat("yyyy-MM-dd").parse(date); + return new Snapshot().setCreatedAt(createdAt); + } + } diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest/shared.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest/shared.xml new file mode 100644 index 00000000000..d419e635011 --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest/shared.xml @@ -0,0 +1,76 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest/shouldLoadSnapshotsFromDatabase.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest/shouldLoadSnapshotsFromDatabase.xml deleted file mode 100644 index 4429d7c65c4..00000000000 --- a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByDaysTest/shouldLoadSnapshotsFromDatabase.xml +++ /dev/null @@ -1,76 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file -- cgit v1.2.3