Class: Karafka::Web::Ui::Models::Metrics::Charts::Aggregated

Inherits:
Lib::HashProxy
  • Object
show all
Defined in:
lib/karafka/web/ui/models/metrics/charts/aggregated.rb

Overview

Model for formatting aggregated metrics data for charts

Instance Method Summary collapse

Methods inherited from Lib::HashProxy

#to_h

Constructor Details

#initialize(aggregated, period) ⇒ Aggregated

Returns a new instance of Aggregated.

Parameters:

  • aggregated (Hash)

    all aggregated for all periods

  • period (Symbol)

    period that we are interested in



14
15
16
17
# File 'lib/karafka/web/ui/models/metrics/charts/aggregated.rb', line 14

def initialize(aggregated, period)
  @period = period
  @data = aggregated.to_h.fetch(period)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments) ⇒ Object

Handles delegation to fetch appropriate historical metrics based on their name

Parameters:

  • method_name (String)
  • arguments (Array)

    missing method call arguments



70
71
72
73
74
75
76
# File 'lib/karafka/web/ui/models/metrics/charts/aggregated.rb', line 70

def method_missing(method_name, *arguments)
  if @data.last.last.key?(method_name.to_sym)
    @data.map { |a| [a.first, a.last[method_name]] }
  else
    super
  end
end

Instance Method Details

#active_listenersArray<Array<Symbol, Integer>>

Returns active listeners statistics.

Returns:

  • (Array<Array<Symbol, Integer>>)

    active listeners statistics



47
48
49
50
51
# File 'lib/karafka/web/ui/models/metrics/charts/aggregated.rb', line 47

def active_listeners
  listeners.map do |listener|
    [listener[0], listener[1].fetch(:active)]
  end
end

#data_transfersString

Returns JSON with bytes sent and bytes received metrics.

Returns:

  • (String)

    JSON with bytes sent and bytes received metrics



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/karafka/web/ui/models/metrics/charts/aggregated.rb', line 20

def data_transfers
  scale_factor = Processing::TimeSeriesTracker::TIME_RANGES
                 .fetch(@period)
                 .fetch(:resolution)
                 .then { |factor| factor / 1_024.to_f }

  received = bytes_received.map do |element|
    [element[0], element[1] * scale_factor]
  end

  sent = bytes_sent.map do |element|
    [element[0], element[1] * scale_factor]
  end

  { received: received, sent: sent }.to_json
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Parameters:

  • method_name (String)
  • include_private (Boolean) (defaults to: false)

Returns:

  • (Boolean)


62
63
64
# File 'lib/karafka/web/ui/models/metrics/charts/aggregated.rb', line 62

def respond_to_missing?(method_name, include_private = false)
  @data.last.last.key?(method_name.to_sym) || super
end

#standby_listenersArray<Array<Symbol, Integer>>

Returns standby listeners statistics.

Returns:

  • (Array<Array<Symbol, Integer>>)

    standby listeners statistics



54
55
56
57
58
# File 'lib/karafka/web/ui/models/metrics/charts/aggregated.rb', line 54

def standby_listeners
  listeners.map do |listener|
    [listener[0], listener[1].fetch(:standby)]
  end
end

#with(*args) ⇒ String

Returns JSON with data about all the charts we were interested in.

Parameters:

  • args (Array<String>)

    names of aggregated we want to show

Returns:

  • (String)

    JSON with data about all the charts we were interested in



39
40
41
42
43
44
# File 'lib/karafka/web/ui/models/metrics/charts/aggregated.rb', line 39

def with(*args)
  args
    .map { |name| [name.to_sym, public_send(name)] }
    .to_h
    .to_json
end