Search in sources :

Example 1 with TimeRange

use of org.graylog2.plugin.indexer.searches.timeranges.TimeRange in project graylog2-server by Graylog2.

the class SearchesTest method determineAffectedIndicesWithRangesDoesNotIncludesDeflectorTargetIfMissing.

@Test
public void determineAffectedIndicesWithRangesDoesNotIncludesDeflectorTargetIfMissing() throws Exception {
    final DateTime now = DateTime.now(DateTimeZone.UTC);
    final MongoIndexRange indexRange0 = MongoIndexRange.create("graylog_0", now, now.plusDays(1), now, 0);
    final MongoIndexRange indexRange1 = MongoIndexRange.create("graylog_1", now.plusDays(1), now.plusDays(2), now, 0);
    final SortedSet<IndexRange> indices = ImmutableSortedSet.orderedBy(IndexRange.COMPARATOR).add(indexRange0).add(indexRange1).build();
    when(indexRangeService.find(any(DateTime.class), any(DateTime.class))).thenReturn(indices);
    final TimeRange absoluteRange = AbsoluteRange.create(now.minusDays(1), now.plusDays(1));
    final TimeRange keywordRange = KeywordRange.create("1 day ago");
    final TimeRange relativeRange = RelativeRange.create(3600);
    assertThat(searches.determineAffectedIndicesWithRanges(absoluteRange, null)).containsExactly(indexRange0, indexRange1);
    assertThat(searches.determineAffectedIndicesWithRanges(keywordRange, null)).containsExactly(indexRange0, indexRange1);
    assertThat(searches.determineAffectedIndicesWithRanges(relativeRange, null)).containsExactly(indexRange0, indexRange1);
}
Also used : MongoIndexRange(org.graylog2.indexer.ranges.MongoIndexRange) IndexRange(org.graylog2.indexer.ranges.IndexRange) TimeRange(org.graylog2.plugin.indexer.searches.timeranges.TimeRange) MongoIndexRange(org.graylog2.indexer.ranges.MongoIndexRange) ZonedDateTime(java.time.ZonedDateTime) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 2 with TimeRange

use of org.graylog2.plugin.indexer.searches.timeranges.TimeRange in project graylog2-server by Graylog2.

the class SearchesTest method determineAffectedIndicesFilterIndexPrefix.

@Test
public void determineAffectedIndicesFilterIndexPrefix() throws Exception {
    final DateTime now = DateTime.now(DateTimeZone.UTC);
    final MongoIndexRange indexRange0 = MongoIndexRange.create("graylog_0", now, now.plusDays(1), now, 0);
    final MongoIndexRange indexRange1 = MongoIndexRange.create("graylog_1", now.plusDays(1), now.plusDays(2), now, 0);
    final MongoIndexRange b0 = MongoIndexRange.create("b_0", now.plusDays(1), now.plusDays(2), now, 0);
    final MongoIndexRange b1 = MongoIndexRange.create("b_1", now.plusDays(1), now.plusDays(2), now, 0);
    final SortedSet<IndexRange> indices = ImmutableSortedSet.orderedBy(IndexRange.COMPARATOR).add(indexRange0).add(indexRange1).add(b0).add(b1).build();
    final Stream bStream = mock(Stream.class);
    when(indexRangeService.find(any(DateTime.class), any(DateTime.class))).thenReturn(indices);
    when(streamService.load(eq("123456789ABCDEF"))).thenReturn(bStream);
    final IndexSet indexSet = mock(IndexSet.class);
    when(indexSet.isManagedIndex(startsWith("b_"))).thenReturn(true);
    when(bStream.getIndexSet()).thenReturn(indexSet);
    final TimeRange absoluteRange = AbsoluteRange.create(now.minusDays(1), now.plusDays(1));
    assertThat(searches.determineAffectedIndices(absoluteRange, "streams:123456789ABCDEF")).containsOnly(b0.indexName(), b1.indexName());
}
Also used : MongoIndexRange(org.graylog2.indexer.ranges.MongoIndexRange) IndexRange(org.graylog2.indexer.ranges.IndexRange) TimeRange(org.graylog2.plugin.indexer.searches.timeranges.TimeRange) Stream(org.graylog2.plugin.streams.Stream) MongoIndexRange(org.graylog2.indexer.ranges.MongoIndexRange) ZonedDateTime(java.time.ZonedDateTime) DateTime(org.joda.time.DateTime) IndexSet(org.graylog2.indexer.IndexSet) TestIndexSet(org.graylog2.indexer.TestIndexSet) Test(org.junit.Test)

Example 3 with TimeRange

use of org.graylog2.plugin.indexer.searches.timeranges.TimeRange in project graylog2-server by Graylog2.

the class SearchesTest method determineAffectedIndicesWithRangesIncludesDeflectorTarget.

