Class: Pencil::Models::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/pencil/models/base.rb

Direct Known Subclasses

Dashboard, Graph, Host

Constant Summary collapse

@@objects =
Hash.new { |h, k| h[k] = Hash.new }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, params = {}) ⇒ Base

Returns a new instance of Base.



7
8
9
10
11
12
# File 'lib/pencil/models/base.rb', line 7

def initialize(name, params={})
  @name = name
  @match_name = name
  @params = params
  @@objects[self.class.to_s][name] = self
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/pencil/models/base.rb', line 5

def name
  @name
end

Class Method Details

.allObject



23
24
25
# File 'lib/pencil/models/base.rb', line 23

def self.all
  return @@objects[self.name].values
end

.each(&block) ⇒ Object



18
19
20
21
# File 'lib/pencil/models/base.rb', line 18

def self.each(&block)
  h = @@objects[self.name] rescue {}
  h.each { |k, v| yield(k, v) }
end

.find(name) ⇒ Object



14
15
16
# File 'lib/pencil/models/base.rb', line 14

def self.find(name)
  return @@objects[self.name][name] rescue []
end

Instance Method Details

#<=>(other) ⇒ Object



53
54
55
# File 'lib/pencil/models/base.rb', line 53

def <=>(other)
  return to_s <=> other.to_s
end

#[](key) ⇒ Object



27
28
29
# File 'lib/pencil/models/base.rb', line 27

def [](key)
  return @params[key] rescue []
end

#compose_metric(m, c, h) ⇒ Object

compose a metric using a :metric_format format string with %c for metric, %c for cluster, and %h for host



63
64
65
# File 'lib/pencil/models/base.rb', line 63

def compose_metric (m, c, h)
  @params[:metric_format].dup.gsub("%m", m).gsub("%c", c).gsub("%h", h)
end

#compose_metric2(m, c, h) ⇒ Object

used to make sure partial metrics are not considered e.g. foo.bar.baz.colo1.host1 but not

foo.bar.baz.subkey.subkey2[.colo1.host1]

where the latter is returned by the graphite expand api but not as a full metric essentially equivalent to matching like /[:METRIC:]$/



73
74
75
# File 'lib/pencil/models/base.rb', line 73

def compose_metric2 (m, c, h)
  compose_metric(m, c, h) + ".*"
end

#match(glob) ⇒ Object



31
32
33
34
35
36
# File 'lib/pencil/models/base.rb', line 31

def match(glob)
  return true if glob == '*'
  # convert glob to a regular expression
  glob_re = /^#{Regexp.escape(glob).gsub('\*', '.*').gsub('\#', '\d+')}$/
  return @match_name.match(glob_re)
end

#multi_match(globs) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/pencil/models/base.rb', line 38

def multi_match(globs)
  ret = false

  globs.each do |glob|
    ret = match(glob)
    break if ret
  end

  return ret
end

#to_sObject



49
50
51
# File 'lib/pencil/models/base.rb', line 49

def to_s
  return @name
end

#update_params(hash) ⇒ Object



57
58
59
# File 'lib/pencil/models/base.rb', line 57

def update_params(hash)
  @params.merge!(hash)
end