Class: ActiveRecordQueryCount::Printer::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_query_count/printer/base.rb

Direct Known Subclasses

Console, Html, HtmlCompare

Constant Summary collapse

TEMPLATE_PATH =
File.join(parent_dir, 'assets', 'template.html.erb')
TEMPLATE_COMPARING_PATH =
File.join(parent_dir, 'assets', 'comparing.html.erb')
CSS_PATH =
File.join(parent_dir, 'assets', 'style.css')
JS_PATH =
File.join(parent_dir, 'assets', 'bar_chart.js')
CHART_JS_CONTENT =
File.join(parent_dir, 'assets', 'chart.min.js')
BASE_QUERY_COUNTER_PATH =
File.join(parent_dir, 'assets', 'template_base_query_counter.html.erb')
INJECT_TEMPLATE_PATH =
File.join(parent_dir, 'assets', 'template_for_inject.html.erb')

Instance Method Summary collapse

Instance Method Details

#base_query_counter_contentObject



65
66
67
# File 'lib/active_record_query_count/printer/base.rb', line 65

def base_query_counter_content
  File.read(BASE_QUERY_COUNTER_PATH)
end

#chart_js_contentObject



49
50
51
# File 'lib/active_record_query_count/printer/base.rb', line 49

def chart_js_content
  File.read(CHART_JS_CONTENT)
end

#css_contentObject



53
54
55
# File 'lib/active_record_query_count/printer/base.rb', line 53

def css_content
  File.read(CSS_PATH)
end

#filter_data(data) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/active_record_query_count/printer/base.rb', line 13

def filter_data data
  data.each_value do |info|
    info[:location].each_value do |detail|
      detail[:duration] = detail[:duration].truncate(2)
    end
  end
  data = data.select { |_, v| v[:count] >= Configuration.ignore_table_count }
  data = data.sort_by { |_, v| -v[:count] }.each do |_category, info|
    info[:location] = info[:location].sort_by do |_, detail|
      -detail[:count]
    end.first(Configuration.max_locations_per_table).to_h
  end
  data.to_h
end

#inject_template_contentObject



69
70
71
# File 'lib/active_record_query_count/printer/base.rb', line 69

def inject_template_content
  File.read(INJECT_TEMPLATE_PATH)
end

#js_contentObject



45
46
47
# File 'lib/active_record_query_count/printer/base.rb', line 45

def js_content
  File.read(JS_PATH)
end

#open_file(html_dest) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/active_record_query_count/printer/base.rb', line 37

def open_file html_dest
  if ENV['WSL_DISTRIBUTION']
    Launchy.open("file://wsl%24/#{ENV['WSL_DISTRIBUTION']}#{html_dest}")
  else
    Launchy.open(html_dest)
  end
end

#sort_data(data) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/active_record_query_count/printer/base.rb', line 28

def sort_data data
  data = data.sort_by { |_, v| -v[:count] }.each do |_category, info|
    info[:location] = info[:location].sort_by do |_, detail|
      -detail[:count]
    end.to_h
  end
  data.to_h
end

#template_comparing_contentObject



61
62
63
# File 'lib/active_record_query_count/printer/base.rb', line 61

def template_comparing_content
  File.read(TEMPLATE_COMPARING_PATH)
end

#template_contentObject



57
58
59
# File 'lib/active_record_query_count/printer/base.rb', line 57

def template_content
  File.read(TEMPLATE_PATH)
end