Class: Store::Vaultaire

Inherits:
Store
  • Object
show all
Includes:
Helpers
Defined in:
lib/store/vaultaire.rb

Overview

Storage for Vaultaire TSDB (github.com/anchor/vaultaire) via it's RESTful backend (github.com/anchor/sieste)

Constant Summary

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

Constructor Details

#initialize(origin, settings) ⇒ Vaultaire

Returns a new instance of Vaultaire


5
6
7
8
# File 'lib/store/vaultaire.rb', line 5

def initialize origin, settings
	super
	@base_url = mandatory_param :host, "store_settings"
end

Instance Method Details

#get_metric(m, start, stop, step) ⇒ Object

Get the metric data from sieste/vaultaire, and do some basic validations on the result


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/store/vaultaire.rb', line 98

def get_metric m, start, stop, step
	uri = get_metric_url m, start, stop, step

               factor = 1000000000
               data = json_metrics uri

               if (data.is_a? Hash) then
                       if data[:error] then
                               raise Store::Error, "Sieste Exception raised: #{data[:error]}"
                       end
               end

               metric = []
               data.each do |node|
                       metric << {x: node[0] / factor, y: node[1]}
               end

               if data.empty? then
                       raise Store::Error, "No data returned from sieste query for #{m.titleize}"
               end

               if stop - start == step then
                       # only one point required so get next closest to start
                       return [metric.select{|a| a[:x] >= start}.first]
               end

	data_sanitize metric, start, stop, step
end

#get_metric_url(m, start, stop, step) ⇒ Object

Generate the url for the datastream for a given metric


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/store/vaultaire.rb', line 68

def get_metric_url m, start, stop, step
	query = []

	metakeys = m.
	float = metakeys["_float"]

	_start, _stop = validate_time(start, stop)

               query << "start=#{_start}"
               query << "end=#{_stop}"
               query << "interval=#{step}"
               query << "as_double=true" if float

               query_string = "?" + query.join("&")

               uri = "#{@base_url}/interpolated/#{@origin_id}/#{m.metric_id}#{query_string}"

	return uri
end

#get_metrics_listObject

Return an empty array. This backend isn't cached


93
94
95
# File 'lib/store/vaultaire.rb', line 93

def get_metrics_list
	return []
end

#machiavelli_encode(m) ⇒ Object

Convert a sieste-encoded metric into a machiavelli one


39
40
41
# File 'lib/store/vaultaire.rb', line 39

def machiavelli_encode m
        m.gsub(SEP,":")
end

#metadata(metric_id) ⇒ Object

Sieste can be used to query for a metric's metadata based on it's origin and metric_id alone


11
12
13
14
15
# File 'lib/store/vaultaire.rb', line 11

def  metric_id
	meta = get_json("#{@base_url}/simple/search?origin=#{@origin_id}&address=#{metric_id}").first
	keys = keysplit(machiavelli_encode(meta))
	return keys
end

#metadata_table(metric) ⇒ Object

Split the metadata into nice pieces and make a HTML table.


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/store/vaultaire.rb', line 18

def  metric
	t = metric.map{|k,v| [k,v]}

	table = ""
	t.each {|a|
		table += "<tr><td>#{a[0]}</td><td> = #{a[1]}</td></tr>"
	}

	header = "<tr><td>#{@origin_id}</td><td> - #{self.class.name}</td></tr>"
	return "<table style='text-align: left'>#{header}#{table}</table>"
end

#refresh_metrics_cacheObject

Do nothing. This backend isn't cached


89
90
# File 'lib/store/vaultaire.rb', line 89

def refresh_metrics_cache
end

#search_metrics(q, args = {}) ⇒ Object

Use Sieste to search for metrics given an origin id and a query string


54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/store/vaultaire.rb', line 54

def search_metrics q, args={}
	page = args[:page] || 1
               page_size = args[:page_size] || 25
               uri = "#{@base_url}/simple/search?origin=#{@origin_id}&q=#{q}&page=#{page - 1}&page_size=#{page_size}"
               result = json_metrics_list uri

	# Beta Sieste - remove known bad metadata items
	result.delete_if{|a| a.include? "%3b"} # semicolon
               result.delete_if{|a| !a.include? DELIM} # remove address-only listings

               result.map{|x| "#{@origin_id}#{SEP}#{machiavelli_encode x}"}
end

#sieste_encode(m) ⇒ Object

Convert a string into a uri-transferable sieste metric


31
32
33
34
35
36
# File 'lib/store/vaultaire.rb', line 31

def sieste_encode m
        n = m.gsub(":",SEP)
        replace = [ ["/", "%2f"], ["_","%5f"]]
        replace.each { |r| n.gsub!(r[0], r[1]) }
        n
end

#validate_time(start, stop) ⇒ Object

Monkeypatch to prevent data pulls from when there is known to not be data


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

def validate_time start, stop
	limit_start = 1405916335 # collectors 2.1 staart
               if start < limit_start
                       puts "Limit start to #{limit_start}"
                       start = limit_start
               end
               return start, stop
end