Search in sources :

Example 1 with CountResult

use of org.graylog2.indexer.results.CountResult in project graylog2-server by Graylog2.

the class SearchesTest method countRecordsMetrics.

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void countRecordsMetrics() throws Exception {
    CountResult result = searches.count("*", AbsoluteRange.create(new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC), new DateTime(2015, 1, 2, 0, 0, DateTimeZone.UTC)));
    assertThat(metricRegistry.getTimers()).containsKey(REQUEST_TIMER_NAME);
    assertThat(metricRegistry.getHistograms()).containsKey(RANGES_HISTOGRAM_NAME);
    Timer timer = metricRegistry.timer(REQUEST_TIMER_NAME);
    assertThat(timer.getCount()).isEqualTo(1L);
}
Also used : Timer(com.codahale.metrics.Timer) CountResult(org.graylog2.indexer.results.CountResult) ZonedDateTime(java.time.ZonedDateTime) DateTime(org.joda.time.DateTime) UsingDataSet(com.lordofthejars.nosqlunit.annotation.UsingDataSet) Test(org.junit.Test)

Example 2 with CountResult

use of org.graylog2.indexer.results.CountResult in project graylog2-server by Graylog2.

the class SearchResultCountWidgetStrategy method computeInternal.

protected ComputationResult computeInternal(String filter) {
    final TimeRange timeRange = this.timeRange;
    CountResult cr = searches.count(query, timeRange, filter);
    if (trend && timeRange instanceof RelativeRange) {
        DateTime toPrevious = timeRange.getFrom();
        DateTime fromPrevious = toPrevious.minus(Seconds.seconds(((RelativeRange) timeRange).getRange()));
        TimeRange previousTimeRange = AbsoluteRange.create(fromPrevious, toPrevious);
        CountResult previousCr = searches.count(query, previousTimeRange);
        Map<String, Object> results = Maps.newHashMap();
        results.put("now", cr.count());
        results.put("previous", previousCr.count());
        long tookMs = cr.tookMs() + previousCr.tookMs();
        return new ComputationResult(results, tookMs);
    } else {
        return new ComputationResult(cr.count(), cr.tookMs());
    }
}
Also used : TimeRange(org.graylog2.plugin.indexer.searches.timeranges.TimeRange) RelativeRange(org.graylog2.plugin.indexer.searches.timeranges.RelativeRange) ComputationResult(org.graylog2.plugin.dashboards.widgets.ComputationResult) CountResult(org.graylog2.indexer.results.CountResult) DateTime(org.joda.time.DateTime)

Example 3 with CountResult

use of org.graylog2.indexer.results.CountResult in project graylog2-server by Graylog2.

the class MessageCountAlertCondition method runCheck.

@Override
public CheckResult runCheck() {
    try {
        // Create an absolute range from the relative range to make sure it doesn't change during the two
        // search requests. (count and find messages)
        // This is needed because the RelativeRange computes the range from NOW on every invocation of getFrom() and
        // getTo().
        // See: https://github.com/Graylog2/graylog2-server/issues/2382
        final RelativeRange relativeRange = RelativeRange.create(time * 60);
        final AbsoluteRange range = AbsoluteRange.create(relativeRange.getFrom(), relativeRange.getTo());
        final String filter = "streams:" + stream.getId();
        final CountResult result = searches.count("*", range, filter);
        final long count = result.count();
        LOG.debug("Alert check <{}> result: [{}]", id, count);
        final boolean triggered;
        switch(thresholdType) {
            case MORE:
                triggered = count > threshold;
                break;
            case LESS:
                triggered = count < threshold;
                break;
            default:
                triggered = false;
        }
        if (triggered) {
            final List<MessageSummary> summaries = Lists.newArrayList();
            if (getBacklog() > 0) {
                final SearchResult backlogResult = searches.search("*", filter, range, getBacklog(), 0, new Sorting("timestamp", Sorting.Direction.DESC));
                for (ResultMessage resultMessage : backlogResult.getResults()) {
                    final Message msg = resultMessage.getMessage();
                    summaries.add(new MessageSummary(resultMessage.getIndex(), msg));
                }
            }
            final String resultDescription = "Stream had " + count + " messages in the last " + time + " minutes with trigger condition " + thresholdType.toString().toLowerCase(Locale.ENGLISH) + " than " + threshold + " messages. " + "(Current grace time: " + grace + " minutes)";
            return new CheckResult(true, this, resultDescription, Tools.nowUTC(), summaries);
        } else {
            return new NegativeCheckResult();
        }
    } catch (InvalidRangeParametersException e) {
        // cannot happen lol
        LOG.error("Invalid timerange.", e);
        return null;
    } catch (InvalidRangeFormatException e) {
        // lol same here
        LOG.error("Invalid timerange format.", e);
        return null;
    }
}
Also used : InvalidRangeFormatException(org.graylog2.indexer.InvalidRangeFormatException) InvalidRangeParametersException(org.graylog2.plugin.indexer.searches.timeranges.InvalidRangeParametersException) ResultMessage(org.graylog2.indexer.results.ResultMessage) Message(org.graylog2.plugin.Message) AbsoluteRange(org.graylog2.plugin.indexer.searches.timeranges.AbsoluteRange) SearchResult(org.graylog2.indexer.results.SearchResult) CountResult(org.graylog2.indexer.results.CountResult) ResultMessage(org.graylog2.indexer.results.ResultMessage) Sorting(org.graylog2.indexer.searches.Sorting) RelativeRange(org.graylog2.plugin.indexer.searches.timeranges.RelativeRange) MessageSummary(org.graylog2.plugin.MessageSummary)

