Class: MetricsController

Inherits:
ApplicationController show all
Includes:
Layouts::ApplicationLayoutHelper
Defined in:
app/controllers/metrics_controller.rb

Overview

Controller for Metrics Because metrics can come from many source, we serve them from our own itty bitty endpoint, passing them through the ruby server-side code and preventing their exposure directly to the user.

Constant Summary collapse

START =
60*60*3
STOP =
0
STEP =
10

Constants included from Layouts::ApplicationLayoutHelper

Layouts::ApplicationLayoutHelper::UI_DEFAULTS

Constants included from Helpers

Helpers::REDIS_KEY

Instance Method Summary collapse

Methods included from Layouts::ApplicationLayoutHelper

#add_qs, #alter_qs, #check_param, #chg_qs, #chk_qs, #dropdown, #flash_class, #get_param, #hash_query, #is_epoch, #is_nicetime, #navbar_buttons, #navbar_dropdown, #nicetime_split, #obl_qs, #que_qs, #query_hash, #refresh_errors, #rem_qs, #settings_suppress, #to_bool, #to_epoch, #ui_default, #ui_message, #version

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

#getObject

Basic GET point - given a metric name only, and provided or default 3st parameters, get the metric information


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/metrics_controller.rb', line 13

def get
	unless params[:metric] then
		render json: {error: "must provide a metric"}
		return
	end
	m = params[:metric]

	now = Time.now().to_i
	start  = (params[:start] || now - START).to_i 
	stop   = (params[:stop] || now - STOP).to_i
	step   = (params[:step] || STEP).to_i

	begin
		metric = Metric.new(m).get_metric(start, stop, step)
		render json: metric	
	rescue StandardError, Store::Error => e
		render json: { error: e.to_s } 
		return
	end
end

#listObject

List all the metrics available, limited by search terms if given


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/metrics_controller.rb', line 35

def list

	settings_origins =  Settings.origins.map{|a,b| a.to_s}
	b = [params[:origin] || settings_origins].flatten

	search = params[:q] || "*"

	page = params[:page]
	page ||= 1

	page_size = params[:page_size] || 25
	
	search.gsub!(" ","*") 
	list = []

	# For all the backends, search their metric listings
	b.each do |x|
		begin
			origin, settings = Settings.origins.find{|o,k| o.to_s == x}
			be = init_store settings.store, origin, settings
			ret = be.search_metrics(search, { page: page.to_i, page_size: page_size.to_i})
			ret.each do |r| 
				m = Metric.new r
				list << {id: m.id, text: m.titleize}
			end
		rescue Store::Error, Errno::ECONNREFUSED => e
			 # Do not render errors if we are in an AJAX callback
			 unless  params[:callback] then
				 render json: { error: e.to_s } 
				 return
			 end
		end
	end

	list.flatten!

	# Sort it outselves
	list.sort!{|i,j| i[:text] <=> j[:text]}

	if params[:callback] then
		# Generate a nice AJAX callback listing
		render json: "#{params[:callback]}({metrics:#{list.to_json}});"
	else
		# Just return the IDs. Not usually called from the UI.
		render json: list.map{|x| x[:id]}.to_json
	end
	
end