Search in sources :

Example 1 with DecorationStats

use of org.graylog2.rest.models.messages.responses.DecorationStats in project graylog2-server by Graylog2.

the class DecoratorProcessorImpl method decorate.

@Override
public SearchResponse decorate(SearchResponse searchResponse, Optional<String> streamId) {
    try {
        final List<SearchResponseDecorator> searchResponseDecorators = streamId.isPresent() ? decoratorResolver.searchResponseDecoratorsForStream(streamId.get()) : decoratorResolver.searchResponseDecoratorsForGlobal();
        final Optional<SearchResponseDecorator> metaDecorator = searchResponseDecorators.stream().reduce((f, g) -> (v) -> g.apply(f.apply(v)));
        if (metaDecorator.isPresent()) {
            final Map<String, ResultMessageSummary> originalMessages = searchResponse.messages().stream().collect(Collectors.toMap(this::getMessageKey, Function.identity()));
            final SearchResponse newSearchResponse = metaDecorator.get().apply(searchResponse);
            final Set<String> newFields = extractFields(newSearchResponse.messages());
            final List<ResultMessageSummary> decoratedMessages = newSearchResponse.messages().stream().map(resultMessage -> {
                final ResultMessageSummary originalMessage = originalMessages.get(getMessageKey(resultMessage));
                if (originalMessage != null) {
                    return resultMessage.toBuilder().decorationStats(DecorationStats.create(originalMessage.message(), resultMessage.message())).build();
                }
                return resultMessage;
            }).collect(Collectors.toList());
            return newSearchResponse.toBuilder().messages(decoratedMessages).fields(newFields).decorationStats(this.getSearchDecoratorStats(decoratedMessages)).build();
        }
    } catch (Exception e) {
        LOG.error("Error decorating search response", e);
    }
    return searchResponse;
}
Also used : Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) SearchResponse(org.graylog2.rest.resources.search.responses.SearchResponse) Set(java.util.Set) SearchResponseDecorator(org.graylog2.plugin.decorators.SearchResponseDecorator) SearchDecorationStats(org.graylog2.rest.resources.search.responses.SearchDecorationStats) DecorationStats(org.graylog2.rest.models.messages.responses.DecorationStats) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ResultMessageSummary(org.graylog2.rest.models.messages.responses.ResultMessageSummary) Inject(javax.inject.Inject) HashSet(java.util.HashSet) List(java.util.List) Map(java.util.Map) Optional(java.util.Optional) Message(org.graylog2.plugin.Message) SearchResponseDecorator(org.graylog2.plugin.decorators.SearchResponseDecorator) ResultMessageSummary(org.graylog2.rest.models.messages.responses.ResultMessageSummary) SearchResponse(org.graylog2.rest.resources.search.responses.SearchResponse)

Example 2 with DecorationStats

use of org.graylog2.rest.models.messages.responses.DecorationStats in project graylog2-server by Graylog2.

the class SimulatorResource method simulate.

@ApiOperation(value = "Simulate the execution of the pipeline message processor")
@POST
@RequiresPermissions(PipelineRestPermissions.PIPELINE_RULE_READ)
@NoAuditEvent("only used to test pipelines, no changes made in the system")
public SimulationResponse simulate(@ApiParam(name = "simulation", required = true) @NotNull SimulationRequest request) throws NotFoundException {
    checkPermission(RestPermissions.STREAMS_READ, request.streamId());
    final Message message = new Message(request.message());
    // Save off the original message fields to compare post pipeline processing
    Map<String, Object> originalFields = new HashMap<>(message.getFields());
    final Stream stream = streamService.load(request.streamId());
    message.addStream(stream);
    if (!Strings.isNullOrEmpty(request.inputId())) {
        message.setSourceInputId(request.inputId());
    }
    final List<ResultMessageSummary> simulationResults = new ArrayList<>();
    final PipelineInterpreterTracer pipelineInterpreterTracer = new PipelineInterpreterTracer();
    org.graylog2.plugin.Messages processedMessages = pipelineInterpreter.process(message, pipelineInterpreterTracer.getSimulatorInterpreterListener(), pipelineStateUpdater.getLatestState());
    for (Message processedMessage : processedMessages) {
        ResultMessageSummary summary = ResultMessageSummary.create(null, processedMessage.getFields(), "");
        // generate the DecorationStats and add it to the summary
        DecorationStats decorationStats = DecorationStats.create(originalFields, processedMessage.getFields());
        simulationResults.add(summary.toBuilder().decorationStats(decorationStats).build());
    }
    return SimulationResponse.create(simulationResults, pipelineInterpreterTracer.getExecutionTrace(), pipelineInterpreterTracer.took());
}
Also used : Message(org.graylog2.plugin.Message) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ResultMessageSummary(org.graylog2.rest.models.messages.responses.ResultMessageSummary) PipelineInterpreterTracer(org.graylog.plugins.pipelineprocessor.simulator.PipelineInterpreterTracer) Stream(org.graylog2.plugin.streams.Stream) DecorationStats(org.graylog2.rest.models.messages.responses.DecorationStats) RequiresPermissions(org.apache.shiro.authz.annotation.RequiresPermissions) POST(javax.ws.rs.POST) ApiOperation(io.swagger.annotations.ApiOperation) NoAuditEvent(org.graylog2.audit.jersey.NoAuditEvent)

