File: put_metric_data.rb

package info (click to toggle)
ruby-fog-aws 3.3.0-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,816 kB
  • sloc: ruby: 68,587; makefile: 6
file content (72 lines) | stat: -rw-r--r-- 3,136 bytes parent folder | download | duplicates (5)
1
2
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
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
module Fog
  module AWS
    class CloudWatch
      class Real
        require 'fog/aws/parsers/cloud_watch/put_metric_data'

        # Publishes one or more data points to CloudWatch. A new metric is created if necessary
        # ==== Options
        # * Namespace<~String>: the namespace of the metric data
        # * MetricData<~Array>: the datapoints to publish of the metric
        #     * MetricName<~String>: the name of the metric
        #     * Timestamp<~String>: the timestamp for the data point. If omitted defaults to the time at which the data is received by CloudWatch
        #     * Unit<~String>: the unit
        #     * Value<~Double> the value for the metric
        #     * StatisticValues<~Hash>:
        #         * Maximum<~Double>: the maximum value of the sample set
        #         * Sum<~Double>: the sum of the values of the sample set
        #         * SampleCount<~Double>: the number of samples used for the statistic set
        #         * Minimum<~Double>: the minimum value of the sample set
        #     * Dimensions<~Array>: the dimensions for the metric. From 0 to 10 may be included
        #          * Name<~String>
        #         * Value<~String>
        # ==== Returns
        # * response<~Excon::Response>:
        #
        # ==== See Also
        # http://docs.amazonwebservices.com/AmazonCloudWatch/latest/APIReference/API_PutMetricData.html
        #

        def put_metric_data(namespace, metric_data)
          options = {'Namespace' => namespace}

          #first index the dimensions for any of the datums that have dimensions
          metric_data.map! do |metric_datum|
            if dimensions = metric_datum.delete('Dimensions')
              metric_datum.merge!(AWS.indexed_param('Dimensions.member.%d.Name', dimensions.map {|dimension| dimension['Name']}))
              metric_datum.merge!(AWS.indexed_param('Dimensions.member.%d.Value', dimensions.map {|dimension| dimension['Value']}))
            end
            metric_datum
          end
          #then flatten out an hashes in the metric_data array
          metric_data.map! { |metric_datum| flatten_hash(metric_datum) }
          #then index the metric_data array
          options.merge!(AWS.indexed_param('MetricData.member.%d', [*metric_data]))
          #then finally flatten out an hashes in the overall options array
          options = flatten_hash(options)

          request({
              'Action'    => 'PutMetricData',
              :parser     => Fog::Parsers::AWS::CloudWatch::PutMetricData.new
            }.merge(options))
        end
        private

        def flatten_hash(starting)
          finishing = {}
          starting.each do |top_level_key, top_level_value|
            if top_level_value.is_a?(Hash)
              nested_hash = top_level_value
              nested_hash.each do |nested_key, nested_value|
                finishing["#{top_level_key}.#{nested_key}"] = nested_value
              end
            else
              finishing[top_level_key] = top_level_value
            end
          end
          return finishing
        end
      end
    end
  end
end