Class: PatientHttp::Sidekiq::WebUI

Inherits:
Object
  • Object
show all
Defined in:
lib/patient_http/sidekiq/web_ui.rb

Overview

Web UI extension for Sidekiq Adds an "Async HTTP" tab to the main Sidekiq dashboard Works with Sidekiq 7.3+ and 8.0+

Constant Summary collapse

ROOT =
File.join(__dir__, "web_ui")
VIEWS =
File.join(ROOT, "views")
INFLIGHT_LIMIT =

Number of in-flight requests listed on the dashboard. The oldest are listed, because those are the ones worth looking at.

50

Class Method Summary collapse

Class Method Details

.processor_rows(stats_by_processor, capacity_by_processor) ⇒ Array<Hash>

Build the per-processor rows for the dashboard by combining the cumulative counters with the live capacity each processor reports.

Statistics are not recorded per processor when only one is configured, because they would duplicate the overall totals, so the breakdown is empty in that case.

Parameters:

  • stats_by_processor (Hash, nil)

    cumulative counters by processor name

  • capacity_by_processor (Hash)

    inflight and capacity by processor name

Returns:

  • (Array<Hash>)

    one row per processor, sorted by name



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/patient_http/sidekiq/web_ui.rb', line 45

def processor_rows(stats_by_processor, capacity_by_processor)
  stats_by_processor ||= {}
  return [] if stats_by_processor.empty? && capacity_by_processor.size < 2

  names = (stats_by_processor.keys | capacity_by_processor.keys).sort
  names.map do |name|
    counts = stats_by_processor[name] || {}
    capacity = capacity_by_processor[name] || {}
    requests = counts["requests"].to_i
    max_capacity = capacity[:max_capacity].to_i
    inflight = capacity[:inflight].to_i

    {
      name: name,
      inflight: inflight,
      peak_inflight: counts["max_inflight"].to_i,
      max_capacity: max_capacity,
      utilization: (max_capacity > 0) ? (inflight.to_f / max_capacity * 100).round(1) : 0,
      requests: requests,
      errors: counts["errors"].to_i,
      max_capacity_exceeded: counts["max_capacity_exceeded"].to_i,
      avg_duration: (requests > 0) ? (counts["duration"].to_f / requests).round(3) : 0.0
    }
  end
end

.registered(app) ⇒ Object

This method is called by Sidekiq::Web when registering the extension



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
# File 'lib/patient_http/sidekiq/web_ui.rb', line 72

def registered(app)
  # GET route for the main PatientHttp dashboard page
  app.get "/patient-http" do
    stats = PatientHttp::Sidekiq::Stats.new

    # Get process-level inflight and capacity data from TaskMonitor
    processes = PatientHttp::Sidekiq::TaskMonitor.inflight_counts_by_process

    # Get totals and calculate derived values
    totals = stats.get_totals
    total_requests = totals["requests"] || 0
    avg_duration = (total_requests > 0) ? ((totals["duration"] || 0).to_f / total_requests).round(3) : 0.0

    # Capacity metrics from TaskMonitor
    max_capacity = processes.values.sum { |data| data[:max_capacity] }
    current_inflight = processes.values.sum { |data| data[:inflight] }
    utilization = (max_capacity > 0) ? (current_inflight.to_f / max_capacity * 100).round(1) : 0

    # Per-processor breakdown, combining the cumulative counters with
    # the capacity each processor reports.
    processors = PatientHttp::Sidekiq::WebUI.processor_rows(
      totals["processors"],
      PatientHttp::Sidekiq::TaskMonitor.inflight_counts_by_processor(processes)
    )

    # The requests that have been in flight the longest.
    inflight = PatientHttp::Sidekiq::TaskMonitor.inflight_details(limit: INFLIGHT_LIMIT)

    erb(PatientHttp::Sidekiq::WebUI.template, locals: {
      totals: totals,
      total_requests: total_requests,
      avg_duration: avg_duration,
      max_capacity: max_capacity,
      current_inflight: current_inflight,
      utilization: utilization,
      processes: processes,
      processors: processors,
      inflight: inflight
    })
  end

  # POST route for clearing statistics
  app.post "/patient-http/clear" do
    PatientHttp::Sidekiq::Stats.new.reset!
    redirect "#{root_path}patient-http"
  end
end

.templateObject

Sidekiq resolves symbol view names to different file names depending on the version (*.erb before 8.1, *.html.erb from 8.1 on), so the template is rendered from a string instead.



31
32
33
# File 'lib/patient_http/sidekiq/web_ui.rb', line 31

def template
  @template ||= File.read(File.join(VIEWS, "patient_http.html.erb"))
end