]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
pybind/mgr/insights: PEP8 cleanups
authorKefu Chai <kchai@redhat.com>
Sun, 31 Jan 2021 09:46:46 +0000 (17:46 +0800)
committerKefu Chai <kchai@redhat.com>
Wed, 3 Feb 2021 09:17:24 +0000 (17:17 +0800)
Signed-off-by: Kefu Chai <kchai@redhat.com>
src/pybind/mgr/insights/health.py
src/pybind/mgr/insights/module.py
src/pybind/mgr/insights/tests/test_health.py
src/pybind/mgr/tox.ini

index 48e5c37f41b899bda6f1d15d6493e36aab54ebb3..1deb5d3a611889bbc876b9aca888f0036d41fb16 100644 (file)
@@ -3,30 +3,33 @@ from collections import defaultdict
 import datetime
 
 # freq to write cached state to disk
-PERSIST_PERIOD = datetime.timedelta(seconds = 10)
+PERSIST_PERIOD = datetime.timedelta(seconds=10)
 # on disk key prefix
 HEALTH_HISTORY_KEY_PREFIX = "health_history/"
 # apply on offset to "now": used for testing
 NOW_OFFSET = None
 
+
 class HealthEncoder(json.JSONEncoder):
     def default(self, obj):
         if isinstance(obj, set):
             return list(obj)
         return json.JSONEncoder.default(self, obj)
 
+
 class HealthCheckAccumulator(object):
     """
     Deuplicated storage of health checks.
     """
-    def __init__(self, init_checks = None):
+
+    def __init__(self, init_checks=None):
         # check : severity : { summary, detail }
         # summary and detail are deduplicated
         self._checks = defaultdict(lambda:
-            defaultdict(lambda: {
-                "summary": set(),
-                "detail": set()
-            }))
+                                   defaultdict(lambda: {
+                                       "summary": set(),
+                                       "detail": set()
+                                   }))
 
         if init_checks:
             self._update(init_checks)
@@ -88,6 +91,7 @@ class HealthCheckAccumulator(object):
 
         return changed
 
+
 class HealthHistorySlot(object):
     """
     Manage the life cycle of a health history time slot.
@@ -99,7 +103,8 @@ class HealthHistorySlot(object):
     need_flush returns true. Once the state has been flushed, reset the dirty
     bit by calling mark_flushed.
     """
-    def __init__(self, init_health = dict()):
+
+    def __init__(self, init_health=dict()):
         self._checks = HealthCheckAccumulator(init_health.get("checks"))
         self._slot = self._curr_slot()
         self._next_flush = None
@@ -109,7 +114,7 @@ class HealthHistorySlot(object):
             self.key(), self._next_flush, self._checks)
 
     def health(self):
-        return dict(checks = self._checks.checks())
+        return dict(checks=self._checks.checks())
 
     def key(self):
         """Identifier in the persist store"""
@@ -150,7 +155,7 @@ class HealthHistorySlot(object):
     def key_range(hours):
         """Return the time slot keys for the past N hours"""
         def inner(curr, hours):
-            slot = curr - datetime.timedelta(hours = hours)
+            slot = curr - datetime.timedelta(hours=hours)
             return HealthHistorySlot._key(slot)
         curr = HealthHistorySlot._curr_slot()
         return map(lambda i: inner(curr, i), range(hours))
@@ -184,7 +189,7 @@ class HealthHistorySlot(object):
         """Slot for the current UTC time"""
         dt = HealthHistorySlot._now()
         return datetime.datetime(
-            year  = dt.year,
-            month = dt.month,
-            day   = dt.day,
-            hour  = dt.hour)
+            year=dt.year,
+            month=dt.month,
+            day=dt.day,
+            hour=dt.hour)
index 3566ffb78a6b0b48a408554bacd6653f13ba94bf..a4be6d74a40a1ed0e8cb9b2f29aaf219f91aadba 100644 (file)
@@ -95,7 +95,7 @@ class Module(MgrModule):
             # build store data entry
             slot = self._health_slot.health()
             assert "version" not in slot
