Skip to content

Commit

Permalink
move query_settings to its own file. rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
technicalpickles committed Feb 15, 2024
1 parent b3e0b51 commit 4ac9e5a
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 117 deletions.
119 changes: 2 additions & 117 deletions lib/mini_profiler.rb
Expand Up @@ -14,126 +14,13 @@
require 'mini_profiler/snapshots_transporter'
require 'mini_profiler/views'
require 'mini_profiler/actions'
require 'mini_profiler/query_settings'

module Rack
class MiniProfiler
include Actions
include Views

class QuerySettings
def initialize(query_string, profile_parameter, skip_paths, path)
@query_string = query_string
@query_params = Rack::Utils.parse_nested_query(query_string)

@profile_parameter = profile_parameter
@skip_paths = skip_paths

@path = path
end

def skip?
@query_string.match?(/#{@profile_parameter}=skip/)
end

def skip_path?
@skip_paths && @skip_paths.any? do |p|
if p.instance_of?(String)
@path.start_with?(p)
elsif p.instance_of?(Regexp)
p.match?(@path)
end
end
end

def profile_value
@query_params[@profile_parameter]
end

def manual_enable?
profile_value == 'enable'
end

def manual_disable?
profile_value == 'disable'
end

def normal_backtrace?
profile_value == 'normal-backtrace'
end

def no_backtrace?
profile_value == 'no-backtrace'
end

def full_backtrace?
profile_value == 'full-backtrace'
end

def trace_exceptions?
profile_value == 'trace-exceptions'
end

# FIXME this should use profile_parameter and be the same as flamegraph?
def pp_flamegraph?
@query_string.match?(/pp=(async-)?flamegraph/)
end

def flamegraph_sample_rate
match_data = @query_string.match(/flamegraph_sample_rate=([\d\.]+)/)
if match_data && !match_data[1].to_f.zero?
match_data[1].to_f
end
end

VALID_MODES = [:cpu, :wall, :object, :custom].freeze
def flamegraph_mode
mode_match_data = @query_string.match(/flamegraph_mode=([a-zA-Z]+)/)

if mode_match_data && VALID_MODES.include?(mode_match_data[1].to_sym)
mode_match_data[1].to_sym
end
end

def trace_exceptions_filter
@query_params['trace_exceptions_filter']
end

def env?
profile_value == 'env'
end

def analyze_memory?
profile_value == 'analyze-memory'
end

def help?
profile_value == 'help'
end

def flamegraph?
profile_value == 'flamegraph'
end

def profile_gc?
profile_value == 'profile-gc'
end

def profile_memory?
profile_value == 'profile-memory'
end

def memory_profiler_options
options = {
ignore_files: @query_params['memory_profiler_ignore_files'],
allow_files: @query_params['memory_profiler_allow_files'],
}

options[:top] = Integer(@query_params['memory_profiler_top']) if @query_params.key?('memory_profiler_top')

options
end
end

class << self
include Rack::MiniProfiler::ProfilingMethods
attr_accessor :subscribe_sql_active_record
Expand Down Expand Up @@ -274,7 +161,7 @@ def call(env)
MiniProfiler.deauthorize_request if @config.authorization_mode == :allow_authorized

status = headers = body = nil
path = env['PATH_INFO'].sub('//', '/')
path = env['PATH_INFO'].sub('//', '/')
query_settings = QuerySettings.new(env['QUERY_STRING'], @config.profile_parameter, @config.skip_paths, path)

# Someone (e.g. Rails engine) could change the SCRIPT_NAME so we save it
Expand Down Expand Up @@ -317,8 +204,6 @@ def call(env)

# profile gc
if query_settings.profile_gc?
return tool_disabled_message(client_settings) if !advanced_debugging_enabled?

current.measure = false if current
return serve_profile_gc(env, client_settings)
end
Expand Down
117 changes: 117 additions & 0 deletions lib/mini_profiler/query_settings.rb
@@ -0,0 +1,117 @@
module Rack
class MiniProfiler
class QuerySettings
def initialize(query_string, profile_parameter, skip_paths, path)
@query_string = query_string
@query_params = Rack::Utils.parse_nested_query(query_string)

@profile_parameter = profile_parameter
@skip_paths = skip_paths

@path = path
end

def skip?
@query_string.match?(/#{@profile_parameter}=skip/)
end

def skip_path?
@skip_paths && @skip_paths.any? do |p|
if p.instance_of?(String)
@path.start_with?(p)
elsif p.instance_of?(Regexp)
p.match?(@path)
end
end
end

def profile_value
@query_params[@profile_parameter]
end

def manual_enable?
profile_value == 'enable'
end

def manual_disable?
profile_value == 'disable'
end

def normal_backtrace?
profile_value == 'normal-backtrace'
end

def no_backtrace?
profile_value == 'no-backtrace'
end

def full_backtrace?
profile_value == 'full-backtrace'
end

def trace_exceptions?
profile_value == 'trace-exceptions'
end

# FIXME this should use profile_parameter and be the same as flamegraph?
def pp_flamegraph?
@query_string.match?(/pp=(async-)?flamegraph/)
end

def flamegraph_sample_rate
match_data = @query_string.match(/flamegraph_sample_rate=([\d\.]+)/)
if match_data && !match_data[1].to_f.zero?
match_data[1].to_f
end
end

VALID_MODES = [:cpu, :wall, :object, :custom].freeze
def flamegraph_mode
mode_match_data = @query_string.match(/flamegraph_mode=([a-zA-Z]+)/)

if mode_match_data && VALID_MODES.include?(mode_match_data[1].to_sym)
mode_match_data[1].to_sym
end
end

def trace_exceptions_filter
@query_params['trace_exceptions_filter']
end

def env?
profile_value == 'env'
end

def analyze_memory?
profile_value == 'analyze-memory'
end

def help?
profile_value == 'help'
end

def flamegraph?
profile_value == 'flamegraph'
end

def profile_gc?
profile_value == 'profile-gc'
end

def profile_memory?
profile_value == 'profile-memory'
end

def memory_profiler_options
options = {
ignore_files: @query_params['memory_profiler_ignore_files'],
allow_files: @query_params['memory_profiler_allow_files'],
}

options[:top] = Integer(@query_params['memory_profiler_top']) if @query_params.key?('memory_profiler_top')

options
end
end
end
end

0 comments on commit 4ac9e5a

Please sign in to comment.