use of org.graylog2.savedsearches.SavedSearch in project graylog2-server by Graylog2.
the class SavedSearchesResource method create.
@POST
@Timed
@ApiOperation(value = "Create a new saved search")
@RequiresPermissions(RestPermissions.SAVEDSEARCHES_CREATE)
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponse(code = 400, message = "Validation error")
@AuditEvent(type = AuditEventTypes.SAVED_SEARCH_CREATE)
public Response create(@ApiParam(name = "JSON body", required = true) @Valid CreateSavedSearchRequest cr) throws ValidationException {
if (!isTitleTaken("", cr.title())) {
final String msg = "Cannot save search " + cr.title() + ". Title is already taken.";
throw new BadRequestException(msg);
}
final SavedSearch search = savedSearchService.create(cr.title(), cr.query(), getCurrentUser().getName(), Tools.nowUTC());
final String id = savedSearchService.save(search);
final URI searchUri = getUriBuilderToSelf().path(SavedSearchesResource.class).path("{searchId}").build(id);
return Response.created(searchUri).entity(ImmutableMap.of("search_id", id)).build();
}
use of org.graylog2.savedsearches.SavedSearch in project graylog2-server by Graylog2.
the class SavedSearchesResource method delete.
@DELETE
@Path("/{searchId}")
@Timed
@ApiOperation(value = "Delete a saved search")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Saved search not found."), @ApiResponse(code = 400, message = "Invalid ObjectId.") })
@AuditEvent(type = AuditEventTypes.SAVED_SEARCH_DELETE)
public void delete(@ApiParam(name = "searchId", required = true) @PathParam("searchId") String searchId) throws NotFoundException {
checkPermission(RestPermissions.SAVEDSEARCHES_EDIT, searchId);
final SavedSearch search = savedSearchService.load(searchId);
savedSearchService.destroy(search);
}
use of org.graylog2.savedsearches.SavedSearch in project graylog2-server by Graylog2.
the class SavedSearchesResource method update.
@PUT
@Path("/{searchId}")
@Timed
@RequiresPermissions(RestPermissions.SAVEDSEARCHES_EDIT)
@ApiOperation(value = "Update a saved search")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@ApiResponses(value = { @ApiResponse(code = 404, message = "Saved search not found."), @ApiResponse(code = 400, message = "Invalid ObjectId."), @ApiResponse(code = 400, message = "Validation error") })
@AuditEvent(type = AuditEventTypes.SAVED_SEARCH_UPDATE)
public Map<String, Object> update(@ApiParam(name = "searchId", required = true) @PathParam("searchId") String searchId, @ApiParam(name = "JSON body", required = true) @Valid CreateSavedSearchRequest cr) throws NotFoundException, ValidationException {
final SavedSearch search = savedSearchService.load(searchId);
if (!isTitleTaken(searchId, cr.title())) {
final String msg = "Cannot save search " + cr.title() + ". Title is already taken.";
throw new BadRequestException(msg);
}
savedSearchService.update(search, cr.title(), cr.query());
return search.asMap();
}
use of org.graylog2.savedsearches.SavedSearch in project graylog2-server by Graylog2.
the class V20191203120602_MigrateSavedSearchesToViews method migrateSavedSearch.
private Map.Entry<View, Search> migrateSavedSearch(SavedSearch savedSearch) {
final String histogramId = randomUUIDProvider.get();
final String messageListId = randomUUIDProvider.get();
final Set<ViewWidget> widgets = ImmutableSet.of(AggregationWidget.create(histogramId), savedSearch.query().toMessagesWidget(messageListId));
final Map<String, Set<String>> widgetMapping = new HashMap<>(widgets.size());
final Set<SearchType> searchTypes = widgets.stream().flatMap(widget -> {
final Set<SearchType> widgetSearchTypes = widget.toSearchTypes(randomUUIDProvider);
widgetMapping.put(widget.id(), widgetSearchTypes.stream().map(SearchType::id).collect(Collectors.toSet()));
return widgetSearchTypes.stream();
}).collect(Collectors.toSet());
final Query.Builder queryBuilder = Query.builder().id(randomUUIDProvider.get()).timerange(savedSearch.query().toTimeRange()).query(savedSearch.query().query()).searchTypes(searchTypes);
final Query query = savedSearch.query().streamId().map(queryBuilder::streamId).orElse(queryBuilder).build();
final Search newSearch = Search.create(randomObjectIdProvider.get(), Collections.singleton(query), savedSearch.creatorUserId(), savedSearch.createdAt());
final Titles titles = Titles.ofWidgetTitles(ImmutableMap.of(histogramId, "Message Count", messageListId, "All Messages"));
final Map<String, ViewWidgetPosition> widgetPositions = ImmutableMap.of(histogramId, ViewWidgetPosition.builder().col(Position.fromInt(1)).row(Position.fromInt(1)).height(Position.fromInt(2)).width(Position.infinity()).build(), messageListId, ViewWidgetPosition.builder().col(Position.fromInt(1)).row(Position.fromInt(3)).height(Position.fromInt(6)).width(Position.infinity()).build());
final ViewState viewState = ViewState.create(titles, widgets, widgetMapping, widgetPositions);
final View newView = View.create(randomObjectIdProvider.get(), "Saved Search: " + savedSearch.title(), "This Search was migrated automatically from the \"" + savedSearch.title() + "\" saved search.", "", newSearch.id(), Collections.singletonMap(query.id(), viewState), Optional.of(savedSearch.creatorUserId()), savedSearch.createdAt());
return new AbstractMap.SimpleEntry<>(newView, newSearch);
}
Aggregations