use of io.micronaut.http.sse.Event in project akhq by tchiotludo.
the class RecordRepository method tail.
public Flowable<Event<TailEvent>> tail(String clusterId, TailOptions options) {
return Flowable.generate(() -> {
KafkaConsumer<byte[], byte[]> consumer = this.kafkaModule.getConsumer(options.clusterId);
Map<String, Topic> topics = topicRepository.findByName(clusterId, options.topics).stream().collect(Collectors.toMap(Topic::getName, Function.identity()));
consumer.assign(topics.values().stream().flatMap(topic -> topic.getPartitions().stream().map(partition -> new TopicPartition(topic.getName(), partition.getId()))).collect(Collectors.toList()));
if (options.getAfter() != null) {
options.getAfter().forEach(s -> {
String[] split = s.split(",");
consumer.seek(new TopicPartition(split[0], Integer.parseInt(split[1])), Long.parseLong(split[2]));
});
}
return new TailState(consumer, new TailEvent(), topics);
}, (state, subscriber) -> {
ConsumerRecords<byte[], byte[]> records = this.poll(state.getConsumer());
TailEvent tailEvent = state.getTailEvent();
List<Record> list = new ArrayList<>();
for (ConsumerRecord<byte[], byte[]> record : records) {
tailEvent.offsets.put(ImmutableMap.of(record.topic(), record.partition()), record.offset());
Record current = newRecord(record, options, state.getTopics().get(record.topic()));
if (searchFilter(options, current)) {
list.add(current);
log.trace("Record [topic: {}] [partition: {}] [offset: {}] [key: {}]", record.topic(), record.partition(), record.offset(), record.key());
}
}
tailEvent.records = list;
subscriber.onNext(Event.of(tailEvent).name("tailBody"));
state.tailEvent = tailEvent;
return state;
});
}
Aggregations