Module: RailsPulse::StatusHelper

Included in:
ApplicationHelper
Defined in:
app/helpers/rails_pulse/status_helper.rb

Instance Method Summary collapse

Instance Method Details

#categorize_operation(operation_type) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
# File 'app/helpers/rails_pulse/status_helper.rb', line 206

def categorize_operation(operation_type)
  case operation_type
  when "sql"
    :database
  when "template", "partial", "layout", "collection"
    :view
  when "controller"
    :application
  else
    :other
  end
end

#duration_options(type = :route) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
283
# File 'app/helpers/rails_pulse/status_helper.rb', line 272

def duration_options(type = :route)
  thresholds = RailsPulse.configuration.public_send("#{type}_thresholds")

  first_label = "All #{type.to_s.humanize.pluralize}"

  [
    [ first_label, :all ],
    [ "Slow (≥ #{thresholds[:slow]}ms)", :slow ],
    [ "Very Slow (≥ #{thresholds[:very_slow]}ms)", :very_slow ],
    [ "Critical (≥ #{thresholds[:critical]}ms)", :critical ]
  ]
end

#event_color(operation_type) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
# File 'app/helpers/rails_pulse/status_helper.rb', line 257

def event_color(operation_type)
  case operation_type
  when "sql"
    "#d27d6b"
  when "template", "partial", "layout", "collection"
    "#6c7ab9"
  when "controller"
    "#5ba6b0"
  else
    "#a6a6a6"
  end
end

#operation_category_label(operation_type) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
# File 'app/helpers/rails_pulse/status_helper.rb', line 219

def operation_category_label(operation_type)
  case categorize_operation(operation_type)
  when :database
    "Database"
  when :view
    "View Rendering"
  when :application
    "Application Logic"
  else
    "Other Operations"
  end
end

#operation_status_indicator(operation) ⇒ Object



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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/helpers/rails_pulse/status_helper.rb', line 123

def operation_status_indicator(operation)
  # Define operation-specific thresholds
  thresholds = case operation.operation_type
  when "sql"
    { slow: 50, very_slow: 100, critical: 500 }
  when "template", "partial", "layout", "collection"
    { slow: 50, very_slow: 150, critical: 300 }
  when "controller"
    { slow: 200, very_slow: 500, critical: 1000 }
  when "cache_read", "cache_write"
    { slow: 10, very_slow: 50, critical: 100 }
  when "http"
    { slow: 500, very_slow: 1000, critical: 3000 }
  when "job"
    { slow: 1000, very_slow: 5000, critical: 10000 }
  when "mailer"
    { slow: 500, very_slow: 2000, critical: 5000 }
  when "storage"
    { slow: 500, very_slow: 1000, critical: 3000 }
  else
    { slow: 100, very_slow: 300, critical: 1000 }
  end

  duration = operation.duration.to_f
  status_value = case duration
  when 0...thresholds[:slow]
    0 # Healthy
  when thresholds[:slow]...thresholds[:very_slow]
    1 # Warning
  when thresholds[:very_slow]...thresholds[:critical]
    2 # Slow
  else
    3 # Critical
  end

  case status_value
  when 0
    # Healthy operations show no icon to reduce visual clutter
    ""
  when 1
    (
      :span,
      lucide_icon("alert-triangle", width: "16", height: "16", class: "text-yellow-600"),
      title: "Warning - Operation time > #{thresholds[:slow]} ms"
    )
  when 2
    (
      :span,
      lucide_icon("alert-circle", width: "16", height: "16", class: "text-orange-600"),
      title: "Slow - Operation time > #{thresholds[:very_slow]} ms"
    )
  when 3
    (
      :span,
      lucide_icon("x-circle", width: "16", height: "16", class: "text-red-600"),
      title: "Critical - Operation time > #{thresholds[:critical]} ms"
    )
  else
    (
      :span,
      lucide_icon("help-circle", width: "16", height: "16", class: "text-gray-400"),
      title: "Unknown status"
    )
  end
end

#operations_performance_breakdown(operations) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'app/helpers/rails_pulse/status_helper.rb', line 189

def operations_performance_breakdown(operations)
  return { database: 0, view: 0, application: 0, other: 0 } if operations.empty?

  total_duration = operations.sum(&:duration).to_f
  return { database: 0, view: 0, application: 0, other: 0 } if total_duration.zero?

  breakdown = operations.group_by { |op| categorize_operation(op.operation_type) }
    .transform_values { |ops| ops.sum(&:duration) }

  {
    database: ((breakdown[:database] || 0) / total_duration * 100).round(1),
    view: ((breakdown[:view] || 0) / total_duration * 100).round(1),
    application: ((breakdown[:application] || 0) / total_duration * 100).round(1),
    other: ((breakdown[:other] || 0) / total_duration * 100).round(1)
  }
