Class: Karafka::Web::Ui::Models::Topic

Inherits:
Lib::HashProxy show all
Defined in:
lib/karafka/web/ui/models/topic.rb

Overview

Single topic data representation model

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Lib::HashProxy

#initialize, #method_missing, #respond_to_missing?, #to_h

Constructor Details

This class inherits a constructor from Karafka::Web::Ui::Lib::HashProxy

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Karafka::Web::Ui::Lib::HashProxy

Class Method Details

.allArray<Broker>

Returns all topics in the cluster.

Returns:

  • (Array<Broker>)

    all topics in the cluster



11
12
13
14
15
# File 'lib/karafka/web/ui/models/topic.rb', line 11

def all
  ClusterInfo.fetch.topics.map do |topic|
    new(topic)
  end
end

.find(topic_name) ⇒ Topic

Finds requested topic

Parameters:

  • topic_name (String)

    name of the topic

Returns:

Raises:



22
23
24
25
26
27
28
# File 'lib/karafka/web/ui/models/topic.rb', line 22

def find(topic_name)
  found = all.find { |topic| topic.topic_name == topic_name }

  return found if found

  raise(::Karafka::Web::Errors::Ui::NotFoundError, topic_name)
end

Instance Method Details

#configsArray<Karafka::Admin::Configs::Config>

Returns all topic configs.

Returns:

  • (Array<Karafka::Admin::Configs::Config>)

    all topic configs



41
42
43
44
45
46
47
48
# File 'lib/karafka/web/ui/models/topic.rb', line 41

def configs
  @configs ||= ::Karafka::Admin::Configs.describe(
    ::Karafka::Admin::Configs::Resource.new(
      type: :topic,
      name: topic_name
    )
  ).first.configs.dup
end

#distribution(partitions) ⇒ Array<HashProxy, Array<HashProxy>>

Generates info about estimated messages distribution in partitions, allowing for inspection and detection of imbalances

Parameters:

  • partitions (Array<Integer>)

    partitions we’re interested in

Returns:

  • (Array<HashProxy, Array<HashProxy>>)

    array where first value contains aggregated statistics and then the second value is an array with per partition data



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/karafka/web/ui/models/topic.rb', line 57

def distribution(partitions)
  sum = 0.0
  avg = 0.0

  counts = partitions.map do |partition_id|
    offsets = Admin.read_watermark_offsets(topic_name, partition_id)
    count = offsets.last - offsets.first

    sum += count

    {
      count: count,
      partition_id: partition_id
    }
  end

  avg = sum / counts.size

  counts.each do |part_stats|
    count = part_stats[:count]

    part_stats[:share] = ((count / sum) * 100).round(2)
    part_stats[:diff] = ((count - avg) / avg) * 100
  end

  variance = counts
             .map { |part_stats| part_stats[:count] }
             .sum { |count| (count - avg)**2 } / counts.size

  std_dev = Math.sqrt(variance)
  std_dev_rel = ((std_dev / avg) * 100).round(2)

  [
    # round stdev since its message count
    Lib::HashProxy.new(std_dev: std_dev.round, std_dev_rel: std_dev_rel, sum: sum),
    counts.map { |part_stats| Lib::HashProxy.new(part_stats) }
  ]
end

#partitionsArray<Partition>

Returns All topic partitions data.

Returns:

  • (Array<Partition>)

    All topic partitions data



32
33
34
35
36
37
38
# File 'lib/karafka/web/ui/models/topic.rb', line 32

def partitions
  super.map do |partition_id, partition_hash|
    partition_hash[:partition_id] = partition_id

    Partition.new(partition_hash)
  end
end