Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BE: Messages: Allow searching in message headers #695

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading