use of org.graylog2.plugin.streams.StreamRule in project graylog2-server by Graylog2.
the class GreaterMatcherTest method testMissedMatchMissingField.
@Test
public void testMissedMatchMissingField() {
StreamRule rule = getSampleRule();
rule.setValue("42");
Message msg = getSampleMessage();
msg.addField("someother", "50");
StreamRuleMatcher matcher = getMatcher(rule);
assertFalse(matcher.match(msg, rule));
}
use of org.graylog2.plugin.streams.StreamRule in project graylog2-server by Graylog2.
the class GreaterMatcherTest method testMissedMatchWithInvalidValue.
@Test
public void testMissedMatchWithInvalidValue() {
StreamRule rule = getSampleRule();
rule.setValue("LOL I AM NOT EVEN A NUMBER");
Message msg = getSampleMessage();
msg.addField("something", "90000");
StreamRuleMatcher matcher = getMatcher(rule);
assertFalse(matcher.match(msg, rule));
}
use of org.graylog2.plugin.streams.StreamRule in project graylog2-server by Graylog2.
the class GreaterMatcherTest method testSuccessfullInvertedMatch.
@Test
public void testSuccessfullInvertedMatch() {
StreamRule rule = getSampleRule();
rule.setValue("10");
rule.setInverted(true);
Message msg = getSampleMessage();
msg.addField("something", "4");
StreamRuleMatcher matcher = getMatcher(rule);
assertTrue(matcher.match(msg, rule));
}
use of org.graylog2.plugin.streams.StreamRule in project graylog2-server by Graylog2.
the class StreamResource method cloneStream.
@POST
@Path("/{streamId}/clone")
@Timed
@ApiOperation(value = "Clone a stream")
@ApiResponses(value = { @ApiResponse(code = 404, message = "Stream not found."), @ApiResponse(code = 400, message = "Invalid or missing Stream id.") })
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@AuditEvent(type = AuditEventTypes.STREAM_CREATE)
public Response cloneStream(@ApiParam(name = "streamId", required = true) @PathParam("streamId") String streamId, @ApiParam(name = "JSON body", required = true) @Valid @NotNull CloneStreamRequest cr, @Context UserContext userContext) throws ValidationException, NotFoundException {
checkPermission(RestPermissions.STREAMS_CREATE);
checkPermission(RestPermissions.STREAMS_READ, streamId);
checkNotEditableStream(streamId, "The stream cannot be cloned.");
final Stream sourceStream = streamService.load(streamId);
final String creatorUser = getCurrentUser().getName();
final List<StreamRule> sourceStreamRules = streamRuleService.loadForStream(sourceStream);
final ImmutableSet.Builder<StreamRule> newStreamRules = ImmutableSet.builderWithExpectedSize(sourceStreamRules.size());
for (StreamRule streamRule : sourceStreamRules) {
final Map<String, Object> streamRuleData = Maps.newHashMapWithExpectedSize(6);
streamRuleData.put(StreamRuleImpl.FIELD_TYPE, streamRule.getType().toInteger());
streamRuleData.put(StreamRuleImpl.FIELD_FIELD, streamRule.getField());
streamRuleData.put(StreamRuleImpl.FIELD_VALUE, streamRule.getValue());
streamRuleData.put(StreamRuleImpl.FIELD_INVERTED, streamRule.getInverted());
streamRuleData.put(StreamRuleImpl.FIELD_DESCRIPTION, streamRule.getDescription());
final StreamRule newStreamRule = streamRuleService.create(streamRuleData);
newStreamRules.add(newStreamRule);
}
final Map<String, Object> streamData = Maps.newHashMap();
streamData.put(StreamImpl.FIELD_TITLE, cr.title());
streamData.put(StreamImpl.FIELD_DESCRIPTION, cr.description());
streamData.put(StreamImpl.FIELD_CREATOR_USER_ID, creatorUser);
streamData.put(StreamImpl.FIELD_CREATED_AT, Tools.nowUTC());
streamData.put(StreamImpl.FIELD_MATCHING_TYPE, sourceStream.getMatchingType().toString());
streamData.put(StreamImpl.FIELD_REMOVE_MATCHES_FROM_DEFAULT_STREAM, cr.removeMatchesFromDefaultStream());
streamData.put(StreamImpl.FIELD_DISABLED, true);
streamData.put(StreamImpl.FIELD_INDEX_SET_ID, cr.indexSetId());
final Stream stream = streamService.create(streamData);
final String savedStreamId = streamService.saveWithRulesAndOwnership(stream, newStreamRules.build(), userContext.getUser());
final ObjectId savedStreamObjectId = new ObjectId(savedStreamId);
for (AlertCondition alertCondition : streamService.getAlertConditions(sourceStream)) {
try {
final AlertCondition clonedAlertCondition = alertService.fromRequest(CreateConditionRequest.create(alertCondition.getType(), alertCondition.getTitle(), alertCondition.getParameters()), stream, creatorUser);
streamService.addAlertCondition(stream, clonedAlertCondition);
} catch (ConfigurationException e) {
LOG.warn("Unable to clone alert condition <" + alertCondition + "> - skipping: ", e);
}
}
for (AlarmCallbackConfiguration alarmCallbackConfiguration : alarmCallbackConfigurationService.getForStream(sourceStream)) {
final CreateAlarmCallbackRequest request = CreateAlarmCallbackRequest.create(alarmCallbackConfiguration);
final AlarmCallbackConfiguration alarmCallback = alarmCallbackConfigurationService.create(stream.getId(), request, getCurrentUser().getName());
alarmCallbackConfigurationService.save(alarmCallback);
}
final Set<ObjectId> outputIds = sourceStream.getOutputs().stream().map(Output::getId).map(ObjectId::new).collect(Collectors.toSet());
streamService.addOutputs(savedStreamObjectId, outputIds);
final Map<String, String> result = ImmutableMap.of("stream_id", savedStreamId);
final URI streamUri = getUriBuilderToSelf().path(StreamResource.class).path("{streamId}").build(savedStreamId);
return Response.created(streamUri).entity(result).build();
}
use of org.graylog2.plugin.streams.StreamRule in project graylog2-server by Graylog2.
the class StreamResource method getPage.
@GET
@Timed
@Path("/paginated")
@ApiOperation(value = "Get a paginated list of streams")
@Produces(MediaType.APPLICATION_JSON)
public StreamPageListResponse getPage(@ApiParam(name = "page") @QueryParam("page") @DefaultValue("1") int page, @ApiParam(name = "per_page") @QueryParam("per_page") @DefaultValue("50") int perPage, @ApiParam(name = "query") @QueryParam("query") @DefaultValue("") String query, @ApiParam(name = "sort", value = "The field to sort the result on", required = true, allowableValues = "title,description") @DefaultValue(StreamImpl.FIELD_TITLE) @QueryParam("sort") String sort, @ApiParam(name = "order", value = "The sort direction", allowableValues = "asc, desc") @DefaultValue("asc") @QueryParam("order") String order) {
SearchQuery searchQuery;
try {
searchQuery = searchQueryParser.parse(query);
} catch (IllegalArgumentException e) {
throw new BadRequestException("Invalid argument in search query: " + e.getMessage());
}
final Predicate<StreamDTO> permissionFilter = streamDTO -> isPermitted(RestPermissions.STREAMS_READ, streamDTO.id());
final PaginatedList<StreamDTO> result = paginatedStreamService.findPaginated(searchQuery, permissionFilter, page, perPage, sort, order);
final List<String> streamIds = result.stream().map(streamDTO -> streamDTO.id()).collect(Collectors.toList());
final Map<String, List<StreamRule>> streamRuleMap = streamRuleService.loadForStreamIds(streamIds);
final List<StreamDTO> streams = result.stream().map(streamDTO -> {
List<StreamRule> rules = streamRuleMap.getOrDefault(streamDTO.id(), Collections.emptyList());
return streamDTO.toBuilder().rules(rules).build();
}).collect(Collectors.toList());
final long total = paginatedStreamService.count();
final PaginatedList<StreamDTO> streamDTOS = new PaginatedList<>(streams, result.pagination().total(), result.pagination().page(), result.pagination().perPage());
return StreamPageListResponse.create(query, streamDTOS.pagination(), total, sort, order, streams);
}
Aggregations