Add unit support to cloudwatch get_metric_statistics (#3932)
* Add unit support to cloudwatch get_metric_statistics * fix linting
This commit is contained in:
parent
58fd4aeaae
commit
03b2009a1c
@ -168,7 +168,7 @@ def are_dimensions_same(metric_dimensions, dimensions):
|
|||||||
|
|
||||||
|
|
||||||
class MetricDatum(BaseModel):
|
class MetricDatum(BaseModel):
|
||||||
def __init__(self, namespace, name, value, dimensions, timestamp):
|
def __init__(self, namespace, name, value, dimensions, timestamp, unit=None):
|
||||||
self.namespace = namespace
|
self.namespace = namespace
|
||||||
self.name = name
|
self.name = name
|
||||||
self.value = value
|
self.value = value
|
||||||
@ -176,6 +176,7 @@ class MetricDatum(BaseModel):
|
|||||||
self.dimensions = [
|
self.dimensions = [
|
||||||
Dimension(dimension["Name"], dimension["Value"]) for dimension in dimensions
|
Dimension(dimension["Name"], dimension["Value"]) for dimension in dimensions
|
||||||
]
|
]
|
||||||
|
self.unit = unit
|
||||||
|
|
||||||
def filter(self, namespace, name, dimensions, already_present_metrics):
|
def filter(self, namespace, name, dimensions, already_present_metrics):
|
||||||
if namespace and namespace != self.namespace:
|
if namespace and namespace != self.namespace:
|
||||||
@ -385,6 +386,7 @@ class CloudWatchBackend(BaseBackend):
|
|||||||
float(metric_member.get("Value", 0)),
|
float(metric_member.get("Value", 0)),
|
||||||
metric_member.get("Dimensions.member", _EMPTY_LIST),
|
metric_member.get("Dimensions.member", _EMPTY_LIST),
|
||||||
timestamp,
|
timestamp,
|
||||||
|
metric_member.get("Unit"),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -449,7 +451,7 @@ class CloudWatchBackend(BaseBackend):
|
|||||||
return results
|
return results
|
||||||
|
|
||||||
def get_metric_statistics(
|
def get_metric_statistics(
|
||||||
self, namespace, metric_name, start_time, end_time, period, stats
|
self, namespace, metric_name, start_time, end_time, period, stats, unit=None
|
||||||
):
|
):
|
||||||
period_delta = timedelta(seconds=period)
|
period_delta = timedelta(seconds=period)
|
||||||
filtered_data = [
|
filtered_data = [
|
||||||
@ -460,6 +462,9 @@ class CloudWatchBackend(BaseBackend):
|
|||||||
and start_time <= md.timestamp <= end_time
|
and start_time <= md.timestamp <= end_time
|
||||||
]
|
]
|
||||||
|
|
||||||
|
if unit:
|
||||||
|
filtered_data = [md for md in filtered_data if md.unit == unit]
|
||||||
|
|
||||||
# earliest to oldest
|
# earliest to oldest
|
||||||
filtered_data = sorted(filtered_data, key=lambda x: x.timestamp)
|
filtered_data = sorted(filtered_data, key=lambda x: x.timestamp)
|
||||||
if not filtered_data:
|
if not filtered_data:
|
||||||
|
@ -119,7 +119,6 @@ class CloudWatchResponse(BaseResponse):
|
|||||||
def put_metric_data(self):
|
def put_metric_data(self):
|
||||||
namespace = self._get_param("Namespace")
|
namespace = self._get_param("Namespace")
|
||||||
metric_data = self._get_multi_param("MetricData.member")
|
metric_data = self._get_multi_param("MetricData.member")
|
||||||
|
|
||||||
self.cloudwatch_backend.put_metric_data(namespace, metric_data)
|
self.cloudwatch_backend.put_metric_data(namespace, metric_data)
|
||||||
template = self.response_template(PUT_METRIC_DATA_TEMPLATE)
|
template = self.response_template(PUT_METRIC_DATA_TEMPLATE)
|
||||||
return template.render()
|
return template.render()
|
||||||
@ -151,7 +150,7 @@ class CloudWatchResponse(BaseResponse):
|
|||||||
unit = self._get_param("Unit")
|
unit = self._get_param("Unit")
|
||||||
extended_statistics = self._get_param("ExtendedStatistics")
|
extended_statistics = self._get_param("ExtendedStatistics")
|
||||||
dimensions = self._get_param("Dimensions")
|
dimensions = self._get_param("Dimensions")
|
||||||
if unit or extended_statistics or dimensions:
|
if extended_statistics or dimensions:
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
# TODO: this should instead throw InvalidParameterCombination
|
# TODO: this should instead throw InvalidParameterCombination
|
||||||
@ -161,7 +160,7 @@ class CloudWatchResponse(BaseResponse):
|
|||||||
)
|
)
|
||||||
|
|
||||||
datapoints = self.cloudwatch_backend.get_metric_statistics(
|
datapoints = self.cloudwatch_backend.get_metric_statistics(
|
||||||
namespace, metric_name, start_time, end_time, period, statistics
|
namespace, metric_name, start_time, end_time, period, statistics, unit
|
||||||
)
|
)
|
||||||
template = self.response_template(GET_METRIC_STATISTICS_TEMPLATE)
|
template = self.response_template(GET_METRIC_STATISTICS_TEMPLATE)
|
||||||
return template.render(label=metric_name, datapoints=datapoints)
|
return template.render(label=metric_name, datapoints=datapoints)
|
||||||
@ -514,7 +513,9 @@ GET_METRIC_STATISTICS_TEMPLATE = """<GetMetricStatisticsResponse xmlns="http://m
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<Timestamp>{{ datapoint.timestamp }}</Timestamp>
|
<Timestamp>{{ datapoint.timestamp }}</Timestamp>
|
||||||
|
{% if datapoint.unit is not none %}
|
||||||
<Unit>{{ datapoint.unit }}</Unit>
|
<Unit>{{ datapoint.unit }}</Unit>
|
||||||
|
{% endif %}
|
||||||
</member>
|
</member>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</Datapoints>
|
</Datapoints>
|
||||||
|
@ -158,6 +158,7 @@ def test_get_metric_statistics():
|
|||||||
value=1.5,
|
value=1.5,
|
||||||
dimensions={"InstanceId": ["i-0123456,i-0123457"]},
|
dimensions={"InstanceId": ["i-0123456,i-0123457"]},
|
||||||
timestamp=metric_timestamp,
|
timestamp=metric_timestamp,
|
||||||
|
unit="Count",
|
||||||
)
|
)
|
||||||
|
|
||||||
metric_kwargs = dict(
|
metric_kwargs = dict(
|
||||||
@ -167,6 +168,7 @@ def test_get_metric_statistics():
|
|||||||
end_time=datetime.now(),
|
end_time=datetime.now(),
|
||||||
period=3600,
|
period=3600,
|
||||||
statistics=["Minimum"],
|
statistics=["Minimum"],
|
||||||
|
unit="Count",
|
||||||
)
|
)
|
||||||
|
|
||||||
datapoints = conn.get_metric_statistics(**metric_kwargs)
|
datapoints = conn.get_metric_statistics(**metric_kwargs)
|
||||||
@ -175,6 +177,19 @@ def test_get_metric_statistics():
|
|||||||
datapoint.should.have.key("Minimum").which.should.equal(1.5)
|
datapoint.should.have.key("Minimum").which.should.equal(1.5)
|
||||||
datapoint.should.have.key("Timestamp").which.should.equal(metric_timestamp)
|
datapoint.should.have.key("Timestamp").which.should.equal(metric_timestamp)
|
||||||
|
|
||||||
|
metric_kwargs = dict(
|
||||||
|
namespace="tester",
|
||||||
|
metric_name="metric",
|
||||||
|
start_time=metric_timestamp,
|
||||||
|
end_time=datetime.now(),
|
||||||
|
period=3600,
|
||||||
|
statistics=["Minimum"],
|
||||||
|
unit="Percent",
|
||||||
|
)
|
||||||
|
|
||||||
|
datapoints = conn.get_metric_statistics(**metric_kwargs)
|
||||||
|
datapoints.should.have.length_of(0)
|
||||||
|
|
||||||
|
|
||||||
# TODO: THIS IS CURRENTLY BROKEN!
|
# TODO: THIS IS CURRENTLY BROKEN!
|
||||||
# @mock_s3_deprecated
|
# @mock_s3_deprecated
|
||||||
|
Loading…
Reference in New Issue
Block a user