Module: Karafka::Web::Ui::Helpers::TopicsHelper

Included in:
Base
Defined in:
lib/karafka/web/ui/helpers/topics_helper.rb

Overview

Helper module for formatting Kafka topic and partition information in various contexts within the Karafka Web UI.

This module provides consistent formatting for topic-partition assignments across different display contexts like inline text, labels, and identifiers.

Constant Summary collapse

DEFAULT_LIMIT =

Default limit for displaying partitions before truncation

5

Instance Method Summary collapse

Instance Method Details

#topics_assignment_label(topic, partitions, limit: DEFAULT_LIMIT) ⇒ String

Formats topic and partitions for human-readable labels and headers.

This method provides more descriptive formatting suitable for page titles, section headers, and other contexts where additional context is helpful.

Examples:

Consecutive partitions with count

topics_assignment_label("user-events", [0, 1, 2, 3])
# => "user-events-[0-3] (4 partitions total)"

Truncated with remaining count

topics_assignment_label("user-events", [0, 1, 2, 3, 4, 5], limit: 3)
# => "user-events-[0,1,2] (+3 more)"

Non-consecutive partitions

topics_assignment_label("user-events", [0, 2, 4])
# => "user-events-[0,2,4]"

Parameters:

  • topic (String)

    the Kafka topic name

  • partitions (Array<Integer>, Integer)

    partition number(s) to format

  • limit (Integer) (defaults to: DEFAULT_LIMIT)

    maximum number of partitions to display before truncation

Returns:

  • (String)

    formatted topic-partition label with additional context



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/karafka/web/ui/helpers/topics_helper.rb', line 90

def topics_assignment_label(topic, partitions, limit: DEFAULT_LIMIT)
  partitions = Array(partitions)

  sorted_partitions = partitions.map(&:to_i).sort
  if topics_consecutive?(sorted_partitions)
    "#{topic}-[#{sorted_partitions.first}-#{sorted_partitions.last}] " \
      "(#{partitions.size} partitions total)"
  elsif sorted_partitions.size > limit
    displayed = sorted_partitions.first(limit)
    remaining = sorted_partitions.size - limit
    "#{topic}-[#{displayed.join(',')}] (+#{remaining} more)"
  else
    "#{topic}-[#{sorted_partitions.join(',')}]"
  end
end

#topics_assignment_text(topic, partitions, limit: DEFAULT_LIMIT) ⇒ String

Formats topic and partitions for inline text display in views.

This method is optimized for compact display in assignments, logs, and other inline contexts where space is limited.

Examples:

Single partition

topics_assignment_text("user-events", 0)
# => "user-events-[0]"

Multiple consecutive partitions

topics_assignment_text("user-events", [0, 1, 2, 3])
# => "user-events-[0-3]"

Multiple non-consecutive partitions

topics_assignment_text("user-events", [0, 2, 4])
# => "user-events-[0,2,4]"

Truncated partitions list

topics_assignment_text("user-events", [0, 1, 2, 3, 4, 5, 6], limit: 3)
# => "user-events-[0,1,2...]"

Empty partitions

topics_assignment_text("user-events", [])
# => "user-events"

Parameters:

  • topic (String)

    the Kafka topic name

  • partitions (Array<Integer>, Integer)

    partition number(s) to format

  • limit (Integer) (defaults to: DEFAULT_LIMIT)

    maximum number of partitions to display before truncation

Returns:

  • (String)

    formatted topic-partition string



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/karafka/web/ui/helpers/topics_helper.rb', line 47

def topics_assignment_text(topic, partitions, limit: DEFAULT_LIMIT)
  partitions = Array(partitions)

  if partitions.empty?
    topic
  elsif partitions.size == 1
    "#{topic}-[#{partitions.first}]"
  else
    sorted_partitions = partitions.map(&:to_i).sort
    # Check for consecutive first (best representation)
    if topics_consecutive?(sorted_partitions) && sorted_partitions.size > 2
      "#{topic}-[#{sorted_partitions.first}-#{sorted_partitions.last}]"
    # Apply limit if specified and partitions exceed it
    elsif limit && sorted_partitions.size > limit
      displayed_partitions = sorted_partitions.first(limit)
      "#{topic}-[#{displayed_partitions.join(',')}...]"
    else
      "#{topic}-[#{sorted_partitions.join(',')}]"
    end
  end
end

#topics_partition_identifier(topic, partition) ⇒ String

Creates a specific identifier for topic-partition combinations.

This method generates consistent identifiers used in metrics collection, cache keys, and other contexts requiring unique topic-partition identification.

Examples:

Basic identifier

topics_partition_identifier("user-events", 0)
# => "user-events-0"

Used for cache keys

cache_key = topics_partition_identifier("orders", 3)
Rails.cache.fetch(cache_key) { expensive_operation }

Parameters:

  • topic (String)

    the Kafka topic name

  • partition (Integer)

    the partition number

Returns:

  • (String)

    formatted topic-partition identifier



122
123
124
# File 'lib/karafka/web/ui/helpers/topics_helper.rb', line 122

def topics_partition_identifier(topic, partition)
  "#{topic}-#{partition}"
end