@Test
public void determineAffectedIndicesWithRangesIncludesDeflectorTarget() throws Exception {
    final DateTime now = DateTime.now(DateTimeZone.UTC);
    final MongoIndexRange indexRange0 = MongoIndexRange.create("graylog_0", now, now.plusDays(1), now, 0);
    final MongoIndexRange indexRange1 = MongoIndexRange.create("graylog_1", now.plusDays(1), now.plusDays(2), now, 0);
    final MongoIndexRange indexRangeLatest = MongoIndexRange.create("graylog_2", new DateTime(0L, DateTimeZone.UTC), new DateTime(0L, DateTimeZone.UTC), now, 0);
    final SortedSet<IndexRange> indices = ImmutableSortedSet.orderedBy(IndexRange.COMPARATOR).add(indexRange0).add(indexRange1).add(indexRangeLatest).build();
    when(indexRangeService.find(any(DateTime.class), any(DateTime.class))).thenReturn(indices);
    final TimeRange absoluteRange = AbsoluteRange.create(now.minusDays(1), now.plusDays(1));
    final TimeRange keywordRange = KeywordRange.create("1 day ago");
    final TimeRange relativeRange = RelativeRange.create(3600);
    assertThat(searches.determineAffectedIndicesWithRanges(absoluteRange, null)).containsExactly(indexRangeLatest, indexRange0, indexRange1);
    assertThat(searches.determineAffectedIndicesWithRanges(keywordRange, null)).containsExactly(indexRangeLatest, indexRange0, indexRange1);
    assertThat(searches.determineAffectedIndicesWithRanges(relativeRange, null)).containsExactly(indexRangeLatest, indexRange0, indexRange1);
}
Also used : MongoIndexRange(org.graylog2.indexer.ranges.MongoIndexRange) IndexRange(org.graylog2.indexer.ranges.IndexRange) TimeRange(org.graylog2.plugin.indexer.searches.timeranges.TimeRange) MongoIndexRange(org.graylog2.indexer.ranges.MongoIndexRange) ZonedDateTime(java.time.ZonedDateTime) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 4 with TimeRange

use of org.graylog2.plugin.indexer.searches.timeranges.TimeRange in project graylog2-server by Graylog2.

the class TimeRangesTest method toSecondsHandlesIncompleteTimeRange.

@Test
public void toSecondsHandlesIncompleteTimeRange() throws Exception {
    assertThat(TimeRanges.toSeconds(new TimeRange() {

        @Override
        public String type() {
            return AbsoluteRange.ABSOLUTE;
        }

        @Override
        public DateTime getFrom() {
            return DateTime.now(DateTimeZone.UTC);
        }

        @Override
        public DateTime getTo() {
            return null;
        }

        @Override
        public Map<String, Object> getPersistedConfig() {
            return null;
        }
    })).isEqualTo(0);
    assertThat(TimeRanges.toSeconds(new TimeRange() {

        @Override
        public String type() {
            return AbsoluteRange.ABSOLUTE;
        }

        @Override
        public DateTime getFrom() {
            return null;
        }

        @Override
        public DateTime getTo() {
            return DateTime.now(DateTimeZone.UTC);
        }

        @Override
        public Map<String, Object> getPersistedConfig() {
            return null;
        }
    })).isEqualTo(0);
}
Also used : TimeRange(org.graylog2.plugin.indexer.searches.timeranges.TimeRange) Map(java.util.Map) DateTime(org.joda.time.DateTime) Test(org.junit.Test)

Example 5 with TimeRange

use of org.graylog2.plugin.indexer.searches.timeranges.TimeRange in project graylog2-server by Graylog2.

the class KeywordSearchResource method searchKeywordChunked.

@GET
@Timed
@ApiOperation(value = "Message search with keyword as timerange.", notes = "Search for messages in a timerange defined by a keyword like \"yesterday\" or \"2 weeks ago to wednesday\".")
@Produces(MoreMediaTypes.TEXT_CSV)
@ApiResponses(value = { @ApiResponse(code = 400, message = "Invalid keyword provided.") })
public ChunkedOutput<ScrollResult.ScrollChunk> searchKeywordChunked(@ApiParam(name = "query", value = "Query (Lucene syntax)", required = true) @QueryParam("query") @NotEmpty String query, @ApiParam(name = "keyword", value = "Range keyword", required = true) @QueryParam("keyword") String keyword, @ApiParam(name = "limit", value = "Maximum number of messages to return.", required = false) @QueryParam("limit") int limit, @ApiParam(name = "offset", value = "Offset", required = false) @QueryParam("offset") int offset, @ApiParam(name = "filter", value = "Filter", required = false) @QueryParam("filter") String filter, @ApiParam(name = "fields", value = "Comma separated list of fields to return", required = true) @QueryParam("fields") String fields) {
    checkSearchPermission(filter, RestPermissions.SEARCHES_KEYWORD);
    final List<String> fieldList = parseFields(fields);
    final TimeRange timeRange = buildKeywordTimeRange(keyword);
    try {
        final ScrollResult scroll = searches.scroll(query, timeRange, limit, offset, fieldList, filter);
        return buildChunkedOutput(scroll, limit);
    } catch (SearchPhaseExecutionException e) {
        throw createRequestExceptionForParseFailure(query, e);
    }
}
Also used : TimeRange(org.graylog2.plugin.indexer.searches.timeranges.TimeRange) ScrollResult(org.graylog2.indexer.results.ScrollResult) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

TimeRange (org.graylog2.plugin.indexer.searches.timeranges.TimeRange)21 DateTime (org.joda.time.DateTime)14 Test (org.junit.Test)10 Timed (com.codahale.metrics.annotation.Timed)8 ApiOperation (io.swagger.annotations.ApiOperation)8 ApiResponses (io.swagger.annotations.ApiResponses)8 Produces (javax.ws.rs.Produces)8 ZonedDateTime (java.time.ZonedDateTime)6 GET (javax.ws.rs.GET)6 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)6 SearchRequest (org.elasticsearch.action.search.SearchRequest)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 IndexRange (org.graylog2.indexer.ranges.IndexRange)6 InvalidRangeParametersException (org.graylog2.plugin.indexer.searches.timeranges.InvalidRangeParametersException)6 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)5 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)5 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)5 MongoIndexRange (org.graylog2.indexer.ranges.MongoIndexRange)5 Sorting (org.graylog2.indexer.searches.Sorting)5 ScrollResult (org.graylog2.indexer.results.ScrollResult)4