Skip to content

Commit

Permalink
Allow stack trace paths to be included by regex
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieltaylor committed Sep 8, 2016
1 parent 0a1db35 commit 7977b78
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 11 deletions.
8 changes: 4 additions & 4 deletions lib/bullet/detector/n_plus_one_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ def create_notification(callers, klazz, associations)
def caller_in_project
app_root = rails? ? Rails.root.to_s : Dir.pwd
vendor_root = app_root + "/vendor"
caller.select do |c|
c.include?(app_root) && !c.include?(vendor_root) ||
Bullet.stacktrace_includes.any? { |include| c.include?(include) }
caller.select do |included_path|
included_path.include?(app_root) && !included_path.include?(vendor_root) ||
Bullet.stacktrace_includes.any? { |regex| included_path =~ regex }
end
end

def excluded_stacktrace_path?
Bullet.stacktrace_excludes.any? do |excluded_path|
caller_in_project.any? { |c| c.include?(excluded_path) }
caller_in_project.any? { |regex| excluded_path =~ regex }
end
end
end
Expand Down
23 changes: 20 additions & 3 deletions lib/bullet/detector/unused_eager_loading.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module Bullet
module Detector
class UnusedEagerLoading < Association
extend Dependency

class <<self
# check if there are unused preload associations.
# get related_objects from eager_loadings associated with object and associations
Expand All @@ -15,7 +17,7 @@ def check_unused_preload_associations
next if object_association_diff.empty?

Bullet.debug("detect unused preload", "object: #{bullet_key}, associations: #{object_association_diff}")
create_notification bullet_key.bullet_class_name, object_association_diff
create_notification(caller_in_project, bullet_key.bullet_class_name, object_association_diff)
end
end

Expand Down Expand Up @@ -53,11 +55,11 @@ def add_eager_loadings(objects, associations)
end

private
def create_notification(klazz, associations)
def create_notification(callers, klazz, associations)
notify_associations = Array(associations) - Bullet.get_whitelist_associations(:unused_eager_loading, klazz)

if notify_associations.present?
notice = Bullet::Notification::UnusedEagerLoading.new(klazz, notify_associations)
notice = Bullet::Notification::UnusedEagerLoading.new(callers, klazz, notify_associations)
Bullet.notification_collector.add(notice)
end
end
Expand All @@ -76,6 +78,21 @@ def diff_object_associations(bullet_key, associations)
potential_associations = associations - call_associations(bullet_key, associations)
potential_associations.reject { |a| a.is_a?(Hash) }
end

def caller_in_project
app_root = rails? ? Rails.root.to_s : Dir.pwd
vendor_root = app_root + "/vendor"
caller.select do |included_path|
included_path.include?(app_root) && !included_path.include?(vendor_root) ||
Bullet.stacktrace_includes.any? { |regex| included_path =~ regex }
end
end

def excluded_stacktrace_path?
Bullet.stacktrace_excludes.any? do |excluded_path|
caller_in_project.any? { |regex| excluded_path =~ regex }
end
end
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions lib/bullet/notification/unused_eager_loading.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
module Bullet
module Notification
class UnusedEagerLoading < Base
def initialize(callers, base_class, associations, path = nil)
super(base_class, associations, path)

@callers = callers
end

def notification_data
super.merge(
:backtrace => @callers
)
end

def body
"#{klazz_associations_str}\n Remove from your finder: #{associations_str}"
end
Expand Down
4 changes: 2 additions & 2 deletions spec/bullet/detector/n_plus_one_query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ module Detector
end

context "stacktrace_excludes" do
before { Bullet.stacktrace_excludes = [ 'def' ] }
before { Bullet.stacktrace_excludes = [ /def/ ] }
after { Bullet.stacktrace_excludes = nil }

it "should not create notification when stacktrace contains paths that are in the exclude list" do
Expand All @@ -114,7 +114,7 @@ module Detector
end

context "stacktrace_includes" do
before { Bullet.stacktrace_includes = [ 'def' ] }
before { Bullet.stacktrace_includes = [ /def/ ] }
after { Bullet.stacktrace_includes = nil }

it "should include paths that are in the stacktrace_include list" do
Expand Down
4 changes: 3 additions & 1 deletion spec/bullet/detector/unused_eager_loading_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ module Detector
end

context ".check_unused_preload_associations" do
let(:paths) { ["/dir1", "/dir1/subdir"] }
it "should create notification if object_association_diff is not empty" do
UnusedEagerLoading.add_object_associations(@post, :association)
expect(UnusedEagerLoading).to receive(:create_notification).with("Post", [:association])
allow(UnusedEagerLoading).to receive(:caller_in_project).and_return(paths)
expect(UnusedEagerLoading).to receive(:create_notification).with(paths, "Post", [:association])
UnusedEagerLoading.check_unused_preload_associations
end

Expand Down
2 changes: 1 addition & 1 deletion spec/bullet/notification/unused_eager_loading_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Bullet
module Notification
describe UnusedEagerLoading do
subject { UnusedEagerLoading.new(Post, [:comments, :votes], "path") }
subject { UnusedEagerLoading.new([""], Post, [:comments, :votes], "path") }

it { expect(subject.body).to eq(" Post => [:comments, :votes]\n Remove from your finder: :includes => [:comments, :votes]") }
it { expect(subject.title).to eq("Unused Eager Loading in path") }
Expand Down

0 comments on commit 7977b78

Please sign in to comment.