com.epam.eco.kafkamanager.ConsumerGroupListSearchCriteria Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kafka-manager-commons Show documentation
Show all versions of kafka-manager-commons Show documentation
Common models, utilities, etc
The newest version!
/*******************************************************************************
* Copyright 2022 EPAM Systems
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*******************************************************************************/
package com.epam.eco.kafkamanager;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.BiPredicate;
import org.apache.kafka.common.ConsumerGroupState;
/**
* @author Mikhail_Vershkov
*/
public class ConsumerGroupListSearchCriteria extends AbstractSearchCriteria {
private static final String GROUP_NAME_ATTR = "groupName";
private static final String STATE_ATTR = "state";
private static final String STORAGE_ATTR = "storage";
private static final String TOPICS_ATTR = "topics";
private static final String TOPICS_COUNT_ATTR = "topicsCount";
private static final String MEMBERS_ATTR = "members";
private static final String MEMBERS_COUNT_ATTR = "membersCount";
private static final String DESCRIPTION_ATTR = "description";
private static final String[] ARRAY_ATTRS = {GROUP_NAME_ATTR, STATE_ATTR, STORAGE_ATTR, TOPICS_ATTR,
TOPICS_COUNT_ATTR, MEMBERS_ATTR, MEMBERS_COUNT_ATTR, DESCRIPTION_ATTR};
private ConsumerGroupListSearchCriteria(Set clauses) {
super(clauses);
}
public static ConsumerGroupListSearchCriteria fromJsonWith(Map map) {
return parseTopicCriteria(map);
}
static boolean ifKeyExists(String key) {
return Arrays.stream(ARRAY_ATTRS).anyMatch(key::startsWith);
}
public static ConsumerGroupListSearchCriteria parseTopicCriteria(Map map) {
Set> groupNameClauses = new HashSet<>();
Set> stateClauses = new HashSet<>();
Set> storageClauses = new HashSet<>();
Set> topicsClauses = new HashSet<>();
Set> topicsCountClauses = new HashSet<>();
Set> membersClauses = new HashSet<>();
Set> membersCountClauses = new HashSet<>();
Set> descriptionClauses = new HashSet<>();
for(String key : map.keySet()) {
if(ifKeyExists(key) && key.indexOf(OPERATION_SEPARATOR) > 0) {
String filterColumn = key.substring(0, key.indexOf(OPERATION_SEPARATOR));
String rawOperation = key.substring(key.indexOf(OPERATION_SEPARATOR) + 1);
Operation filterOperation = Operation.valueOf(rawOperation);
switch (filterColumn) {
case GROUP_NAME_ATTR -> groupNameClauses.add(new SingleClause<>((String) map.get(key), filterOperation));
case STATE_ATTR -> stateClauses.add(new SingleClause<>(ConsumerGroupState.valueOf((String) map.get(key)), filterOperation));
case STORAGE_ATTR -> storageClauses.add(new SingleClause<>(ConsumerGroupInfo.StorageType.valueOf((String)map.get(key)), filterOperation));
case TOPICS_ATTR -> topicsClauses.add(new SingleClause<>((String) map.get(key), filterOperation));
case TOPICS_COUNT_ATTR -> topicsCountClauses.add(new SingleClause<>(Integer.valueOf((String) map.get(key)), filterOperation));
case MEMBERS_ATTR -> membersClauses.add(new SingleClause<>((String) map.get(key), filterOperation));
case MEMBERS_COUNT_ATTR -> membersCountClauses.add(new SingleClause<>(Integer.valueOf((String) map.get(key)), filterOperation));
case DESCRIPTION_ATTR -> descriptionClauses.add(new SingleClause<>((String) map.get(key), filterOperation));
default -> {
}
}
}
}
return new ConsumerGroupListSearchCriteria(
Set.of(new ClausesWithHandler<>(groupNameClauses, stringClausesHandler, ConsumerGroupInfo::getName),
new ClausesWithHandler(storageClauses, storageClausesHandler,
consumerGroupInfo -> consumerGroupInfo),
new ClausesWithHandler(stateClauses, stateClausesHandler, consumerGroupInfo -> consumerGroupInfo),
new ClausesWithHandler(topicsClauses, topicsClausesHandler, consumerGroupInfo -> consumerGroupInfo),
new ClausesWithHandler(topicsCountClauses, numericClausesHandler, consumerInfo->consumerInfo.getTopicNames().size()),
new ClausesWithHandler(membersClauses, membersClausesHandler, groupInfo -> groupInfo),
new ClausesWithHandler(membersCountClauses, numericClausesHandler, consumerInfo->consumerInfo.getMembers().size()),
new ClausesWithHandler(descriptionClauses, stringClausesHandler,
groupInfo -> groupInfo.getMetadata().map(Metadata::getDescription).orElse(null))
));
}
private static final BiPredicate>, ConsumerGroupInfo> storageClausesHandler = (Set> clauses, ConsumerGroupInfo groupInfo) -> clauses.stream().allMatch(
clause -> groupInfo.getStorageType() == clause.filterValue());
private static final BiPredicate>, ConsumerGroupInfo> stateClausesHandler = (Set> clauses, ConsumerGroupInfo groupInfo) -> clauses.stream().allMatch(
clause -> groupInfo.getState() == clause.filterValue());
private static final BiPredicate>, ConsumerGroupInfo> topicsClausesHandler = (Set> clauses, ConsumerGroupInfo groupInfo) -> clauses.stream().allMatch(
clause -> groupInfo.getTopicNames().stream().map(topic->compareStringValues(clause.filterValue(), topic, clause.operation())).reduce((x, y) -> x||y).orElse(false));
private static final BiPredicate>, ConsumerGroupInfo> membersClausesHandler = (Set> clauses, ConsumerGroupInfo groupInfo) -> clauses.stream().allMatch(
clause -> groupInfo.getMembers().stream().map(member->compareStringValues(clause.filterValue(),
member.getMemberId() + member.getClientHost(), clause.operation())).reduce((x, y) -> x||y).orElse(false));
}