parent
d8f6f77a0f
commit
81f3cbb548
@ -10,6 +10,8 @@ from .utils import make_arn_for_dashboard
|
|||||||
|
|
||||||
DEFAULT_ACCOUNT_ID = 123456789012
|
DEFAULT_ACCOUNT_ID = 123456789012
|
||||||
|
|
||||||
|
_EMPTY_LIST = tuple()
|
||||||
|
|
||||||
|
|
||||||
class Dimension(object):
|
class Dimension(object):
|
||||||
|
|
||||||
@ -146,14 +148,14 @@ class Statistics:
|
|||||||
return sum(self.values)
|
return sum(self.values)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def min(self):
|
def minimum(self):
|
||||||
if 'Minimum' not in self.stats:
|
if 'Minimum' not in self.stats:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
return min(self.values)
|
return min(self.values)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def max(self):
|
def maximum(self):
|
||||||
if 'Maximum' not in self.stats:
|
if 'Maximum' not in self.stats:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@ -228,7 +230,7 @@ class CloudWatchBackend(BaseBackend):
|
|||||||
def put_metric_data(self, namespace, metric_data):
|
def put_metric_data(self, namespace, metric_data):
|
||||||
for metric_member in metric_data:
|
for metric_member in metric_data:
|
||||||
self.metric_data.append(MetricDatum(
|
self.metric_data.append(MetricDatum(
|
||||||
namespace, metric_member['MetricName'], float(metric_member['Value']), metric_member['Dimensions.member'], metric_member.get('Timestamp')))
|
namespace, metric_member['MetricName'], float(metric_member['Value']), metric_member.get('Dimensions.member', _EMPTY_LIST), metric_member.get('Timestamp')))
|
||||||
|
|
||||||
def get_metric_statistics(self, namespace, metric_name, start_time, end_time, period, stats):
|
def get_metric_statistics(self, namespace, metric_name, start_time, end_time, period, stats):
|
||||||
period_delta = timedelta(seconds=period)
|
period_delta = timedelta(seconds=period)
|
||||||
|
@ -276,27 +276,27 @@ GET_METRIC_STATISTICS_TEMPLATE = """<GetMetricStatisticsResponse xmlns="http://m
|
|||||||
<Datapoints>
|
<Datapoints>
|
||||||
{% for datapoint in datapoints %}
|
{% for datapoint in datapoints %}
|
||||||
<Datapoint>
|
<Datapoint>
|
||||||
{% if datapoint.sum %}
|
{% if datapoint.sum is not none %}
|
||||||
<Sum>{{ datapoint.sum }}</Sum>
|
<Sum>{{ datapoint.sum }}</Sum>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if datapoint.average %}
|
{% if datapoint.average is not none %}
|
||||||
<Average>{{ datapoint.average }}</Average>
|
<Average>{{ datapoint.average }}</Average>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if datapoint.maximum %}
|
{% if datapoint.maximum is not none %}
|
||||||
<Maximum>{{ datapoint.maximum }}</Maximum>
|
<Maximum>{{ datapoint.maximum }}</Maximum>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if datapoint.minimum %}
|
{% if datapoint.minimum is not none %}
|
||||||
<Minimum>{{ datapoint.minimum }}</Minimum>
|
<Minimum>{{ datapoint.minimum }}</Minimum>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if datapoint.sample_count %}
|
{% if datapoint.sample_count is not none %}
|
||||||
<SampleCount>{{ datapoint.sample_count }}</SampleCount>
|
<SampleCount>{{ datapoint.sample_count }}</SampleCount>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if datapoint.extended_statistics %}
|
{% if datapoint.extended_statistics is not none %}
|
||||||
<ExtendedStatistics>{{ datapoint.extended_statistics }}</ExtendedStatistics>
|
<ExtendedStatistics>{{ datapoint.extended_statistics }}</ExtendedStatistics>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
|
@ -1,5 +1,8 @@
|
|||||||
import boto
|
import boto
|
||||||
from boto.ec2.cloudwatch.alarm import MetricAlarm
|
from boto.ec2.cloudwatch.alarm import MetricAlarm
|
||||||
|
import boto3
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
import pytz
|
||||||
import sure # noqa
|
import sure # noqa
|
||||||
|
|
||||||
from moto import mock_cloudwatch_deprecated
|
from moto import mock_cloudwatch_deprecated
|
||||||
|
@ -2,6 +2,8 @@ from __future__ import unicode_literals
|
|||||||
|
|
||||||
import boto3
|
import boto3
|
||||||
from botocore.exceptions import ClientError
|
from botocore.exceptions import ClientError
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
import pytz
|
||||||
import sure # noqa
|
import sure # noqa
|
||||||
|
|
||||||
from moto import mock_cloudwatch
|
from moto import mock_cloudwatch
|
||||||
@ -137,6 +139,52 @@ def test_alarm_state():
|
|||||||
len(resp['MetricAlarms']).should.equal(2)
|
len(resp['MetricAlarms']).should.equal(2)
|
||||||
|
|
||||||
|
|
||||||
|
@mock_cloudwatch
|
||||||
|
def test_put_metric_data_no_dimensions():
|
||||||
|
conn = boto3.client('cloudwatch', region_name='us-east-1')
|
||||||
|
|
||||||
|
conn.put_metric_data(
|
||||||
|
Namespace='tester',
|
||||||
|
MetricData=[
|
||||||
|
dict(
|
||||||
|
MetricName='metric',
|
||||||
|
Value=1.5,
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
metrics = conn.list_metrics()['Metrics']
|
||||||
|
metrics.should.have.length_of(1)
|
||||||
|
metric = metrics[0]
|
||||||
|
metric['Namespace'].should.equal('tester')
|
||||||
|
metric['MetricName'].should.equal('metric')
|
||||||
|
|
||||||
|
|
||||||
|
@mock_cloudwatch
|
||||||
|
def test_get_metric_statistics():
|
||||||
|
conn = boto3.client('cloudwatch', region_name='us-east-1')
|
||||||
|
utc_now = datetime.now(tz=pytz.utc)
|
||||||
|
|
||||||
|
conn.put_metric_data(
|
||||||
|
Namespace='tester',
|
||||||
|
MetricData=[
|
||||||
|
dict(
|
||||||
|
MetricName='metric',
|
||||||
|
Value=1.5,
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
stats = conn.get_metric_statistics(
|
||||||
|
Namespace='tester',
|
||||||
|
MetricName='metric',
|
||||||
|
StartTime=utc_now,
|
||||||
|
EndTime=utc_now + timedelta(seconds=60),
|
||||||
|
Period=60,
|
||||||
|
Statistics=['SampleCount', 'Sum']
|
||||||
|
)
|
||||||
|
|
||||||
|
stats['Datapoints'].should.have.length_of(1)
|
||||||
|
datapoint = stats['Datapoints'][0]
|
||||||
|
datapoint['SampleCount'].should.equal(1.0)
|
||||||
|
datapoint['Sum'].should.equal(1.5)
|
||||||
|
Loading…
Reference in New Issue
Block a user