From 46124a16b74a5e20b25a87e804ecce9baf135f16 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Fri, 3 Dec 2010 17:51:58 +0000 Subject: [PATCH] SONAR-249 add the select-box to display variation periods --- .../org/sonar/plugins/core/CorePlugin.java | 3 +- .../timemachine/NewViolationsDecorator.java | 2 +- ...riationSnapshot.java => PastSnapshot.java} | 10 +-- .../core/timemachine/PastSnapshotFinder.java | 89 +++++++++++++++++++ ....java => PastSnapshotFinderByVersion.java} | 39 ++++---- .../timemachine/PastViolationsLoader.java | 9 +- .../timemachine/TimeMachineConfiguration.java | 12 +-- .../TimeMachineConfigurationPersister.java | 16 ++-- .../core/timemachine/VariationDecorator.java | 8 +- .../PastSnapshotFinderByVersionTest.java | 51 +++++++++++ ...rTest.java => PastSnapshotFinderTest.java} | 14 +-- ...TimeMachineConfigurationPersisterTest.java | 8 +- .../TimeMachineConfigurationTest.java | 6 +- .../shared.xml | 42 +++++++++ .../WEB-INF/app/helpers/dashboard_helper.rb | 37 ++++---- .../webapp/WEB-INF/app/models/snapshot.rb | 15 ++-- .../app/views/dashboard/_header.html.erb | 11 ++- 17 files changed, 287 insertions(+), 85 deletions(-) rename plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/{VariationSnapshot.java => PastSnapshot.java} (84%) create mode 100644 plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinder.java rename plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/{VariationSnapshotFinder.java => PastSnapshotFinderByVersion.java} (51%) create mode 100644 plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest.java rename plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/{VariationSnapshotFinderTest.java => PastSnapshotFinderTest.java} (79%) create mode 100644 plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest/shared.xml diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java index bb61a9004a1..59e25fcd896 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java @@ -212,8 +212,9 @@ public class CorePlugin implements Plugin { // time machine extensions.add(TendencyDecorator.class); extensions.add(PastSnapshotFinderByDays.class); + extensions.add(PastSnapshotFinderByVersion.class); extensions.add(PastMeasuresLoader.class); - extensions.add(VariationSnapshotFinder.class); + extensions.add(PastSnapshotFinder.class); extensions.add(TimeMachineConfiguration.class); extensions.add(VariationDecorator.class); extensions.add(PastViolationsLoader.class); 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 d7a27736296..7a134d83bd3 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 @@ -53,7 +53,7 @@ public class NewViolationsDecorator implements Decorator { public void decorate(Resource resource, DecoratorContext context) { Measure measure = new Measure(CoreMetrics.NEW_VIOLATIONS); - for (VariationSnapshot variationSnapshot : timeMachineConfiguration.getVariationSnapshots()) { + for (PastSnapshot variationSnapshot : timeMachineConfiguration.getVariationSnapshots()) { Date date = variationSnapshot.getDate(); double value = countViolationsAfterDate(context.getViolations(), date) + sumChildren(context, variationSnapshot.getIndex()); measure.setVariation(variationSnapshot.getIndex(), value); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationSnapshot.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshot.java similarity index 84% rename from plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationSnapshot.java rename to plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshot.java index 25fddf3f66b..3da2517f606 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationSnapshot.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshot.java @@ -23,13 +23,13 @@ import org.sonar.api.database.model.Snapshot; import java.util.Date; -public final class VariationSnapshot { +public final class PastSnapshot { private int index; private String mode, modeParameter; private Snapshot projectSnapshot; - public VariationSnapshot(int index, String mode, Snapshot projectSnapshot) { + public PastSnapshot(int index, String mode, Snapshot projectSnapshot) { this.index = index; this.mode = mode; this.projectSnapshot = projectSnapshot; @@ -47,15 +47,15 @@ public final class VariationSnapshot { return projectSnapshot.getCreatedAt(); } - public String getMode() { + public String getConfigurationMode() { return mode; } - public String getModeParameter() { + public String getConfigurationModeParameter() { return modeParameter; } - public VariationSnapshot setModeParameter(String s) { + public PastSnapshot setConfigurationModeParameter(String s) { this.modeParameter = s; return this; } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinder.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinder.java new file mode 100644 index 00000000000..11b931c3cd3 --- /dev/null +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinder.java @@ -0,0 +1,89 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * 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.plugins.core.timemachine; + +import org.apache.commons.configuration.Configuration; +import org.apache.commons.lang.StringUtils; +import org.sonar.api.BatchExtension; +import org.sonar.api.database.model.Snapshot; + +public class PastSnapshotFinder implements BatchExtension { + + private PastSnapshotFinderByDays finderByDays; + private PastSnapshotFinderByVersion finderByVersion; + + public PastSnapshotFinder(PastSnapshotFinderByDays finderByDays, PastSnapshotFinderByVersion finderByVersion) { + this.finderByDays = finderByDays; + this.finderByVersion = finderByVersion; + } + + public PastSnapshot find(Configuration conf, int index) { + return find(index, conf.getString("sonar.timemachine.variation" + index)); + } + + public PastSnapshot find(int index, String property) { + if (StringUtils.isBlank(property)) { + return null; + } + + PastSnapshot result = null; + Integer days = getValueInDays(property); + if (days != null) { + result = findSnapshotInDays(index, days); + } else { + String version = getValueVersion(property); + if (StringUtils.isNotBlank(version)) { + result = findSnapshotByVersion(index, version); + } + } + + return result; + } + + private PastSnapshot findSnapshotByVersion(int index, String version) { + Snapshot projectSnapshot = finderByVersion.findVersion(version); + if (projectSnapshot != null) { + return new PastSnapshot(index, "version", projectSnapshot).setConfigurationModeParameter(version); + } + return null; + } + + private String getValueVersion(String property) { + // todo check if it's a version with a regexp + return null; + } + + private PastSnapshot findSnapshotInDays(int index, Integer days) { + Snapshot projectSnapshot = finderByDays.findInDays(days); + if (projectSnapshot != null) { + return new PastSnapshot(index, "days", projectSnapshot).setConfigurationModeParameter(String.valueOf(days)); + } + return null; + } + + private Integer getValueInDays(String property) { + try { + return Integer.parseInt(property); + + } catch (NumberFormatException e) { + return null; + } + } +} diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationSnapshotFinder.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersion.java similarity index 51% rename from plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationSnapshotFinder.java rename to plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersion.java index e8d290f9d04..fc2ec93bf27 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationSnapshotFinder.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersion.java @@ -19,32 +19,31 @@ */ package org.sonar.plugins.core.timemachine; -import org.apache.commons.configuration.Configuration; -import org.apache.commons.lang.StringUtils; import org.sonar.api.BatchExtension; +import org.sonar.api.database.DatabaseSession; import org.sonar.api.database.model.Snapshot; -public class VariationSnapshotFinder implements BatchExtension { +import java.util.List; - private PastSnapshotFinderByDays finderInDays; +public class PastSnapshotFinderByVersion implements BatchExtension { + private Snapshot projectSnapshot; // TODO replace by PersistenceManager + private DatabaseSession session; - public VariationSnapshotFinder(PastSnapshotFinderByDays finderInDays) { - this.finderInDays = finderInDays; + public PastSnapshotFinderByVersion(Snapshot projectSnapshot, DatabaseSession session) { + this.projectSnapshot = projectSnapshot; + this.session = session; } - public VariationSnapshot find(Configuration conf, int index) { - return find(index, conf.getString("sonar.timemachine.variation" + index)); - } - - public VariationSnapshot find(int index, String property) { - if (StringUtils.isNotBlank(property)) { - // todo manage non-integer values - int days = Integer.parseInt(property); - Snapshot projectSnapshot = finderInDays.findInDays(days); - if (projectSnapshot != null) { - return new VariationSnapshot(index, "days", projectSnapshot).setModeParameter(String.valueOf(days)); - } - } - return null; + Snapshot findVersion(String version) { + String hql = "from " + Snapshot.class.getSimpleName() + " where version=:version AND resourceId=:resourceId AND status=:status order by createdAt desc"; + List snapshots = session.createQuery(hql) + .setParameter("version", version) + .setParameter("resourceId", projectSnapshot.getResourceId()) + .setParameter("status", Snapshot.STATUS_PROCESSED) + .setMaxResults(1) + .getResultList(); + + return snapshots.isEmpty() ? null : snapshots.get(0); } + } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastViolationsLoader.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastViolationsLoader.java index 30810cef199..d8b064fbee0 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastViolationsLoader.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/PastViolationsLoader.java @@ -6,6 +6,7 @@ import org.sonar.api.database.model.RuleFailureModel; import org.sonar.api.database.model.Snapshot; import org.sonar.api.database.model.SnapshotSource; import org.sonar.api.resources.Resource; +import org.sonar.api.utils.SonarException; import org.sonar.batch.index.ResourcePersister; import java.util.Collections; @@ -22,10 +23,14 @@ public class PastViolationsLoader implements BatchExtension { } public List getPastViolations(Resource resource) { - Snapshot snapshot = resourcePersister.getSnapshot(resource); - if (snapshot == null) { // TODO Godin: Prevent NPE with Natural and VB plugins + if (resource == null) { return Collections.emptyList(); } + + Snapshot snapshot = resourcePersister.getSnapshot(resource); + if (snapshot == null) { + throw new SonarException("This resource has no snapshot ???" + resource); + } Snapshot previousLastSnapshot = resourcePersister.getLastSnapshot(snapshot, true); if (previousLastSnapshot == null) { return Collections.emptyList(); 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 8e990ad2d74..1f0894fd085 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 @@ -32,17 +32,17 @@ public class TimeMachineConfiguration implements BatchExtension { private static final int NUMBER_OF_VARIATION_SNAPSHOTS = 3; private final Configuration configuration; - private List variationSnapshots; + private List variationSnapshots; - public TimeMachineConfiguration(Configuration configuration, VariationSnapshotFinder variationSnapshotFinder) { + public TimeMachineConfiguration(Configuration configuration, PastSnapshotFinder variationSnapshotFinder) { this.configuration = configuration; initVariationSnapshots(variationSnapshotFinder); } - private void initVariationSnapshots(VariationSnapshotFinder variationSnapshotFinder) { + private void initVariationSnapshots(PastSnapshotFinder variationSnapshotFinder) { variationSnapshots = Lists.newLinkedList(); for (int index = 1; index <= NUMBER_OF_VARIATION_SNAPSHOTS; index++) { - VariationSnapshot variationSnapshot = variationSnapshotFinder.find(configuration, index); + PastSnapshot variationSnapshot = variationSnapshotFinder.find(configuration, index); if (variationSnapshot != null) { variationSnapshots.add(variationSnapshot); } @@ -52,7 +52,7 @@ public class TimeMachineConfiguration implements BatchExtension { /** * for unit tests */ - TimeMachineConfiguration(Configuration configuration, List variationSnapshots) { + TimeMachineConfiguration(Configuration configuration, List variationSnapshots) { this.configuration = configuration; this.variationSnapshots = variationSnapshots; } @@ -74,7 +74,7 @@ public class TimeMachineConfiguration implements BatchExtension { return configuration.getInt(CoreProperties.CORE_TENDENCY_DEPTH_PROPERTY, CoreProperties.CORE_TENDENCY_DEPTH_DEFAULT_VALUE); } - public List getVariationSnapshots() { + public List getVariationSnapshots() { return variationSnapshots; } } diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersister.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersister.java index df731278b5e..b587d2707c5 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersister.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersister.java @@ -38,20 +38,20 @@ public final class TimeMachineConfigurationPersister implements BatchExtension { } public void start() { - List variationSnapshots = configuration.getVariationSnapshots(); - for (VariationSnapshot variationSnapshot : variationSnapshots) { + List variationSnapshots = configuration.getVariationSnapshots(); + for (PastSnapshot variationSnapshot : variationSnapshots) { switch (variationSnapshot.getIndex()) { case 1: - projectSnapshot.setVarMode1(variationSnapshot.getMode()); - projectSnapshot.setVarLabel1(variationSnapshot.getModeParameter()); + projectSnapshot.setVarMode1(variationSnapshot.getConfigurationMode()); + projectSnapshot.setVarLabel1(variationSnapshot.getConfigurationModeParameter()); break; case 2: - projectSnapshot.setVarMode2(variationSnapshot.getMode()); - projectSnapshot.setVarLabel2(variationSnapshot.getModeParameter()); + projectSnapshot.setVarMode2(variationSnapshot.getConfigurationMode()); + projectSnapshot.setVarLabel2(variationSnapshot.getConfigurationModeParameter()); break; case 3: - projectSnapshot.setVarMode3(variationSnapshot.getMode()); - projectSnapshot.setVarLabel3(variationSnapshot.getModeParameter()); + projectSnapshot.setVarMode3(variationSnapshot.getConfigurationMode()); + projectSnapshot.setVarLabel3(variationSnapshot.getConfigurationModeParameter()); break; } session.save(projectSnapshot); diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java index 423971fdf9f..03b594c36bf 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/VariationDecorator.java @@ -39,14 +39,14 @@ import java.util.Map; @DependedUpon(DecoratorBarriers.END_OF_TIME_MACHINE) public class VariationDecorator implements Decorator { - private List targets; + private List targets; private PastMeasuresLoader pastMeasuresLoader; public VariationDecorator(PastMeasuresLoader pastMeasuresLoader, TimeMachineConfiguration configuration) { this(pastMeasuresLoader, configuration.getVariationSnapshots()); } - VariationDecorator(PastMeasuresLoader pastMeasuresLoader, List targets) { + VariationDecorator(PastMeasuresLoader pastMeasuresLoader, List targets) { this.pastMeasuresLoader = pastMeasuresLoader; this.targets = targets; } @@ -67,13 +67,13 @@ public class VariationDecorator implements Decorator { public void decorate(Resource resource, DecoratorContext context) { if (shouldCalculateVariations(resource)) { - for (VariationSnapshot target : targets) { + for (PastSnapshot target : targets) { calculateVariation(resource, context, target); } } } - private void calculateVariation(Resource resource, DecoratorContext context, VariationSnapshot target) { + private void calculateVariation(Resource resource, DecoratorContext context, PastSnapshot target) { List pastMeasures = pastMeasuresLoader.getPastMeasures(resource, target.getProjectSnapshot()); compareWithPastMeasures(context, target.getIndex(), pastMeasures); } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest.java new file mode 100644 index 00000000000..8e6d40ef1ea --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest.java @@ -0,0 +1,51 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2009 SonarSource SA + * 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.plugins.core.timemachine; + +import org.junit.Test; +import org.sonar.api.database.model.Snapshot; +import org.sonar.jpa.test.AbstractDbUnitTestCase; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.Assert.assertThat; + +public class PastSnapshotFinderByVersionTest extends AbstractDbUnitTestCase { + + @Test + public void shouldFindByVersion() { + setupData("shared"); + + Snapshot currentProjectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1010); + PastSnapshotFinderByVersion finder = new PastSnapshotFinderByVersion(currentProjectSnapshot, getSession()); + + assertThat(finder.findVersion("1.1").getId(), is(1009)); + } + + @Test + public void shouldNotFindVersion() { + setupData("shared"); + + Snapshot currentProjectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1010); + PastSnapshotFinderByVersion finder = new PastSnapshotFinderByVersion(currentProjectSnapshot, getSession()); + + assertThat(finder.findVersion("1.0"), nullValue()); + } +} diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationSnapshotFinderTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderTest.java similarity index 79% rename from plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationSnapshotFinderTest.java rename to plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderTest.java index d4a01501142..535913efbbe 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/VariationSnapshotFinderTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/PastSnapshotFinderTest.java @@ -29,29 +29,29 @@ import static org.mockito.Mockito.*; import static org.hamcrest.core.Is.is; -public class VariationSnapshotFinderTest { +public class PastSnapshotFinderTest { @Test public void shouldFindByNumberOfDays() { PastSnapshotFinderByDays finderByDays = mock(PastSnapshotFinderByDays.class); when(finderByDays.findInDays(30)).thenReturn(new Snapshot()); - VariationSnapshotFinder finder = new VariationSnapshotFinder(finderByDays); - VariationSnapshot variationSnapshot = finder.find(1, "30"); + PastSnapshotFinder finder = new PastSnapshotFinder(finderByDays, null); + PastSnapshot variationSnapshot = finder.find(1, "30"); verify(finderByDays).findInDays(30); assertNotNull(variationSnapshot); assertThat(variationSnapshot.getIndex(), is(1)); - assertThat(variationSnapshot.getMode(), is("days")); - assertThat(variationSnapshot.getModeParameter(), is("30")); + assertThat(variationSnapshot.getConfigurationMode(), is("days")); + assertThat(variationSnapshot.getConfigurationModeParameter(), is("30")); } @Test public void shouldNotFindByNumberOfDays() { PastSnapshotFinderByDays finderByDays = mock(PastSnapshotFinderByDays.class); - VariationSnapshotFinder finder = new VariationSnapshotFinder(finderByDays); - VariationSnapshot variationSnapshot = finder.find(1, "30"); + PastSnapshotFinder finder = new PastSnapshotFinder(finderByDays, null); + PastSnapshot variationSnapshot = finder.find(1, "30"); verify(finderByDays).findInDays(30); assertNull(variationSnapshot); diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java index d1f21db252d..900f8c45711 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/TimeMachineConfigurationPersisterTest.java @@ -35,10 +35,10 @@ public class TimeMachineConfigurationPersisterTest extends AbstractDbUnitTestCas setupData("shared"); TimeMachineConfiguration conf = mock(TimeMachineConfiguration.class); - VariationSnapshot vs1 = new VariationSnapshot(1, "days", getSession().getSingleResult(Snapshot.class, "id", 100)) - .setModeParameter("30"); - VariationSnapshot vs3 = new VariationSnapshot(3, "version", getSession().getSingleResult(Snapshot.class, "id", 300)) - .setModeParameter("1.2.3"); + PastSnapshot vs1 = new PastSnapshot(1, "days", getSession().getSingleResult(Snapshot.class, "id", 100)) + .setConfigurationModeParameter("30"); + PastSnapshot vs3 = new PastSnapshot(3, "version", getSession().getSingleResult(Snapshot.class, "id", 300)) + .setConfigurationModeParameter("1.2.3"); when(conf.getVariationSnapshots()).thenReturn(Arrays.asList(vs1, vs3)); Snapshot projectSnapshot = getSession().getSingleResult(Snapshot.class, "id", 1000); 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 87d7d9f4235..4a3d89ba489 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 @@ -46,9 +46,9 @@ public class TimeMachineConfigurationTest extends AbstractDbUnitTestCase { @Test public void shouldInitSnapshotReferences() { PropertiesConfiguration conf = new PropertiesConfiguration(); - VariationSnapshotFinder snapshotReferenceFinder = mock(VariationSnapshotFinder.class); - when(snapshotReferenceFinder.find(conf, 1)).thenReturn(new VariationSnapshot(1, "days", null)); - when(snapshotReferenceFinder.find(conf, 3)).thenReturn(new VariationSnapshot(3, "days", null)); + 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)); TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration(conf, snapshotReferenceFinder); diff --git a/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest/shared.xml b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest/shared.xml new file mode 100644 index 00000000000..34d04755d1a --- /dev/null +++ b/plugins/sonar-core-plugin/src/test/resources/org/sonar/plugins/core/timemachine/PastSnapshotFinderByVersionTest/shared.xml @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboard_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboard_helper.rb index 6cd75f7d723..81eed039cd6 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboard_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/dashboard_helper.rb @@ -20,23 +20,6 @@ module DashboardHelper include WidgetPropertiesHelper - def active_widgets_ids_formatted(column) - active_widget_ids=[] - @dashboard.widgets.find(:all, :conditions => {:column_index => column}, :order => 'row_index ASC').each do |widget| - widget_view=nil - found_index=-1 - @widgets.each_with_index {|item, index| - if item.getId()==widget.widget_key - found_index=index - end - } - if found_index>-1 - active_widget_ids=active_widget_ids << (widget.widget_key+"_"+found_index.to_s()) - end - end - return "\'"+active_widget_ids.join("\',\'")+"\'" - end - def formatted_value(measure, default='') measure ? measure.formatted_value : default end @@ -45,4 +28,24 @@ module DashboardHelper @snapshot.measure(metric_key) end + def variation_select_option(snapshot, index) + return nil if snapshot.nil? || snapshot.project_snapshot.nil? + mode=snapshot.project_snapshot.send "var_mode_#{index}" + mode_param=snapshot.project_snapshot.send "var_label_#{index}" + + if mode + if mode=='days' + label = "Last %s days" % mode_param + elsif mode=='version' + label = "Version %s" % mode_param + end + if label + selected=(params[:var]==index.to_s ? 'selected' : '') + "" + end + else + nil + end + + end end \ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/snapshot.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/snapshot.rb index 61beff4ccf2..94daa90e107 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/snapshot.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/snapshot.rb @@ -81,12 +81,15 @@ class Snapshot < ActiveRecord::Base end def project_snapshot - if scope==Project::SCOPE_SET - self - elsif parent_snapshot_id - parent_snapshot.project - else - nil + @project_snapshot ||= + begin + if scope==Project::SCOPE_SET + self + elsif parent_snapshot_id + parent_snapshot.project_snapshot + else + nil + end end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/dashboard/_header.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/dashboard/_header.html.erb index 040d8ba867f..f9565ca5d0b 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/dashboard/_header.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/dashboard/_header.html.erb @@ -21,7 +21,16 @@ profile_measure=@snapshot.measure(Metric::PROFILE) %> <%= link_to_favourite(@project) -%> <%= "#{version_event.fullname} - " if version_event %> <%= l @snapshot.created_at %> - <% if profile_measure %> - profile <%= link_to profile_measure.data, :controller => '/rules_configuration', :action => 'index', :id => profile_measure.value.to_i %><% end %> + <% if profile_measure %> - Profile <%= link_to profile_measure.data, :controller => '/rules_configuration', :action => 'index', :id => profile_measure.value.to_i %><% end %> + - Compare to +
+ +
<% end %> -- 2.39.5