-            slot.update(dict(version = ON_DISK_VERSION))
+            slot.update(dict(version=ON_DISK_VERSION))
             data = json.dumps(slot, cls=health_util.HealthEncoder)
 
             self.log.debug("Storing health key {} data {}".format(
@@ -113,7 +113,7 @@ class Module(MgrModule):
 
     def _health_prune_history(self, hours):
         """Prune old health entries"""
-        cutoff = datetime.datetime.utcnow() - datetime.timedelta(hours = hours)
+        cutoff = datetime.datetime.utcnow() - datetime.timedelta(hours=hours)
         for key in self._health_filter(lambda ts: ts <= cutoff):
             self.log.info("Removing old health slot key {}".format(key))
             self.set_store(key, None)
@@ -139,8 +139,8 @@ class Module(MgrModule):
         collector.merge(self._health_slot)
 
         return dict(
-           current = json.loads(self.get("health")["json"]),
-           history = collector.health()
+            current=json.loads(self.get("health")["json"]),
+            history=collector.health()
         )
 
     def _version_parse(self, version):
@@ -209,7 +209,6 @@ class Module(MgrModule):
             for s in ['osd.numpg', 'osd.stat_bytes', 'osd.stat_bytes_used']:
                 osd['stats'][s.split('.')[1]] = self.get_latest('osd', str(osd["osd"]), s)
 
-
     def _config_dump(self):
         """Report cluster configuration
 
@@ -217,7 +216,7 @@ class Module(MgrModule):
         configuration defaults; these can be inferred from the version number.
         """
         result = CommandResult("")
-        args = dict(prefix = "config dump", format = "json")
+        args = dict(prefix="config dump", format="json")
         self.send_command(result, "mon", "", json.dumps(args), "")
         ret, outb, outs = result.wait()
         if ret == 0:
@@ -236,8 +235,8 @@ class Module(MgrModule):
         report = {}
 
         report.update({
-            "version": dict(full = self.version,
-                **self._version_parse(self.version))
+            "version": dict(full=self.version,
+                            **self._version_parse(self.version))
         })
 
         # crash history
index c39e1097d4637ccfb1e9ba2a9c903ea9ddd0e990..06e6454f92ce873c3ce363921bba32529856f86f 100644 (file)
@@ -2,6 +2,7 @@ import unittest
 from tests import mock
 from ..health import *
 
+
 class HealthChecksTest(unittest.TestCase):
     def test_check_accum_empty(self):
         # health checks accum initially empty reports empty
@@ -86,7 +87,7 @@ class HealthChecksTest(unittest.TestCase):
         self.assertFalse(h.add({
             "C0": {
                 "severity": "S0",
-                "summary": { "message": "s0" },
+                "summary": {"message": "s0"},
                 "detail": []
             }
         }))
@@ -94,16 +95,16 @@ class HealthChecksTest(unittest.TestCase):
         self.assertFalse(h.add({
             "C0": {
                 "severity": "S0",
-                "summary": { "message": "s1" },
-                "detail": [{ "message": "d1" }]
+                "summary": {"message": "s1"},
+                "detail": [{"message": "d1"}]
             }
         }))
 
         self.assertFalse(h.add({
             "C0": {
                 "severity": "S0",
-                "summary": { "message": "s0" },
-                "detail": [{ "message": "d1" }, { "message": "d2" }]
+                "summary": {"message": "s0"},
+                "detail": [{"message": "d1"}, {"message": "d2"}]
             }
         }))
 
@@ -114,7 +115,7 @@ class HealthChecksTest(unittest.TestCase):
         self.assertTrue(h.add({
             "C0": {
                 "severity": "S0",
-                "summary": { "message": "s3" },
+                "summary": {"message": "s3"},
                 "detail": []
             }
         }))
@@ -122,24 +123,24 @@ class HealthChecksTest(unittest.TestCase):
         self.assertTrue(h.add({
             "C0": {
                 "severity": "S0",
-                "summary": { "message": "s1" },
-                "detail": [{ "message": "d4" }]
+                "summary": {"message": "s1"},
+                "detail": [{"message": "d4"}]
             }
         }))
 
         self.assertTrue(h.add({
             "C0": {
                 "severity": "S2",
-                "summary": { "message": "s0" },
-                "detail": [{ "message": "d0" }]
+                "summary": {"message": "s0"},
+                "detail": [{"message": "d0"}]
             }
         }))
 
         self.assertTrue(h.add({
             "C2": {
                 "severity": "S0",
-                "summary": { "message": "s0" },
-                "detail": [{ "message": "d0" }, { "message": "d1" }]
+                "summary": {"message": "s0"},
+                "detail": [{"message": "d0"}, {"message": "d1"}]
             }
         }))
 
@@ -172,6 +173,7 @@ class HealthChecksTest(unittest.TestCase):
             }
         })
 
