Cloudwatch get metric data return by period (#3626)

* Cloudwatch get metric data return by period

- addresses https://github.com/localstack/localstack/issues/3493

* fix lint issues

* remove unused import

* added test cases for min max and average
This commit is contained in:
Rahul Ranjan 2021-02-02 14:38:53 +05:30 committed by GitHub
commit 78a5661093
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 120 additions and 36 deletions

View file

@ -678,6 +678,7 @@ def test_get_metric_data_partially_within_timeframe():
}
],
)
cloudwatch.put_metric_data(
Namespace=namespace1,
MetricData=[
@ -686,31 +687,89 @@ def test_get_metric_data_partially_within_timeframe():
"Value": 50,
"Unit": "Seconds",
"Timestamp": last_week,
}
],
)
# get_metric_data
response = cloudwatch.get_metric_data(
MetricDataQueries=[
},
{
"Id": "result",
"MetricStat": {
"Metric": {"Namespace": namespace1, "MetricName": "metric1"},
"Period": 60,
"Stat": "Sum",
},
}
"MetricName": "metric1",
"Value": 10,
"Unit": "Seconds",
"Timestamp": last_week + timedelta(seconds=10),
},
{
"MetricName": "metric1",
"Value": 20,
"Unit": "Seconds",
"Timestamp": last_week + timedelta(seconds=15),
},
{
"MetricName": "metric1",
"Value": 40,
"Unit": "Seconds",
"Timestamp": last_week + timedelta(seconds=30),
},
],
StartTime=yesterday - timedelta(seconds=60),
EndTime=utc_now + timedelta(seconds=60),
)
#
# data for average, min, max
def get_data(start, end, stat="Sum", scanBy="TimestampAscending"):
# get_metric_data
response = cloudwatch.get_metric_data(
MetricDataQueries=[
{
"Id": "result",
"MetricStat": {
"Metric": {"Namespace": namespace1, "MetricName": "metric1"},
"Period": 60,
"Stat": stat,
},
}
],
StartTime=start,
EndTime=end,
ScanBy=scanBy,
)
return response
response = get_data(
start=yesterday - timedelta(seconds=60), end=utc_now + timedelta(seconds=60),
)
# Assert Last week's data is not returned
len(response["MetricDataResults"]).should.equal(1)
sum_ = response["MetricDataResults"][0]
sum_["Label"].should.equal("metric1 Sum")
sum_["StatusCode"].should.equal("Complete")
sum_["Values"].should.equal([30.0])
sum_["Values"].should.equal([20.0, 10.0])
response = get_data(
start=yesterday - timedelta(seconds=60),
end=utc_now + timedelta(seconds=60),
scanBy="TimestampDescending",
)
response["MetricDataResults"][0]["Values"].should.equal([10.0, 20.0])
response = get_data(
start=last_week - timedelta(seconds=1),
end=utc_now + timedelta(seconds=60),
stat="Average",
)
# assert average
response["MetricDataResults"][0]["Values"].should.equal([30.0, 20.0, 10.0])
response = get_data(
start=last_week - timedelta(seconds=1),
end=utc_now + timedelta(seconds=60),
stat="Maximum",
)
# assert maximum
response["MetricDataResults"][0]["Values"].should.equal([50.0, 20.0, 10.0])
response = get_data(
start=last_week - timedelta(seconds=1),
end=utc_now + timedelta(seconds=60),
stat="Minimum",
)
# assert minimum
response["MetricDataResults"][0]["Values"].should.equal([10.0, 20.0, 10.0])
@mock_cloudwatch