Class: Sidekiq::TUI::Tabs::Metrics

Inherits:
BaseTab
  • Object
show all
Includes:
Filtering
Defined in:
lib/sidekiq/tui/tabs/metrics.rb

Constant Summary collapse

COLORS =
%i[light_blue light_cyan light_yellow light_red light_green white gray]

Constants included from Controls

Controls::GLOBAL, Controls::SHARED

Instance Attribute Summary

Attributes inherited from BaseTab

#data, #name

Instance Method Summary collapse

Methods included from Filtering

#append_to_filter, #current_filter, #filtering?, #remove_last_char_from_filter, #start_filtering, #stop_and_clear_filtering, #stop_filtering

Methods inherited from BaseTab

#each_selection, #error, #error=, #filtering?, #format_memory, #initialize, #navigate_row, #next_page, #number_with_delimiter, #prev_page, #refresh_data_for_stats, #render_stats_section, #render_table, #reset_data, #selected?, #t, #toggle_select

Methods included from Controls

#controls

Constructor Details

This class inherits a constructor from Sidekiq::TUI::BaseTab

Instance Method Details

#featuresObject



11
12
13
# File 'lib/sidekiq/tui/tabs/metrics.rb', line 11

def features
  %i[filterable]
end

#on_filter_changeObject



15
16
17
# File 'lib/sidekiq/tui/tabs/metrics.rb', line 15

def on_filter_change
  @data[:metrics_refresh] = nil
end

#refresh_dataObject



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sidekiq/tui/tabs/metrics.rb', line 23

def refresh_data
  refresh_data_for_stats

  # only need to refresh every 60 seconds
  if !@data[:metrics_refresh] || @data[:metrics_refresh] < Time.now
    q = Sidekiq::Metrics::Query.new
    query_result = q.top_jobs(class_filter: regexp, minutes: 60)
    @data[:metrics] = query_result
    @data[:metrics_refresh] = Time.now + 60
  end
end

#regexpObject



19
20
21
# File 'lib/sidekiq/tui/tabs/metrics.rb', line 19

def regexp
  filtering? ? Regexp.new(Regexp.escape(current_filter), Regexp::IGNORECASE) : nil
end

#render(tui, frame, area) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sidekiq/tui/tabs/metrics.rb', line 35

def render(tui, frame, area)
  chunks = tui.layout_split(
    area,
    direction: :vertical,
    constraints: [
      tui.constraint_length(4), # Stats
      tui.constraint_fill(1) # Chart
      # TOOD Table
    ]
  )

  render_stats_section(tui, frame, chunks[0])
  render_metrics_chart(tui, frame, chunks[1])
end

#render_metrics_chart(tui, frame, area) ⇒ Object

Run to generate metrics data:

cd myapp && bundle install
bundle exec rake seed_jobs
bundle exec sidekiq


54
55
56
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/sidekiq/tui/tabs/metrics.rb', line 54

def render_metrics_chart(tui, frame, area)
  y_max = 5
  csize = COLORS.size
  q = @data[:metrics]
  job_results = q.job_results.sort_by { |(kls, jr)| jr.totals["s"] }.reverse.first(COLORS.size)
  # visible_kls = job_results.first(5).map(&:first)
  # chart_data = {
  #   series: job_results.map { |(kls, jr)| [kls, jr.dig("series", "s")] }.to_h,
  #   marks: query_result.marks.map { |m| [m.bucket, m.label] },
  #   starts_at: query_result.starts_at.iso8601,
  #   ends_at: query_result.ends_at.iso8601,
  #   visibleKls: visible_kls,
  #   yLabel: 'TotalExecutionTime',
  #   units: 'seconds',
  #   markLabel: '*',
  # }

  datasets = job_results.map.with_index do |(kls, data), idx|
    # log kls, data, idx
    hrdata = data.dig("series", "s")
    tm = Time.now
    tmi = tm.to_i
    tm = Time.at(tmi - (tmi % 60)).utc
    data = Array.new(60) { |idx| idx }.map do |bucket_idx|
      jumpback = bucket_idx * 60
      value = hrdata[(tm - jumpback).iso8601] || 0
      y_max = value if value > y_max
      # we have 60 data points, newest data should be
      # at highest indexes so we have to rejigger the index
      # here
      [59 - bucket_idx, value]
    end
    # log data

    # log(data)
    tui.dataset(name: kls,
      data: data,
      style: tui.style(fg: COLORS[idx % csize]),
      marker: :dot,
      graph_type: :line)
  end

  num_labels = 5
  y_labels = (0...num_labels).map do |i|
    value = ((y_max * i) / (num_labels - 1)).round
    value.to_s
  end
  xlabels = [
    q.starts_at.iso8601[11..15],
    q.ends_at.iso8601[11..15]
  ]

  # beacon_pulse = (Time.now.to_i % 2 == 0) ? "●" : " "

  chart = tui.chart(
    datasets: datasets,
    x_axis: tui.axis(
      bounds: [0.0, 60.0],
      labels: xlabels,
      style: tui.style(fg: :white)
    ),
    y_axis: tui.axis(
      bounds: [0.0, y_max.to_f],
      labels: y_labels,
      style: tui.style(fg: :white)
    ),
    block: tui.block(
      title: t(name),
      borders: [:all]
    )
  )

  frame.render_widget(chart, area)
end