diff --git a/vitrage/datasources/cinder/volume/transformer.py b/vitrage/datasources/cinder/volume/transformer.py index d8e8d1d92..cb8223473 100644 --- a/vitrage/datasources/cinder/volume/transformer.py +++ b/vitrage/datasources/cinder/volume/transformer.py @@ -27,6 +27,7 @@ from vitrage.datasources import transformer_base as tbase from vitrage.datasources.transformer_base import build_key from vitrage.datasources.transformer_base import extract_field_value import vitrage.graph.utils as graph_utils +from vitrage.utils.datetime import format_timestamp class CinderVolumeTransformer(ResourceTransformerBase): @@ -121,7 +122,7 @@ class CinderVolumeTransformer(ResourceTransformerBase): vitrage_sample_timestamp=vitrage_sample_timestamp, entity_id=volume_id, entity_state=volume_state, - update_timestamp=update_timestamp, + update_timestamp=format_timestamp(update_timestamp), metadata=metadata) def _create_snapshot_neighbors(self, entity_event): diff --git a/vitrage/datasources/driver_base.py b/vitrage/datasources/driver_base.py index df4a70f39..cccd70962 100644 --- a/vitrage/datasources/driver_base.py +++ b/vitrage/datasources/driver_base.py @@ -97,7 +97,7 @@ class DriverBase(object): @staticmethod def _add_sampling_time(entity): - entity[DSProps.SAMPLE_DATE] = str(datetime_utils.utcnow()) + entity[DSProps.SAMPLE_DATE] = datetime_utils.format_utcnow() @staticmethod def _add_datasource_action(entity, datasource_action): diff --git a/vitrage/datasources/transformer_base.py b/vitrage/datasources/transformer_base.py index ac4337ca1..127b47e5d 100644 --- a/vitrage/datasources/transformer_base.py +++ b/vitrage/datasources/transformer_base.py @@ -359,7 +359,9 @@ class TransformerBase(object): @staticmethod def _format_update_timestamp(update_timestamp, sample_timestamp): - return update_timestamp if update_timestamp else sample_timestamp + update_timestamp = update_timestamp if update_timestamp \ + else sample_timestamp + return datetime_utils.format_timestamp(update_timestamp) @staticmethod def get_enrich_query(event): diff --git a/vitrage/tests/mocks/mock_driver.py b/vitrage/tests/mocks/mock_driver.py index f81d86f9d..275ce4096 100644 --- a/vitrage/tests/mocks/mock_driver.py +++ b/vitrage/tests/mocks/mock_driver.py @@ -396,7 +396,7 @@ def simple_static_generators(switch_num=2, host_num=10, snap_vals = {} snap_vals.update({ DSProps.DATASOURCE_ACTION: DatasourceAction.SNAPSHOT, - DSProps.SAMPLE_DATE: utcnow()}) + DSProps.SAMPLE_DATE: str(utcnow())}) test_entity_spec_list.append( {tg.DYNAMIC_INFO_FKEY: tg.DRIVER_STATIC_SNAPSHOT_D, tg.STATIC_INFO_FKEY: tg.DRIVER_STATIC_SNAPSHOT_S, @@ -411,7 +411,7 @@ def simple_static_generators(switch_num=2, host_num=10, update_vals = {} update_vals.update({ DSProps.DATASOURCE_ACTION: DatasourceAction.UPDATE, - DSProps.SAMPLE_DATE: utcnow()}) + DSProps.SAMPLE_DATE: str(utcnow())}) test_entity_spec_list.append( {tg.DYNAMIC_INFO_FKEY: tg.DRIVER_STATIC_SNAPSHOT_D, tg.STATIC_INFO_FKEY: None, diff --git a/vitrage/utils/datetime.py b/vitrage/utils/datetime.py index 1af364b3f..65b560b2b 100644 --- a/vitrage/utils/datetime.py +++ b/vitrage/utils/datetime.py @@ -28,6 +28,10 @@ def utcnow(with_timezone=True): return timeutils.utcnow(with_timezone) +def format_utcnow(with_timezone=True, date_format=TIMESTAMP_FORMAT): + return utcnow(with_timezone).strftime(date_format) + + def change_time_str_format(timestamp_str, old_format, new_format): utc = datetime.strptime(timestamp_str, old_format) return utc.strftime(new_format) @@ -43,3 +47,8 @@ def change_to_utc_time_and_format(timestamp_str, new_format): def format_unix_timestamp(timestamp, date_format=TIMESTAMP_FORMAT): return datetime.fromtimestamp(float(timestamp)) \ .strftime(date_format) + + +def format_timestamp(timestamp_str, new_format=TIMESTAMP_FORMAT): + return parser.parse(timestamp_str).strftime(new_format) if timestamp_str \ + else None