Module: Pencil::Helpers

Includes:
Models
Defined in:
lib/pencil/helpers.rb

Constant Summary collapse

@@prefs =
[["Start Time", "start"],
["Duration", "duration"],
["Width", "width"],
["Height", "height"]]

Instance Method Summary collapse

Instance Method Details

#append_query_string(str) ⇒ Object



115
116
117
118
119
# File 'lib/pencil/helpers.rb', line 115

def append_query_string(str)
  v = str.dup
  (v << "?#{request.query_string}") unless request.query_string.empty?
  return v
end

#cluster_graph(g, cluster, title = "wtf") ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/pencil/helpers.rb', line 18

def cluster_graph(g, cluster, title="wtf")
  image_url = \
  @dash.render_cluster_graph(g, cluster,
                             :title => title,
                             :dynamic_url_opts => merge_opts)
  zoom_url = cluster_graph_link(@dash, g, cluster)
  return image_url, zoom_url
end


27
28
29
30
31
# File 'lib/pencil/helpers.rb', line 27

def cluster_graph_link(dash, g, cluster)
  link = dash.graph_opts[g]["click"] ||
    "/dash/#{cluster}/#{dash.name}/#{g.name}"
  return append_query_string(link)
end


90
91
92
# File 'lib/pencil/helpers.rb', line 90

def cluster_link(cluster)
  return append_query_string("/dash/#{cluster}")
end

#cluster_selectorObject



144
145
146
147
# File 'lib/pencil/helpers.rb', line 144

def cluster_selector
  @clusters = settings.config.clusters.sort + ["global"]
  erb :'partials/cluster_selector', :layout => false
end

#cluster_switcher(clusters) ⇒ Object



131
132
133
134
# File 'lib/pencil/helpers.rb', line 131

def cluster_switcher(clusters)
  @clusters = clusters
  erb :'partials/cluster_switcher', :layout => false
end

#cluster_zoom_graph(g, cluster, host, title) ⇒ Object



33
34
35
36
37
38
# File 'lib/pencil/helpers.rb', line 33

def cluster_zoom_graph(g, cluster, host, title)
  image_url = g.render_url([host.name], [cluster], :title => title,
                           :dynamic_url_opts => merge_opts)
  zoom_url = cluster_zoom_link(cluster, host)
  return image_url, zoom_url
end


40
41
42
# File 'lib/pencil/helpers.rb', line 40

def cluster_zoom_link(cluster, host)
  return append_query_string("/host/#{cluster}/#{host}")
end

#css_urlObject

fixme this isn’t used anymore, but should



95
96
97
98
99
100
# File 'lib/pencil/helpers.rb', line 95

def css_url
  style = File.join(settings.root, "public/style.css")
  mtime = File.mtime(style).to_i.to_s
  return \
  %Q[<link href="/style.css?#{mtime}" rel="stylesheet" type="text/css">]
end


86
87
88
# File 'lib/pencil/helpers.rb', line 86

def dash_link(dash, cluster)
  return append_query_string("/dash/#{cluster}/#{dash.name}")
end

#dash_switcherObject



136
137
138
# File 'lib/pencil/helpers.rb', line 136

def dash_switcher
  erb :'partials/dash_switcher', :layout => false
end


159
160
161
162
# File 'lib/pencil/helpers.rb', line 159

def dash_uplink
  link = append_query_string(request.path.split("/")[0..-2].join("/"))
  "zoom out: <a href=\"#{link}\">#{@params[:cluster]}</a>"
end

#graph_switcherObject



140
141
142
# File 'lib/pencil/helpers.rb', line 140

def graph_switcher
  erb :'partials/graph_switcher', :layout => false
end


154
155
156
157
# File 'lib/pencil/helpers.rb', line 154

def graph_uplink
  link = append_query_string(request.path.split("/")[0..-2].join("/"))
  "zoom out: <a href=\"#{link}\">#{@dash}</a>"
end

#header(str) ⇒ Object

fixme this is a hack



165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/pencil/helpers.rb', line 165

def header(str)
  <<-FOO
  <div class="path_container">
            <h2>#{@title}</h2>
              <span class="zoom">
              #{str}
              </span>
          </div>
          <div id="timeslice_container">
            <h3 id="timeslice">#{range_string}</h3>
            #{permalink unless @no_graphs}
          </div>
  FOO
end


149
150
151
152
# File 'lib/pencil/helpers.rb', line 149

def host_uplink
  link = "/dash/#{append_query_string(@host.cluster)}"
  "zoom out: <a href=\"#{link}\">#{@host.cluster}</a>"
end

