Class: AWS::CloudWatch::Metric

Inherits:
MetricBase show all
Defined in:
lib/aws/cloud_watch/metric.rb

Constant Summary collapse

MAX_RECORDS =
1440

Instance Attribute Summary collapse

Attributes inherited from MetricBase

#dimensions, #name, #namespace

Instance Method Summary collapse

Methods inherited from MetricBase

#camel_case, #initialize, #inspect, #set_attributes, #to_sym

Constructor Details

This class inherits a constructor from AWS::CloudWatch::MetricBase

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/aws/cloud_watch/metric.rb', line 81

def method_missing name, *args
  if [:average, :sum, :sample_count, :maximum, :minimum].include?(name)
    data = get name, *args
    if data.kind_of? Array
      data.each do |point|
        point[:value] = point[name]
      end
    end
    data
  else
    super
  end
end

Instance Attribute Details

#unitObject

Returns the value of attribute unit.



22
23
24
# File 'lib/aws/cloud_watch/metric.rb', line 22

def unit
  @unit
end

Instance Method Details

#attr_namesObject



28
29
30
# File 'lib/aws/cloud_watch/metric.rb', line 28

def attr_names
  super + [:unit]
end

#get(statistics, period, range = Time.now, opts = {}) ⇒ Object



34
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
# File 'lib/aws/cloud_watch/metric.rb', line 34

def get statistics, period, range = Time.now, opts = {}
  statistics = [statistics] unless statistics.kind_of? Array
  unless range.kind_of? Range
    range = (range - period)..range
    single_point = true
  else
    single_point = false
  end
  single_point &&= statistics.length == 1
  
  def get_batch statistics, period, range, opts
    options = self.options.merge({
      :end_time => range.end.getutc.iso8601,
      :start_time => range.begin.getutc.iso8601,
      :period => period.to_i,
      :statistics => statistics.map { |s| camel_case(s) } })
    options[:unit] = camel_case(opts[:unit]) if opts[:unit]
    return [] if options[:end_time] == options[:start_time]
    
    data = client.get_metric_statistics(options).datapoints
  end
  
  if single_point
    data = get_batch statistics, period, range, opts
    
    if data.length == 0
      return nil
    else
      return data[0][statistics[0]]
    end
  end
  
  return [] if range.begin + period > range.end

  max_period = MAX_RECORDS * period / statistics.length
  
  ranges = [range]
  while (ranges[-1].end - ranges[-1].begin) > max_period
    range = ranges.pop
    pivot = range.begin + max_period
    ranges.push range.begin..pivot
    ranges.push pivot..range.end
  end
  
  ranges.map{|range| get_batch statistics, period, range, opts}.flatten.sort{|a, b| a[:timestamp] <=> b[:timestamp]}
end

#get_batch(statistics, period, range, opts) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/aws/cloud_watch/metric.rb', line 44

def get_batch statistics, period, range, opts
  options = self.options.merge({
    :end_time => range.end.getutc.iso8601,
    :start_time => range.begin.getutc.iso8601,
    :period => period.to_i,
    :statistics => statistics.map { |s| camel_case(s) } })
  options[:unit] = camel_case(opts[:unit]) if opts[:unit]
  return [] if options[:end_time] == options[:start_time]
  
  data = client.get_metric_statistics(options).datapoints
end

#optionsObject



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

def options
  super.merge(unit ? {:unit => unit} : {})
end