Search in sources :

Example 1 with SavedSearch

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();
}
Also used : SavedSearch(org.graylog2.savedsearches.SavedSearch) BadRequestException(javax.ws.rs.BadRequestException) URI(java.net.URI) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) 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) ApiResponse(io.swagger.annotations.ApiResponse)

Example 2 with SavedSearch

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);
}
Also used : SavedSearch(org.graylog2.savedsearches.SavedSearch) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Timed(com.codahale.metrics.annotation.Timed) ApiOperation(io.swagger.annotations.ApiOperation) AuditEvent(org.graylog2.audit.jersey.AuditEvent) ApiResponses(io.swagger.annotations.ApiResponses)

Example 3 with SavedSearch

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();
}
Also used : SavedSearch(org.graylog2.savedsearches.SavedSearch) BadRequestException(javax.ws.rs.BadRequestException) Path(javax.ws.rs.Path) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) 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) PUT(javax.ws.rs.PUT) ApiResponses(io.swagger.annotations.ApiResponses)

Example 4 with SavedSearch

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);
}
Also used : ViewService(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.view.ViewService) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) ZonedDateTime(java.time.ZonedDateTime) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ViewWidgetPosition(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.view.ViewWidgetPosition) ViewState(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.view.ViewState) Inject(javax.inject.Inject) ClusterConfigService(org.graylog2.plugin.cluster.ClusterConfigService) Map(java.util.Map) Migration(org.graylog2.migrations.Migration) RandomObjectIdProvider(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.view.RandomObjectIdProvider) ImmutableSet(com.google.common.collect.ImmutableSet) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) SearchType(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.search.SearchType) View(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.view.View) SavedSearch(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.savedsearch.SavedSearch) Query(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.search.Query) Set(java.util.Set) ViewWidget(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.view.ViewWidget) Collectors(java.util.stream.Collectors) SavedSearchService(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.savedsearch.SavedSearchService) Search(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.search.Search) AbstractMap(java.util.AbstractMap) RandomUUIDProvider(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.view.RandomUUIDProvider) JsonCreator(com.fasterxml.jackson.annotation.JsonCreator) AutoValue(com.google.auto.value.AutoValue) Position(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.view.Position) Optional(java.util.Optional) AggregationWidget(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.view.AggregationWidget) Titles(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.view.Titles) Collections(java.util.Collections) SearchService(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.search.SearchService) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Query(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.search.Query) HashMap(java.util.HashMap) ViewState(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.view.ViewState) View(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.view.View) Titles(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.view.Titles) ViewWidget(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.view.ViewWidget) ViewWidgetPosition(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.view.ViewWidgetPosition) SavedSearch(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.savedsearch.SavedSearch) Search(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.search.Search) SearchType(org.graylog.plugins.views.migrations.V20191203120602_MigrateSavedSearchesToViewsSupport.search.SearchType)

Aggregations

Timed (com.codahale.metrics.annotation.Timed)3 ApiOperation (io.swagger.annotations.ApiOperation)3 AuditEvent (org.graylog2.audit.jersey.AuditEvent)3 SavedSearch (org.graylog2.savedsearches.SavedSearch)3 ApiResponses (io.swagger.annotations.ApiResponses)2 BadRequestException (javax.ws.rs.BadRequestException)2 Consumes (javax.ws.rs.Consumes)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 RequiresPermissions (org.apache.shiro.authz.annotation.RequiresPermissions)2 JsonCreator (com.fasterxml.jackson.annotation.JsonCreator)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 AutoValue (com.google.auto.value.AutoValue)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ApiResponse (io.swagger.annotations.ApiResponse)1 URI (java.net.URI)1 ZonedDateTime (java.time.ZonedDateTime)1 AbstractMap (java.util.AbstractMap)1 Collections (java.util.Collections)1