Skip to content

Commit

Permalink
BE: Messages: Allow searching in message headers (#695)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeikel authored Dec 8, 2024
1 parent 168fb83 commit 3c0cd73
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
18 changes: 17 additions & 1 deletion api/src/main/java/io/kafbat/ui/emitter/MessageFilters.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,23 @@ public static Predicate<TopicMessageDTO> noop() {

public static Predicate<TopicMessageDTO> containsStringFilter(String string) {
return msg -> StringUtils.contains(msg.getKey(), string)
|| StringUtils.contains(msg.getContent(), string);
|| StringUtils.contains(msg.getContent(), string) || headersContains(msg, string);
}

private static boolean headersContains(TopicMessageDTO msg, String searchString) {
final var headers = msg.getHeaders();

if (headers == null) {
return false;
}

for (final var entry : headers.entrySet()) {
if (StringUtils.contains(entry.getKey(), searchString) || StringUtils.contains(entry.getValue(), searchString)) {
return true;
}
}

return false;
}

public static Predicate<TopicMessageDTO> celScriptFilter(String script) {
Expand Down
15 changes: 14 additions & 1 deletion api/src/test/java/io/kafbat/ui/emitter/MessageFiltersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class StringContainsFilter {
Predicate<TopicMessageDTO> filter = containsStringFilter("abC");

@Test
void returnsTrueWhenStringContainedInKeyOrContentOrInBoth() {
void returnsTrueWhenStringContainedInKeyOrContentOrHeadersOrInAllThree() {
assertTrue(
filter.test(msg().key("contains abCd").content("some str"))
);
Expand All @@ -40,6 +40,14 @@ void returnsTrueWhenStringContainedInKeyOrContentOrInBoth() {
assertTrue(
filter.test(msg().key("contains abCd").content("contains abCd"))
);

assertTrue(
filter.test(msg().key("dfg").content("does-not-contain").headers(Map.of("abC", "value")))
);

assertTrue(
filter.test(msg().key("dfg").content("does-not-contain").headers(Map.of("x1", "some abC")))
);
}

@Test
Expand All @@ -55,6 +63,11 @@ void returnsFalseOtherwise() {
assertFalse(
filter.test(msg().key("aBc").content("AbC"))
);

assertFalse(
filter.test(msg().key("aBc").content("AbC").headers(Map.of("abc", "value")))
);

}

}
Expand Down

0 comments on commit 3c0cd73

Please sign in to comment.