Consistent timestamp format for all datasources

All timestamps in Vitrage should be of the same format:
'%Y-%m-%dT%H:%M:%SZ'

This is needed so webhooks will know what format to expect.

Story: 1776181
Task: 22405


Change-Id: I9849ce0850de46f928a7c01009c7738976aa3478
(cherry picked from commit 75fb4d1213)
This commit is contained in:
Ifat Afek 2018-12-17 11:38:34 +00:00 committed by Eyal
parent 66b6dd4879
commit a6939dde9e
5 changed files with 17 additions and 5 deletions

View File

@ -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):

View File

@ -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):

View File

@ -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):

View File

@ -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,

View File

@ -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