]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
mgr/devicehealth: fix missing timezone from time delta calculation
authorYaarit Hatuka <yaarit@redhat.com>
Mon, 4 Oct 2021 18:50:01 +0000 (18:50 +0000)
committerYaarit Hatuka <yaarit@redhat.com>
Tue, 5 Oct 2021 01:03:46 +0000 (01:03 +0000)
An error occurs when subtracting a datetime object that is offset-naive
(i.e. unaware of timezone) from a datetime object which is offset-aware.

datetime.utcnow() is missing timezone info, e.g.:
'2021-09-22 13:18:45.021712',
while life_expectancy_max is in the format of:
'2021-09-28 00:00:00.000000+00:00',
hence we need to add timezone info to the former when calculating
their time delta.

Please note that we calculate time delta using `datetime.utcnow()` in
`serve()` in this module, but there we refer to the delta in seconds,
which works fine.

Fixes: https://tracker.ceph.com/issues/52327
Signed-off-by: Yaarit Hatuka <yaarit@redhat.com>
src/pybind/mgr/devicehealth/module.py

index 9812c91a87cc7ef659cd1aa2c46c8f148020ff38..9b6c1fe431c508523ff95b7a398547ff1a2f1eae 100644 (file)
@@ -9,7 +9,7 @@ import operator
 import rados
 import re
 from threading import Event
-from datetime import datetime, timedelta
+from datetime import datetime, timedelta, timezone
 from typing import cast, Any, Dict, List, Optional, Sequence, Tuple, TYPE_CHECKING, Union
 
 TIME_FORMAT = '%Y%m%d-%H%M%S'
@@ -588,7 +588,7 @@ CREATE TABLE DeviceHealthMetrics (
         devs = self.get("devices")
         osds_in = {}
         osds_out = {}
-        now = datetime.utcnow()
+        now = datetime.now(timezone.utc)  # e.g. '2021-09-22 13:18:45.021712+00:00'
         osdmap = self.get("osd_map")
         assert osdmap is not None
         for dev in devs['devices']:
@@ -602,7 +602,7 @@ CREATE TABLE DeviceHealthMetrics (
                 continue
             # life_expectancy_(min/max) is in the format of:
             # '%Y-%m-%dT%H:%M:%S.%f%z', e.g.:
-            # '2019-01-20T21:12:12.000000Z'
+            # '2019-01-20 21:12:12.000000+00:00'
             life_expectancy_max = datetime.strptime(
                 dev['life_expectancy_max'],
                 '%Y-%m-%dT%H:%M:%S.%f%z')