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);
}
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());
}
}
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;
}
}
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);
}
}
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);
}
Aggregations