use of org.graylog2.indexer.results.HistogramResult in project graylog2-server by Graylog2.
the class StackedChartWidgetStrategy method compute.
@Override
public ComputationResult compute() {
String filter = null;
if (!isNullOrEmpty(streamId)) {
filter = "streams:" + streamId;
}
final List<Map> results = new ArrayList<>(chartSeries.size());
DateTime from = null;
DateTime to = null;
long tookMs = 0;
for (Series series : chartSeries) {
try {
final HistogramResult histogramResult = searches.fieldHistogram(series.query, series.field, Searches.DateHistogramInterval.valueOf(interval.toString().toUpperCase(Locale.ENGLISH)), filter, this.timeRange, "cardinality".equalsIgnoreCase(series.statisticalFunction));
if (from == null) {
from = histogramResult.getHistogramBoundaries().getFrom();
}
to = histogramResult.getHistogramBoundaries().getTo();
results.add(histogramResult.getResults());
tookMs += histogramResult.took().millis();
} catch (Searches.FieldTypeException e) {
String msg = "Could not calculate [" + this.getClass().getCanonicalName() + "] widget <" + widgetId + ">. Not a numeric field? The field was [" + series.field + "]";
LOG.error(msg, e);
throw new RuntimeException(msg, e);
}
}
final AbsoluteRange computationTimeRange = AbsoluteRange.create(from, to);
return new ComputationResult(results, tookMs, computationTimeRange);
}
use of org.graylog2.indexer.results.HistogramResult in project graylog2-server by Graylog2.
the class Searches method histogram.
public HistogramResult histogram(String query, DateHistogramInterval interval, String filter, TimeRange range) {
FilterAggregationBuilder builder = AggregationBuilders.filter(AGG_FILTER).subAggregation(AggregationBuilders.dateHistogram(AGG_HISTOGRAM).field("timestamp").interval(interval.toESInterval())).filter(standardAggregationFilters(range, filter));
QueryStringQueryBuilder qs = queryStringQuery(query);
qs.allowLeadingWildcard(configuration.isAllowLeadingWildcardSearches());
final Set<String> affectedIndices = determineAffectedIndices(range, filter);
final SearchRequestBuilder srb = c.prepareSearch(affectedIndices.toArray(new String[affectedIndices.size()])).setIndicesOptions(IndicesOptions.lenientExpandOpen()).setQuery(qs).addAggregation(builder);
final SearchRequest request = srb.request();
SearchResponse r = c.search(request).actionGet();
recordEsMetrics(r, range);
final Filter f = r.getAggregations().get(AGG_FILTER);
return new DateHistogramResult(f.getAggregations().get(AGG_HISTOGRAM), query, request.source(), interval, r.getTook());
}
use of org.graylog2.indexer.results.HistogramResult in project graylog2-server by Graylog2.
the class SearchesTest method testFieldHistogram.
@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
@SuppressWarnings("unchecked")
public void testFieldHistogram() throws Exception {
final AbsoluteRange range = AbsoluteRange.create(new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC).withZone(UTC), new DateTime(2015, 1, 2, 0, 0, DateTimeZone.UTC).withZone(UTC));
HistogramResult h = searches.fieldHistogram("*", "n", Searches.DateHistogramInterval.HOUR, null, range, false);
assertThat(h.getInterval()).isEqualTo(Searches.DateHistogramInterval.HOUR);
assertThat(h.getHistogramBoundaries()).isEqualTo(range);
assertThat(h.getResults()).hasSize(5);
assertThat((Map<String, Number>) h.getResults().get(new DateTime(2015, 1, 1, 1, 0, UTC).getMillis() / 1000L)).containsEntry("total_count", 2L).containsEntry("total", 0.0);
assertThat((Map<String, Number>) h.getResults().get(new DateTime(2015, 1, 1, 2, 0, UTC).getMillis() / 1000L)).containsEntry("total_count", 2L).containsEntry("total", 4.0).containsEntry("mean", 2.0);
}
use of org.graylog2.indexer.results.HistogramResult in project graylog2-server by Graylog2.
the class SearchesTest method testHistogramWithNonExistingIndex.
@Test
@UsingDataSet(loadStrategy = LoadStrategyEnum.CLEAN_INSERT)
@SuppressWarnings("unchecked")
public void testHistogramWithNonExistingIndex() throws Exception {
final SortedSet<IndexRange> indexRanges = ImmutableSortedSet.orderedBy(IndexRange.COMPARATOR).add(MongoIndexRange.create(INDEX_NAME, new DateTime(0L, UTC), new DateTime(2015, 1, 1, 0, 0, UTC), DateTime.now(UTC), 0, null)).add(MongoIndexRange.create("does-not-exist", new DateTime(0L, UTC), new DateTime(2015, 1, 1, 0, 0, UTC), DateTime.now(UTC), 0, null)).build();
when(indexRangeService.find(any(DateTime.class), any(DateTime.class))).thenReturn(indexRanges);
final AbsoluteRange range = AbsoluteRange.create(new DateTime(2015, 1, 1, 0, 0, DateTimeZone.UTC).withZone(UTC), new DateTime(2015, 1, 2, 0, 0, DateTimeZone.UTC).withZone(UTC));
HistogramResult h = searches.histogram("*", Searches.DateHistogramInterval.HOUR, range);
assertThat(h.getInterval()).isEqualTo(Searches.DateHistogramInterval.HOUR);
assertThat(h.getHistogramBoundaries()).isEqualTo(range);
assertThat(h.getResults()).hasSize(5).containsEntry(new DateTime(2015, 1, 1, 1, 0, UTC).getMillis() / 1000L, 2L).containsEntry(new DateTime(2015, 1, 1, 2, 0, UTC).getMillis() / 1000L, 2L).containsEntry(new DateTime(2015, 1, 1, 3, 0, UTC).getMillis() / 1000L, 2L).containsEntry(new DateTime(2015, 1, 1, 4, 0, UTC).getMillis() / 1000L, 2L).containsEntry(new DateTime(2015, 1, 1, 5, 0, UTC).getMillis() / 1000L, 2L);
}
Aggregations