Search in sources :

Example 6 with ResultMessageSummary

use of org.graylog2.rest.models.messages.responses.ResultMessageSummary 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 7 with ResultMessageSummary

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

the class SearchResponseCsvWriter method writeTo.

@Override
public void writeTo(SearchResponse searchResponse, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
    final CSVWriter csvWriter = new CSVWriter(new OutputStreamWriter(entityStream, StandardCharsets.UTF_8));
    final ImmutableSortedSet<String> sortedFields = ImmutableSortedSet.copyOf(Iterables.concat(searchResponse.fields(), Lists.newArrayList("source", "timestamp")));
    // write field headers
    csvWriter.writeNext(sortedFields.toArray(new String[sortedFields.size()]));
    // write result set in same order as the header row
    final String[] fieldValues = new String[sortedFields.size()];
    for (ResultMessageSummary message : searchResponse.messages()) {
        int idx = 0;
        // first collect all values from the current message
        for (String fieldName : sortedFields) {
            final Object val = message.message().get(fieldName);
            fieldValues[idx++] = ((val == null) ? null : val.toString().replaceAll("\n", "\\\\n"));
            fieldValues[idx++] = ((val == null) ? null : val.toString().replaceAll("\r", "\\\\r"));
        }
        // write the complete line, some fields might not be present in the message, so there might be null values
        csvWriter.writeNext(fieldValues);
    }
    if (csvWriter.checkError()) {
        LOG.error("Encountered unspecified error when writing message result as CSV, result is likely malformed.");
    }
    csvWriter.close();
}
Also used : CSVWriter(au.com.bytecode.opencsv.CSVWriter) OutputStreamWriter(java.io.OutputStreamWriter) ResultMessageSummary(org.graylog2.rest.models.messages.responses.ResultMessageSummary)

Example 8 with ResultMessageSummary

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

the class PipelineProcessorMessageDecorator method apply.

@Override
public SearchResponse apply(SearchResponse searchResponse) {
    final List<ResultMessageSummary> results = new ArrayList<>();
    if (pipelines.isEmpty()) {
        return searchResponse;
    }
    searchResponse.messages().forEach((inMessage) -> {
        final Message message = new Message(inMessage.message());
        final List<Message> additionalCreatedMessages = pipelineInterpreter.processForPipelines(message, pipelines, new NoopInterpreterListener(), pipelineStateUpdater.getLatestState());
        results.add(ResultMessageSummary.create(inMessage.highlightRanges(), message.getFields(), inMessage.index()));
        additionalCreatedMessages.forEach((additionalMessage) -> {
            // TODO: pass proper highlight ranges. Need to rebuild them for new messages.
            results.add(ResultMessageSummary.create(ImmutableMultimap.of(), additionalMessage.getFields(), "[created from decorator]"));
        });
    });
    return searchResponse.toBuilder().messages(results).build();
}
Also used : Message(org.graylog2.plugin.Message) NoopInterpreterListener(org.graylog.plugins.pipelineprocessor.processors.listeners.NoopInterpreterListener) ArrayList(java.util.ArrayList) ResultMessageSummary(org.graylog2.rest.models.messages.responses.ResultMessageSummary)

Example 9 with ResultMessageSummary

use of org.graylog2.rest.models.messages.responses.ResultMessageSummary 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 10 with ResultMessageSummary

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

the class LegacyChunkDecorator method legacySearchResponseFrom.

private SearchResponse legacySearchResponseFrom(SimpleMessageChunk chunk, ExportMessagesCommand command) {
    final List<ResultMessageSummary> legacyMessages = legacyMessagesFrom(chunk);
    String queryString = command.queryString().queryString();
    TimeRange timeRange = command.timeRange();
    return SearchResponse.create(queryString, queryString, Collections.emptySet(), legacyMessages, chunk.fieldsInOrder(), -1, -1, timeRange.getFrom(), timeRange.getTo());
}
Also used : TimeRange(org.graylog2.plugin.indexer.searches.timeranges.TimeRange) ResultMessageSummary(org.graylog2.rest.models.messages.responses.ResultMessageSummary)

Aggregations

ResultMessageSummary (org.graylog2.rest.models.messages.responses.ResultMessageSummary)13 SearchResponse (org.graylog2.rest.resources.search.responses.SearchResponse)7 Message (org.graylog2.plugin.Message)6 List (java.util.List)4 Map (java.util.Map)4 Optional (java.util.Optional)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 Inject (javax.inject.Inject)4 DecorationStats (org.graylog2.rest.models.messages.responses.DecorationStats)4 HashSet (java.util.HashSet)3 LegacyDecoratorProcessor (org.graylog.plugins.views.search.LegacyDecoratorProcessor)3 AbsoluteRange (org.graylog2.plugin.indexer.searches.timeranges.AbsoluteRange)3 IndexRangeSummary (org.graylog2.rest.models.system.indexer.responses.IndexRangeSummary)3 SearchDecorationStats (org.graylog2.rest.resources.search.responses.SearchDecorationStats)3 DateTime (org.joda.time.DateTime)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 MoreObjects.firstNonNull (com.google.common.base.MoreObjects.firstNonNull)2 Named (com.google.inject.name.Named)2 ArrayList (java.util.ArrayList)2