rreddy-22 commented on code in PR #15150:
URL: https://github.com/apache/kafka/pull/15150#discussion_r1482167679


##########
core/src/main/scala/kafka/admin/ConsumerGroupCommand.scala:
##########
@@ -189,16 +199,68 @@ object ConsumerGroupCommand extends Logging {
     }
 
     def listGroups(): Unit = {
-      if (opts.options.has(opts.stateOpt)) {
-        val stateValue = opts.options.valueOf(opts.stateOpt)
-        val states = if (stateValue == null || stateValue.isEmpty)
-          Set[ConsumerGroupState]()
-        else
-          consumerGroupStatesFromString(stateValue)
-        val listings = listConsumerGroupsWithState(states)
-        printGroupStates(listings.map(e => (e.groupId, e.state.get.toString)))
-      } else
+      val includeType = opts.options.has(opts.typeOpt)
+      val includeState = opts.options.has(opts.stateOpt)
+
+      val groupInfoMap = mutable.Map[String, (String, String)]()
+
+      if (includeType || includeState) {
+        val types = getTypeValues()
+        val states = getStateValues()
+        val listings = {
+          listConsumerGroupsWithFilters(types, states)
+        }
+
+        listings.foreach { listing =>
+          val groupId = listing.groupId
+          val groupType = 
listing.groupType().orElse(GroupType.UNKNOWN).toString
+          val state = 
listing.state().orElse(ConsumerGroupState.UNKNOWN).toString
+          groupInfoMap.update(groupId, (groupType, state))
+        }
+
+        printGroupInfo(groupInfoMap, includeType, includeState)
+
+      } else {
         listConsumerGroups().foreach(println(_))
+      }
+    }
+
+    private def getStateValues(): Set[ConsumerGroupState] = {
+      val stateValue = opts.options.valueOf(opts.stateOpt)
+      if (stateValue == null || stateValue.isEmpty)
+        Set[ConsumerGroupState]()
+      else
+        consumerGroupStatesFromString(stateValue)
+    }
+
+    private def getTypeValues(): Set[GroupType] = {
+      val typeValue = opts.options.valueOf(opts.typeOpt)
+      if (typeValue == null || typeValue.isEmpty)
+        Set[GroupType]()
+      else
+        consumerGroupTypesFromString(typeValue)
+    }
+
+    private def printGroupInfo(groupsAndInfo: Map[String, (String, String)], 
includeType: Boolean, includeState: Boolean): Unit = {
+      val maxGroupLen: Int = groupsAndInfo.keys.foldLeft(15)((maxLen, groupId) 
=> Math.max(maxLen, groupId.length))
+      var header = "GROUP"
+      var format = s"%-${maxGroupLen}s"
+
+      if (includeType) {
+        header += " TYPE"
+        format += " %-20s"
+      }
+      if (includeState) {
+        header += " STATE"
+        format += " %-20s"
+      }
+
+      println(format.format(ArraySeq.unsafeWrapArray(header.split(" ")): _*))
+
+      groupsAndInfo.foreach { case (groupId, (groupType, state)) =>
+        val info = List(groupId) ++ (if (includeType) List(groupType) else 
List()) ++ (if (includeState) List(state) else List())

Review Comment:
   oo makes sense! Thanks!



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: jira-unsubscr...@kafka.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to