forked from errbit/errbit
/
notice.rb
192 lines (155 loc) · 4.1 KB
/
notice.rb
1
2
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
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
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
188
189
190
191
require 'recurse'
class Notice < ActiveRecord::Base
include Authority::Abilities
include NoticeRepository
serialize :server_environment, Hash
serialize :request, Hash
serialize :notifier, Hash
serialize :user_attributes, Hash
serialize :current_user, Hash
delegate :lines, :to => :backtrace, :prefix => true
delegate :app, :problem, :to => :err
belongs_to :err
belongs_to :backtrace
counter_culture [:err, :problem]
after_commit :unresolve_problem, on: :create
after_commit :cache_attributes_on_problem, on: :create
after_commit :increase_in_distributions, on: :create
after_commit :decrease_in_distributions, on: :destroy
before_save :sanitize
after_initialize :default_values
validates_presence_of :backtrace, :server_environment, :notifier
def default_values
if self.new_record?
self.server_environment ||= Hash.new
self.request ||= Hash.new
self.notifier ||= Hash.new
self.user_attributes ||= Hash.new
self.current_user ||= Hash.new
end
end
def message_signature
m = message.clone
m.gsub!(/".+?"/, '"%STR%"')
m.gsub!(/'.+?'/, '\'%STR%\'')
m.gsub!(/\h{8}-\h{4}-\h{4}-\h{4}-\h{12}/, '%UUID%')
m.gsub!(/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/, '%IP%')
m.gsub!(/0x\h+/, '%HEX%')
m.gsub!(/\h{7,}/, '%HEXSTR%')
m.gsub!(/\d+/, '%NUM%')
m.truncate(150)
end
def user_agent
agent_string = env_vars['HTTP_USER_AGENT']
agent_string.blank? ? nil : UserAgent.parse(agent_string)
end
def user_agent_string
if user_agent.nil? || user_agent.none?
"N/A"
else
"#{user_agent.browser} #{user_agent.version} (#{user_agent.os})"
end
end
def environment_name
server_environment['server-environment'] || server_environment['environment-name']
end
def component
request['component']
end
def action
request['action']
end
def where
where = component.to_s.dup
where << "##{action}" if action.present?
where
end
def request
super || {}
rescue Psych::SyntaxError
# some notices may have incorrect yaml inside request field
{}
end
def url
request['url']
end
def host
uri = url && URI.parse(url)
uri.blank? ? "N/A" : uri.host
rescue URI::InvalidURIError
"N/A"
end
def env_vars
request['cgi-data'] || {}
end
def params
request['params'] || {}
end
def session
request['session'] || {}
end
def in_app_backtrace_lines
backtrace_lines.in_app
end
def similar_count
problem.notices_count
end
def emailable?
app.email_at_notices.include?(problem.notices_count_since_unresolve)
end
def should_email?
app.emailable? && emailable?
end
def should_notify?
app.notification_service_configured? &&
(app.notification_service.notify_at_notices.include?(0) || app.notification_service.notify_at_notices.include?(problem.notices_count_since_unresolve))
end
##
# TODO: Move on decorator maybe
#
def project_root
if server_environment
server_environment['project-root'] || ''
end
end
def app_version
if server_environment
server_environment['app-version'] || ''
end
end
protected
def increase_in_distributions
problem.increase_in_message_distribution message_signature
problem.increase_in_host_distribution host
problem.increase_in_user_agent_distribution user_agent_string
end
def decrease_in_distributions
problem.decrease_in_message_distribution message_signature
problem.decrease_in_host_distribution host
problem.decrease_in_user_agent_distribution user_agent_string
end
def unresolve_problem
return if problem.unresolved?
problem.unresolve!
end
def cache_attributes_on_problem
ProblemUpdaterCache.new(problem, self).update
end
def sanitize
[:server_environment, :request, :notifier].each do |h|
send("#{h}=",sanitize_hash(send(h)))
end
end
def sanitize_hash(h)
h.recurse do
|h| h.inject({}) do |h,(k,v)|
if k.is_a?(String)
h[k.gsub(/\./,'.').gsub(/^\$/,'$')] = v
else
h[k] = v
end
h
end
end
end
end