Example 3 with DecorationStats

use of org.graylog2.rest.models.messages.responses.DecorationStats in project graylog2-server by Graylog2.

the class DecoratorProcessorImpl method getSearchDecoratorStats.

private SearchDecorationStats getSearchDecoratorStats(List<ResultMessageSummary> decoratedMessages) {
    final Set<String> addedFields = new HashSet<>();
    final Set<String> changedFields = new HashSet<>();
    final Set<String> removedFields = new HashSet<>();
    decoratedMessages.forEach(message -> {
        final DecorationStats decorationStats = message.decorationStats();
        if (decorationStats != null) {
            addedFields.addAll(decorationStats.addedFields().keySet());
            changedFields.addAll(decorationStats.changedFields().keySet());
            removedFields.addAll(decorationStats.removedFields().keySet());
        }
    });
    return SearchDecorationStats.create(addedFields, changedFields, removedFields);
}
Also used : SearchDecorationStats(org.graylog2.rest.resources.search.responses.SearchDecorationStats) DecorationStats(org.graylog2.rest.models.messages.responses.DecorationStats) HashSet(java.util.HashSet)

Example 4 with DecorationStats

use of org.graylog2.rest.models.messages.responses.DecorationStats in project graylog2-server by Graylog2.

the class DecoratorProcessorImpl method decorate.

@Override
public SearchResponse decorate(SearchResponse searchResponse, List<SearchResponseDecorator> searchResponseDecorators) {
    try {
        final Optional<SearchResponseDecorator> metaDecorator = searchResponseDecorators.stream().reduce((f, g) -> (v) -> g.apply(f.apply(v)));
        if (metaDecorator.isPresent()) {
            final Map<String, ResultMessageSummary> originalMessages = searchResponse.messages().stream().collect(Collectors.toMap(this::getMessageKey, Function.identity()));
            final SearchResponse newSearchResponse = metaDecorator.get().apply(searchResponse);
            final Set<String> newFields = extractFields(newSearchResponse.messages());
            final List<ResultMessageSummary> decoratedMessages = newSearchResponse.messages().stream().map(resultMessage -> {
                final ResultMessageSummary originalMessage = originalMessages.get(getMessageKey(resultMessage));
                if (originalMessage != null) {
                    return resultMessage.toBuilder().decorationStats(DecorationStats.create(originalMessage.message(), resultMessage.message())).build();
                }
                return resultMessage;
            }).collect(Collectors.toList());
            return newSearchResponse.toBuilder().messages(decoratedMessages).fields(newFields).decorationStats(this.getSearchDecoratorStats(decoratedMessages)).build();
        }
    } catch (Exception e) {
        LOG.error("Error decorating search response", e);
    }
    return searchResponse;
}
Also used : Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) SearchResponse(org.graylog2.rest.resources.search.responses.SearchResponse) Set(java.util.Set) SearchResponseDecorator(org.graylog2.plugin.decorators.SearchResponseDecorator) SearchDecorationStats(org.graylog2.rest.resources.search.responses.SearchDecorationStats) DecorationStats(org.graylog2.rest.models.messages.responses.DecorationStats) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ResultMessageSummary(org.graylog2.rest.models.messages.responses.ResultMessageSummary) Inject(javax.inject.Inject) HashSet(java.util.HashSet) List(java.util.List) Map(java.util.Map) Optional(java.util.Optional) Message(org.graylog2.plugin.Message) SearchResponseDecorator(org.graylog2.plugin.decorators.SearchResponseDecorator) ResultMessageSummary(org.graylog2.rest.models.messages.responses.ResultMessageSummary) SearchResponse(org.graylog2.rest.resources.search.responses.SearchResponse)

Aggregations

DecorationStats (org.graylog2.rest.models.messages.responses.DecorationStats)4 HashSet (java.util.HashSet)3 Message (org.graylog2.plugin.Message)3 ResultMessageSummary (org.graylog2.rest.models.messages.responses.ResultMessageSummary)3 SearchDecorationStats (org.graylog2.rest.resources.search.responses.SearchDecorationStats)3 List (java.util.List)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Set (java.util.Set)2 Function (java.util.function.Function)2 Collectors (java.util.stream.Collectors)2 Inject (javax.inject.Inject)2 SearchResponseDecorator (org.graylog2.plugin.decorators.SearchResponseDecorator)2 SearchResponse (org.graylog2.rest.resources.search.responses.SearchResponse)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 ApiOperation (io.swagger.annotations.ApiOperation)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 POST (javax.ws.rs.POST)1