use of org.graylog2.plugin.indexer.searches.timeranges.TimeRange in project graylog2-server by Graylog2.
the class Searches method terms.
public TermsResult terms(String field, int size, String query, String filter, TimeRange range, Sorting.Direction sorting) {
Terms.Order termsOrder;
if (size == 0) {
size = 50;
}
if (sorting == Sorting.Direction.DESC) {
termsOrder = Terms.Order.count(false);
} else {
termsOrder = Terms.Order.count(true);
}
SearchRequestBuilder srb;
if (filter == null) {
srb = standardSearchRequest(query, determineAffectedIndices(range, null), range);
} else {
srb = filteredSearchRequest(query, filter, determineAffectedIndices(range, filter), range);
}
FilterAggregationBuilder builder = AggregationBuilders.filter(AGG_FILTER).subAggregation(AggregationBuilders.terms(AGG_TERMS).field(field).size(size).order(termsOrder)).subAggregation(AggregationBuilders.missing("missing").field(field)).filter(standardAggregationFilters(range, filter));
srb.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 TermsResult(f.getAggregations().get(AGG_TERMS), f.getAggregations().get("missing"), f.getDocCount(), query, request.source(), r.getTook());
}
use of org.graylog2.plugin.indexer.searches.timeranges.TimeRange 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.plugin.indexer.searches.timeranges.TimeRange in project graylog2-server by Graylog2.
the class DashboardWidgetCreator method fromPersisted.
public DashboardWidget fromPersisted(BasicDBObject fields) throws DashboardWidget.NoSuchWidgetTypeException, InvalidRangeParametersException, InvalidWidgetConfigurationException {
final String type = (String) fields.get(DashboardWidget.FIELD_TYPE);
final BasicDBObject config = (BasicDBObject) fields.get(DashboardWidget.FIELD_CONFIG);
final String widgetId = (String) fields.get(DashboardWidget.FIELD_ID);
// Build timerange.
final BasicDBObject timerangeConfig = (BasicDBObject) config.get("timerange");
final TimeRange timeRange = timeRangeFactory.create(timerangeConfig);
final String description = (String) fields.get(DashboardWidget.FIELD_DESCRIPTION);
final int cacheTime = (int) firstNonNull(fields.get(DashboardWidget.FIELD_CACHE_TIME), 0);
return buildDashboardWidget(type, widgetId, description, cacheTime, config, timeRange, (String) fields.get(DashboardWidget.FIELD_CREATOR_USER_ID));
}
use of org.graylog2.plugin.indexer.searches.timeranges.TimeRange in project graylog2-server by Graylog2.
the class DashboardWidgetsResource method addWidget.
@POST
@Timed
@ApiOperation(value = "Add a widget to a dashboard")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(value = { @ApiResponse(code = 404, message = "Dashboard not found."), @ApiResponse(code = 400, message = "Validation error."), @ApiResponse(code = 400, message = "No such widget type.") })
@AuditEvent(type = AuditEventTypes.DASHBOARD_WIDGET_CREATE)
public Response addWidget(@ApiParam(name = "dashboardId", required = true) @PathParam("dashboardId") String dashboardId, @ApiParam(name = "JSON body", required = true) AddWidgetRequest awr) throws ValidationException, NotFoundException {
checkPermission(RestPermissions.DASHBOARDS_EDIT, dashboardId);
// Bind to streams for reader users and check stream permission.
if (awr.config().containsKey("stream_id")) {
checkPermission(RestPermissions.STREAMS_READ, (String) awr.config().get("stream_id"));
} else {
checkPermission(RestPermissions.SEARCHES_ABSOLUTE);
checkPermission(RestPermissions.SEARCHES_RELATIVE);
checkPermission(RestPermissions.SEARCHES_KEYWORD);
}
final DashboardWidget widget;
try {
widget = dashboardWidgetCreator.fromRequest(awr, getCurrentUser().getName());
final Dashboard dashboard = dashboardService.load(dashboardId);
dashboardService.addWidget(dashboard, widget);
} catch (DashboardWidget.NoSuchWidgetTypeException e2) {
LOG.debug("No such widget type.", e2);
throw new BadRequestException("No such widget type.", e2);
} catch (InvalidRangeParametersException e3) {
LOG.debug("Invalid timerange parameters provided.", e3);
throw new BadRequestException("Invalid timerange parameters provided.", e3);
} catch (InvalidWidgetConfigurationException e4) {
LOG.debug("Invalid widget configuration.", e4);
throw new BadRequestException("Invalid widget configuration.", e4);
}
final Map<String, String> result = ImmutableMap.of("widget_id", widget.getId());
final URI widgetUri = getUriBuilderToSelf().path(DashboardWidgetsResource.class, "getWidget").build(dashboardId, widget.getId());
return Response.created(widgetUri).entity(result).build();
}
use of org.graylog2.plugin.indexer.searches.timeranges.TimeRange in project graylog2-server by Graylog2.
the class DashboardWidgetsResource method updateWidget.
@PUT
@Timed
@ApiOperation(value = "Update a widget")
@Path("/{widgetId}")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Dashboard not found."), @ApiResponse(code = 404, message = "Widget not found.") })
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.DASHBOARD_WIDGET_UPDATE)
public void updateWidget(@ApiParam(name = "dashboardId", required = true) @PathParam("dashboardId") String dashboardId, @ApiParam(name = "widgetId", required = true) @PathParam("widgetId") String widgetId, @ApiParam(name = "JSON body", required = true) @Valid @NotNull AddWidgetRequest awr) throws ValidationException, NotFoundException {
checkPermission(RestPermissions.DASHBOARDS_EDIT, dashboardId);
final Dashboard dashboard = dashboardService.load(dashboardId);
final DashboardWidget widget = dashboard.getWidget(widgetId);
if (widget == null) {
final String msg = "Widget " + widgetId + " on dashboard " + dashboardId + " not found.";
LOG.error(msg);
throw new javax.ws.rs.NotFoundException(msg);
}
try {
final DashboardWidget updatedWidget = dashboardWidgetCreator.fromRequest(widgetId, awr, widget.getCreatorUserId());
updatedWidget.setCacheTime(awr.cacheTime());
dashboardService.removeWidget(dashboard, widget);
dashboardService.addWidget(dashboard, updatedWidget);
this.clusterEventBus.post(WidgetUpdatedEvent.create(updatedWidget));
} catch (DashboardWidget.NoSuchWidgetTypeException e2) {
LOG.error("No such widget type.", e2);
throw new BadRequestException(e2);
} catch (InvalidRangeParametersException e3) {
LOG.error("Invalid timerange parameters provided.", e3);
throw new BadRequestException(e3);
} catch (InvalidWidgetConfigurationException e4) {
LOG.error("Invalid widget configuration.", e4);
throw new BadRequestException(e4);
}
LOG.info("Updated widget <" + widgetId + "> on dashboard <" + dashboardId + ">. Reason: REST request.");
}
Aggregations