Module: RailsPerformance::RailsPerformanceHelper

Defined in:
app/helpers/rails_performance/rails_performance_helper.rb

Instance Method Summary collapse

Instance Method Details

#active?(section) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 131

def active?(section)
  case section
  when :dashboard
    "is-active" if controller_name == "rails_performance" && action_name == "index"
  when :crashes
    "is-active" if controller_name == "rails_performance" && action_name == "crashes"
  when :requests
    "is-active" if controller_name == "rails_performance" && action_name == "requests"
  when :recent
    "is-active" if controller_name == "rails_performance" && action_name == "recent"
  when :slow
    "is-active" if controller_name == "rails_performance" && action_name == "slow"
  when :sidekiq
    "is-active" if controller_name == "rails_performance" && action_name == "sidekiq"
  when :delayed_job
    "is-active" if controller_name == "rails_performance" && action_name == "delayed_job"
  when :grape
    "is-active" if controller_name == "rails_performance" && action_name == "grape"
  when :rake
    "is-active" if controller_name == "rails_performance" && action_name == "rake"
  when :custom
    "is-active" if controller_name == "rails_performance" && action_name == "custom"
  end
end

#bot_icon(user_agent) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 95

def bot_icon(user_agent)
  return nil if user_agent.blank?

  browser = Browser.new(user_agent)
  if browser.bot?
    (:span, class: "user-agent-icon", title: browser.bot&.name) do
      icon("bot")
    end
  else
    (:span, class: "user-agent-icon user-agent-icon-user", title: "Real User") do
      icon("user")
    end
  end
end

#duraction_alert_class(duration_str) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 11

def duraction_alert_class(duration_str)
  if duration_str.to_s =~ /(\d+.?\d+?)/
    duration = $1.to_f
    if duration >= 500
      'has-background-danger has-text-white-bis'
    elsif duration >= 200
      'has-background-warning has-text-black-ter'
    else
      'has-background-success has-text-white-bis'
    end
  else
    'has-background-light'
  end
end

#extract_duration(str) ⇒ Object



26
27
28
29
30
31
32
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 26

def extract_duration(str)
  if (str =~ /Duration: (\d+.?\d+?ms)/i)
    $1
  else
    '-'
  end
end

#format_datetime(e) ⇒ Object



126
127
128
129
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 126

def format_datetime(e)
  dt = RailsPerformance::Reports::BaseReport::time_in_app_time_zone(e)
  I18n.l(dt, format: "%Y-%m-%d %H:%M:%S")
end

#icon(name) ⇒ Object



110
111
112
113
114
115
116
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 110

def icon(name)
  @icons ||= {}
  @icons[name] ||= begin
    # https://www.iconfinder.com/iconsets/vivid
    raw File.read(File.expand_path(File.dirname(__FILE__) +  "/../../assets/images/#{name}.svg"))
  end
end

#insert_css_file(file) ⇒ Object



118
119
120
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 118

def insert_css_file(file)
  raw "<style>#{raw File.read File.expand_path(File.dirname(__FILE__) + "/../../views/rails_performance/stylesheets/#{file}")}</style>"
end

#insert_js_file(file) ⇒ Object



122
123
124
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 122

def insert_js_file(file)
  raw "<script>#{raw File.read File.expand_path(File.dirname(__FILE__) + "/../../views/rails_performance/javascripts/#{file}")}</script>"
end


51
52
53
54
55
56
57
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 51

def link_to_path(e)
  if e[:method] == 'GET'
    link_to(short_path(e[:path]), e[:path], target: '_blank', title: short_path(e[:path]))
  else
    short_path(e[:path])
  end
end

#ms(value, limit = 1) ⇒ Object



38
39
40
41
42
43
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 38

def ms(value, limit = 1)
  result = round_it(value, limit)
  return '-' if result.nil?

  result && result != 0 ? "#{result} ms" : '< 0 ms'
end

#mss(value, limit = 1) ⇒ Object



34
35
36
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 34

def mss(value, limit = 1)
  ms(value.to_f * 1000, limit)
end

#report_name(h) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 59

def report_name(h)
  h.except(:on).collect do |k, v|
    next if v.blank?

    %Q{
    <div class="control">
      <span class="tags has-addons">
        <span class="tag">#{k}</span>
        <span class="tag is-info is-light">#{v}</span>
      </span>
    </div>}
  end.compact.join.html_safe
end

#round_it(value, limit = 1) ⇒ Object



4
5
6
7
8
9
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 4

def round_it(value, limit = 1)
  return nil unless value
  return value if value.is_a?(Integer)

  value.nan? ? nil : value.round(limit)
end

#short_path(path, length: 55) ⇒ Object



45
46
47
48
49
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 45

def short_path(path, length: 55)
   :span, title: path do
    truncate(path, length: length)
  end
end

#status_tag(status) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 73

def status_tag(status)
  klass = case status.to_s
  when /error/
    "tag is-danger"
  when /^5/
    "tag is-danger"
  when /^4/
    "tag is-warning"
  when /^3/
    "tag is-info"
  when /^2/
    "tag is-success"
  when /success/
    "tag is-success"
  else
    nil
  end
  (:span, class: klass) do
    status
  end
end