def describe_topic(state: State, topic_name: str, consumers: bool, output_format: str): """Describe a topic. Returns information on a given topic and its partitions, with the option of including all consumer groups that read from the topic. """ topic = state.cluster.topic_controller.get_cluster_topic(topic_name) output_dict = { "topic": topic_name, "partitions": [partition.as_dict() for partition in topic.partitions], "config": topic.config, } if consumers: consumergroup_controller = ConsumerGroupController(state.cluster) groups = consumergroup_controller.list_consumer_groups() consumergroups = [ group_name for group_name in groups if topic_name in consumergroup_controller.get_consumergroup(group_name).topics ] output_dict["consumergroups"] = consumergroups click.echo(format_output(output_dict, output_format))
def describe_consumergroup(state: State, consumer_id: str, all_partitions: bool, output_format: str): """Return information on group coordinator, offsets, watermarks, lag, and various metadata for consumer group CONSUMER_GROUP.""" consumer_group = ConsumerGroupController(state.cluster).get_consumergroup(consumer_id) consumer_group_desc = consumer_group.describe(verbose=all_partitions) click.echo(format_output(consumer_group_desc, output_format))
def get_topics(state: State, prefix: str, hide_internal: bool, output_format: str): """List all topics.""" topics = state.cluster.topic_controller.list_topics( search_string=prefix, get_topic_objects=False, hide_internal=hide_internal ) topic_names = [topic.name for topic in topics] click.echo(format_output(topic_names, output_format))
def get_brokers(state: State, output_format: str): """List all brokers. Return the broker id's and socket addresses of all the brokers in the kafka cluster defined in the current context. """ brokers = Broker.get_all(state.cluster) broker_ids_and_hosts = [f"{broker.broker_id}: {broker.host}:{broker.port}" for broker in brokers] click.echo(format_output(broker_ids_and_hosts, output_format))
def get_watermarks(state: State, topic_name: str, output_format: str): """Returns the high and low watermarks for <topic_name>, or if not specified, all topics.""" # TODO: Gathering of all watermarks takes super long topics = state.cluster.topic_controller.list_topics(search_string=topic_name) watermarks = {topic.name: max(v for v in topic.watermarks.values()) for topic in topics} click.echo(format_output(watermarks, output_format))
def describe_broker(state: State, broker: str, output_format: str): """Return configuration options for broker BROKER. BROKER can be given with broker id (integer), the host name (if hostname is unique), or socket address ('hostname:port')""" if broker.isdigit(): broker = Broker.from_id(state.cluster, broker).describe() elif ":" not in broker: broker = Broker.from_host(state.cluster, broker).describe() else: try: host, port = broker.split(":") broker = Broker.from_host_and_port(state.cluster, host, int(port)).describe() except ValueError: raise ValidationException("BROKER must either be the broker id, the hostname, or in the form 'host:port'") click.echo(format_output(broker, output_format))
def get_consumergroups(state: State, output_format: str): """List all consumer groups.""" groups = ConsumerGroupController(state.cluster).list_consumer_groups() click.echo(format_output(groups, output_format))
def test_format_output(output_format: str, loader: Callable): dumped_dict = format_output(TUPLE_BYTES_DICT, output_format) loaded_dict = loader(dumped_dict) check_loaded_dict(TUPLE_BYTES_DICT, loaded_dict) assert loaded_dict["tuple"] == ["a", "b"] assert loaded_dict["bytes"] == "4"
def test_format_output_default(): pretty_output = format_output(TUPLE_BYTES_DICT, None) for key in TUPLE_BYTES_DICT: assert key in pretty_output