diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-11-21 13:37:45 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-11-21 13:49:22 +0100 |
commit | 2f2699e356f1a653148d552c56a730c2af0025fe (patch) | |
tree | ee9f98c7a4fb4c6da204fe91718ce526fa9e91a6 /sonar-core | |
parent | aaa127f45f9e6d499ee9b32b400fdfb046e0a0d7 (diff) | |
download | sonarqube-2f2699e356f1a653148d552c56a730c2af0025fe.tar.gz sonarqube-2f2699e356f1a653148d552c56a730c2af0025fe.zip |
SONAR-3825 new page for measure filters
Diffstat (limited to 'sonar-core')
4 files changed, 106 insertions, 26 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilter.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilter.java index 9b9c124aa13..42a4f3e0f0a 100644 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilter.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilter.java @@ -20,20 +20,26 @@ package org.sonar.core.measure; import com.google.common.collect.Lists; +import org.apache.commons.lang.StringUtils; import org.sonar.api.measures.Metric; +import org.sonar.api.utils.DateUtils; import javax.annotation.Nullable; -import java.util.Collections; + +import java.util.Calendar; import java.util.Date; import java.util.List; +import java.util.Map; public class MeasureFilter { + private static final String[] EMPTY = {}; + // conditions on resources private String baseResourceKey; private boolean onBaseResourceChildren = false; // only if getBaseResourceKey is set - private List<String> resourceScopes = Lists.newArrayList(); - private List<String> resourceQualifiers = Lists.newArrayList(); - private List<String> resourceLanguages = Lists.newArrayList(); + private String[] resourceScopes = EMPTY; + private String[] resourceQualifiers = EMPTY; + private String[] resourceLanguages = EMPTY; private String resourceKeyRegexp; private String resourceName; private Date fromDate = null, toDate = null; @@ -63,28 +69,33 @@ public class MeasureFilter { return onBaseResourceChildren; } - public MeasureFilter setResourceScopes(@Nullable List<String> l) { - this.resourceScopes = (l != null ? l : Collections.<String>emptyList()); + public MeasureFilter setResourceScopes(String... l) { + this.resourceScopes = (l != null ? l : EMPTY); return this; } - public MeasureFilter setResourceQualifiers(List<String> l) { - this.resourceQualifiers = (l != null ? l : Collections.<String>emptyList()); + public MeasureFilter setResourceQualifiers(String... l) { + this.resourceQualifiers = l; return this; } - public MeasureFilter setResourceQualifiers(String... l) { - this.resourceQualifiers = Lists.newArrayList(l); + public MeasureFilter setResourceLanguages(String... l) { + this.resourceLanguages = (l != null ? l : EMPTY); return this; } - public MeasureFilter setResourceLanguages(List<String> l) { - this.resourceLanguages = (l != null ? l : Collections.<String>emptyList()); + public MeasureFilter setResourceScopes(@Nullable List<String> l) { + this.resourceScopes = (l != null ? l.toArray(new String[l.size()]) : EMPTY); return this; } - public MeasureFilter setResourceLanguages(String... l) { - this.resourceLanguages = Lists.newArrayList(l); + public MeasureFilter setResourceQualifiers(@Nullable List<String> l) { + this.resourceQualifiers = (l != null ? l.toArray(new String[l.size()]) : EMPTY); + return this; + } + + public MeasureFilter setResourceLanguages(@Nullable List<String> l) { + this.resourceLanguages = (l != null ? l.toArray(new String[l.size()]) : EMPTY); return this; } @@ -159,15 +170,15 @@ public class MeasureFilter { return toDate; } - public List<String> getResourceScopes() { + public String[] getResourceScopes() { return resourceScopes; } - public List<String> getResourceQualifiers() { + public String[] getResourceQualifiers() { return resourceQualifiers; } - public List<String> getResourceLanguages() { + public String[] getResourceLanguages() { return resourceLanguages; } @@ -179,4 +190,56 @@ public class MeasureFilter { return sort; } + public static MeasureFilter create(Map<String, String> properties) { + MeasureFilter filter = new MeasureFilter(); + filter.setBaseResourceKey(properties.get("base")); + filter.setResourceScopes(toArray(properties.get("scopes"))); + filter.setResourceQualifiers(toArray(properties.get("qualifiers"))); + filter.setResourceLanguages(toArray(properties.get("languages"))); + if (properties.containsKey("onBaseChildren")) { + filter.setOnBaseResourceChildren(Boolean.valueOf(properties.get("onBaseChildren"))); + } + filter.setResourceName(properties.get("nameRegexp")); + filter.setResourceKeyRegexp(properties.get("keyRegexp")); + if (properties.containsKey("fromDate")) { + filter.setFromDate(toDate(properties.get("fromDate"))); + } else if (properties.containsKey("afterDays")) { + filter.setFromDate(toDays(properties.get("afterDays"))); + } + if (properties.containsKey("toDate")) { + filter.setToDate(toDate(properties.get("toDate"))); + } else if (properties.containsKey("beforeDays")) { + filter.setToDate(toDays(properties.get("beforeDays"))); + } + + if (properties.containsKey("favourites")) { + filter.setUserFavourites(Boolean.valueOf(properties.get("favourites"))); + } + return filter; + } + + private static String[] toArray(@Nullable String s) { + if (s == null) { + return EMPTY; + } + return StringUtils.split(s, ","); + } + + private static Date toDate(@Nullable String date) { + if (date != null) { + return DateUtils.parseDate(date); + } + return null; + } + + private static Date toDays(@Nullable String s) { + if (s != null) { + int days = Integer.valueOf(s); + Date date = org.apache.commons.lang.time.DateUtils.truncate(new Date(), Calendar.DATE); + date = org.apache.commons.lang.time.DateUtils.addDays(date, -days); + return date; + } + return null; + } + } diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterEngine.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterEngine.java index 253c2875d6f..fcebe106ea9 100644 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterEngine.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterEngine.java @@ -27,7 +27,9 @@ import org.slf4j.LoggerFactory; import org.sonar.api.ServerComponent; import javax.annotation.Nullable; + import java.util.List; +import java.util.Map; public class MeasureFilterEngine implements ServerComponent { private static final Logger FILTER_LOG = LoggerFactory.getLogger("org.sonar.MEASURE_FILTER"); @@ -44,6 +46,22 @@ public class MeasureFilterEngine implements ServerComponent { return execute(filterJson, userId, FILTER_LOG); } + public List<MeasureFilterRow> execute2(Map<String, String> filterMap, @Nullable Long userId) throws ParseException { + Logger logger = FILTER_LOG; + MeasureFilterContext context = new MeasureFilterContext(); + context.setJson(filterMap.toString()); + context.setUserId(userId); + try { + long start = System.currentTimeMillis(); + MeasureFilter filter = MeasureFilter.create(filterMap); + List<MeasureFilterRow> rows = executor.execute(filter, context); + log(context, rows, (System.currentTimeMillis() - start), logger); + return rows; + } catch (Exception e) { + throw new IllegalStateException("Fail to execute filter: " + context, e); + } + } + @VisibleForTesting List<MeasureFilterRow> execute(String filterJson, @Nullable Long userId, Logger logger) { MeasureFilterContext context = new MeasureFilterContext(); diff --git a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java index c95717e2c2a..bb41a9c77ed 100644 --- a/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java +++ b/sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java @@ -37,7 +37,6 @@ import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; -import java.util.Collection; import java.util.List; class MeasureFilterSql { @@ -150,15 +149,15 @@ class MeasureFilterSql { if (context.getBaseSnapshot() == null) { sql.append(" AND p.copy_resource_id IS NULL "); } - if (!filter.getResourceQualifiers().isEmpty()) { + if (filter.getResourceQualifiers().length > 0) { sql.append(" AND s.qualifier IN "); appendInStatement(filter.getResourceQualifiers(), sql); } - if (!filter.getResourceScopes().isEmpty()) { + if (filter.getResourceScopes().length > 0) { sql.append(" AND s.scope IN "); appendInStatement(filter.getResourceScopes(), sql); } - if (!filter.getResourceLanguages().isEmpty()) { + if (filter.getResourceLanguages().length > 0) { sql.append(" AND p.language IN "); appendInStatement(filter.getResourceLanguages(), sql); } @@ -217,7 +216,7 @@ class MeasureFilterSql { sql.append(" AND s.project_id IN (SELECT rindex.resource_id FROM resource_index rindex WHERE rindex.kee like '"); sql.append(StringEscapeUtils.escapeSql(StringUtils.lowerCase(filter.getResourceName()))); sql.append("%'"); - if (!filter.getResourceQualifiers().isEmpty()) { + if (filter.getResourceQualifiers().length > 0) { sql.append(" AND rindex.qualifier IN "); appendInStatement(filter.getResourceQualifiers(), sql); } @@ -259,7 +258,7 @@ class MeasureFilterSql { } - private static void appendInStatement(Collection<String> values, StringBuilder to) { + private static void appendInStatement(String[] values, StringBuilder to) { to.append(" ('"); to.append(StringUtils.join(values, "','")); to.append("') "); diff --git a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterDecoderTest.java b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterDecoderTest.java index 3dedb2d09a1..ce7ed550464 100644 --- a/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterDecoderTest.java +++ b/sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterDecoderTest.java @@ -62,9 +62,9 @@ public class MeasureFilterDecoderTest { assertThat(filter.getBaseResourceKey()).isEqualTo("org.struts"); assertThat(filter.isOnBaseResourceChildren()).isTrue(); - assertThat(filter.getResourceScopes()).containsExactly("PRJ"); - assertThat(filter.getResourceQualifiers()).containsExactly("TRK", "CLA"); - assertThat(filter.getResourceLanguages()).containsExactly("java", "php"); + assertThat(filter.getResourceScopes()).containsOnly("PRJ"); + assertThat(filter.getResourceQualifiers()).containsOnly("TRK", "CLA"); + assertThat(filter.getResourceLanguages()).containsOnly("java", "php"); assertThat(filter.getResourceName()).isEqualTo("Struts"); assertThat(filter.getResourceKeyRegexp()).isEqualTo("*foo*"); assertThat(filter.getFromDate().getYear()).isEqualTo(2012 - 1900); |