mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
added model and collection methods and tests to allow putting of metric data
This commit is contained in:
parent
7b219b9b5b
commit
94286a9ea9
4 changed files with 65 additions and 7 deletions
|
@ -13,6 +13,33 @@ module Fog
|
|||
attribute :sample_count, :aliases => 'SampleCount'
|
||||
attribute :timestamp, :aliases => 'Timestamp'
|
||||
attribute :unit, :aliases => 'Unit'
|
||||
attribute :metric_name, :aliases => 'MetricName'
|
||||
attribute :namespace, :aliases => 'Namespace'
|
||||
attribute :dimensions, :aliases => 'Dimensions'
|
||||
attribute :value
|
||||
|
||||
def save
|
||||
requires :metric_name
|
||||
requires :namespace
|
||||
requires :unit
|
||||
|
||||
put_opts = {'MetricName' => metric_name, 'Unit' => unit}
|
||||
put_opts.merge!('Dimensions' => dimensions) if dimensions
|
||||
put_opts.merge!('Timestamp' => dimensions) if timestamp
|
||||
if value
|
||||
put_opts.merge!('Value' => value)
|
||||
else
|
||||
put_opts.merge!('StatisticValues' => {
|
||||
'Minimum' => minimum,
|
||||
'Maximum' => maximum,
|
||||
'Sum' => sum,
|
||||
'Average' => average,
|
||||
'SampleCount' => sample_count
|
||||
})
|
||||
end
|
||||
connection.put_metric_data(namespace, [put_opts])
|
||||
true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,12 +4,15 @@ require 'fog/aws/models/cloud_watch/metric_statistic'
|
|||
module Fog
|
||||
module AWS
|
||||
class CloudWatch
|
||||
|
||||
class MetricStatistics < Fog::Collection
|
||||
model Fog::AWS::CloudWatch::MetricStatistic
|
||||
|
||||
def all(filters)
|
||||
metricName = filters['MetricName']
|
||||
namespace = filters['Namespace']
|
||||
dimensions = filters['Dimensions']
|
||||
data = connection.get_metric_statistics(filters).body['GetMetricStatisticsResult']['Datapoints']
|
||||
data.collect! { |datum| datum.merge('MetricName' => metricName, 'Namespace' => namespace, 'Dimensions' => dimensions) }
|
||||
load(data) # data is an array of attribute hashes
|
||||
end
|
||||
|
||||
|
|
|
@ -12,12 +12,40 @@ Shindo.tests("AWS::CloudWatch | metric_statistics", ['aws', 'cloudwatch']) do
|
|||
statisticTypes = ['Minimum','Maximum','Sum','SampleCount','Average']
|
||||
|
||||
tests("#all").succeeds do
|
||||
@statistics = AWS[:cloud_watch].metric_statistics.all({'Statistics' => statisticTypes, 'StartTime' => startTime, 'EndTime' => endTime, 'Period' => period, 'MetricName' => metricName, 'Namespace' => namespace, 'Dimensions' => [{'Name' => 'InstanceId', 'Value' => instanceId}]})) end
|
||||
@statistics = AWS[:cloud_watch].metric_statistics.all({'Statistics' => statisticTypes, 'StartTime' => startTime, 'EndTime' => endTime, 'Period' => period, 'MetricName' => metricName, 'Namespace' => namespace, 'Dimensions' => [{'Name' => 'InstanceId', 'Value' => instanceId}]})
|
||||
end
|
||||
|
||||
tests("#all_not_empty").returns(true) do
|
||||
@statistics.length > 0
|
||||
end
|
||||
|
||||
new_attributes = {
|
||||
:namespace => 'Custom/Test',
|
||||
:metric_name => 'ModelTest',
|
||||
:value => 9,
|
||||
:unit => 'None'
|
||||
}
|
||||
tests('#new').returns(new_attributes) do
|
||||
AWS[:cloud_watch].metric_statistics.new(new_attributes).attributes
|
||||
end
|
||||
|
||||
tests('#create').returns(new_attributes) do
|
||||
AWS[:cloud_watch].metric_statistics.create(new_attributes).attributes
|
||||
end
|
||||
|
||||
stats_set_attributes = {
|
||||
:namespace => 'Custom/Test',
|
||||
:metric_name => 'ModelTest',
|
||||
:minimum => 0,
|
||||
:maximum => 4,
|
||||
:sum => 10,
|
||||
:sample_count => 5,
|
||||
:average => 2,
|
||||
:unit => 'None'
|
||||
}
|
||||
tests('#create_stats_set').returns(stats_set_attributes) do
|
||||
AWS[:cloud_watch].metric_statistics.create(stats_set_attributes).attributes
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -5,22 +5,22 @@ Shindo.tests('AWS::CloudWatch | metric requests', ['aws', 'cloudwatch']) do
|
|||
@puts_format = {'ResponseMetadata' => {'RequestId' => String}}
|
||||
|
||||
tests('#puts_value').formats(@puts_format) do
|
||||
AWS[:cloud_watch].put_metric_data(namespace, [{'MetricName' => 'PutsValue', 'Unit' => 'None', 'Value' => 1}]).body
|
||||
AWS[:cloud_watch].put_metric_data(namespace, [{'MetricName' => 'RequestTest', 'Unit' => 'None', 'Value' => 1}]).body
|
||||
end
|
||||
|
||||
tests('#puts_statistics_set').succeeds do
|
||||
AWS[:cloud_watch].put_metric_data(namespace, [{'MetricName' => 'PutsStatisticsSet', 'Unit' => 'None', 'StatisticValues' => {'Minimum' => 0, 'Maximum' => 9, 'Sum' => 45, 'SampleCount' => 10, 'Average' => 4.5}}]).body
|
||||
AWS[:cloud_watch].put_metric_data(namespace, [{'MetricName' => 'RequestTest', 'Unit' => 'None', 'StatisticValues' => {'Minimum' => 0, 'Maximum' => 9, 'Sum' => 45, 'SampleCount' => 10, 'Average' => 4.5}}]).body
|
||||
end
|
||||
|
||||
tests('#puts with dimensions').succeeds do
|
||||
dimensions = [{}]
|
||||
|
||||
AWS[:cloud_watch].put_metric_data(namespace, [{'MetricName' => 'PutsWithDimensions', 'Unit' => 'None', 'Value' => 1, 'Dimensions' => dimensions}]).body
|
||||
AWS[:cloud_watch].put_metric_data(namespace, [{'MetricName' => 'RequestTest', 'Unit' => 'None', 'Value' => 1, 'Dimensions' => dimensions}]).body
|
||||
end
|
||||
|
||||
tests('#puts more than one').succeeds do
|
||||
datapoints = (0..4).collect do |i|
|
||||
dp = {'MetricName' => "Puts#{i}thValue", 'Unit' => 'None', 'Value' => i}
|
||||
datapoints = (0...3).collect do |i|
|
||||
dp = {'MetricName' => "#{ith}RequestTest", 'Unit' => 'None', 'Value' => i}
|
||||
if i%2==0
|
||||
dp['Dimensions'] = [{'Name' => 'Ruler', 'Value' => "measurement_#{i}"}]
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue