Browse Source

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 75fb4d12133f4f7d304ded5612bbf96bb9438d2e)
changes/46/716346/2
Ifat Afek 1 year ago
committed by Eyal
parent
commit
a6939dde9e
5 changed files with 17 additions and 5 deletions
  1. +2
    -1
      vitrage/datasources/cinder/volume/transformer.py
  2. +1
    -1
      vitrage/datasources/driver_base.py
  3. +3
    -1
      vitrage/datasources/transformer_base.py
  4. +2
    -2
      vitrage/tests/mocks/mock_driver.py
  5. +9
    -0
      vitrage/utils/datetime.py

+ 2
- 1
vitrage/datasources/cinder/volume/transformer.py 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):


+ 1
- 1
vitrage/datasources/driver_base.py 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):


+ 3
- 1
vitrage/datasources/transformer_base.py 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):


+ 2
- 2
vitrage/tests/mocks/mock_driver.py 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,


+ 9
- 0
vitrage/utils/datetime.py 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

Loading…
Cancel
Save