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



86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/helpers/rails_pulse/status_helper.rb', line 86

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



117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/helpers/rails_pulse/status_helper.rb', line 117

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

#duration_threshold_filter_options(type = :route) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/helpers/rails_pulse/status_helper.rb', line 130

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

  all_label =
    case type
    when :job then "All Job Runs"
    else "All #{type.to_s.humanize.pluralize}"
    end

  threshold_options = thresholds.map do |name, value|
    [ "#{name.to_s.humanize} (≥ #{value}ms)", value ]
  end.sort_by { |_, value| value }

  [ [ all_label, nil ] ] + threshold_options
end

#event_color(operation_type) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/helpers/rails_pulse/status_helper.rb', line 104

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_status_indicator(operation) ⇒ 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
34
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
# File 'app/helpers/rails_pulse/status_helper.rb', line 3

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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/helpers/rails_pulse/status_helper.rb', line 69

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

#truncate_sql(sql, length: 100) ⇒ Object



99
100
101
102
# File 'app/helpers/rails_pulse/status_helper.rb', line 99

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