diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2011-06-21 15:12:11 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2011-06-21 15:13:48 +0200 |
commit | 02fdeba8d5cea42b17f0e24bc1d0bdebd9c0d29b (patch) | |
tree | 56b6d7bb26a1b842577c75968269ad3ae9e976ef /sonar-batch | |
parent | 09b9a9586fe2a3a82d79ea2e7c5f1cbe742c8ac5 (diff) | |
download | sonarqube-02fdeba8d5cea42b17f0e24bc1d0bdebd9c0d29b.tar.gz sonarqube-02fdeba8d5cea42b17f0e24bc1d0bdebd9c0d29b.zip |
SONAR-2505 support measure variations
Diffstat (limited to 'sonar-batch')
10 files changed, 33 insertions, 122 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java index 59d3dff04f9..ee7c07ebb3f 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/ProjectModule.java @@ -31,7 +31,6 @@ import org.sonar.api.resources.ProjectFileSystem; import org.sonar.api.rules.DefaultRulesManager; import org.sonar.api.utils.SonarException; import org.sonar.batch.*; -import org.sonar.batch.components.TimeMachineConfiguration; import org.sonar.batch.events.EventBus; import org.sonar.batch.index.DefaultIndex; import org.sonar.batch.index.DefaultResourcePersister; @@ -78,7 +77,6 @@ public class ProjectModule extends Module { if (!dryRun) { // the Snapshot component will be removed when asynchronous measures are improved (required for AsynchronousMeasureSensor) addComponent(getComponent(DefaultResourcePersister.class).getSnapshot(project)); - addComponent(TimeMachineConfiguration.class); } addComponent(org.sonar.api.database.daos.MeasuresDao.class); addComponent(ProfilesDao.class); diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java index 11683350a8b..c4dd2a56dc0 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastMeasuresLoader.java @@ -23,9 +23,11 @@ import com.google.common.collect.Maps; import org.apache.commons.lang.ObjectUtils; import org.sonar.api.BatchExtension; import org.sonar.api.database.DatabaseSession; +import org.sonar.api.database.model.ResourceModel; import org.sonar.api.database.model.Snapshot; import org.sonar.api.measures.Metric; import org.sonar.api.measures.MetricFinder; +import org.sonar.api.resources.Qualifiers; import org.sonar.api.resources.Resource; import java.util.Collection; @@ -57,28 +59,25 @@ public class PastMeasuresLoader implements BatchExtension { } public List<Object[]> getPastMeasures(Resource resource, PastSnapshot projectPastSnapshot) { - if (isPersisted(resource) && projectPastSnapshot != null && projectPastSnapshot.getProjectSnapshot()!=null) { - return getPastMeasures(resource.getId(), projectPastSnapshot.getProjectSnapshot()); + if (projectPastSnapshot != null && projectPastSnapshot.getProjectSnapshot()!=null) { + return getPastMeasures(resource.getEffectiveKey(), projectPastSnapshot.getProjectSnapshot()); } return Collections.emptyList(); } - public List<Object[]> getPastMeasures(int resourceId, Snapshot projectSnapshot) { + public List<Object[]> getPastMeasures(String resourceKey, Snapshot projectPastSnapshot) { String sql = "select m.metric_id, m.characteristic_id, m.value from project_measures m, snapshots s" + " where m.snapshot_id=s.id and m.metric_id in (:metricIds) and m.rule_id is null and m.rule_priority is null " + - " and (s.root_snapshot_id=:rootSnapshotId or s.id=:rootSnapshotId) and s.project_id=:resourceId and s.status=:status"; + " and (s.root_snapshot_id=:rootSnapshotId or s.id=:rootSnapshotId) and s.status=:status and s.project_id=(select p.id from projects p where p.kee=:resourceKey and p.qualifier<>:lib)"; return session.createNativeQuery(sql) .setParameter("metricIds", metricByIds.keySet()) - .setParameter("rootSnapshotId", ObjectUtils.defaultIfNull(projectSnapshot.getRootId(), projectSnapshot.getId())) - .setParameter("resourceId", resourceId) + .setParameter("rootSnapshotId", ObjectUtils.defaultIfNull(projectPastSnapshot.getRootId(), projectPastSnapshot.getId())) + .setParameter("resourceKey", resourceKey) + .setParameter("lib", Qualifiers.LIBRARY) .setParameter("status", Snapshot.STATUS_PROCESSED) .getResultList(); } - private boolean isPersisted(Resource resource) { - return resource.getId() != null; - } - public static int getMetricId(Object[] row) { // can be BigDecimal on Oracle return ((Number)row[0]).intValue(); diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java index 843304af53b..881559f14ed 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshot.java @@ -99,14 +99,14 @@ public class PastSnapshot { @Override public String toString() { - if (StringUtils.equals(mode, PastSnapshotFinderByVersion.MODE)) { + if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_VERSION)) { String label = String.format("Compare to version %s", modeParameter); if (getTargetDate() != null) { label += String.format(" (%s)", DateUtils.formatDate(getTargetDate())); } return label; } - if (StringUtils.equals(mode, PastSnapshotFinderByDays.MODE)) { + if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_DAYS)) { String label = String.format("Compare over %s days (%s", modeParameter, DateUtils.formatDate(getTargetDate())); if (isRelatedToSnapshot()) { label += ", analysis of " + getDate(); @@ -121,7 +121,7 @@ public class PastSnapshot { } return label; } - if (StringUtils.equals(mode, PastSnapshotFinderByDate.MODE)) { + if (StringUtils.equals(mode, CoreProperties.TIMEMACHINE_MODE_DATE)) { String label = "Compare to date " + DateUtils.formatDate(getTargetDate()); if (isRelatedToSnapshot()) { label += String.format(" (analysis of %s)", DateUtils.formatDate(getDate())); diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinder.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinder.java index dd59bb2009d..ec0ef5d2191 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinder.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinder.java @@ -69,6 +69,10 @@ public class PastSnapshotFinder implements BatchExtension { return conf.getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + index, defaultValue); } + public PastSnapshot findPreviousAnalysis(Snapshot projectSnapshot) { + return finderByPreviousAnalysis.findByPreviousAnalysis(projectSnapshot); + } + public PastSnapshot find(Snapshot projectSnapshot, int index, String property) { if (StringUtils.isBlank(property)) { return null; diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java index 4340ac6c4b3..d605b7cabf9 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDate.java @@ -20,6 +20,7 @@ package org.sonar.batch.components; import org.sonar.api.BatchExtension; +import org.sonar.api.CoreProperties; import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.Qualifiers; @@ -31,8 +32,6 @@ import java.util.List; public class PastSnapshotFinderByDate implements BatchExtension { - public static final String MODE = "date"; - private DatabaseSession session; public PastSnapshotFinderByDate(DatabaseSession session) { @@ -45,7 +44,7 @@ public class PastSnapshotFinderByDate implements BatchExtension { snapshot = findSnapshot(projectSnapshot, date); } SimpleDateFormat format = new SimpleDateFormat(DateUtils.DATE_FORMAT); - return new PastSnapshot(MODE, date, snapshot).setModeParameter(format.format(date)); + return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_DATE, date, snapshot).setModeParameter(format.format(date)); } diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDays.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDays.java index b3763107e9c..548d339e31a 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDays.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByDays.java @@ -21,6 +21,7 @@ package org.sonar.batch.components; import org.apache.commons.lang.time.DateUtils; import org.sonar.api.BatchExtension; +import org.sonar.api.CoreProperties; import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.Qualifiers; @@ -30,8 +31,6 @@ import java.util.List; public class PastSnapshotFinderByDays implements BatchExtension { - public static final String MODE = "days"; - private DatabaseSession session; public PastSnapshotFinderByDays(DatabaseSession session) { @@ -49,7 +48,7 @@ public class PastSnapshotFinderByDays implements BatchExtension { .getResultList(); Snapshot snapshot = getNearestToTarget(snapshots, targetDate); - return new PastSnapshot(MODE, targetDate, snapshot).setModeParameter(String.valueOf(days)); + return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_DAYS, targetDate, snapshot).setModeParameter(String.valueOf(days)); } static Snapshot getNearestToTarget(List<Snapshot> snapshots, Date currentDate, int distanceInDays) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByVersion.java b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByVersion.java index c4cc272c5db..c5de66950d6 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByVersion.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/PastSnapshotFinderByVersion.java @@ -20,6 +20,7 @@ package org.sonar.batch.components; import org.sonar.api.BatchExtension; +import org.sonar.api.CoreProperties; import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.Qualifiers; @@ -29,8 +30,6 @@ import java.util.List; public class PastSnapshotFinderByVersion implements BatchExtension { - public static final String MODE = "version"; - private DatabaseSession session; public PastSnapshotFinderByVersion(DatabaseSession session) { @@ -48,11 +47,11 @@ public class PastSnapshotFinderByVersion implements BatchExtension { .getResultList(); if (snapshots.isEmpty()) { - return new PastSnapshot(MODE); + return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_VERSION); } Snapshot snapshot = snapshots.get(0); Date targetDate = snapshot.getCreatedAt(); - return new PastSnapshot(MODE, targetDate, snapshot).setModeParameter(version); + return new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_VERSION, targetDate, snapshot).setModeParameter(version); } } diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java b/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java deleted file mode 100644 index ff317f2b01a..00000000000 --- a/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2011 SonarSource - * mailto:contact AT sonarsource DOT com - * - * Sonar is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with Sonar; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 - */ -package org.sonar.batch.components; - -import com.google.common.collect.Lists; -import org.apache.commons.configuration.Configuration; -import org.apache.commons.lang.StringUtils; -import org.slf4j.LoggerFactory; -import org.sonar.api.BatchExtension; -import org.sonar.api.CoreProperties; -import org.sonar.api.database.model.Snapshot; -import org.sonar.api.resources.Qualifiers; -import org.sonar.api.utils.Logs; - -import java.util.Collections; -import java.util.List; - -public class TimeMachineConfiguration implements BatchExtension { - - private static final int NUMBER_OF_VARIATION_SNAPSHOTS = 5; - - private final Configuration configuration; - private List<PastSnapshot> projectPastSnapshots; - - public TimeMachineConfiguration(Configuration configuration, PastSnapshotFinder pastSnapshotFinder, Snapshot projectSnapshot) { - this.configuration = configuration; - initPastSnapshots(pastSnapshotFinder, projectSnapshot); - } - - private void initPastSnapshots(PastSnapshotFinder pastSnapshotFinder, Snapshot projectSnapshot) { - projectPastSnapshots = Lists.newLinkedList(); - for (int index = 1; index <= NUMBER_OF_VARIATION_SNAPSHOTS; index++) { - PastSnapshot pastSnapshot = pastSnapshotFinder.find(projectSnapshot, configuration, index); - if (pastSnapshot != null) { - log(pastSnapshot); - projectPastSnapshots.add(pastSnapshot); - } - } - } - - private void log(PastSnapshot pastSnapshot) { - String qualifier = pastSnapshot.getQualifier(); - // hack to avoid too many logs when the views plugin is installed - if (StringUtils.equals(Qualifiers.VIEW, qualifier) || StringUtils.equals(Qualifiers.SUBVIEW, qualifier)) { - LoggerFactory.getLogger(getClass()).debug(pastSnapshot.toString()); - } else { - Logs.INFO.info(pastSnapshot.toString()); - } - } - - public TimeMachineConfiguration(Configuration configuration) { - this.configuration = configuration; - this.projectPastSnapshots = Collections.emptyList(); - } - - - public boolean skipTendencies() { - return configuration.getBoolean(CoreProperties.SKIP_TENDENCIES_PROPERTY, CoreProperties.SKIP_TENDENCIES_DEFAULT_VALUE); - } - - public int getTendencyPeriodInDays() { - return configuration.getInt(CoreProperties.CORE_TENDENCY_DEPTH_PROPERTY, CoreProperties.CORE_TENDENCY_DEPTH_DEFAULT_VALUE); - } - - public List<PastSnapshot> getProjectPastSnapshots() { - return projectPastSnapshots; - } -} diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastMeasuresLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastMeasuresLoaderTest.java index 74e19b28ddf..40c253e78b3 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastMeasuresLoaderTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastMeasuresLoaderTest.java @@ -20,10 +20,8 @@ package org.sonar.batch.components; import org.junit.Test; -import org.sonar.api.database.model.MeasureModel; import org.sonar.api.database.model.Snapshot; import org.sonar.api.measures.Metric; -import org.sonar.batch.components.PastMeasuresLoader; import org.sonar.jpa.test.AbstractDbUnitTestCase; import java.util.Arrays; @@ -38,8 +36,8 @@ import static org.junit.internal.matchers.IsCollectionContaining.hasItems; public class PastMeasuresLoaderTest extends AbstractDbUnitTestCase { private static final int PROJECT_SNAPSHOT_ID = 1000; - private static final int PROJECT_ID = 1; - private static final int FILE_ID = 3; + private static final String PROJECT_KEY = "project"; + private static final String FILE_KEY = "project:org.foo.Bar"; @Test public void shouldGetPastResourceMeasures() { @@ -49,7 +47,7 @@ public class PastMeasuresLoaderTest extends AbstractDbUnitTestCase { Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", PROJECT_SNAPSHOT_ID); PastMeasuresLoader loader = new PastMeasuresLoader(getSession(), metrics); - List<Object[]> measures = loader.getPastMeasures(FILE_ID, projectSnapshot); + List<Object[]> measures = loader.getPastMeasures(FILE_KEY, projectSnapshot); assertThat(measures.size(), is(2)); Object[] pastMeasure = measures.get(0); @@ -71,7 +69,7 @@ public class PastMeasuresLoaderTest extends AbstractDbUnitTestCase { Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", PROJECT_SNAPSHOT_ID); PastMeasuresLoader loader = new PastMeasuresLoader(getSession(), metrics); - List<Object[]> measures = loader.getPastMeasures(PROJECT_ID, projectSnapshot); + List<Object[]> measures = loader.getPastMeasures(PROJECT_KEY, projectSnapshot); assertThat(measures.size(), is(2)); Object[] pastMeasure = measures.get(0); diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java index 8bd305e85e2..d403bd0b7a0 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/components/PastSnapshotTest.java @@ -33,37 +33,37 @@ public class PastSnapshotTest { @Test public void testToStringForVersion() { - PastSnapshot pastSnapshot = new PastSnapshot(PastSnapshotFinderByVersion.MODE, new Date()).setModeParameter("2.3"); + PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_VERSION, new Date()).setModeParameter("2.3"); assertThat(pastSnapshot.toString(), startsWith("Compare to version 2.3")); } @Test public void testToStringForVersionWithoutDate() { - PastSnapshot pastSnapshot = new PastSnapshot(PastSnapshotFinderByVersion.MODE).setModeParameter("2.3"); + PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_VERSION).setModeParameter("2.3"); assertThat(pastSnapshot.toString(), equalTo("Compare to version 2.3")); } @Test public void testToStringForNumberOfDays() { - PastSnapshot pastSnapshot = new PastSnapshot(PastSnapshotFinderByDays.MODE, new Date()).setModeParameter("30"); + PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_DAYS, new Date()).setModeParameter("30"); assertThat(pastSnapshot.toString(), startsWith("Compare over 30 days (")); } @Test public void testToStringForNumberOfDaysWithSnapshot() { - PastSnapshot pastSnapshot = new PastSnapshot(PastSnapshotFinderByDays.MODE, new Date(), new Snapshot().setCreatedAt(new Date())).setModeParameter("30"); + PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_DAYS, new Date(), new Snapshot().setCreatedAt(new Date())).setModeParameter("30"); assertThat(pastSnapshot.toString(), startsWith("Compare over 30 days (")); } @Test public void testToStringForDate() { - PastSnapshot pastSnapshot = new PastSnapshot(PastSnapshotFinderByDate.MODE, new Date()); + PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_DATE, new Date()); assertThat(pastSnapshot.toString(), startsWith("Compare to date ")); } @Test public void testToStringForDateWithSnapshot() { - PastSnapshot pastSnapshot = new PastSnapshot(PastSnapshotFinderByDate.MODE, new Date(), new Snapshot().setCreatedAt(new Date())); + PastSnapshot pastSnapshot = new PastSnapshot(CoreProperties.TIMEMACHINE_MODE_DATE, new Date(), new Snapshot().setCreatedAt(new Date())); assertThat(pastSnapshot.toString(), startsWith("Compare to date ")); } |