Example 4 with CountResult

use of org.graylog2.indexer.results.CountResult in project graylog2-server by Graylog2.

the class MessageCountAlertConditionTest method searchCountShouldReturn.

private void searchCountShouldReturn(long count) {
    final CountResult countResult = mock(CountResult.class);
    when(countResult.count()).thenReturn(count);
    try {
        when(searches.count(anyString(), any(TimeRange.class), anyString())).thenReturn(countResult);
    } catch (InvalidRangeFormatException e) {
        assertNotNull("This should not return an exception!", e);
    }
}
Also used : TimeRange(org.graylog2.plugin.indexer.searches.timeranges.TimeRange) InvalidRangeFormatException(org.graylog2.indexer.InvalidRangeFormatException) CountResult(org.graylog2.indexer.results.CountResult)

Example 5 with CountResult

use of org.graylog2.indexer.results.CountResult in project graylog2-server by Graylog2.

the class SearchesTest method testCount.

@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
public void testCount() throws Exception {
    CountResult result = searches.count("*", AbsoluteRange.create(new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC), new DateTime(2015, 1, 2, 0, 0, DateTimeZone.UTC)));
    assertThat(result.count()).isEqualTo(10L);
}
Also used : CountResult(org.graylog2.indexer.results.CountResult) ZonedDateTime(java.time.ZonedDateTime) DateTime(org.joda.time.DateTime) UsingDataSet(com.lordofthejars.nosqlunit.annotation.UsingDataSet) Test(org.junit.Test)

Aggregations

CountResult (org.graylog2.indexer.results.CountResult)5 DateTime (org.joda.time.DateTime)3 UsingDataSet (com.lordofthejars.nosqlunit.annotation.UsingDataSet)2 ZonedDateTime (java.time.ZonedDateTime)2 InvalidRangeFormatException (org.graylog2.indexer.InvalidRangeFormatException)2 RelativeRange (org.graylog2.plugin.indexer.searches.timeranges.RelativeRange)2 TimeRange (org.graylog2.plugin.indexer.searches.timeranges.TimeRange)2 Test (org.junit.Test)2 Timer (com.codahale.metrics.Timer)1 ResultMessage (org.graylog2.indexer.results.ResultMessage)1 SearchResult (org.graylog2.indexer.results.SearchResult)1 Sorting (org.graylog2.indexer.searches.Sorting)1 Message (org.graylog2.plugin.Message)1 MessageSummary (org.graylog2.plugin.MessageSummary)1 ComputationResult (org.graylog2.plugin.dashboards.widgets.ComputationResult)1 AbsoluteRange (org.graylog2.plugin.indexer.searches.timeranges.AbsoluteRange)1 InvalidRangeParametersException (org.graylog2.plugin.indexer.searches.timeranges.InvalidRangeParametersException)1