From 88d587a047bf87f3532d2f35c0801cb540d3d056 Mon Sep 17 00:00:00 2001 From: James Gu Date: Thu, 21 Dec 2017 12:50:49 -0800 Subject: [PATCH] Statistics api failure when end time is not used Statisics api fails when no end time is present with Casssandra. Fixed the bug and added a test case with no end time. Change-Id: I39fd349ab03877d4a67ec89a7859adf2707233f6 story: 2001461 task: 6175 --- AUTHORS | 1 + .../cassandra/metrics_repository.py | 5 +- monasca_api/tests/test_repositories.py | 50 ++++++++++++++++++- .../tests/api/test_statistics.py | 13 ++++- 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/AUTHORS b/AUTHORS index 732413fc0..c0f03967e 100644 --- a/AUTHORS +++ b/AUTHORS @@ -8,6 +8,7 @@ Anh Tran Artur Basiak Ben Motz Bertrand Lallau +Boris Bobrov Brad Klein Cao Xuan Hoang Christoph Held diff --git a/monasca_api/common/repositories/cassandra/metrics_repository.py b/monasca_api/common/repositories/cassandra/metrics_repository.py index 23301052f..9fd910f8d 100644 --- a/monasca_api/common/repositories/cassandra/metrics_repository.py +++ b/monasca_api/common/repositories/cassandra/metrics_repository.py @@ -776,7 +776,10 @@ class MetricsRepository(metrics_repository.AbstractMetricsRepository): columns.extend([x for x in ['avg', 'min', 'max', 'count', 'sum'] if x in statistics]) start_time = datetime.utcfromtimestamp(start_timestamp) - end_time = datetime.utcfromtimestamp(end_timestamp) + if end_timestamp: + end_time = datetime.utcfromtimestamp(end_timestamp) + else: + end_time = datetime.utcnow() for series in series_list: diff --git a/monasca_api/tests/test_repositories.py b/monasca_api/tests/test_repositories.py index 979e94123..11a776b26 100644 --- a/monasca_api/tests/test_repositories.py +++ b/monasca_api/tests/test_repositories.py @@ -1,7 +1,7 @@ # Copyright 2015 Cray Inc. All Rights Reserved. # (C) Copyright 2016-2017 Hewlett Packard Enterprise Development LP # Copyright 2017 Fujitsu LIMITED -# (C) Copyright 2017 SUSE LLC +# (C) Copyright 2017-2018 SUSE LLC # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -419,6 +419,54 @@ class TestRepoMetricsCassandra(base.BaseTestCase): } ], result) + cassandra_future_mock.result.side_effect = [ + [ + Metric( + metric_id=binascii.unhexlify(b"01d39f19798ed27bbf458300bf843edd17654614"), + metric_name='cpu.idle_perc', + dimensions=[ + 'device\trootfs', + 'hostname\thost0', + 'hosttype\tnative', + 'mount_point\t/'] + ) + ], + [ + Measurement(self._convert_time_string("2016-05-19T11:58:24Z"), 95.0, '{}'), + Measurement(self._convert_time_string("2016-05-19T11:58:25Z"), 97.0, '{}'), + Measurement(self._convert_time_string("2016-05-19T11:58:26Z"), 94.0, '{}'), + Measurement(self._convert_time_string("2016-05-19T11:58:27Z"), 96.0, '{}'), + ] + ] + + result = repo.metrics_statistics( + "tenant_id", + "region", + name="cpu.idle_perc", + dimensions=None, + start_timestamp=start_timestamp, + end_timestamp=None, + statistics=['avg', 'min', 'max', 'count', 'sum'], + period=300, + offset=None, + limit=1, + merge_metrics_flag=True, + group_by=None) + + self.assertEqual([ + { + u'dimensions': {'device': 'rootfs', + 'hostname': 'host0', + 'hosttype': 'native', + 'mount_point': '/'}, + u'end_time': u'2016-05-19T12:03:23.999Z', + u'statistics': [[u'2016-05-19T11:58:24.000Z', 95.5, 94.0, 97.0, 4, 382.0]], + u'name': u'cpu.idle_perc', + u'columns': [u'timestamp', 'avg', 'min', 'max', 'count', 'sum'], + u'id': '01d39f19798ed27bbf458300bf843edd17654614' + } + ], result) + @patch("monasca_api.common.repositories.cassandra." "metrics_repository.Cluster.connect") def test_alarm_history(self, cassandra_connect_mock): diff --git a/monasca_tempest_tests/tests/api/test_statistics.py b/monasca_tempest_tests/tests/api/test_statistics.py index 26e1d6a72..18599c4fb 100644 --- a/monasca_tempest_tests/tests/api/test_statistics.py +++ b/monasca_tempest_tests/tests/api/test_statistics.py @@ -1,5 +1,5 @@ # (C) Copyright 2015-2016 Hewlett Packard Enterprise Development LP -# (C) Copyright 2017 SUSE LLC +# (C) Copyright 2017-2018 SUSE LLC # # Licensed under the Apache License, Version 2.0 (the "License"); you may # not use this file except in compliance with the License. You may obtain @@ -121,11 +121,20 @@ class TestStatistics(base.BaseMonascaTest): @decorators.attr(type="gate") def test_list_statistics(self): + self._test_list_statistic(with_end_time=True) + + @decorators.attr(type="gate") + def test_list_statistics_with_no_end_time(self): + self._test_list_statistic(with_end_time=False) + + def _test_list_statistic(self, with_end_time=True): query_parms = '?name=' + str(self._test_name) + \ '&statistics=' + urlparse.quote('avg,sum,min,max,count') + \ '&start_time=' + str(self._start_time_iso) + \ - '&end_time=' + str(self._end_time_iso) + \ '&merge_metrics=true' + '&period=100000' + if with_end_time is True: + query_parms += '&end_time=' + str(self._end_time_iso) + resp, response_body = self.monasca_client.list_statistics( query_parms) self.assertEqual(200, resp.status)