Module: AwsReporting::Statistics

Defined in:
lib/aws-reporting/statistics.rb

Class Method Summary collapse

Class Method Details

.get(region, namespace, metric_name, start_time, end_time, period, dimensions, statistics) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/aws-reporting/statistics.rb', line 3

def get(region, namespace, metric_name, start_time, end_time, period, dimensions, statistics)
  client = AWS::CloudWatch::Client.new(:config => Config.get, :region => region)

  datapoints = client.get_metric_statistics(:namespace => namespace,
                                            :metric_name => metric_name,
                                            :start_time => start_time.iso8601,
                                            :end_time => end_time.iso8601,
                                            :period => period,
                                            :dimensions => dimensions,
                                            :statistics => statistics)[:datapoints]

  info = {:region => region,
          :namespace => namespace,
          :metric_name => metric_name,
          :dimensions => dimensions,
          :start_time => start_time.iso8601,
          :end_time => end_time.iso8601,
          :period => period,
          :statistics => statistics,
          :unit => datapoints[0] ? datapoints[0][:unit] : nil }

  data = datapoints.sort_by{|d| d[:timestamp] }
                   .map{|d| 
                      datapoint = {}

                      datapoint[:timestamp] = d[:timestamp].iso8601
                      datapoint[:maximum] = d[:maximum] if d[:maximum]
                      datapoint[:minimum] = d[:minimum] if d[:minimum]
                      datapoint[:average] = d[:average] if d[:average]
                      datapoint[:sum] = d[:sum] if d[:sum]
                      datapoint[:sample_count] = d[:sample_count] if d[:sample_count]

                      datapoint
                   }

  {:info => info, :datapoints => data}
end