Implement CloudWatch get_metric_statistics (#1369)
* implement get_metric_statistics
This commit is contained in:
parent
796fa6647b
commit
97687d153a
5 changed files with 233 additions and 53 deletions
|
|
@ -1,13 +1,13 @@
|
|||
import json
|
||||
|
||||
import json
|
||||
from moto.core.utils import iso_8601_datetime_with_milliseconds
|
||||
from moto.core import BaseBackend, BaseModel
|
||||
from moto.core.exceptions import RESTError
|
||||
import boto.ec2.cloudwatch
|
||||
import datetime
|
||||
|
||||
from datetime import datetime, timedelta
|
||||
from dateutil.tz import tzutc
|
||||
from .utils import make_arn_for_dashboard
|
||||
|
||||
|
||||
DEFAULT_ACCOUNT_ID = 123456789012
|
||||
|
||||
|
||||
|
|
@ -18,6 +18,34 @@ class Dimension(object):
|
|||
self.value = value
|
||||
|
||||
|
||||
def daterange(start, stop, step=timedelta(days=1), inclusive=False):
|
||||
"""
|
||||
This method will iterate from `start` to `stop` datetimes with a timedelta step of `step`
|
||||
(supports iteration forwards or backwards in time)
|
||||
|
||||
:param start: start datetime
|
||||
:param stop: end datetime
|
||||
:param step: step size as a timedelta
|
||||
:param inclusive: if True, last item returned will be as step closest to `end` (or `end` if no remainder).
|
||||
"""
|
||||
|
||||
# inclusive=False to behave like range by default
|
||||
total_step_secs = step.total_seconds()
|
||||
assert total_step_secs != 0
|
||||
|
||||
if total_step_secs > 0:
|
||||
while start < stop:
|
||||
yield start
|
||||
start = start + step
|
||||
else:
|
||||
while stop < start:
|
||||
yield start
|
||||
start = start + step
|
||||
|
||||
if inclusive and start == stop:
|
||||
yield start
|
||||
|
||||
|
||||
class FakeAlarm(BaseModel):
|
||||
|
||||
def __init__(self, name, namespace, metric_name, comparison_operator, evaluation_periods,
|
||||
|
|
@ -38,14 +66,14 @@ class FakeAlarm(BaseModel):
|
|||
self.ok_actions = ok_actions
|
||||
self.insufficient_data_actions = insufficient_data_actions
|
||||
self.unit = unit
|
||||
self.configuration_updated_timestamp = datetime.datetime.utcnow()
|
||||
self.configuration_updated_timestamp = datetime.utcnow()
|
||||
|
||||
self.history = []
|
||||
|
||||
self.state_reason = ''
|
||||
self.state_reason_data = '{}'
|
||||
self.state = 'OK'
|
||||
self.state_updated_timestamp = datetime.datetime.utcnow()
|
||||
self.state_updated_timestamp = datetime.utcnow()
|
||||
|
||||
def update_state(self, reason, reason_data, state_value):
|
||||
# History type, that then decides what the rest of the items are, can be one of ConfigurationUpdate | StateUpdate | Action
|
||||
|
|
@ -56,17 +84,18 @@ class FakeAlarm(BaseModel):
|
|||
self.state_reason = reason
|
||||
self.state_reason_data = reason_data
|
||||
self.state = state_value
|
||||
self.state_updated_timestamp = datetime.datetime.utcnow()
|
||||
self.state_updated_timestamp = datetime.utcnow()
|
||||
|
||||
|
||||
class MetricDatum(BaseModel):
|
||||
|
||||
def __init__(self, namespace, name, value, dimensions):
|
||||
def __init__(self, namespace, name, value, dimensions, timestamp):
|
||||
self.namespace = namespace
|
||||
self.name = name
|
||||
self.value = value
|
||||
self.dimensions = [Dimension(dimension['name'], dimension[
|
||||
'value']) for dimension in dimensions]
|
||||
self.timestamp = timestamp or datetime.utcnow().replace(tzinfo=tzutc())
|
||||
self.dimensions = [Dimension(dimension['Name'], dimension[
|
||||
'Value']) for dimension in dimensions]
|
||||
|
||||
|
||||
class Dashboard(BaseModel):
|
||||
|
|
@ -75,7 +104,7 @@ class Dashboard(BaseModel):
|
|||
self.arn = make_arn_for_dashboard(DEFAULT_ACCOUNT_ID, name)
|
||||
self.name = name
|
||||
self.body = body
|
||||
self.last_modified = datetime.datetime.now()
|
||||
self.last_modified = datetime.now()
|
||||
|
||||
@property
|
||||
def last_modified_iso(self):
|
||||
|
|
@ -92,6 +121,53 @@ class Dashboard(BaseModel):
|
|||
return '<CloudWatchDashboard {0}>'.format(self.name)
|
||||
|
||||
|
||||
class Statistics:
|
||||
def __init__(self, stats, dt):
|
||||
self.timestamp = iso_8601_datetime_with_milliseconds(dt)
|
||||
self.values = []
|
||||
self.stats = stats
|
||||
|
||||
@property
|
||||
def sample_count(self):
|
||||
if 'SampleCount' not in self.stats:
|
||||
return None
|
||||
|
||||
return len(self.values)
|
||||
|
||||
@property
|
||||
def unit(self):
|
||||
return None
|
||||
|
||||
@property
|
||||
def sum(self):
|
||||
if 'Sum' not in self.stats:
|
||||
return None
|
||||
|
||||
return sum(self.values)
|
||||
|
||||
@property
|
||||
def min(self):
|
||||
if 'Minimum' not in self.stats:
|
||||
return None
|
||||
|
||||
return min(self.values)
|
||||
|
||||
@property
|
||||
def max(self):
|
||||
if 'Maximum' not in self.stats:
|
||||
return None
|
||||
|
||||
return max(self.values)
|
||||
|
||||
@property
|
||||
def average(self):
|
||||
if 'Average' not in self.stats:
|
||||
return None
|
||||
|
||||
# when moto is 3.4+ we can switch to the statistics module
|
||||
return sum(self.values) / len(self.values)
|
||||
|
||||
|
||||
class CloudWatchBackend(BaseBackend):
|
||||
|
||||
def __init__(self):
|
||||
|
|
@ -150,9 +226,34 @@ class CloudWatchBackend(BaseBackend):
|
|||
self.alarms.pop(alarm_name, None)
|
||||
|
||||
def put_metric_data(self, namespace, metric_data):
|
||||
for name, value, dimensions in metric_data:
|
||||
for metric_member in metric_data:
|
||||
self.metric_data.append(MetricDatum(
|
||||
namespace, name, value, dimensions))
|
||||
namespace, metric_member['MetricName'], float(metric_member['Value']), metric_member['Dimensions.member'], metric_member.get('Timestamp')))
|
||||
|
||||
def get_metric_statistics(self, namespace, metric_name, start_time, end_time, period, stats):
|
||||
period_delta = timedelta(seconds=period)
|
||||
filtered_data = [md for md in self.metric_data if
|
||||
md.namespace == namespace and md.name == metric_name and start_time <= md.timestamp <= end_time]
|
||||
|
||||
# earliest to oldest
|
||||
filtered_data = sorted(filtered_data, key=lambda x: x.timestamp)
|
||||
if not filtered_data:
|
||||
return []
|
||||
|
||||
idx = 0
|
||||
data = list()
|
||||
for dt in daterange(filtered_data[0].timestamp, filtered_data[-1].timestamp + period_delta, period_delta):
|
||||
s = Statistics(stats, dt)
|
||||
while idx < len(filtered_data) and filtered_data[idx].timestamp < (dt + period_delta):
|
||||
s.values.append(filtered_data[idx].value)
|
||||
idx += 1
|
||||
|
||||
if not s.values:
|
||||
continue
|
||||
|
||||
data.append(s)
|
||||
|
||||
return data
|
||||
|
||||
def get_all_metrics(self):
|
||||
return self.metric_data
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import json
|
|||
from moto.core.utils import amzn_request_id
|
||||
from moto.core.responses import BaseResponse
|
||||
from .models import cloudwatch_backends
|
||||
from dateutil.parser import parse as dtparse
|
||||
|
||||
|
||||
class CloudWatchResponse(BaseResponse):
|
||||
|
|
@ -75,35 +76,36 @@ class CloudWatchResponse(BaseResponse):
|
|||
@amzn_request_id
|
||||
def put_metric_data(self):
|
||||
namespace = self._get_param('Namespace')
|
||||
metric_data = []
|
||||
metric_index = 1
|
||||
while True:
|
||||
try:
|
||||
metric_name = self.querystring[
|
||||
'MetricData.member.{0}.MetricName'.format(metric_index)][0]
|
||||
except KeyError:
|
||||
break
|
||||
value = self.querystring.get(
|
||||
'MetricData.member.{0}.Value'.format(metric_index), [None])[0]
|
||||
dimensions = []
|
||||
dimension_index = 1
|
||||
while True:
|
||||
try:
|
||||
dimension_name = self.querystring[
|
||||
'MetricData.member.{0}.Dimensions.member.{1}.Name'.format(metric_index, dimension_index)][0]
|
||||
except KeyError:
|
||||
break
|
||||
dimension_value = self.querystring[
|
||||
'MetricData.member.{0}.Dimensions.member.{1}.Value'.format(metric_index, dimension_index)][0]
|
||||
dimensions.append(
|
||||
{'name': dimension_name, 'value': dimension_value})
|
||||
dimension_index += 1
|
||||
metric_data.append([metric_name, value, dimensions])
|
||||
metric_index += 1
|
||||
metric_data = self._get_multi_param('MetricData.member')
|
||||
|
||||
self.cloudwatch_backend.put_metric_data(namespace, metric_data)
|
||||
template = self.response_template(PUT_METRIC_DATA_TEMPLATE)
|
||||
return template.render()
|
||||
|
||||
@amzn_request_id
|
||||
def get_metric_statistics(self):
|
||||
namespace = self._get_param('Namespace')
|
||||
metric_name = self._get_param('MetricName')
|
||||
start_time = dtparse(self._get_param('StartTime'))
|
||||
end_time = dtparse(self._get_param('EndTime'))
|
||||
period = int(self._get_param('Period'))
|
||||
statistics = self._get_multi_param("Statistics.member")
|
||||
|
||||
# Unsupported Parameters (To Be Implemented)
|
||||
unit = self._get_param('Unit')
|
||||
extended_statistics = self._get_param('ExtendedStatistics')
|
||||
dimensions = self._get_param('Dimensions')
|
||||
if unit or extended_statistics or dimensions:
|
||||
raise NotImplemented()
|
||||
|
||||
# TODO: this should instead throw InvalidParameterCombination
|
||||
if not statistics:
|
||||
raise NotImplemented("Must specify either Statistics or ExtendedStatistics")
|
||||
|
||||
datapoints = self.cloudwatch_backend.get_metric_statistics(namespace, metric_name, start_time, end_time, period, statistics)
|
||||
template = self.response_template(GET_METRIC_STATISTICS_TEMPLATE)
|
||||
return template.render(label=metric_name, datapoints=datapoints)
|
||||
|
||||
@amzn_request_id
|
||||
def list_metrics(self):
|
||||
metrics = self.cloudwatch_backend.get_all_metrics()
|
||||
|
|
@ -150,10 +152,6 @@ class CloudWatchResponse(BaseResponse):
|
|||
template = self.response_template(GET_DASHBOARD_TEMPLATE)
|
||||
return template.render(dashboard=dashboard)
|
||||
|
||||
@amzn_request_id
|
||||
def get_metric_statistics(self):
|
||||
raise NotImplementedError()
|
||||
|
||||
@amzn_request_id
|
||||
def list_dashboards(self):
|
||||
prefix = self._get_param('DashboardNamePrefix', '')
|
||||
|
|
@ -266,6 +264,50 @@ PUT_METRIC_DATA_TEMPLATE = """<PutMetricDataResponse xmlns="http://monitoring.am
|
|||
</ResponseMetadata>
|
||||
</PutMetricDataResponse>"""
|
||||
|
||||
GET_METRIC_STATISTICS_TEMPLATE = """<GetMetricStatisticsResponse xmlns="http://monitoring.amazonaws.com/doc/2010-08-01/">
|
||||
<ResponseMetadata>
|
||||
<RequestId>
|
||||
{{ request_id }}
|
||||
</RequestId>
|
||||
</ResponseMetadata>
|
||||
|
||||
<GetMetricStatisticsResult>
|
||||
<Label> {{ label }} </Label>
|
||||
<Datapoints>
|
||||
{% for datapoint in datapoints %}
|
||||
<Datapoint>
|
||||
{% if datapoint.sum %}
|
||||
<Sum>{{ datapoint.sum }}</Sum>
|
||||
{% endif %}
|
||||
|
||||
{% if datapoint.average %}
|
||||
<Average>{{ datapoint.average }}</Average>
|
||||
{% endif %}
|
||||
|
||||
{% if datapoint.maximum %}
|
||||
<Maximum>{{ datapoint.maximum }}</Maximum>
|
||||
{% endif %}
|
||||
|
||||
{% if datapoint.minimum %}
|
||||
<Minimum>{{ datapoint.minimum }}</Minimum>
|
||||
{% endif %}
|
||||
|
||||
{% if datapoint.sample_count %}
|
||||
<SampleCount>{{ datapoint.sample_count }}</SampleCount>
|
||||
{% endif %}
|
||||
|
||||
{% if datapoint.extended_statistics %}
|
||||
<ExtendedStatistics>{{ datapoint.extended_statistics }}</ExtendedStatistics>
|
||||
{% endif %}
|
||||
|
||||
<Timestamp>{{ datapoint.timestamp }}</Timestamp>
|
||||
<Unit>{{ datapoint.unit }}</Unit>
|
||||
</Datapoint>
|
||||
{% endfor %}
|
||||
</Datapoints>
|
||||
</GetMetricStatisticsResult>
|
||||
</GetMetricStatisticsResponse>"""
|
||||
|
||||
LIST_METRICS_TEMPLATE = """<ListMetricsResponse xmlns="http://monitoring.amazonaws.com/doc/2010-08-01/">
|
||||
<ListMetricsResult>
|
||||
<Metrics>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue