/
bullet.rb
283 lines (230 loc) · 8.5 KB
/
bullet.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# frozen_string_literal: true
require 'active_support/core_ext/module/delegation'
require 'set'
require 'uniform_notifier'
require 'bullet/ext/object'
require 'bullet/ext/string'
require 'bullet/dependency'
require 'bullet/stack_trace_filter'
module Bullet
extend Dependency
autoload :ActiveRecord, "bullet/#{active_record_version}" if active_record?
autoload :Mongoid, "bullet/#{mongoid_version}" if mongoid?
autoload :Rack, 'bullet/rack'
autoload :ActiveJob, 'bullet/active_job'
autoload :Notification, 'bullet/notification'
autoload :Detector, 'bullet/detector'
autoload :Registry, 'bullet/registry'
autoload :NotificationCollector, 'bullet/notification_collector'
if defined?(Rails::Railtie)
class BulletRailtie < Rails::Railtie
initializer 'bullet.configure_rails_initialization' do |app|
if defined?(ActionDispatch::ContentSecurityPolicy::Middleware) && Rails.application.config.content_security_policy
app.middleware.insert_before ActionDispatch::ContentSecurityPolicy::Middleware, Bullet::Rack
else
app.middleware.use Bullet::Rack
end
end
end
end
class << self
attr_writer :n_plus_one_query_enable,
:unused_eager_loading_enable,
:counter_cache_enable,
:stacktrace_includes,
:stacktrace_excludes,
:skip_html_injection
attr_reader :safelist
attr_accessor :add_footer,
:orm_patches_applied,
:skip_http_headers,
:always_append_html_body,
:skip_user_in_notification
available_notifiers =
UniformNotifier::AVAILABLE_NOTIFIERS.select { |notifier| notifier != :raise }
.map { |notifier| "#{notifier}=" }
available_notifiers_options = { to: UniformNotifier }
delegate(*available_notifiers, **available_notifiers_options)
def raise=(should_raise)
UniformNotifier.raise = (should_raise ? Notification::UnoptimizedQueryError : false)
end
DETECTORS = [
Bullet::Detector::NPlusOneQuery,
Bullet::Detector::UnusedEagerLoading,
Bullet::Detector::CounterCache
].freeze
def enable=(enable)
@enable = @n_plus_one_query_enable = @unused_eager_loading_enable = @counter_cache_enable = enable
if enable?
reset_safelist
unless orm_patches_applied
self.orm_patches_applied = true
Bullet::Mongoid.enable if mongoid?
Bullet::ActiveRecord.enable if active_record?
end
end
end
alias enabled= enable=
def enable?
!!@enable
end
alias enabled? enable?
# Rails.root might be nil if `railties` is a dependency on a project that does not use Rails
def app_root
@app_root ||= (defined?(::Rails.root) && !::Rails.root.nil? ? Rails.root.to_s : Dir.pwd).to_s
end
def n_plus_one_query_enable?
enable? && !!@n_plus_one_query_enable
end
def unused_eager_loading_enable?
enable? && !!@unused_eager_loading_enable
end
def counter_cache_enable?
enable? && !!@counter_cache_enable
end
def stacktrace_includes
@stacktrace_includes ||= []
end
def stacktrace_excludes
@stacktrace_excludes ||= []
end
def add_safelist(options)
reset_safelist
@safelist[options[:type]][options[:class_name]] ||= []
@safelist[options[:type]][options[:class_name]] << options[:association].to_sym
end
def delete_safelist(options)
reset_safelist
@safelist[options[:type]][options[:class_name]] ||= []
@safelist[options[:type]][options[:class_name]].delete(options[:association].to_sym)
@safelist[options[:type]].delete_if { |_key, val| val.empty? }
end
def get_safelist_associations(type, class_name)
Array.wrap(@safelist[type][class_name])
end
def reset_safelist
@safelist ||= { n_plus_one_query: {}, unused_eager_loading: {}, counter_cache: {} }
end
def clear_safelist
@safelist = nil
end
def bullet_logger=(active)
if active
require 'fileutils'
FileUtils.mkdir_p(app_root + '/log')
bullet_log_file = File.open("#{app_root}/log/bullet.log", 'a+')
bullet_log_file.sync = true
UniformNotifier.customized_logger = bullet_log_file
end
end
def debug(title, message)
puts "[Bullet][#{title}] #{message}" if ENV['BULLET_DEBUG'] == 'true'
end
def start_request
Thread.current[:bullet_start] = true
Thread.current[:bullet_notification_collector] = Bullet::NotificationCollector.new
Thread.current[:bullet_object_associations] = Bullet::Registry::Base.new
Thread.current[:bullet_call_object_associations] = Bullet::Registry::Base.new
Thread.current[:bullet_possible_objects] = Bullet::Registry::Object.new
Thread.current[:bullet_impossible_objects] = Bullet::Registry::Object.new
Thread.current[:bullet_inversed_objects] = Bullet::Registry::Base.new
Thread.current[:bullet_eager_loadings] = Bullet::Registry::Association.new
Thread.current[:bullet_call_stacks] = Bullet::Registry::CallStack.new
Thread.current[:bullet_counter_possible_objects] ||= Bullet::Registry::Object.new
Thread.current[:bullet_counter_impossible_objects] ||= Bullet::Registry::Object.new
end
def end_request
Thread.current[:bullet_start] = nil
Thread.current[:bullet_notification_collector] = nil
Thread.current[:bullet_object_associations] = nil
Thread.current[:bullet_call_object_associations] = nil
Thread.current[:bullet_possible_objects] = nil
Thread.current[:bullet_impossible_objects] = nil
Thread.current[:bullet_inversed_objects] = nil
Thread.current[:bullet_eager_loadings] = nil
Thread.current[:bullet_counter_possible_objects] = nil
Thread.current[:bullet_counter_impossible_objects] = nil
end
def start?
enable? && Thread.current[:bullet_start]
end
def notification_collector
Thread.current[:bullet_notification_collector]
end
def notification?
return unless start?
Bullet::Detector::UnusedEagerLoading.check_unused_preload_associations
notification_collector.notifications_present?
end
def gather_inline_notifications
responses = []
for_each_active_notifier_with_notification { |notification| responses << notification.notify_inline }
responses.join("\n")
end
def perform_out_of_channel_notifications(env = {})
request_uri = build_request_uri(env)
for_each_active_notifier_with_notification do |notification|
notification.url = request_uri
notification.notify_out_of_channel
end
end
def footer_info
info = []
notification_collector.collection.each { |notification| info << notification.short_notice }
info
end
def text_notifications
info = []
notification_collector.collection.each do |notification|
info << notification.notification_data.values.compact.join("\n")
end
info
end
def warnings
notification_collector.collection.each_with_object({}) do |notification, warnings|
warning_type = notification.class.to_s.split(':').last.tableize
warnings[warning_type] ||= []
warnings[warning_type] << notification
end
end
def profile
return_value = nil
if Bullet.enable?
begin
Bullet.start_request
return_value = yield
Bullet.perform_out_of_channel_notifications if Bullet.notification?
ensure
Bullet.end_request
end
else
return_value = yield
end
return_value
end
def console_enabled?
UniformNotifier.active_notifiers.include?(UniformNotifier::JavascriptConsole)
end
def inject_into_page?
return false if defined?(@skip_html_injection) && @skip_html_injection
console_enabled? || add_footer
end
private
def for_each_active_notifier_with_notification
UniformNotifier.active_notifiers.each do |notifier|
notification_collector.collection.each do |notification|
notification.notifier = notifier
yield notification
end
end
end
def build_request_uri(env)
return "#{env['REQUEST_METHOD']} #{env['REQUEST_URI']}" if env['REQUEST_URI']
if env['QUERY_STRING'].present?
"#{env['REQUEST_METHOD']} #{env['PATH_INFO']}?#{env['QUERY_STRING']}"
else
"#{env['REQUEST_METHOD']} #{env['PATH_INFO']}"
end
end
end
end