Class: Expri::Metric

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

Constant Summary collapse

ATTRIBUTE_DEFAULTS =
{ :frequency => 3 }.freeze
VALID_ATTRIBUTES =
%w{divisor frequency key percentile predicate predicate2
type window value}.map { |x| x.to_sym }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes = {}, options = {}) ⇒ Metric

Returns a new instance of Metric.



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

def initialize(name, attributes={}, options={})
  @name           = name
  @options        = options
  self.attributes = attributes
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



7
8
9
# File 'lib/expri/metric.rb', line 7

def attributes
  @attributes
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/expri/metric.rb', line 7

def name
  @name
end

#optionsObject

Returns the value of attribute options.



7
8
9
# File 'lib/expri/metric.rb', line 7

def options
  @options
end

Class Method Details

.list(options = {}) ⇒ Object



9
10
11
12
# File 'lib/expri/metric.rb', line 9

def self.list(options={})
  metric = Metric.new(nil, {}, {})
  MultiJson.decode(metric.api.get(path: "/metrics", expects: 200).body)
end

Instance Method Details

#==(other) ⇒ Object



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

def ==(other)
  attributes_without_defaults == other.attributes_without_defaults
end

#apiObject



24
25
26
# File 'lib/expri/metric.rb', line 24

def api
  @api ||= Excon.new(api_url)
end

#attributes_without_defaultsObject

only the attributes that are not at their default value



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

def attributes_without_defaults
  attributes = @attributes.dup
  attributes.delete_if { |k, v| ATTRIBUTE_DEFAULTS[k] == v }
end

#destroyObject



49
50
51
52
53
# File 'lib/expri/metric.rb', line 49

def destroy
  puts "destroy name=#{name}" if @options[:verbose]
  api.delete(path: "/metrics/#{name}", body: MultiJson.encode(@attributes),
    expects: 200)
end

#getObject



55
56
57
58
59
# File 'lib/expri/metric.rb', line 55

def get
  puts "show name=#{name}" if @options[:verbose]
  self.attributes =
    MultiJson.decode(api.get(path: "/metrics/#{name}", expects: 200).body)
end

#postObject



61
62
63
64
65
66
67
68
69
# File 'lib/expri/metric.rb', line 61

def post
  puts "create name=#{name}" if @options[:verbose]
  # exprd's put is like most api's post
  api.put(path: "/metrics/#{name}", body: MultiJson.encode(@attributes),
    expects: 200)
rescue Excon::Errors::BadRequest => e
  message = MultiJson.decode(e.response.body)["message"]
  raise(message)
end

#putObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/expri/metric.rb', line 71

def put
  begin
    existing = Metric.new(name, {}, @options)
    existing.get
    if self != existing
      existing.destroy
      post
    end
  rescue Excon::Errors::NotFound
    post
  end
end