Search in sources :

Example 11 with InvalidRangeParametersException

use of org.graylog2.plugin.indexer.searches.timeranges.InvalidRangeParametersException 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));
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) TimeRange(org.graylog2.plugin.indexer.searches.timeranges.TimeRange)

Example 12 with InvalidRangeParametersException

use of org.graylog2.plugin.indexer.searches.timeranges.InvalidRangeParametersException 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();
}
Also used : InvalidRangeParametersException(org.graylog2.plugin.indexer.searches.timeranges.InvalidRangeParametersException) DashboardWidget(org.graylog2.dashboards.widgets.DashboardWidget) Dashboard(org.graylog2.dashboards.Dashboard) BadRequestException(javax.ws.rs.BadRequestException) InvalidWidgetConfigurationException(org.graylog2.dashboards.widgets.InvalidWidgetConfigurationException) URI(java.net.URI) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Example 13 with InvalidRangeParametersException

use of org.graylog2.plugin.indexer.searches.timeranges.InvalidRangeParametersException 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.");
}
Also used : InvalidRangeParametersException(org.graylog2.plugin.indexer.searches.timeranges.InvalidRangeParametersException) DashboardWidget(org.graylog2.dashboards.widgets.DashboardWidget) Dashboard(org.graylog2.dashboards.Dashboard) NotFoundException(org.graylog2.database.NotFoundException) BadRequestException(javax.ws.rs.BadRequestException) InvalidWidgetConfigurationException(org.graylog2.dashboards.widgets.InvalidWidgetConfigurationException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 14 with InvalidRangeParametersException

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

the class BundleImporter method createDashboardWidget.

@SuppressWarnings("unchecked")
private org.graylog2.dashboards.widgets.DashboardWidget createDashboardWidget(final DashboardWidget dashboardWidget, final String userName) throws InvalidRangeParametersException, org.graylog2.dashboards.widgets.DashboardWidget.NoSuchWidgetTypeException, InvalidWidgetConfigurationException {
    final String type = dashboardWidget.getType();
    final Map<String, Object> config = dashboardWidget.getConfiguration();
    // Replace "stream_id" in config if it's set
    final String streamReference = (String) config.get("stream_id");
    if (!isNullOrEmpty(streamReference)) {
        final org.graylog2.plugin.streams.Stream stream = streamsByReferenceId.get(streamReference);
        if (null != stream) {
            config.put("stream_id", stream.getId());
        } else {
            LOG.warn("Couldn't find referenced stream {}", streamReference);
        }
    }
    // Build timerange.
    final Map<String, Object> timerangeConfig = (Map<String, Object>) config.get("timerange");
    final TimeRange timeRange = timeRangeFactory.create(timerangeConfig);
    final String widgetId = UUID.randomUUID().toString();
    return dashboardWidgetCreator.buildDashboardWidget(type, widgetId, dashboardWidget.getDescription(), dashboardWidget.getCacheTime(), config, timeRange, userName);
}
Also used : TimeRange(org.graylog2.plugin.indexer.searches.timeranges.TimeRange) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 15 with InvalidRangeParametersException

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

the class DashboardServiceImpl method create.

private Dashboard create(ObjectId id, Map<String, Object> fields) {
    final Dashboard dashboard = new DashboardImpl(id, fields);
    // Add all widgets of this dashboard.
    if (fields.containsKey(DashboardImpl.EMBEDDED_WIDGETS)) {
        if (fields.get(DashboardImpl.EMBEDDED_WIDGETS) instanceof List) {
            for (BasicDBObject widgetFields : (List<BasicDBObject>) fields.get(DashboardImpl.EMBEDDED_WIDGETS)) {
                try {
                    final DashboardWidget widget = dashboardWidgetCreator.fromPersisted(widgetFields);
                    dashboard.addPersistedWidget(widget);
                } catch (DashboardWidget.NoSuchWidgetTypeException e) {
                    LOG.error("No such widget type: [" + widgetFields.get("type") + "] - Dashboard: [" + dashboard.getId() + "]", e);
                } catch (InvalidRangeParametersException e) {
                    LOG.error("Invalid range parameters of widget in dashboard: [" + dashboard.getId() + "]", e);
                } catch (InvalidWidgetConfigurationException e) {
                    LOG.error("Invalid configuration of widget in dashboard: [" + dashboard.getId() + "]", e);
                }
            }
        }
    }
    return dashboard;
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) InvalidRangeParametersException(org.graylog2.plugin.indexer.searches.timeranges.InvalidRangeParametersException) DashboardWidget(org.graylog2.dashboards.widgets.DashboardWidget) List(java.util.List) InvalidWidgetConfigurationException(org.graylog2.dashboards.widgets.InvalidWidgetConfigurationException)

Aggregations

TimeRange (org.graylog2.plugin.indexer.searches.timeranges.TimeRange)13 DerivedTimeRange (org.graylog.plugins.views.search.timeranges.DerivedTimeRange)10 Test (org.junit.Test)9 InvalidRangeParametersException (org.graylog2.plugin.indexer.searches.timeranges.InvalidRangeParametersException)8 BasicDBObject (com.mongodb.BasicDBObject)3 DashboardWidget (org.graylog2.dashboards.widgets.DashboardWidget)3 InvalidWidgetConfigurationException (org.graylog2.dashboards.widgets.InvalidWidgetConfigurationException)3 ResultMessage (org.graylog2.indexer.results.ResultMessage)3 Message (org.graylog2.plugin.Message)3 MessageSummary (org.graylog2.plugin.MessageSummary)3 Timed (com.codahale.metrics.annotation.Timed)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 BadRequestException (javax.ws.rs.BadRequestException)2 Produces (javax.ws.rs.Produces)2 ElasticsearchQueryString (org.graylog.plugins.views.search.elasticsearch.ElasticsearchQueryString)2 SearchConfig (org.graylog.plugins.views.search.engine.SearchConfig)2