Search in sources :

Example 11 with DashboardWidget

use of org.graylog2.dashboards.widgets.DashboardWidget 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 12 with DashboardWidget

use of org.graylog2.dashboards.widgets.DashboardWidget 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)

Example 13 with DashboardWidget

use of org.graylog2.dashboards.widgets.DashboardWidget in project graylog2-server by Graylog2.

the class DashboardWidgetCreator method fromRequest.

public DashboardWidget fromRequest(String widgetId, AddWidgetRequest awr, String userId) throws DashboardWidget.NoSuchWidgetTypeException, InvalidRangeParametersException, InvalidWidgetConfigurationException {
    final String id = isNullOrEmpty(widgetId) ? UUID.randomUUID().toString() : widgetId;
    // Build timerange.
    final Map<String, Object> timerangeConfig = (Map<String, Object>) awr.config().get("timerange");
    final TimeRange timeRange = timeRangeFactory.create(timerangeConfig);
    return buildDashboardWidget(awr.type(), id, awr.description(), 0, awr.config(), timeRange, userId);
}
Also used : TimeRange(org.graylog2.plugin.indexer.searches.timeranges.TimeRange) BasicDBObject(com.mongodb.BasicDBObject) Map(java.util.Map)

Example 14 with DashboardWidget

use of org.graylog2.dashboards.widgets.DashboardWidget in project graylog2-server by Graylog2.

the class DashboardWidgetsResourceTest method updateWidgetMustSendUpdatedWidgetEvent.

@Test
public void updateWidgetMustSendUpdatedWidgetEvent() throws Exception {
    final DashboardWidget updatedWidget = mock(DashboardWidget.class);
    when(updatedWidget.getId()).thenReturn(widgetId);
    final AddWidgetRequest addWidgetRequest = AddWidgetRequest.create("new description", "new type", 60, Collections.emptyMap());
    when(dashboardWidgetCreator.fromRequest(eq(widgetId), eq(addWidgetRequest), eq(creatorUserId))).thenReturn(updatedWidget);
    this.dashboardWidgetsResource.updateWidget(dashboardId, widgetId, addWidgetRequest);
    verifyWidgetUpdatedEvent(widgetId);
}
Also used : DashboardWidget(org.graylog2.dashboards.widgets.DashboardWidget) AddWidgetRequest(org.graylog2.rest.models.dashboards.requests.AddWidgetRequest) Test(org.junit.Test)

Aggregations

DashboardWidget (org.graylog2.dashboards.widgets.DashboardWidget)9 Timed (com.codahale.metrics.annotation.Timed)7 ApiOperation (io.swagger.annotations.ApiOperation)7 ApiResponses (io.swagger.annotations.ApiResponses)7 Produces (javax.ws.rs.Produces)7 Dashboard (org.graylog2.dashboards.Dashboard)7 Path (javax.ws.rs.Path)6 AuditEvent (org.graylog2.audit.jersey.AuditEvent)5 NotFoundException (org.graylog2.database.NotFoundException)5 BasicDBObject (com.mongodb.BasicDBObject)4 Map (java.util.Map)3 PUT (javax.ws.rs.PUT)3 InvalidWidgetConfigurationException (org.graylog2.dashboards.widgets.InvalidWidgetConfigurationException)3 InvalidRangeParametersException (org.graylog2.plugin.indexer.searches.timeranges.InvalidRangeParametersException)3 TimeRange (org.graylog2.plugin.indexer.searches.timeranges.TimeRange)3 List (java.util.List)2 BadRequestException (javax.ws.rs.BadRequestException)2 GET (javax.ws.rs.GET)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1