Class: Metric

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/metric.rb

Overview

The metric object

- A `metric` represents a stream of data collected from a `source` and stored in a `store`

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(metric) ⇒ Metric

Take all the YAML file settings, and store them more user friendly forms


7
8
9
10
11
12
13
# File 'lib/metric.rb', line 7

def initialize metric
	metric_id = get_id metric
	@origin_id, @metric_id = metric_id.split(SEP)
	@settings = origin_settings(@origin_id).last
	@store = init_store @settings.store, @origin_id, @settings
	@source = init_source @settings.source, @origin_id, @settings
end

Instance Method Details

#build_id(o = @origin_id, m = @metric_id) ⇒ Object

build the metric's unique identifer, using the global variables if none are provided


84
85
86
# File 'lib/metric.rb', line 84

def build_id o=@origin_id, m=@metric_id
	"#{o}#{SEP}#{m}"
end

#feedObject

Generate the metric controller feed for the metric


45
46
47
# File 'lib/metric.rb', line 45

def feed
	"/metric/?metric=#{@origin_id}#{SEP}#{@metric_id}"
end

#get_id(str) ⇒ Object

Given an id, metadata string or metric_id, return the metric's id


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/metric.rb', line 89

def get_id str
	return str if is_id? str
	if is_metadata? str
		if str.include? SEP
			# Origin~[meta]
			origin, str = str.split(SEP) 
		end
		# Split to get address, and rebuild
		keys = keysplit(str)
		return build_id origin, keys["address"]

	elsif is_metric_id? str
		return build_id @origin_id, str
	end
end

#get_metric(start, stop, step) ⇒ Object

Use the store to pull data for the metric


64
65
66
# File 'lib/metric.rb', line 64

def get_metric start, stop, step
	@store.get_metric self, start, stop, step
end

#get_metric_url(start, stop, step) ⇒ Object

Use the store to generate the metric's external url


59
60
61
# File 'lib/metric.rb', line 59

def get_metric_url start, stop, step
	@store.get_metric_url self, start, stop, step
end

#idObject

id accessor method, which leverages a builder function


79
80
81
# File 'lib/metric.rb', line 79

def id
	build_id
end

#is_id?(str) ⇒ Boolean

True if Locator16~HashIdentifer (from anchor/vaultaire-common)

Returns:

  • (Boolean)

106
107
108
109
110
# File 'lib/metric.rb', line 106

def is_id? str
        match = [KVP,DELIM]
        return false if match.any? {|w| str.include? w}
        return true
end

#is_metadata?(str) ⇒ Boolean

True if it contains keys and values and delimiters

Returns:

  • (Boolean)

113
114
115
116
117
# File 'lib/metric.rb', line 113

def is_metadata? str
        match = [KVP,DELIM]
        return true if match.any? {|w| str.include? w}
        return false
end

#is_metric_id?(str) ⇒ Boolean

True if hashIdentifier only (from anchor/vaultaire-common)

Returns:

  • (Boolean)

120
121
122
123
124
# File 'lib/metric.rb', line 120

def is_metric_id? str
        match = [KVP,DELIM,SEP]
        return false if match.any? {|w| str.include? w}
        return true
end

#live?Boolean

Use the store to query about the liveliness of the metric

Returns:

  • (Boolean)

74
75
76
# File 'lib/metric.rb', line 74

def live?
	@store.live?
end

#metadataObject

metadata accessor function, but generate it only once


50
51
52
53
54
55
56
# File 'lib/metric.rb', line 50

def 
	if @meta.nil?
		@meta = @store. @metric_id
		@meta = @source.metaadd @meta
	end
	return @meta
end

#metadata_tableObject

Use the store to generate a nice html table for the metadata


69
70
71
# File 'lib/metric.rb', line 69

def 
	@store. 
end

#metric_idObject

metric_id accessor function


21
22
23
# File 'lib/metric.rb', line 21

def metric_id
	@metric_id
end

#origin_idObject

origin_id accessor function


16
17
18
# File 'lib/metric.rb', line 16

def origin_id
		@origin_id
end

#sourceObject

source accessor function


26
27
28
# File 'lib/metric.rb', line 26

def source
	@source
end

#storeObject

store accessor function


31
32
33
# File 'lib/metric.rb', line 31

def store
	@store
end

#titleizeObject

Get the human readable metric title from the available metadata, and prepend the title, or the origin_id


36
37
38
39
40
41
42
# File 'lib/metric.rb', line 36

def titleize
	if @title.nil?
		meta = @source.titleize 
		@title = "#{@settings.title || @origin_id} - #{meta}"
	end
	return @title
end