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;
}
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();
}
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();
}
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());
}
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());
}
Aggregations