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)


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

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 92

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



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

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



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

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

#format_datetime(e) ⇒ Object



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

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



107
108
109
110
111
112
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 107

def icon(name)
  @icons ||= {}

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

#insert_css_file(file) ⇒ Object



114
115
116
# File 'app/helpers/rails_performance/rails_performance_helper.rb', line 114

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



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

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


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

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



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

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



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

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

#report_name(h) ⇒ Object



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

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

    %(
    <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



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

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



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

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

#status_tag(status) ⇒ Object



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

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"
  end
  (:span, class: klass) do
    status
  end
end