소스 검색

Cleanup code

German Osin 5 년 전
부모
커밋
21ac087f0b

+ 0 - 1
kafka-ui-api/src/main/java/com/provectus/kafka/ui/cluster/model/InternalTopic.java

@@ -26,5 +26,4 @@ public class InternalTopic {
     private final long segmentSize;
     private final int segmentCount;
     private final Map<TopicPartition, Long> partitionSegmentSize;
-    private final List<TopicPartitionDto> offsets;
 }

+ 5 - 4
kafka-ui-api/src/main/java/com/provectus/kafka/ui/cluster/service/ClusterService.java

@@ -53,12 +53,13 @@ public class ClusterService {
                 ).orElse(Collections.emptyList());
     }
 
-    public Optional<TopicDetails> getTopicDetails(String name, String topicName) { ;
+    public Optional<TopicDetails> getTopicDetails(String name, String topicName) {
         return clustersStorage.getClusterByName(name)
                 .map(c -> {
-                 var topic = c.getTopics().get(topicName);
-                 var internalTopic = kafkaService.fillOffsets(topic, c);
-                 return clusterMapper.toTopicDetails(internalTopic);
+                     var topic = c.getTopics().get(topicName);
+                     return clusterMapper
+                             .toTopicDetails(topic)
+                             .partitions(kafkaService.partitionDtoList(topic, c));
                 });
     }
                                                                            

+ 12 - 16
kafka-ui-api/src/main/java/com/provectus/kafka/ui/kafka/KafkaService.java

@@ -348,27 +348,23 @@ public class KafkaService {
             );
     }
 
-    public InternalTopic fillOffsets (InternalTopic topic, KafkaCluster cluster) {
+    public List<TopicPartitionDto> partitionDtoList (InternalTopic topic, KafkaCluster cluster) {
         var topicPartitions = topic.getPartitions().stream().map(t -> new TopicPartition(topic.getName(), t.getPartition())).collect(Collectors.toList());
-        return topic.toBuilder().offsets(getTopicPartitionOffset(cluster, topicPartitions)).build();
+        return getTopicPartitionOffset(cluster, topicPartitions);
     }
 
     private List<TopicPartitionDto> getTopicPartitionOffset(KafkaCluster c, List<TopicPartition> topicPartitions )  {
         try (var consumer = createConsumer(c)) {
-            var offset = consumer.beginningOffsets(topicPartitions).entrySet().stream()
-                    .map(e -> {
-                        var tempResult = new TopicPartitionDto();
-                        tempResult.setTopic(e.getKey().topic());
-                        tempResult.setPartition(e.getKey().partition());
-                        tempResult.setOffsetMin(e.getValue().intValue());
-                        return tempResult;
-                    }).map(o -> consumer.endOffsets(topicPartitions).entrySet()
-                            .stream().filter(max -> o.getTopic().equals(max.getKey().topic()))
-                            .map(max -> {
-                                o.setOffsetMax(max.getValue().intValue());
-                                return o;
-                            }).findFirst().orElse(o)).collect(Collectors.toList());
-            return offset;
+            final Map<TopicPartition, Long> earliest = consumer.beginningOffsets(topicPartitions);
+            final Map<TopicPartition, Long> latest = consumer.endOffsets(topicPartitions);
+
+            return topicPartitions.stream()
+                    .map( tp -> new TopicPartitionDto()
+                            .topic(tp.topic())
+                            .partition(tp.partition())
+                            .offsetMin(Optional.ofNullable(earliest.get(tp)).orElse(0L))
+                            .offsetMax(Optional.ofNullable(latest.get(tp)).orElse(0L))
+                    ).collect(Collectors.toList());
         } catch (Exception e) {
             return Collections.emptyList();
         }

+ 3 - 1
kafka-ui-contract/src/main/resources/swagger/kafka-ui-api.yaml

@@ -383,7 +383,7 @@ components:
     TopicDetails:
       type: object
       properties:
-        offsets:
+        partitions:
           type: array
           items:
             $ref: "#/components/schemas/TopicPartitionDto"
@@ -492,8 +492,10 @@ components:
           type: integer
         offsetMax:
           type: integer
+          format: int64
         offsetMin:
           type: integer
+          format: int64
       required:
         - topic
         - partition