+
 class HealthHistoryTest(unittest.TestCase):
     def _now(self):
         # return some time truncated at 30 minutes past the hour. this lets us
@@ -180,11 +182,11 @@ class HealthHistoryTest(unittest.TestCase):
         # tracking health history.
         dt = datetime.datetime.utcnow()
         return datetime.datetime(
-            year   = dt.year,
-            month  = dt.month,
-            day    = dt.day,
-            hour   = dt.hour,
-            minute = 30)
+            year=dt.year,
+            month=dt.month,
+            day=dt.day,
+            hour=dt.hour,
+            minute=30)
 
     def test_empty_slot(self):
         now = self._now()
@@ -193,7 +195,7 @@ class HealthHistoryTest(unittest.TestCase):
         h = HealthHistorySlot()
 
         # reports no historical checks
-        self.assertEqual(h.health(), { "checks": {} })
+        self.assertEqual(h.health(), {"checks": {}})
 
         # an empty slot doesn't need to be flushed
         self.assertFalse(h.need_flush())
@@ -206,7 +208,7 @@ class HealthHistoryTest(unittest.TestCase):
         self.assertFalse(h.expired())
 
         # an hour from now it would be expired
-        future = now + datetime.timedelta(hours = 1)
+        future = now + datetime.timedelta(hours=1)
         HealthHistorySlot._now = mock.Mock(return_value=future)
         self.assertTrue(h.expired())
 
@@ -217,11 +219,11 @@ class HealthHistoryTest(unittest.TestCase):
         h = HealthHistorySlot()
         self.assertFalse(h.need_flush())
 
-        self.assertTrue(h.add(dict(checks = {
+        self.assertTrue(h.add(dict(checks={
             "C0": {
                 "severity": "S0",
-                "summary": { "message": "s0" },
-                "detail": [{ "message": "d0" }]
+                "summary": {"message": "s0"},
+                "detail": [{"message": "d0"}]
             }
         })))
         # no flush needed, yet...
@@ -241,23 +243,23 @@ class HealthHistoryTest(unittest.TestCase):
         # been dirty for the persistence period
         dt = datetime.datetime.utcnow()
         now = datetime.datetime(
-            year   = dt.year,
-            month  = dt.month,
-            day    = dt.day,
-            hour   = dt.hour,
-            minute = 59,
-            second = 59)
+            year=dt.year,
+            month=dt.month,
+            day=dt.day,
+            hour=dt.hour,
+            minute=59,
+            second=59)
         HealthHistorySlot._now = mock.Mock(return_value=now)
         h = HealthHistorySlot()
         self.assertFalse(h.expired())
         self.assertFalse(h.need_flush())
 
         # now it is dirty, but it doesn't need a flush
-        self.assertTrue(h.add(dict(checks = {
+        self.assertTrue(h.add(dict(checks={
             "C0": {
                 "severity": "S0",
-                "summary": { "message": "s0" },
-                "detail": [{ "message": "d0" }]
+                "summary": {"message": "s0"},
+                "detail": [{"message": "d0"}]
             }
         })))
         self.assertFalse(h.expired())
@@ -266,7 +268,7 @@ class HealthHistoryTest(unittest.TestCase):
         # advance time past the hour so it expires, but not past the persistence
         # period deadline for the last event that set the dirty bit
         self.assertTrue(PERSIST_PERIOD.total_seconds() > 5)
-        future = now + datetime.timedelta(seconds = 5)
+        future = now + datetime.timedelta(seconds=5)
         HealthHistorySlot._now = mock.Mock(return_value=future)
 
         self.assertTrue(h.expired())
index bd0d7c861fd07d021adaba9b578294f28c758fd3..ebf7979325ae53fb01af2f935db5d12ad3070e74 100644 (file)
@@ -88,6 +88,7 @@ deps =
     autopep8
 modules =
     cephadm
+    insights
     orchestrator
     prometheus
     telemetry