Class: Binocs::RequestsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/binocs/requests_controller.rb

Instance Method Summary collapse

Instance Method Details

#analyticsObject



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
# File 'app/controllers/binocs/requests_controller.rb', line 72

def analytics
  @total_requests = Request.count
  @today_requests = Request.today.count
  @avg_duration = Request.average_duration
  @error_rate = Request.error_rate

  # Hourly traffic for the last 24 hours
  @hourly_traffic = Request
    .where("created_at >= ?", 24.hours.ago)
    .group_by_hour
    .count

  # Status code distribution
  @status_distribution = Request.status_breakdown

  # Method distribution
  @method_distribution = Request.methods_breakdown

  # Top endpoints by volume
  @top_endpoints = Request
    .group(:method, :path)
    .select("method, path, COUNT(*) as hit_count, AVG(duration_ms) as avg_duration")
    .order(Arel.sql("COUNT(*) DESC"))
    .limit(15)

  # Slowest endpoints (avg)
  @slowest_endpoints = Request
    .group(:method, :path)
    .select("method, path, COUNT(*) as hit_count, AVG(duration_ms) as avg_duration, MAX(duration_ms) as max_duration")
    .having("COUNT(*) >= 2")
    .order(Arel.sql("AVG(duration_ms) DESC"))
    .limit(10)

  # Error hotspots
  @error_endpoints = Request
    .where("status_code >= 400")
    .group(:method, :path, :status_code)
    .select("method, path, status_code, COUNT(*) as error_count")
    .order(Arel.sql("COUNT(*) DESC"))
    .limit(10)

  # Response time distribution (buckets)
  @duration_buckets = build_duration_buckets
end

#clearObject



174
175
176
177
178
179
180
181
# File 'app/controllers/binocs/requests_controller.rb', line 174

def clear
  Request.delete_all

  respond_to do |format|
    format.html { redirect_to requests_path, notice: "All requests cleared." }
    format.turbo_stream { render turbo_stream: turbo_stream.replace("requests-list", partial: "binocs/requests/empty_list") }
  end
end

#destroyObject



33
34
35
36
37
38
39
40
# File 'app/controllers/binocs/requests_controller.rb', line 33

def destroy
  @request.destroy

  respond_to do |format|
    format.html { redirect_to requests_path, notice: "Request deleted." }
    format.turbo_stream { render turbo_stream: turbo_stream.remove(@request) }
  end
end

#heatmapObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/binocs/requests_controller.rb', line 48

def heatmap
  @endpoints = Request
    .group(:method, :path)
    .select(
      "method",
      "path",
      "COUNT(*) as hit_count",
      "AVG(duration_ms) as avg_duration",
      "MAX(duration_ms) as max_duration",
      "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("COUNT(*) DESC"))

  # Try to match endpoints to swagger spec paths for grouping
  @swagger_spec = Binocs::Swagger::Client.fetch_spec rescue nil
  @endpoint_groups = build_endpoint_groups(@endpoints, @swagger_spec)

  @total_requests = Request.count
  @max_hits = @endpoints.map(&:hit_count).max || 1
  @max_avg_duration = @endpoints.map { |e| e.avg_duration.to_f }.max || 1
  @view_mode = params[:view] || "frequency"
end

#indexObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/controllers/binocs/requests_controller.rb', line 7

def index
  @requests = Request.recent
  @requests = apply_filters(@requests)
  @requests = @requests.page(params[:page]).per(50) if @requests.respond_to?(:page)
  @requests = @requests.limit(50) unless @requests.respond_to?(:page)

  @stats = {
    total: Request.count,
    today: Request.today.count,
    avg_duration: Request.average_duration,
    error_rate: Request.error_rate
  }

  respond_to do |format|
    format.html
    format.turbo_stream
  end
end

#lifecycleObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'app/controllers/binocs/requests_controller.rb', line 117

def lifecycle
  logs = Array(@request.logs)

  # Extract the controller log entry (the summary line from ActiveSupport::Notifications)
  controller_log = logs.find { |l| l["type"] == "controller" }

  # Timing breakdown
  total_duration = @request.duration_ms.to_f
  controller_duration = controller_log&.dig("duration").to_f
  view_runtime = controller_log&.dig("view_runtime").to_f
  db_runtime = controller_log&.dig("db_runtime").to_f

  middleware_time = [total_duration - controller_duration, 0].max
  other_time = [controller_duration - view_runtime - db_runtime, 0].max

  @lifecycle = {
    total_duration: total_duration,
    controller_duration: controller_duration,
    middleware_time: middleware_time,
    view_runtime: view_runtime,
    db_runtime: db_runtime,
    other_time: other_time
  }

  # SQL queries from logs
  @sql_queries = logs.select { |l| l["type"] == "sql" }

  # Render entries from logs
  @render_entries = logs.select { |l| l["type"] == "render" }

  # Halted filter (if any before_action halted the chain)
  @halted_filter = logs.find { |l| l["type"] == "halted" }

  # Redirect info
  @redirect = logs.find { |l| l["type"] == "redirect" }

  # Exception info (from logs or request model)
  @exception_log = logs.find { |l| l["type"] == "exception" }

  # Generic log entries
  @log_entries = logs.select { |l| l["type"] == "log" }

  # Middleware stack from the host Rails app
  @middleware_stack = begin
    Rails.application.middleware.map do |middleware|
      name = middleware.klass.is_a?(String) ? middleware.klass : middleware.klass.name
      name
    end.compact
  rescue
    []
  end
end

#rawObject



170
171
172
# File 'app/controllers/binocs/requests_controller.rb', line 170

def raw
  @section = params[:section].presence || "full"
end

#sequenceObject



42
43
44
45
46
# File 'app/controllers/binocs/requests_controller.rb', line 42

def sequence
  @client_identifiers = Request.client_identifiers
  @selected_client = params[:client].presence || @client_identifiers.first
  @requests = Request.by_client(@selected_client).for_sequence.limit(200)
end

#showObject



26
27
28
29
30
31
# File 'app/controllers/binocs/requests_controller.rb', line 26

def show
  respond_to do |format|
    format.html
    format.turbo_stream
  end
end