fix: use DATETIME_FORMAT_MS_WITH_TZ instead of isoformat for last_data_timestamp

Addresses review feedback: prefer sientia_do's explicit datetime format
constant over Python's isoformat()/fromisoformat() when round-tripping
last_data_timestamp through Redis, matching the convention already used
elsewhere in mongo_db.py and slot_manager.py.
This commit is contained in:
Eduardo Rios
2026-09-09 10:54:22 -03:00
parent 2467d24188
commit 3c69b35b3f
4 changed files with 16 additions and 12 deletions

View File

@@ -453,7 +453,7 @@ class MongoDB(SientiaMonitoring):
- metadata (dict[str, Any]): Workflow execution metadata - metadata (dict[str, Any]): Workflow execution metadata
- collection_name (str): Name of the MongoDB collection - collection_name (str): Name of the MongoDB collection
- last_data_timestamp (str | None): Last processed timestamp - last_data_timestamp (str | None): Last processed timestamp
(ISO-format string) for filtering (formatted per ``DATETIME_FORMAT_MS_WITH_TZ``) for filtering
- base_data_filter (dict[str, Any]): Base query filter conditions - base_data_filter (dict[str, Any]): Base query filter conditions
Returns: Returns:
@@ -475,12 +475,15 @@ class MongoDB(SientiaMonitoring):
else: else:
# ``notification_queue.timestamp`` is stored as a native BSON # ``notification_queue.timestamp`` is stored as a native BSON
# ``Date`` (``Notification.timestamp`` is a ``datetime``). # ``Date`` (``Notification.timestamp`` is a ``datetime``).
# ``last_data_timestamp`` arrives here as an ISO-format string # ``last_data_timestamp`` arrives here as a string formatted per
# (round-tripped through Redis), so it must be parsed back to # ``DATETIME_FORMAT_MS_WITH_TZ`` (round-tripped through Redis),
# ``datetime`` for the ``$gt`` comparison to be type-correct. # so it must be parsed back to ``datetime`` for the ``$gt``
# comparison to be type-correct.
data_filter = { data_filter = {
**base_data_filter, **base_data_filter,
'timestamp': {'$gt': datetime.fromisoformat(last_data_timestamp)}, 'timestamp': {
'$gt': datetime.strptime(last_data_timestamp, DATETIME_FORMAT_MS_WITH_TZ)
},
} }
self.debug(f'Data filter: {data_filter}', metadata=metadata) self.debug(f'Data filter: {data_filter}', metadata=metadata)

View File

@@ -337,8 +337,9 @@ class SlotManager(SientiaMonitoring):
# ``timestamp`` is a native ``datetime``/``Timestamp`` (BSON Date from # ``timestamp`` is a native ``datetime``/``Timestamp`` (BSON Date from
# Mongo), which the Redis repository's plain ``json.dumps`` cannot # Mongo), which the Redis repository's plain ``json.dumps`` cannot
# serialize. Store it as an ISO-format string instead. # serialize. Store it as a string formatted per
last_data_timestamp = last_data_timestamp.isoformat() # ``DATETIME_FORMAT_MS_WITH_TZ`` instead.
last_data_timestamp = last_data_timestamp.strftime(DATETIME_FORMAT_MS_WITH_TZ)
try: try:
self.redis_repository.set(key, last_data_timestamp, ttl=60 * 60 * 5) self.redis_repository.set(key, last_data_timestamp, ttl=60 * 60 * 5)

View File

@@ -504,7 +504,7 @@ def test_load_latest_data_not_none_last_data_timestamp(mongo_db):
{ {
'metadata': {'workflow_name': 'test_pipeline', 'schedule_name': 'test_schedule'}, 'metadata': {'workflow_name': 'test_pipeline', 'schedule_name': 'test_schedule'},
'collection_name': 'test_collection', 'collection_name': 'test_collection',
'last_data_timestamp': '2023-01-01T12:00:00+00:00', 'last_data_timestamp': '2023-01-01 12:00:00.000000+0000',
'base_data_filter': {'level': 'ERROR'}, 'base_data_filter': {'level': 'ERROR'},
} }
) )
@@ -561,7 +561,7 @@ def test_load_latest_data_run_boundary_no_skip_or_duplicate(mongo_db):
{ {
'metadata': {'workflow_name': 'test_pipeline', 'schedule_name': 'test_schedule'}, 'metadata': {'workflow_name': 'test_pipeline', 'schedule_name': 'test_schedule'},
'collection_name': 'test_collection', 'collection_name': 'test_collection',
'last_data_timestamp': boundary_timestamp.isoformat(), 'last_data_timestamp': boundary_timestamp.strftime(DATETIME_FORMAT_MS_WITH_TZ),
'base_data_filter': {'level': 'ERROR'}, 'base_data_filter': {'level': 'ERROR'},
} }
) )
@@ -578,7 +578,7 @@ def test_load_latest_data_error(mongo_db):
{ {
'metadata': {'workflow_name': 'test_pipeline', 'schedule_name': 'test_schedule'}, 'metadata': {'workflow_name': 'test_pipeline', 'schedule_name': 'test_schedule'},
'collection_name': 'test_collection', 'collection_name': 'test_collection',
'last_data_timestamp': '2023-01-01T12:00:00+00:00', 'last_data_timestamp': '2023-01-01 12:00:00.000000+0000',
'base_data_filter': {'level': 'ERROR'}, 'base_data_filter': {'level': 'ERROR'},
} }
) )

View File

@@ -297,10 +297,10 @@ def test_put_last_data_timestamp_not_empty_dataframe(slot_manager):
result = slot_manager.put_last_data_timestamp(test_data) result = slot_manager.put_last_data_timestamp(test_data)
assert result == '2023-01-01T12:00:01+00:00' assert result == '2023-01-01 12:00:01.000000+0000'
slot_manager.redis_repository.set.assert_called_once_with( slot_manager.redis_repository.set.assert_called_once_with(
'notification_last_timestamp:test_mail_type', '2023-01-01T12:00:01+00:00', ttl=18000 'notification_last_timestamp:test_mail_type', '2023-01-01 12:00:01.000000+0000', ttl=18000
) )