lib/shiba/query_watcher.rb in shiba-0.1.2 vs lib/shiba/query_watcher.rb in shiba-0.2.0
- old
+ new
@@ -1,15 +1,15 @@
require 'shiba/query'
+require 'shiba/backtrace'
require 'json'
require 'rails'
module Shiba
class QueryWatcher
- IGNORE = /\.rvm|gem|vendor\/|rbenv|seed|db|shiba|test|spec/
def self.watch(file)
- new(file).watch
+ new(file).tap { |w| w.watch }
end
attr_reader :queries
def initialize(file)
@@ -24,59 +24,16 @@
sql = payload[:sql]
if sql.start_with?("SELECT")
fingerprint = Query.get_fingerprint(sql)
if !@queries[fingerprint]
- if lines = app_backtrace
+ if lines = Backtrace.from_app
@file.puts("#{sql} /*shiba#{lines}*/")
end
end
@queries[fingerprint] = true
end
end
- end
-
- protected
-
- # 8 backtrace lines starting from the app caller, cleaned of app/project cruft.
- def app_backtrace
- app_line_idx = caller_locations.index { |line| line.to_s !~ IGNORE }
- if app_line_idx == nil
- return
- end
-
- caller_locations(app_line_idx+1, 8).map do |loc|
- line = loc.to_s
- line.sub!(backtrace_ignore_pattern, '')
- line
- end
- end
-
- def backtrace_ignore_pattern
- @roots ||= begin
- paths = Gem.path
- paths << Rails.root.to_s if Rails.root
- paths << repo_root
- paths << ENV['HOME']
- paths.uniq!
- paths.compact!
- # match and replace longest path first
- paths.sort_by!(&:size).reverse!
-
- Regexp.new(paths.map {|r| Regexp.escape(r) }.join("|"))
- end
- end
-
- # /user/git_repo => "/user/git_repo"
- # /user/not_a_repo => nil
- def repo_root
- root = nil
- Open3.popen3('git rev-parse --show-toplevel') {|_,o,_,_|
- if root = o.gets
- root = root.chomp
- end
- }
- root
end
end
end