]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5329 - Added Type filter to ActivityService
authorStephane Gamard <stephane.gamard@searchbox.com>
Thu, 19 Jun 2014 08:48:45 +0000 (10:48 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Thu, 19 Jun 2014 08:48:45 +0000 (10:48 +0200)
sonar-server/src/main/java/org/sonar/server/activity/index/ActivityIndex.java
sonar-server/src/main/java/org/sonar/server/activity/index/ActivityQuery.java
sonar-server/src/test/java/org/sonar/server/activity/ActivityBackendMediumTest.java
sonar-server/src/test/java/org/sonar/server/activity/ActivityServiceMediumTest.java

index 6d5f36bf2fc387687ef8d2d0aff269c8f29ebe63..1f55f196fa0cbe31b37765116061468c6bc5597a 100644 (file)
@@ -25,6 +25,8 @@ import org.elasticsearch.action.search.SearchType;
 import org.elasticsearch.common.settings.ImmutableSettings;
 import org.elasticsearch.common.settings.Settings;
 import org.elasticsearch.common.unit.TimeValue;
+import org.elasticsearch.index.query.FilterBuilders;
+import org.elasticsearch.index.query.OrFilterBuilder;
 import org.elasticsearch.index.query.QueryBuilders;
 import org.sonar.core.activity.Activity;
 import org.sonar.core.activity.db.ActivityDto;
@@ -97,8 +99,14 @@ public class ActivityIndex extends BaseIndex<Activity, ActivityDto, ActivityKey>
       .setTypes(this.getIndexType())
       .setIndices(this.getIndexName());
 
+    OrFilterBuilder filter = FilterBuilders.orFilter();
+    for (Activity.Type type : query.getTypes()) {
+      filter.add(FilterBuilders.termFilter(ActivityNormalizer.LogFields.TYPE.field(), type));
+    }
+
     // TODO implement query and filters based on LogQuery
-    esSearch.setQuery(QueryBuilders.matchAllQuery());
+    esSearch.setQuery(QueryBuilders.filteredQuery(
+      QueryBuilders.matchAllQuery(), filter));
 
     if (options.isScroll()) {
       esSearch.setSearchType(SearchType.SCAN);
index 76e88f5c46439c91daf0f71c2d61412c7d174a75..53c8749ae211d732b34a4cdf6e70c868fc563d65 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.activity.index;
 
+import com.google.common.collect.Lists;
 import org.sonar.core.activity.Activity;
 
 import java.util.Collection;
@@ -34,29 +35,33 @@ public class ActivityQuery {
   private Collection<Activity.Type> types;
 
   public ActivityQuery() {
+    types = Lists.newArrayList();
   }
 
   public Date getSince() {
     return since;
   }
 
-  public void setSince(Date since) {
+  public ActivityQuery setSince(Date since) {
     this.since = since;
+    return this;
   }
 
   public Date getTo() {
     return to;
   }
 
-  public void setTo(Date to) {
+  public ActivityQuery setTo(Date to) {
     this.to = to;
+    return this;
   }
 
   public Collection<Activity.Type> getTypes() {
     return types;
   }
 
-  public void setTypes(Collection<Activity.Type> types) {
+  public ActivityQuery setTypes(Collection<Activity.Type> types) {
     this.types = types;
+    return this;
   }
 }
index 69c6cd55b0c42a2a0febeab4feaf39e72755d73e..960a9b53e5223cffd38f3bea839e499c45302507 100644 (file)
@@ -121,6 +121,5 @@ public class ActivityBackendMediumTest {
       count++;
     }
     assertThat(count).isEqualTo(max);
-
   }
 }
index ab18c56563e888fcb95261f31309e551cae2c24f..c765cdc103f8bacbc124566024877bab9e8cd8f5 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.activity;
 
 import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.ImmutableSet;
 import org.elasticsearch.common.collect.Iterables;
 import org.junit.After;
 import org.junit.Before;
@@ -30,6 +31,7 @@ import org.sonar.core.activity.ActivityLog;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.server.activity.db.ActivityDao;
 import org.sonar.server.activity.index.ActivityIndex;
+import org.sonar.server.activity.index.ActivityQuery;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.search.QueryOptions;
 import org.sonar.server.search.Result;
@@ -65,7 +67,7 @@ public class ActivityServiceMediumTest {
 
   @Test
   public void find_all() throws InterruptedException {
-    final String testValue = "hello world";
+
     service.write(dbSession, Activity.Type.ACTIVE_RULE, testValue);
     dbSession.commit();
     assertThat(index.findAll().getTotal()).isEqualTo(1);
@@ -77,7 +79,6 @@ public class ActivityServiceMediumTest {
 
   @Test
   public void search_message_log() throws InterruptedException {
-    final String testValue = "hello world";
     service.write(dbSession, Activity.Type.ACTIVE_RULE, testValue);
     dbSession.commit();
     assertThat(index.findAll().getTotal()).isEqualTo(1);
@@ -87,23 +88,38 @@ public class ActivityServiceMediumTest {
     assertThat(result.getHits().get(0).message()).isEqualTo(testValue);
   }
 
+
   @Test
   public void search_activity_log() throws InterruptedException {
-    final String value = "world";
-    final String key = "hello";
-    ActivityLog activity = new ActivityLog() {
-      @Override
-      public Map<String, String> getDetails() {
-        return ImmutableMap.of(key, value);
-      }
-    };
-    service.write(dbSession, Activity.Type.ACTIVE_RULE, activity);
+
+    service.write(dbSession, Activity.Type.ACTIVE_RULE, getActivity());
     dbSession.commit();
     assertThat(index.findAll().getTotal()).isEqualTo(1);
 
     Result<Activity> result = index.search(service.newActivityQuery(), new QueryOptions());
     assertThat(result.getTotal()).isEqualTo(1L);
-    assertThat(result.getHits().get(0).details().get(key)).isEqualTo(value);
+    assertThat(result.getHits().get(0).details().get(test_key)).isEqualTo(test_value);
+  }
+
+
+  @Test
+  public void search_by_type() {
+    service.write(dbSession, Activity.Type.NONE, getActivity());
+    service.write(dbSession, Activity.Type.SERVER, getActivity());
+    service.write(dbSession, Activity.Type.SERVER, testValue);
+    service.write(dbSession, Activity.Type.ACTIVE_RULE, getActivity());
+    dbSession.commit();
+
+    assertThat(service.search(new ActivityQuery(),
+      new QueryOptions()).getHits()).hasSize(4);
+
+    assertThat(service.search(new ActivityQuery()
+        .setTypes(ImmutableSet.of(Activity.Type.SERVER)),
+      new QueryOptions()).getHits()).hasSize(2);
+
+    assertThat(service.search(new ActivityQuery()
+        .setTypes(ImmutableSet.of(Activity.Type.ACTIVE_RULE)),
+      new QueryOptions()).getHits()).hasSize(1);
   }
 
   @Test
@@ -129,4 +145,18 @@ public class ActivityServiceMediumTest {
     }
     assertThat(count).isEqualTo(max);
   }
+
+
+  final String test_key = "hello";
+  final String test_value = "world";
+  final String testValue = "hello world";
+
+  private ActivityLog getActivity() {
+    return new ActivityLog() {
+      @Override
+      public Map<String, String> getDetails() {
+        return ImmutableMap.of(test_key, test_value);
+      }
+    };
+  }
 }