#hosts_selector(hosts, print_clusters = false) ⇒ Object



109
110
111
112
113
# File 'lib/pencil/helpers.rb', line 109

def hosts_selector(hosts, print_clusters=false)
  @print_clusters = print_clusters
  @hosts = hosts
  erb :'partials/hosts_selector', :layout => false
end

#input_boxesObject

generate the input box fields, filled in to current parameters if specified



81
82
83
84
# File 'lib/pencil/helpers.rb', line 81

def input_boxes
  @prefs = @@prefs
  erb :'partials/input_boxes', :layout => false
end

#merge_optsObject



121
122
123
124
125
# File 'lib/pencil/helpers.rb', line 121

def merge_opts
  static_opts = ["cluster", "dashboard", "zoom", "host", "session_id"]
  opts = params.dup
  session.merge(opts).delete_if { |k,v| static_opts.member?(k) || v.empty? }
end

#nowishObject



180
181
182
183
184
185
186
# File 'lib/pencil/helpers.rb', line 180

def nowish
  if settings.config.global_config[:now_threshold] == false
    return false
  end
  threshold = settings.config.global_config[:now_threshold] || 300
  return @request_time.to_i - @etime.to_i < threshold
end

#param_lookup(name) ⇒ Object

convert keys to symbols before lookup



11
12
13
14
15
16
# File 'lib/pencil/helpers.rb', line 11

def param_lookup(name)
  sym_hash = {}
  session.each { |k,v| sym_hash[k.to_sym] = v unless v.empty? }
  params.each { |k,v| sym_hash[k.to_sym] = v unless v.empty? }
  settings.config.global_config[:url_opts].merge(sym_hash)[name.to_sym]
end


206
207
208
209
210
211
212
213
# File 'lib/pencil/helpers.rb', line 206

def permalink
  return "" unless @stime && @duration
  format = "%F %T" # chronic REALLY understands this
  url = request.path + "?"
  url << "&amp;start=#{@stime.strftime(format)}"
  url << "&amp;duration=#{ChronicDuration.output(@duration)}"
  "<a id=\"permalink\" href=\"#{url}\">permalink</a>"
end

#range_stringObject



188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/pencil/helpers.rb', line 188

def range_string
  format = settings.config.global_config[:date_format] || "%X %x"
  if @stime && @etime
    if nowish
      "timeslice: from #{@stime.strftime(format)}"
    else
      "timeslice: #{@stime.strftime(format)} - #{@etime.strftime(format)}"
    end
  else
    "invalid time range"
  end

end

#refreshObject



102
103
104
105
106
107
# File 'lib/pencil/helpers.rb', line 102

def refresh
  if settings.config.global_config[:refresh_rate] != false && nowish
    rate = settings.config.global_config[:refresh_rate] || 60
    return %Q[<meta http-equiv="refresh" content="#{rate}">]
  end
end

#shortcuts(str) ⇒ Object



127
128
129
130
# File 'lib/pencil/helpers.rb', line 127

def shortcuts(str)
  @str = str
  erb :'partials/shortcuts', :layout => false
end


44
45
46
47
48
49
50
51
# File 'lib/pencil/helpers.rb', line 44

def suggest_cluster_links(clusters, g)
  links = []
  clusters.each do |c|
    href = append_query_string("/dash/#{c}/#{params[:dashboard]}/#{g.name}")
    links << "<a href=\"#{href}\">#{c}</a>"
  end
  return "zoom (" + links.join(", ") + ")"
end

#suggest_dashboards(host, graph) ⇒ Object

it’s mildly annoying that when this set is empty there’re no uplinks consider adding a link up to the cluster (which is best we can do)



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pencil/helpers.rb', line 67

def suggest_dashboards(host, graph)
  ret = Set.new

  host.graphs.each do |g|
    Dashboard.find_by_graph(g).each do |d|
      valid, _ = d.get_valid_hosts(g, host['cluster'])
      ret << d.name if valid.member?(host)
    end
  end

  return ret
end


53
54
55
56
57
58
59
60
61
62
63
# File 'lib/pencil/helpers.rb', line 53

def suggest_dashboards_links(host, graph)
  suggested = suggest_dashboards(host, graph)
  return "" if suggested.length == 0

  links = []
  suggested.each do |d|
    links << "<a href=\"/dash/#{host.cluster}/#{append_query_string(d)}\">" +
      "#{d}</a>"
  end
  return "(" + links.join(", ") + ")"
end

#valid_time(s) ⇒ Object



202
203
204
# File 'lib/pencil/helpers.rb', line 202

def valid_time (s)
  Chronic.parse(s) || s =~ /^\d+$/
end