end

#performance_badge_class(percentile) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
# File 'app/helpers/rails_pulse/status_helper.rb', line 232

def performance_badge_class(percentile)
  case percentile
  when 0..50
    "badge--positive"
  when 51..75
    "badge--warning"
  when 76..90
    "badge--negative"
  else
    "badge--critical"
  end
end

#query_status_indicator(avg_duration) ⇒ Object



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
# File 'app/helpers/rails_pulse/status_helper.rb', line 79

def query_status_indicator(avg_duration)
  thresholds = RailsPulse.configuration.query_thresholds
  status_value = case avg_duration.to_f
  when 0...thresholds[:slow]
    0 # Healthy
  when thresholds[:slow]...thresholds[:very_slow]
    1 # Warning
  when thresholds[:very_slow]...thresholds[:critical]
    2 # Slow
  else
    3 # Critical
  end

  case status_value
  when 0
    # Healthy queries show no icon to reduce visual clutter
    ""
  when 1
    (
      :span,
      lucide_icon("alert-triangle", width: "16", height: "16", class: "text-yellow-600"),
      title: "Warning - Query time > #{thresholds[:slow]} ms"
    )
  when 2
    (
      :span,
      lucide_icon("alert-circle", width: "16", height: "16", class: "text-orange-600"),
      title: "Slow - Query time > #{thresholds[:very_slow]} ms"
    )
  when 3
    (
      :span,
      lucide_icon("x-circle", width: "16", height: "16", class: "text-red-600"),
      title: "Critical - Query time > #{thresholds[:critical]} ms"
    )
  else
    (
      :span,
      lucide_icon("help-circle", width: "16", height: "16", class: "text-gray-400"),
      title: "Unknown status"
    )
  end
end

#request_status_indicator(duration) ⇒ Object



35
36
37
38
39
40
41
42
43
44
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
70
71
72
73
74
75
76
77
# File 'app/helpers/rails_pulse/status_helper.rb', line 35

def request_status_indicator(duration)
  thresholds = RailsPulse.configuration.request_thresholds
  status_value = case duration.to_i
  when 0...thresholds[:slow]
    0 # Healthy
  when thresholds[:slow]...thresholds[:very_slow]
    1 # Warning
  when thresholds[:very_slow]...thresholds[:critical]
    2 # Slow
  else
    3 # Critical
  end

  case status_value
  when 0
    # Healthy requests show no icon to reduce visual clutter
    ""
  when 1
    (
      :span,
      lucide_icon("alert-triangle", width: "16", height: "16", class: "text-yellow-600"),
      title: "Warning - Response time > #{thresholds[:slow]} ms"
    )
  when 2
    (
      :span,
      lucide_icon("alert-circle", width: "16", height: "16", class: "text-orange-600"),
      title: "Slow - Response time > #{thresholds[:very_slow]} ms"
    )
  when 3
    (
      :span,
      lucide_icon("x-circle", width: "16", height: "16", class: "text-red-600"),
      title: "Critical - Response time > #{thresholds[:critical]} ms"
    )
  else
    (
      :span,
      lucide_icon("help-circle", width: "16", height: "16", class: "text-gray-400"),
      title: "Unknown status"
    )
  end
end

#rescue_template_missingObject



245
246
247
248
249
250
# File 'app/helpers/rails_pulse/status_helper.rb', line 245

def rescue_template_missing
  yield
  true
rescue ActionView::MissingTemplate
  false
end

#route_status_indicator(status_value) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/helpers/rails_pulse/status_helper.rb', line 3

def route_status_indicator(status_value)
  case status_value.to_i
  when 0
    # Healthy routes show no icon to reduce visual clutter
    ""
  when 1
    (
      :span,
      lucide_icon("alert-triangle", width: "16", height: "16", class: "text-yellow-600"),
      title: "Warning - Response time > #{RailsPulse.configuration.route_thresholds[:slow]} ms"
    )
  when 2
    (
      :span,
      lucide_icon("alert-circle", width: "16", height: "16", class: "text-orange-600"),
      title: "Slow - Response time > #{RailsPulse.configuration.route_thresholds[:very_slow]} ms"
    )
  when 3
    (
      :span,
      lucide_icon("x-circle", width: "16", height: "16", class: "text-red-600"),
      title: "Critical - Response time > #{RailsPulse.configuration.route_thresholds[:critical]} ms"
    )
  else
    (
      :span,
      lucide_icon("help-circle", width: "16", height: "16", class: "text-gray-400"),
      title: "Unknown status"
    )
  end
end

#truncate_sql(sql, length: 100) ⇒ Object



252
253
254
255
# File 'app/helpers/rails_pulse/status_helper.rb', line 252

def truncate_sql(sql, length: 100)
  return sql if sql.length <= length
  sql.truncate(length)
end