String query = params.get("query");
String rangeType = params.get("rangeType");
String description = params.get("description");
Dashboard dashboard = dashboardService.get(dashboardId);
// Determine timerange type.
TimeRange timerange;
try {
int relative = 0;
if (params.get("relative") != null) {
relative = Integer.parseInt(params.get("relative"));
}
timerange = TimeRange.factory(rangeType, relative, params.get("from"), params.get("to"), params.get("keyword"));
} catch (InvalidRangeParametersException e2) {
return status(400, views.html.errors.error.render("Invalid range parameters provided.", e2, request()));
} catch (IllegalArgumentException e1) {
return status(400, views.html.errors.error.render("Invalid range type provided.", e1, request()));
}
String streamId;
if (params.containsKey("streamId")) {
streamId = params.get("streamId");
} else {
streamId = params.get("streamid");
}
DashboardWidget widget;
try {
switch (DashboardWidget.Type.valueOf(params.get("widgetType"))) {
case SEARCH_RESULT_COUNT:
widget = new SearchResultCountWidget(dashboard, query, timerange, description);
break;
case STREAM_SEARCH_RESULT_COUNT:
if (!canReadStream(streamId)) return unauthorized();
widget = new StreamSearchResultCountWidget(dashboard, query, timerange, description, streamId);
break;
case FIELD_CHART:
Map<String, Object> config = new HashMap<String, Object>() {{
put("field", params.get("field"));
put("valuetype", params.get("valuetype"));
put("renderer", params.get("renderer"));
put("interpolation", params.get("interpolation"));
put("interval", params.get("interval"));
}};
if (!canReadStream(streamId)) return unauthorized();
widget = new FieldChartWidget(dashboard, query, timerange, description, streamId, config);
break;
case QUICKVALUES:
if (!canReadStream(streamId)) return unauthorized();
widget = new QuickvaluesWidget(dashboard, query, timerange, params.get("field"), description, streamId);
break;
case SEARCH_RESULT_CHART:
if (!canReadStream(streamId)) return unauthorized();
widget = new SearchResultChartWidget(dashboard, query, timerange, description, streamId, params.get("interval"));
break;
default:
throw new IllegalArgumentException();
}
} catch (IllegalArgumentException e) {
Logger.error("No such widget type: " + params.get("widgetType"));
return badRequest();
}
dashboard.addWidget(widget);
return created();
} catch (APIException e) {
String message = "Could not add widget. We got a HTTP " + e.getHttpCode() + ".";
return status(504, views.html.errors.error.render(message, e, request()));