From 977f7d4e9dde1289b2e81be02837bab1710a93db Mon Sep 17 00:00:00 2001 From: Abhinav Date: Tue, 7 Feb 2017 02:45:51 +0530 Subject: [PATCH] Subtracted utc offset from update_timestamp Closes-Bug: #1594979 Change-Id: Ifcb8d738a3093064677660d0eef645d26b2a078c --- vitrage/datasources/nagios/transformer.py | 4 +--- .../unit/datasources/nagios/test_nagios_transformer.py | 9 +++++++-- vitrage/utils/datetime.py | 8 ++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/vitrage/datasources/nagios/transformer.py b/vitrage/datasources/nagios/transformer.py index 8d910e2f5..e65a0a7b5 100644 --- a/vitrage/datasources/nagios/transformer.py +++ b/vitrage/datasources/nagios/transformer.py @@ -38,13 +38,11 @@ class NagiosTransformer(AlarmTransformerBase): def _create_vertex(self, entity_event): - update_timestamp = datetime_utils.change_time_str_format( + update_timestamp = datetime_utils.change_to_utc_time_and_format( entity_event[NagiosProperties.LAST_CHECK], '%Y-%m-%d %H:%M:%S', tbase.TIMESTAMP_FORMAT) - sample_timestamp = entity_event[DSProps.SAMPLE_DATE] - update_timestamp = self._format_update_timestamp(update_timestamp, sample_timestamp) diff --git a/vitrage/tests/unit/datasources/nagios/test_nagios_transformer.py b/vitrage/tests/unit/datasources/nagios/test_nagios_transformer.py index 65a51ff3e..5ed0a5c76 100644 --- a/vitrage/tests/unit/datasources/nagios/test_nagios_transformer.py +++ b/vitrage/tests/unit/datasources/nagios/test_nagios_transformer.py @@ -30,10 +30,11 @@ from vitrage.datasources.nagios.properties import NagiosTestStatus from vitrage.datasources.nagios.transformer import NagiosTransformer from vitrage.datasources.nova.host import NOVA_HOST_DATASOURCE from vitrage.datasources.nova.host.transformer import HostTransformer +from vitrage.datasources import transformer_base as tbase from vitrage.datasources.transformer_base import TransformerBase from vitrage.tests import base from vitrage.tests.mocks import mock_driver as mock_sync - +from vitrage.utils import datetime as datetime_utils LOG = logging.getLogger(__name__) @@ -119,7 +120,11 @@ class NagiosTransformerTest(base.BaseTest): self.assertEqual(EntityCategory.ALARM, vertex[VProps.CATEGORY]) self.assertEqual(event[DSProps.ENTITY_TYPE], vertex[VProps.TYPE]) self.assertEqual(event[NagiosProperties.SERVICE], vertex[VProps.NAME]) - + self.assertEqual(datetime_utils.change_to_utc_time_and_format( + event[NagiosProperties.LAST_CHECK], + '%Y-%m-%d %H:%M:%S', + tbase.TIMESTAMP_FORMAT), + vertex[VProps.UPDATE_TIMESTAMP]) event_type = event.get(DSProps.EVENT_TYPE, None) if event_type is not None: self.assertEqual(vertex[VProps.STATE], diff --git a/vitrage/utils/datetime.py b/vitrage/utils/datetime.py index 40bbe8688..8ebd27ae1 100644 --- a/vitrage/utils/datetime.py +++ b/vitrage/utils/datetime.py @@ -15,6 +15,7 @@ from __future__ import absolute_import from datetime import datetime +from datetime import timedelta from oslo_utils import timeutils @@ -31,6 +32,13 @@ def change_time_str_format(timestamp_str, old_format, new_format): return utc.strftime(new_format) +def change_to_utc_time_and_format(timestamp_str, old_format, new_format): + timestamp = datetime.strptime(timestamp_str, old_format) + timestamp = timestamp - timedelta(seconds=( + datetime.now() - datetime.utcnow()).total_seconds()) + return timestamp.strftime(new_format) + + def format_unix_timestamp(timestamp, date_format=TIMESTAMP_FORMAT): return datetime.fromtimestamp(float(timestamp)) \ .strftime(date_format)