Module: ErrorRadar::Integrations::RailsAdmin

Defined in:
lib/error_radar/integrations/rails_admin.rb

Overview

Registers ErrorRadar::ErrorLog as a first-class RailsAdmin model (a triage board) plus four member actions: start / resolve / ignore / reopen. Everything is guarded so a host without RailsAdmin is unaffected.

Constant Summary collapse

MODEL =
'ErrorRadar::ErrorLog'
ACTIONS =

status -> { update attrs, flash verb, icon }

{
  start_error_log:   { icon: 'fa-solid fa-person-digging', verb: 'marked in progress', update: { status: :in_progress } },
  ignore_error_log:  { icon: 'fa-solid fa-ban',            verb: 'ignored',            update: { status: :ignored } },
  reopen_error_log:  { icon: 'fa-solid fa-rotate-left',    verb: 'reopened',           method: :reopen! },
  resolve_error_log: { icon: 'fa-solid fa-circle-check',   verb: 'marked resolved',    method: :resolve! }
}.freeze

Class Method Summary collapse

Class Method Details

.configure_model!Object



62
63
64
65
66
67
68
69
70
71
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/error_radar/integrations/rails_admin.rb', line 62

def self.configure_model!
  ::RailsAdmin.config do |config|
    config.model MODEL do
      navigation_label 'Monitoring'
      navigation_icon 'fa-solid fa-triangle-exclamation'
      label 'Error / Task'
      label_plural 'Errors / Tasks'
      weight(-100)

      list do
        scopes %i[unresolved] + [nil] + %i[open in_progress resolved ignored]
        sort_by :last_seen_at
        items_per_page 50

        field :id
        field :status do
          pretty_value do
            colors = { 'open' => '#dc3545', 'in_progress' => '#fd7e14', 'resolved' => '#28a745', 'ignored' => '#6c757d' }
            v = bindings[:object].status
            %(<span class="label" style="padding:2px 8px;border-radius:10px;color:#fff;background:#{colors[v] || '#6c757d'}">#{v}</span>).html_safe
          end
        end
        field :severity do
          pretty_value do
            colors = { 'info' => '#17a2b8', 'warning' => '#fd7e14', 'error' => '#dc3545', 'critical' => '#7b001c' }
            v = bindings[:object].severity
            %(<span class="label" style="padding:2px 8px;border-radius:10px;color:#fff;background:#{colors[v] || '#6c757d'}">#{v}</span>).html_safe
          end
        end
        field :category
        field :source
        field :error_class
        field :message do
          formatted_value { bindings[:object].short_message }
        end
        field :occurrences
        field :http_status
        field :last_seen_at
        field :first_seen_at
      end

      show do
        field :id
        field :status
        field :severity
        field :category
        field :source
        field :error_class
        field :message
        field :occurrences
        field :first_seen_at
        field :last_seen_at
        field :http_status
        field :api_code
        field :api_subcode
        field :request_url
        field :context do
          pretty_value do
            %(<pre style="white-space:pre-wrap;max-height:400px;overflow:auto">#{JSON.pretty_generate(bindings[:object].context || {})}</pre>).html_safe
          end
        end
        field :backtrace do
          pretty_value do
            %(<pre style="white-space:pre-wrap;max-height:500px;overflow:auto">#{ERB::Util.html_escape(bindings[:object].backtrace)}</pre>).html_safe
          end
        end
        field :resolved_at
        field :resolved_by
        field :resolution_note
        field :created_at
        field :updated_at
      end

      edit do
        field :status
        field :severity
        field :resolution_note
      end
    end
  end
end

.define_actions!Object



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
# File 'lib/error_radar/integrations/rails_admin.rb', line 26

def self.define_actions!
  require 'rails_admin/config/actions'
  require 'rails_admin/config/actions/base'

  ACTIONS.each do |name, spec|
    next if ::RailsAdmin::Config::Actions.find(name)

    klass = Class.new(::RailsAdmin::Config::Actions::Base) do
      register_instance_option(:only) { MODEL }
      register_instance_option(:member) { true }
      register_instance_option(:http_methods) { %i[get put] }
      register_instance_option(:link_icon) { spec[:icon] }
      register_instance_option(:pjax?) { false }
      register_instance_option(:turbo?) { false }
      register_instance_option(:controller) do
        proc do
          actor = (_current_user.try(:email) rescue nil)
          if spec[:method] == :resolve!
            @object.resolve!(by: actor)
          elsif spec[:method]
            @object.public_send(spec[:method])
          else
            @object.update!(spec[:update])
          end
          flash[:notice] = "Error ##{@object.id} #{spec[:verb]}."
          redirect_to back_or_index
        end
      end
    end

    const_name = name.to_s.split('_').map(&:capitalize).join
    ErrorRadar::Integrations::RailsAdmin.const_set(const_name, klass) unless const_defined?(const_name)
    ::RailsAdmin::Config::Actions.register(name, klass)
  end
end

.install!Object



19
20
21
22
23
24
# File 'lib/error_radar/integrations/rails_admin.rb', line 19

def self.install!
  define_actions!
  configure_model!
rescue StandardError => e
  ErrorRadar::Tracking.warn_internal("RailsAdmin integration failed: #{e.class}: #{e.message}")
end