Module: Layouts::ApplicationLayoutHelper

Includes:
Helpers
Included in:
GraphsController, MetricsController
Defined in:
app/helpers/layouts/application_layout_helper.rb

Constant Summary collapse

UI_DEFAULTS =

Defaults for our UI

{	
	start: "3h",
	stop: "now",
	points: 600,
	graph: "standard",
	time: "relative",
	render: "line",
	stack: "off",
	clock: "utc",
	zeromin: "off"
}

Constants included from Helpers

Helpers::REDIS_KEY

Instance Method Summary collapse

Methods included from Helpers

#data_sanitize, #delete_metrics_cache, #get_json, #init_lib, #init_source, #init_store, #interpolate, #is_up?, #json_metrics, #json_metrics_list, #keysplit, #mandatory_param, #optional_param, #origin_settings, #redis_conn, #top_tail_pad

Instance Method Details

#add_qs(k, v, p = {}) ⇒ Object

`Add` the key & value


152
# File 'app/helpers/layouts/application_layout_helper.rb', line 152

def add_qs k,v,p={}; alter_qs :add, k,v,p; end

#alter_qs(method, k, v, p = {}) ⇒ Object

Alter Query String using the flags from the *_qs methods


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'app/helpers/layouts/application_layout_helper.rb', line 187

def alter_qs method, k, v, p={}
	k = k.to_s
	hash = query_hash p
	case method
		when :chk
			return (hash[k] ? (hash[k] == v ? true : false) : nil)
		when :que #ry
			return Array(hash[k]) || []
		when :add
			if v.is_a? Array 
				hash[k] ? hash[k] += v : hash[k] = v
			else
				hash[k] ? hash[k] << v : hash[k] = v
			end
		when :chg 
			hash[k] = v
		when :rem #ove
			x = Array(hash[k])
			x.delete(v)
			x.empty? ? hash.delete(k) : hash[k] = x
		when :obl #iterate
			hash.delete(k)
	end
	
	hash_query hash
end

#check_param(s, allowed) ⇒ Object


135
136
137
138
# File 'app/helpers/layouts/application_layout_helper.rb', line 135

def check_param(s,allowed)
	v = get_param(s)
	v if allowed.include? v.to_s
end

#chg_qs(k, v, p = {}) ⇒ Object

`Change` the key &


149
# File 'app/helpers/layouts/application_layout_helper.rb', line 149

def chg_qs k,v,p={}; alter_qs :chg, k,v,p; end

#chk_qs(k, v, p = {}) ⇒ Object

`Check` the key matches the value provided


143
# File 'app/helpers/layouts/application_layout_helper.rb', line 143

def chk_qs k,v,p={}; alter_qs :chk, k,v,p; end

Generator for generic Dropdowns


84
85
86
87
88
89
90
91
92
93
# File 'app/helpers/layouts/application_layout_helper.rb', line 84

def dropdown inner, args={}
	a = []
	prompt = (args && args[:prompt]) ? args[:prompt] + "  " : ""
	a << '<a type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">'+prompt+'<span class="caret">'\
	     '</span><span class="sr-only">Toggle Dropdown</span></a>'

	a << inner

	a.flatten
end

#flash_class(level) ⇒ Object

Flash message styling based on the error level


60
61
62
63
64
65
66
67
# File 'app/helpers/layouts/application_layout_helper.rb', line 60

def flash_class(level)
    case level
	when :notice  then "alert alert-info"
	when :success then "alert alert-success"
	when :error   then "alert alert-danger"
	when :warning then "alert alert-warning"
    end
end

#get_param(s) ⇒ Object

Get the parameter from the HREF params, or use our default


30
31
32
33
34
# File 'app/helpers/layouts/application_layout_helper.rb', line 30

def get_param s
	p = que_qs(s).first
	p = ui_default(s) if p.nil?
	p
end

#hash_query(hash) ⇒ Object

Convert a hash into a query string


180
181
182
183
184
# File 'app/helpers/layouts/application_layout_helper.rb', line 180

def hash_query hash
	x = []
	hash.each {|l,m| Array(m).each {|a| x << "#{l}=#{a}"}}
	"?#{x.join("&")}"
end

#is_epoch(s) ⇒ Object

True if the parameter is an integer (not a good epoch check, but the best we can get)


217
218
219
# File 'app/helpers/layouts/application_layout_helper.rb', line 217

def is_epoch s 
	s.to_i.to_s == s
end

#is_nicetime(s) ⇒ Object

True if the parameter is a “nice-time” (e.g. 3h, 12d)


227
228
229
230
231
# File 'app/helpers/layouts/application_layout_helper.rb', line 227

def is_nicetime s
	return false if is_epoch(s) #fast fail
	a,b = nicetime_split s
	return true if a.to_i.to_s == a and b.is_a? String
end

Generator for Navbar Buttons


71
72
73
74
75
76
77
78
79
80
81
# File 'app/helpers/layouts/application_layout_helper.rb', line 71

def navbar_buttons param, buttons, args={}
	a = []
	buttons.each do |b|
		html = "<a type='button' class='btn btn-default "
		p = chk_qs(param,b) 
		html += "active" if (p || p.nil? && UI_DEFAULTS[param] == b) || args[:active]
		html += "' href='"+ chg_qs(param, b) +"'>"+b+"</a>"
		a << html
	end
	a
