Skip to content

Commit 7d950d8

Browse files
authored
BE: Fixes #498 Added sort by number of messages (#1330)
1 parent 58c361f commit 7d950d8

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

api/src/main/java/io/kafbat/ui/controller/TopicsController.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import static io.kafbat.ui.model.rbac.permission.TopicAction.EDIT;
88
import static io.kafbat.ui.model.rbac.permission.TopicAction.VIEW;
99
import static java.util.stream.Collectors.toList;
10-
import static org.apache.commons.lang3.Strings.CI;
1110

1211
import io.kafbat.ui.api.TopicsApi;
1312
import io.kafbat.ui.config.ClustersProperties;
@@ -362,6 +361,10 @@ private Comparator<InternalTopic> getComparatorForTopic(
362361
case OUT_OF_SYNC_REPLICAS -> Comparator.comparing(t -> t.getReplicas() - t.getInSyncReplicas());
363362
case REPLICATION_FACTOR -> Comparator.comparing(InternalTopic::getReplicationFactor);
364363
case SIZE -> Comparator.comparing(InternalTopic::getSegmentSize);
364+
case MESSAGES_COUNT -> Comparator.comparing(
365+
InternalTopic::getMessagesCount,
366+
Comparator.nullsLast(Long::compareTo)
367+
);
365368
default -> defaultComparator;
366369
};
367370
}

api/src/main/java/io/kafbat/ui/mapper/ClusterMapper.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import org.apache.kafka.common.resource.ResourceType;
5252
import org.mapstruct.Mapper;
5353
import org.mapstruct.Mapping;
54+
import org.openapitools.jackson.nullable.JsonNullable;
5455

5556
@Mapper(componentModel = "spring")
5657
public interface ClusterMapper {
@@ -104,6 +105,14 @@ default ConfigSynonymDTO toConfigSynonym(ConfigEntry.ConfigSynonym config) {
104105

105106
TopicDTO toTopic(InternalTopic topic);
106107

108+
default <T> JsonNullable<T> toJsonNullable(T value) {
109+
if (value == null) {
110+
return JsonNullable.undefined();
111+
} else {
112+
return JsonNullable.of(value);
113+
}
114+
}
115+
107116
PartitionDTO toPartition(InternalPartition topic);
108117

109118
BrokerDTO toBrokerDto(InternalBroker broker);

api/src/main/java/io/kafbat/ui/model/InternalTopic.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import lombok.Data;
1111
import org.apache.kafka.clients.admin.ConfigEntry;
1212
import org.apache.kafka.clients.admin.TopicDescription;
13-
import org.apache.kafka.common.TopicPartition;
1413

1514
@Data
1615
@Builder(toBuilder = true)
@@ -143,4 +142,16 @@ public static InternalTopic from(TopicDescription topicDescription,
143142
return topic.build();
144143
}
145144

145+
public @Nullable Long getMessagesCount() {
146+
Long result = null;
147+
if (cleanUpPolicy.equals(CleanupPolicy.DELETE)) {
148+
result = 0L;
149+
if (partitions != null && !partitions.isEmpty()) {
150+
for (InternalPartition partition : partitions.values()) {
151+
result += (partition.getOffsetMax() - partition.getOffsetMin());
152+
}
153+
}
154+
}
155+
return result;
156+
}
146157
}

contract-typespec/api/topics.tsp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ enum TopicColumnsToSort {
156156
TOTAL_PARTITIONS,
157157
REPLICATION_FACTOR,
158158
SIZE,
159+
MESSAGES_COUNT
159160
}
160161

161162
model Topic {
@@ -170,6 +171,7 @@ model Topic {
170171
bytesInPerSec?: float64;
171172
bytesOutPerSec?: float64;
172173
underReplicatedPartitions?: int32;
174+
messagesCount?: int64 | null;
173175
cleanUpPolicy?: CleanUpPolicy;
174176
partitions?: Partition[];
175177
}

contract/src/main/resources/swagger/kafbat-ui-api.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2692,6 +2692,7 @@ components:
26922692
- TOTAL_PARTITIONS
26932693
- REPLICATION_FACTOR
26942694
- SIZE
2695+
- MESSAGES_COUNT
26952696

26962697
SchemaColumnsToSort:
26972698
type: string
@@ -2747,6 +2748,10 @@ components:
27472748
type: array
27482749
items:
27492750
$ref: "#/components/schemas/Partition"
2751+
messagesCount:
2752+
type: integer
2753+
format: int64
2754+
nullable: true
27502755
required:
27512756
- name
27522757

0 commit comments

Comments
 (0)