diff --git a/java/src/main/java/monasca/api/infrastructure/persistence/influxdb/InfluxV9Utils.java b/java/src/main/java/monasca/api/infrastructure/persistence/influxdb/InfluxV9Utils.java index 8af239275..031380e55 100644 --- a/java/src/main/java/monasca/api/infrastructure/persistence/influxdb/InfluxV9Utils.java +++ b/java/src/main/java/monasca/api/infrastructure/persistence/influxdb/InfluxV9Utils.java @@ -57,11 +57,13 @@ public class InfluxV9Utils { final StringBuilder sb = new StringBuilder(); if (startTime != null) { - sb.append(String.format(" and time > %1$ds", startTime.getMillis() / 1000)); + sb.append(String.format(" and time > " + "'" + ISODateTimeFormat.dateTime().print(startTime) + + "'")); } if (endTime != null) { - sb.append(String.format(" and time < %1$ds", endTime.getMillis() / 1000)); + sb.append(String.format(" and time < " + "'" + ISODateTimeFormat.dateTime().print(endTime) + + "'")); } return sb.toString(); diff --git a/monasca_api/common/repositories/influxdb/metrics_repository.py b/monasca_api/common/repositories/influxdb/metrics_repository.py index 473ccfec3..f8f831763 100644 --- a/monasca_api/common/repositories/influxdb/metrics_repository.py +++ b/monasca_api/common/repositories/influxdb/metrics_repository.py @@ -133,10 +133,12 @@ class MetricsRepository(metrics_repository.MetricsRepository): 'utf8')) if start_timestamp: - where_clause += " and time > " + str(int(start_timestamp)) + "s" + where_clause += " and time > " + str(int(start_timestamp * + 1000000)) + "u" if end_timestamp: - where_clause += " and time < " + str(int(end_timestamp)) + "s" + where_clause += " and time < " + str(int(end_timestamp * + 1000000)) + "u" return where_clause @@ -468,11 +470,12 @@ class MetricsRepository(metrics_repository.MetricsRepository): time_clause = '' if start_timestamp: - time_clause += (" and time > " + str(int(start_timestamp)) + - "s ") + time_clause += " and time > " + str(int(start_timestamp * + 1000000)) + "u " if end_timestamp: - time_clause += " and time < " + str(int(end_timestamp)) + "s " + time_clause += " and time < " + str(int(end_timestamp * + 1000000)) + "u " offset_clause = self._build_offset_clause(offset, limit) diff --git a/monasca_api/v2/reference/helpers.py b/monasca_api/v2/reference/helpers.py index 04a3c2d7e..41a3ca2ef 100644 --- a/monasca_api/v2/reference/helpers.py +++ b/monasca_api/v2/reference/helpers.py @@ -20,6 +20,7 @@ import urlparse import falcon from oslo_log import log +from oslo_utils import timeutils import simplejson from monasca_api.common.repositories import constants @@ -222,7 +223,8 @@ def get_query_endtime_timestamp(req, required=True): def _convert_time_string(date_time_string): - dt = datetime.datetime.strptime(date_time_string, "%Y-%m-%dT%H:%M:%SZ") + dt = timeutils.parse_isotime(date_time_string) + dt = dt.replace(tzinfo=None) timestamp = (dt - datetime.datetime(1970, 1, 1)).total_seconds() return timestamp diff --git a/monasca_tempest_tests/tests/api/test_alarms_state_history.py b/monasca_tempest_tests/tests/api/test_alarms_state_history.py index 00453dd37..e6a01411b 100644 --- a/monasca_tempest_tests/tests/api/test_alarms_state_history.py +++ b/monasca_tempest_tests/tests/api/test_alarms_state_history.py @@ -31,9 +31,6 @@ class TestAlarmsStateHistory(base.BaseMonascaTest): def resource_setup(cls): super(TestAlarmsStateHistory, cls).resource_setup() - cls._test_start_time = timeutils.iso8601_from_timestamp( - int(time.time()) - 60) - for i in xrange(1, NUM_ALARM_DEFINITIONS + 1): alarm_definition = helpers.create_alarm_definition( name=data_utils.rand_name('alarm_state_history' + str(i)), @@ -98,41 +95,87 @@ class TestAlarmsStateHistory(base.BaseMonascaTest): @test.attr(type="gate") def test_list_alarms_state_history_with_start_time(self): - current_timestamp = int(time.time()) - current_time = timeutils.iso8601_from_timestamp(current_timestamp) - end_time = timeutils.iso8601_from_timestamp(current_timestamp + 120) - query_parms = '?start_time=' + str(current_time) + '&end_time=' + \ - str(end_time) - resp, response_body = self.monasca_client.list_alarms_state_history( - query_parms) - elements = response_body['elements'] - self.assertEqual(0, len(elements)) - return - resp, response_body = self.monasca_client.list_alarms_state_history() - elements = response_body['elements'] - timestamp = elements[1]['timestamp'] - query_parms = '?start_time=' + str(timestamp) - resp, response_body = self.monasca_client.list_alarms_state_history( - query_parms) - elements = response_body['elements'] - self.assertEqual(2, len(elements)) + # 1, get all histories + resp, all_response_body = self.monasca_client.\ + list_alarms_state_history() + all_elements = all_response_body['elements'] + + if len(all_elements) < 2: + error_msg = "Failed test_list_alarms_state_history_with_" \ + "start_time: need 2 or more alarms state history " \ + "to test." + self.fail(error_msg) + + # 2, query min(timestamp) < x + min_element, max_element = self._get_elements_with_min_max_timestamp( + all_elements) + start_time = min_element['timestamp'] + query_params = '?start_time=' + str(start_time) + resp, selected_response_body = self.monasca_client.\ + list_alarms_state_history(query_params) + selected_elements = selected_response_body['elements'] + + # 3. compare #1 and #2 + expected_elements = all_elements + expected_elements.remove(min_element) + self.assertEqual(expected_elements, selected_elements) @test.attr(type="gate") def test_list_alarms_state_history_with_end_time(self): - query_parms = '?end_time=' + str(self._test_start_time) - resp, response_body = self.monasca_client.list_alarms_state_history( - query_parms) - elements = response_body['elements'] - self.assertEqual(0, len(elements)) - return - resp, response_body = self.monasca_client.list_alarms_state_history() - elements = response_body['elements'] - timestamp = elements[2]['timestamp'] - query_parms = '?end_time=' + str(timestamp) - resp, response_body = self.monasca_client.list_alarms_state_history( - query_parms) - elements = response_body['elements'] - self.assertEqual(1, len(elements)) + # 1, get all histories + resp, all_response_body = self.monasca_client.\ + list_alarms_state_history() + all_elements = all_response_body['elements'] + + if len(all_elements) < 2: + error_msg = "Failed test_list_alarms_state_history_with_" \ + "end_time: need 2 or more alarms state history " \ + "to test." + self.fail(error_msg) + + # 2, query x < max(timestamp) + min_element, max_element = self._get_elements_with_min_max_timestamp( + all_elements) + end_time = max_element['timestamp'] + query_params = '?end_time=' + str(end_time) + resp, selected_response_body = self.monasca_client.\ + list_alarms_state_history(query_params) + selected_elements = selected_response_body['elements'] + + # 3. compare #1 and #2 + expected_elements = all_elements + expected_elements.remove(max_element) + self.assertEqual(expected_elements, selected_elements) + + @test.attr(type="gate") + def test_list_alarms_state_history_with_start_end_time(self): + # 1, get all histories + resp, all_response_body = self.monasca_client.\ + list_alarms_state_history() + all_elements = all_response_body['elements'] + + if len(all_elements) < 3: + error_msg = "Failed test_list_alarms_state_history_with_" \ + "start_end_time: need 3 or more alarms state history " \ + "to test." + self.fail(error_msg) + + # 2, query min(timestamp) < x < max(timestamp) + min_element, max_element = self._get_elements_with_min_max_timestamp( + all_elements) + start_time = min_element['timestamp'] + end_time = max_element['timestamp'] + query_params = '?start_time=' + str(start_time) + '&end_time=' + \ + str(end_time) + resp, selected_response_body = self.monasca_client.\ + list_alarms_state_history(query_params) + selected_elements = selected_response_body['elements'] + + # 3. compare #1 and #2 + expected_elements = all_elements + expected_elements.remove(min_element) + expected_elements.remove(max_element) + self.assertEqual(expected_elements, selected_elements) @test.attr(type="gate") def test_list_alarms_state_history_with_offset_limit(self): @@ -224,3 +267,10 @@ class TestAlarmsStateHistory(base.BaseMonascaTest): error_msg = "Failed test_list_alarm_state_history_with_offset" \ "_limit: at least one alarms state history is needed." self.fail(error_msg) + + def _get_elements_with_min_max_timestamp(self, elements): + sorted_elements = sorted(elements, key=lambda element: timeutils. + parse_isotime(element['timestamp'])) + min_element = sorted_elements[0] + max_element = sorted_elements[-1] + return min_element, max_element