Skip to content
This repository was archived by the owner on Mar 21, 2023. It is now read-only.

Commit 8f6cc74

Browse files
edmundoabernd
authored andcommitted
Use raw messages on simulator (#42)
* Use arrow function to keep 'this' binding * Adapt simulator to use raw message loader At this point the pipeline simulator only makes sense for messages that were not already processed by Graylog, as it can be really tricky to see changes done in already processed messages. Therefore, we replace the message loader, using the raw message loader.
1 parent f883be9 commit 8f6cc74

File tree

4 files changed

+45
-53
lines changed

4 files changed

+45
-53
lines changed

src/main/java/org/graylog/plugins/pipelineprocessor/rest/SimulationRequest.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,27 @@
2121
import com.fasterxml.jackson.annotation.JsonProperty;
2222
import com.google.auto.value.AutoValue;
2323

24+
import java.util.Map;
25+
2426
@AutoValue
2527
@JsonAutoDetect
2628
public abstract class SimulationRequest {
2729
@JsonProperty
2830
public abstract String streamId();
2931

3032
@JsonProperty
31-
public abstract String index();
32-
33-
@JsonProperty
34-
public abstract String messageId();
33+
public abstract Map<String, Object> message();
3534

3635
public static Builder builder() {
3736
return new AutoValue_SimulationRequest.Builder();
3837
}
3938

4039
@JsonCreator
4140
public static SimulationRequest create (@JsonProperty("stream_id") String streamId,
42-
@JsonProperty("index") String index,
43-
@JsonProperty("message_id") String messageId) {
41+
@JsonProperty("message") Map<String, Object> message) {
4442
return builder()
4543
.streamId(streamId)
46-
.index(index)
47-
.messageId(messageId)
44+
.message(message)
4845
.build();
4946
}
5047

@@ -54,8 +51,6 @@ public abstract static class Builder {
5451

5552
public abstract Builder streamId(String streamId);
5653

57-
public abstract Builder index(String index);
58-
59-
public abstract Builder messageId(String messageId);
54+
public abstract Builder message(Map<String, Object> message);
6055
}
6156
}

src/main/java/org/graylog/plugins/pipelineprocessor/rest/SimulatorResource.java

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@
2424
import org.graylog.plugins.pipelineprocessor.processors.PipelineInterpreter;
2525
import org.graylog.plugins.pipelineprocessor.simulator.PipelineInterpreterTracer;
2626
import org.graylog2.database.NotFoundException;
27-
import org.graylog2.indexer.messages.DocumentNotFoundException;
28-
import org.graylog2.indexer.messages.Messages;
29-
import org.graylog2.indexer.results.ResultMessage;
3027
import org.graylog2.messageprocessors.OrderedMessageProcessors;
3128
import org.graylog2.plugin.Message;
3229
import org.graylog2.plugin.messageprocessors.MessageProcessor;
3330
import org.graylog2.plugin.rest.PluginRestResource;
3431
import org.graylog2.plugin.streams.Stream;
3532
import org.graylog2.rest.models.messages.responses.ResultMessageSummary;
33+
import org.graylog2.rest.resources.messages.MessageResource;
3634
import org.graylog2.shared.rest.resources.RestResource;
3735
import org.graylog2.shared.security.RestPermissions;
3836
import org.graylog2.streams.StreamService;
@@ -54,45 +52,40 @@
5452
@RequiresAuthentication
5553
public class SimulatorResource extends RestResource implements PluginRestResource {
5654
private final OrderedMessageProcessors orderedMessageProcessors;
57-
private final Messages messages;
55+
private final MessageResource messageResource;
5856
private final StreamService streamService;
5957

6058
@Inject
61-
public SimulatorResource(OrderedMessageProcessors orderedMessageProcessors, Messages messages, StreamService streamService) {
59+
public SimulatorResource(OrderedMessageProcessors orderedMessageProcessors, MessageResource messageResource, StreamService streamService) {
6260
this.orderedMessageProcessors = orderedMessageProcessors;
63-
this.messages = messages;
61+
this.messageResource = messageResource;
6462
this.streamService = streamService;
6563
}
6664

6765
@ApiOperation(value = "Simulate the execution of the pipeline message processor")
6866
@POST
6967
@RequiresPermissions(PipelineRestPermissions.PIPELINE_RULE_READ)
7068
public SimulationResponse simulate(@ApiParam(name = "simulation", required = true) @NotNull SimulationRequest request) throws NotFoundException {
71-
checkPermission(RestPermissions.MESSAGES_READ, request.messageId());
7269
checkPermission(RestPermissions.STREAMS_READ, request.streamId());
73-
try {
74-
final ResultMessage resultMessage = messages.get(request.messageId(), request.index());
75-
final Message message = resultMessage.getMessage();
76-
if (!request.streamId().equals("default")) {
77-
final Stream stream = streamService.load(request.streamId());
78-
message.addStream(stream);
79-
}
8070

81-
final List<ResultMessageSummary> simulationResults = new ArrayList<>();
82-
final PipelineInterpreterTracer pipelineInterpreterTracer = new PipelineInterpreterTracer();
71+
final Message message = new Message(request.message());
72+
if (!request.streamId().equals("default")) {
73+
final Stream stream = streamService.load(request.streamId());
74+
message.addStream(stream);
75+
}
76+
77+
final List<ResultMessageSummary> simulationResults = new ArrayList<>();
78+
final PipelineInterpreterTracer pipelineInterpreterTracer = new PipelineInterpreterTracer();
8379

84-
for (MessageProcessor messageProcessor : orderedMessageProcessors) {
85-
if (messageProcessor instanceof PipelineInterpreter) {
86-
org.graylog2.plugin.Messages processedMessages = ((PipelineInterpreter)messageProcessor).process(message, pipelineInterpreterTracer.getSimulatorInterpreterListener());
87-
for (Message processedMessage : processedMessages) {
88-
simulationResults.add(ResultMessageSummary.create(null, processedMessage.getFields(), ""));
89-
}
80+
for (MessageProcessor messageProcessor : orderedMessageProcessors) {
81+
if (messageProcessor instanceof PipelineInterpreter) {
82+
org.graylog2.plugin.Messages processedMessages = ((PipelineInterpreter) messageProcessor).process(message, pipelineInterpreterTracer.getSimulatorInterpreterListener());
83+
for (Message processedMessage : processedMessages) {
84+
simulationResults.add(ResultMessageSummary.create(null, processedMessage.getFields(), ""));
9085
}
9186
}
92-
93-
return SimulationResponse.create(simulationResults, pipelineInterpreterTracer.getExecutionTrace(), pipelineInterpreterTracer.took());
94-
} catch (DocumentNotFoundException e) {
95-
throw new NotFoundException(e);
9687
}
88+
89+
return SimulationResponse.create(simulationResults, pipelineInterpreterTracer.getExecutionTrace(), pipelineInterpreterTracer.took());
9790
}
9891
}

src/web/simulator/ProcessorSimulator.jsx

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import React from 'react';
22
import { Col, Row } from 'react-bootstrap';
33

4-
import LoaderTabs from 'components/messageloaders/LoaderTabs';
4+
import RawMessageLoader from 'components/messageloaders/RawMessageLoader';
55
import SimulationResults from './SimulationResults';
66

77
import SimulatorActions from './SimulatorActions';
8+
// eslint-disable-next-line no-unused-vars
89
import SimulatorStore from './SimulatorStore';
910

1011
const ProcessorSimulator = React.createClass({
@@ -24,14 +25,16 @@ const ProcessorSimulator = React.createClass({
2425
_onMessageLoad(message) {
2526
this.setState({ message: message, simulation: undefined, loading: true, error: undefined });
2627

27-
SimulatorActions.simulate.triggerPromise(this.props.stream, message.index, message.id).then(
28-
response => {
29-
this.setState({ simulation: response, loading: false });
30-
},
31-
error => {
32-
this.setState({ loading: false, error: error });
33-
}
34-
);
28+
SimulatorActions.simulate
29+
.triggerPromise(this.props.stream, message.fields)
30+
.then(
31+
response => {
32+
this.setState({ simulation: response, loading: false });
33+
},
34+
error => {
35+
this.setState({ loading: false, error: error });
36+
}
37+
);
3538
},
3639

3740
render() {
@@ -40,9 +43,11 @@ const ProcessorSimulator = React.createClass({
4043
<Row>
4144
<Col md={12}>
4245
<h1>Load a message</h1>
43-
<p>Load a message to be used in the simulation. <strong>No changes will be done in your stored
44-
messages.</strong></p>
45-
<LoaderTabs onMessageLoaded={this._onMessageLoad} disableMessagePreview />
46+
<p>
47+
Load a message to be used in the simulation.{' '}
48+
<strong>No changes will be done in your stored messages.</strong>
49+
</p>
50+
<RawMessageLoader onMessageLoaded={this._onMessageLoad} />
4651
</Col>
4752
</Row>
4853
<SimulationResults stream={this.props.stream}

src/web/simulator/SimulatorStore.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,17 @@ const urlPrefix = '/plugins/org.graylog.plugins.pipelineprocessor';
1212
const SimulatorStore = Reflux.createStore({
1313
listenables: [SimulatorActions],
1414

15-
simulate(stream, index, messageId) {
15+
simulate(stream, messageFields) {
1616
const url = URLUtils.qualifyUrl(`${urlPrefix}/system/pipelines/simulate`);
1717
const simulation = {
1818
stream_id: stream.id,
19-
index: index,
20-
message_id: messageId,
19+
message: messageFields,
2120
};
2221

2322
let promise = fetch('POST', url, simulation);
2423
promise = promise.then(response => {
2524
const formattedResponse = ObjectUtils.clone(response);
26-
formattedResponse.messages = response.messages.map(MessageFormatter.formatMessageSummary);
25+
formattedResponse.messages = response.messages.map(msg => MessageFormatter.formatMessageSummary(msg));
2726

2827
return formattedResponse;
2928
});

0 commit comments

Comments
 (0)