Module: PgBouncerHero::ApplicationHelper

Defined in:
app/helpers/pg_bouncer_hero/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#alert_class_for(flash_type) ⇒ Object



7
8
9
10
11
12
13
14
# File 'app/helpers/pg_bouncer_hero/application_helper.rb', line 7

def alert_class_for(flash_type)
  case flash_type
  when "success" then "success"
  when "error" then "error"
  when "notice" then "info"
  when "warning" then "warning"
  end
end

#alert_style_for(class_name) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'app/helpers/pg_bouncer_hero/application_helper.rb', line 16

def alert_style_for(class_name)
  case class_name
  when "success" then "bg-green-50 text-green-800 border border-green-200"
  when "error" then "bg-red-50 text-red-800 border border-red-200"
  when "info" then "bg-blue-50 text-blue-800 border border-blue-200"
  when "warning" then "bg-yellow-50 text-yellow-800 border border-yellow-200"
  else "bg-gray-50 text-gray-800 border border-gray-200"
  end
end

#fleet_health(summary) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/helpers/pg_bouncer_hero/application_helper.rb', line 35

def fleet_health(summary)
  return { status: "offline", severity: 3, waiting_clients: 0, max_utilization: 0 } unless summary

  pools = summary_details(summary, :pools_details)
  databases = summary_details(summary, :databases_details)
  waiting_clients = pools.sum { |pool| pool["cl_waiting"].to_i }
  max_utilization = databases.filter_map do |database|
    maximum = database["max_connections"].to_i
    (database["current_connections"].to_f / maximum * 100).round if maximum.positive?
  end.max.to_i

  status, severity = if waiting_clients.positive?
    [ "waiting", 2 ]
  elsif max_utilization >= 80
    [ "high_utilization", 1 ]
  else
    [ "healthy", 0 ]
  end

  { status: status, severity: severity, waiting_clients: waiting_clients, max_utilization: max_utilization }
end

#humanize_ms(millis) ⇒ Object



26
27
28
29
30
31
32
33
# File 'app/helpers/pg_bouncer_hero/application_helper.rb', line 26

def humanize_ms(millis)
  [ [ 1000, :ms ], [ 60, :s ], [ 60, :min ], [ 24, :h ], [ 1000, :d ] ].map { |count, name|
    if millis > 0
      millis, n = millis.divmod(count)
      "#{n.to_i} #{name}"
    end
  }.compact.reverse.join(" ")
end

#is_active(action_name) ⇒ Object



3
4
5
# File 'app/helpers/pg_bouncer_hero/application_helper.rb', line 3

def is_active(action_name)
  params[:action] == action_name ? "active" : nil
end

#monitoring_panel(&block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/helpers/pg_bouncer_hero/application_helper.rb', line 57

def monitoring_panel(&block)
  frame_id = "monitoring_#{params[:action]}"

  (:div,
    data: {
      controller: "polling",
      polling_interval_value: 60_000,
      action: "turbo:frame-load->polling#refreshed"
    }) do
    safe_join([
      monitoring_refresh_controls,
      turbo_frame_tag(frame_id, data: { polling_refresh_url: request.path }, &block)
    ])
  end
end