package com.DemandCube.KafkaSphere.kafka;


import kafka.javaapi.TopicMetadataRequest;
import kafka.javaapi.consumer.SimpleConsumer;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;


public class BrokerMetaData {
  public static HashMap<String, HashMap<String, String>> brokerDetails = new HashMap<>();

  public static void main(String[] args){
    List<kafka.javaapi.TopicMetadata> data = MetaDataDump();
    HashMap<String, String> topicDetails = new HashMap<>();
    String topic;

    for (kafka.javaapi.TopicMetadata item : data){
      topic = item.topic();
      for (kafka.javaapi.PartitionMetadata part: item.partitionsMetadata()){
        String replicas = "";
        String isr = "";
        for (kafka.cluster.Broker replica: part.replicas()){
          replicas += " " + replica.host();
        }
        for (kafka.cluster.Broker replica: part.isr()){
          isr += " " + replica.host();
        }
        topicDetails.put("partition", String.valueOf(part.partitionId()) );
        topicDetails.put("leader", part.leader().host() );
        topicDetails.put("replicas", "[" + replicas + "]");
        topicDetails.put("isr", "[" + isr + "]");
      }

      brokerDetails.put(topic, topicDetails);
      System.out.println(brokerDetails);
    }
  }

  public static List<kafka.javaapi.TopicMetadata> MetaDataDump(){
    kafka.javaapi.consumer.SimpleConsumer consumer = new SimpleConsumer("0.0.0.0", 9092, 100000, 64 * 1024, "metadata");
    List<String> topics = new ArrayList<String>();
    TopicMetadataRequest req = new TopicMetadataRequest(topics);
    kafka.javaapi.TopicMetadataResponse res = consumer.send(req);
    return res.topicsMetadata();
  };
}

@@ -236,16 +236,17 @@
 aData %7B%0A
+%0A
   public
@@ -298,27 +298,25 @@
 g%3E%3E 
-broker
+get
 Details
- = new
+()%7B%0A   
  Has
@@ -324,55 +324,73 @@
 Map%3C
-%3E();%0A%0A  public static void main(String%5B%5D args)%7B
+String, HashMap%3CString, String%3E%3E brokerDetails = new HashMap%3C%3E();
 %0A   
@@ -1265,29 +1265,21 @@
     
-  System.out.println(
+%7D%0A    return 
 brok
@@ -1287,24 +1287,17 @@
 rDetails
-);%0A    %7D
+;
 %0A  %7D%0A%0A  

refactor - changed main to getDetails