Class: Binocs::TUI::Endpoints

Inherits:
Window
  • Object
show all
Defined in:
lib/binocs/tui/endpoints.rb

Instance Attribute Summary collapse

Attributes inherited from Window

#height, #left, #top, #width, #win

Instance Method Summary collapse

Methods inherited from Window

#clear, #close, #copy_to_clipboard, #draw_box, #noutrefresh, #refresh, #resize, #write, #write_centered

Constructor Details

#initialize(height:, width:, top:, left:) ⇒ Endpoints

Returns a new instance of Endpoints.



8
9
10
11
12
13
# File 'lib/binocs/tui/endpoints.rb', line 8

def initialize(height:, width:, top:, left:)
  super
  @endpoints = []
  @selected_index = 0
  @scroll_offset = 0
end

Instance Attribute Details

#selected_indexObject (readonly)

Returns the value of attribute selected_index.



6
7
8
# File 'lib/binocs/tui/endpoints.rb', line 6

def selected_index
  @selected_index
end

Instance Method Details

#content_as_textObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/binocs/tui/endpoints.rb', line 85

def content_as_text
  lines = []
  lines << "METHOD  PATH#{' ' * 40}  HITS    AVG        MAX        ERR   LAST HIT"
  lines << "-" * 100
  @endpoints.each do |ep|
    total_errors = ep.error_count.to_i + ep.client_error_count.to_i
    last = ep.respond_to?(:last_hit_at) ? time_ago(ep.last_hit_at) : "-"
    lines << "%-7s %-50s %5d  %9s  %9s  %4s  %s" % [
      ep.method,
      ep.path,
      ep.hit_count,
      format_ms(ep.avg_duration.to_f),
      format_ms(ep.max_duration.to_f),
      total_errors > 0 ? total_errors.to_s : "-",
      last
    ]
  end
  lines.join("\n")
end

#drawObject



76
77
78
79
80
81
82
83
# File 'lib/binocs/tui/endpoints.rb', line 76

def draw
  clear
  draw_box("Endpoints (#{@endpoints.length})")
  draw_header
  draw_separator
  draw_endpoints
  draw_status_bar
end

#go_to_bottomObject



61
62
63
64
# File 'lib/binocs/tui/endpoints.rb', line 61

def go_to_bottom
  @selected_index = [@endpoints.length - 1, 0].max
  adjust_scroll
end

#go_to_topObject



56
57
58
59
# File 'lib/binocs/tui/endpoints.rb', line 56

def go_to_top
  @selected_index = 0
  @scroll_offset = 0
end

#load_dataObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/binocs/tui/endpoints.rb', line 15

def load_data
  rows = Binocs::Request
    .group(:method, :path)
    .select(
      "method",
      "path",
      "COUNT(*) as hit_count",
      "AVG(duration_ms) as avg_duration",
      "MAX(duration_ms) as max_duration",
      "MIN(duration_ms) as min_duration",
      "MAX(created_at) as last_hit_at",
      "SUM(CASE WHEN status_code >= 500 THEN 1 ELSE 0 END) as error_count",
      "SUM(CASE WHEN status_code >= 400 AND status_code < 500 THEN 1 ELSE 0 END) as client_error_count"
    )
    .order(Arel.sql("MAX(created_at) DESC"))
    .to_a

  @endpoints = rows
  @selected_index = [@selected_index, @endpoints.length - 1].min
  @selected_index = 0 if @selected_index < 0
  adjust_scroll
end

#move_downObject



42
43
44
45
46
47
# File 'lib/binocs/tui/endpoints.rb', line 42

def move_down
  if @selected_index < @endpoints.length - 1
    @selected_index += 1
    adjust_scroll
  end
end

#move_upObject



49
50
51
52
53
54
# File 'lib/binocs/tui/endpoints.rb', line 49

def move_up
  if @selected_index > 0
    @selected_index -= 1
    adjust_scroll
  end
end

#page_downObject



66
67
68
69
# File 'lib/binocs/tui/endpoints.rb', line 66

def page_down
  @selected_index = [@selected_index + visible_rows, @endpoints.length - 1].min
  adjust_scroll
end

#page_upObject



71
72
73
74
# File 'lib/binocs/tui/endpoints.rb', line 71

def page_up
  @selected_index = [@selected_index - visible_rows, 0].max
  adjust_scroll
end

#selected_endpointObject



38
39
40
# File 'lib/binocs/tui/endpoints.rb', line 38

def selected_endpoint
  @endpoints[@selected_index]
end