From 612c2181280f382ef6a5fcecd2a0017f237c0bc7 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sun, 31 Jan 2021 17:46:46 +0800 Subject: [PATCH] pybind/mgr/insights: PEP8 cleanups Signed-off-by: Kefu Chai --- src/pybind/mgr/insights/health.py | 31 +++++---- src/pybind/mgr/insights/module.py | 15 +++-- src/pybind/mgr/insights/tests/test_health.py | 66 ++++++++++---------- src/pybind/mgr/tox.ini | 1 + 4 files changed, 60 insertions(+), 53 deletions(-) diff --git a/src/pybind/mgr/insights/health.py b/src/pybind/mgr/insights/health.py index 48e5c37f41b89..1deb5d3a61188 100644 --- a/src/pybind/mgr/insights/health.py +++ b/src/pybind/mgr/insights/health.py @@ -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) diff --git a/src/pybind/mgr/insights/module.py b/src/pybind/mgr/insights/module.py index 3566ffb78a6b0..a4be6d74a40a1 100644 --- a/src/pybind/mgr/insights/module.py +++ b/src/pybind/mgr/insights/module.py @@ -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 diff --git a/src/pybind/mgr/insights/tests/test_health.py b/src/pybind/mgr/insights/tests/test_health.py index c39e1097d4637..06e6454f92ce8 100644 --- a/src/pybind/mgr/insights/tests/test_health.py +++ b/src/pybind/mgr/insights/tests/test_health.py @@ -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()) diff --git a/src/pybind/mgr/tox.ini b/src/pybind/mgr/tox.ini index bd0d7c861fd07..ebf7979325ae5 100644 --- a/src/pybind/mgr/tox.ini +++ b/src/pybind/mgr/tox.ini @@ -88,6 +88,7 @@ deps = autopep8 modules = cephadm + insights orchestrator prometheus telemetry -- 2.39.5