end

Generator for Navbar Dropdowns


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/helpers/layouts/application_layout_helper.rb', line 96

def navbar_dropdown param, buttons, args={}
	p = que_qs(param).first || UI_DEFAULTS[param]
	active, list = buttons.partition{|b| b == p}

	b = []

	b << '<ul class="dropdown-menu" style="min-width: 0px; left: 0px">'
	buttons.each {|l|
		c = l == p ? "class='active'" : ""
		b << "<li #{c}><a href='#{chg_qs(param,l)}'>#{l}</a></li>"
	}

	b << '</ul>'
	a = active.first

	prompt_value = args[:prompt_value] || a

	label = (args[:label]) ? "#{param.to_s}:  #{prompt_value}  " : prompt_value
	dropdown(b, {prompt: label}).flatten

end

#nicetime_split(s) ⇒ Object

Split a string into numerics and non numeric characters for later parsing


222
223
224
# File 'app/helpers/layouts/application_layout_helper.rb', line 222

def nicetime_split s
	s.split(/(\d+)/).reject{|c| c.empty?}
end

#obl_qs(k, p = {}) ⇒ Object

Completely `obliterate` the key and value


158
# File 'app/helpers/layouts/application_layout_helper.rb', line 158

def obl_qs k,  p={}; alter_qs :obl, k, false, p; end

#que_qs(k, p = {}) ⇒ Object

`Query` the key


146
# File 'app/helpers/layouts/application_layout_helper.rb', line 146

def que_qs k,  p={}; alter_qs :que, k, false, p; end

#query_hash(p = {}) ⇒ Object

From the provided query string, or part of the rails `request`, return a hah of of the query string Required due to overloading of single-value parameters (`string[]=`)


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/helpers/layouts/application_layout_helper.rb', line 162

def query_hash p={}
	url = case 
		when p[:url] == :referer 
			request.referer
		when p[:url].is_a?(String)
			p[:url]
		else 
			request.url
		end

	query = URI::parse(url).query
	query.gsub!("metric=","metric[]=") if query
	query.gsub!("right=","right[]=") if query

	Rack::Utils.parse_nested_query(query) || {} 
end

#refresh_errors(method = :show, error = nil) ⇒ Object

Alter the Refresh error listing in redis


119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/helpers/layouts/application_layout_helper.rb', line 119

def refresh_errors method=:show, error=nil
	one = "#"; two = "$"; key = "Machiavelli:RefreshErrors"
	if method == :save
		redis_conn.set key, error.map{|a| a.join(one)}.join(two)
	elsif method == :remove
		redis_conn.del key
	else
		e = redis_conn.get key 
		return [[]] if e.nil?
		e.split(two).map{|a| a.split(one)}
	end
end

#rem_qs(k, v, p = {}) ⇒ Object

`Remove` the key and value


155
# File 'app/helpers/layouts/application_layout_helper.rb', line 155

def rem_qs k,v,p={}; alter_qs :rem, k,v,p; end

#settings_suppress(settings) ⇒ Object


249
250
251
# File 'app/helpers/layouts/application_layout_helper.rb', line 249

def settings_suppress settings
	return (init_store settings.store, "ABC", settings).supress_settings
end

#to_bool(s) ⇒ Object


132
133
134
# File 'app/helpers/layouts/application_layout_helper.rb', line 132

def to_bool(s)
	return s == "on" ? true : false; 
end

#to_epoch(s) ⇒ Object

Convert a “nice time” into it's absolute seconds since epoch time.


234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'app/helpers/layouts/application_layout_helper.rb', line 234

def to_epoch s
	return "" if s.nil?
	return s.to_i if is_epoch(s)
	s = "0h" if s == "now"
	mult, time = nicetime_split s

	time_scale = (case time
		when "min"; "minutes"
		when "h"; "hours"
		when "d"; "days"
		when "w"; "weeks"
		end )
	eval("#{mult}.#{time_scale}.ago.to_i")
end

#ui_default(s) ⇒ Object

Method helper for defaults to the UI


25
26
27
# File 'app/helpers/layouts/application_layout_helper.rb', line 25

def ui_default s
	UI_DEFAULTS[s]
end

#ui_message(msg) ⇒ Object

UI Error/informational messages


37
38
39
40
41
42
43
# File 'app/helpers/layouts/application_layout_helper.rb', line 37

def ui_message msg
	case msg
	when :no_graphs_selected; "No graphs selected. You should <a href='#filter_metrics' role='button' data-toggle='modal' data-target='#filter_metrics'>search</a> for one."
	when :no_backends; "No backends configured. Check your config/settings/{ENV}.yml file."
	else "UI MESSAGE VARIABLE NOT FOUND: #{msg}"
	end
end

#versionObject

Read from the git tags, via git-version-bump, or from the .gvb_version file, for our version


46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/helpers/layouts/application_layout_helper.rb', line 46

def version
	begin
		v = "#{GVB.major_version}.#{GVB.minor_version}.#{GVB.patch_version}"
	rescue
		v = File.read(".gvb_version") if File.exists?(".gvb_version")
	end
	# If we got a version then use it to construct a link to the github tag of the same
	if v
		l = link_to v, "https://github.com/anchor/machiavelli/releases/tag/v#{v}", target: "blank" if v
		return "version #{l} ".html_safe
	end
end