Commit 3ce9a5e7 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

opcodes: Add function for compact summary



Depending on the opcode and its parameters, the existing “Summary”
function can give a rater long summary. For displaying the summary in
logs and in the lock monitor, it should be shorter. Hence this new
function is added to just use the opcode ID with common prefixes
replaced (e.g. “INSTANCE_” becomes “I_”). Opcode values are not used.
Signed-off-by: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent c307ee34
......@@ -129,6 +129,13 @@ _TestClusterOsList = ht.TOr(ht.TNone,
_TestNicDef = ht.TDictOf(ht.TElemOf(constants.INIC_PARAMS),
ht.TOr(ht.TNone, ht.TNonEmptyString))
_SUMMARY_PREFIX = {
"CLUSTER_": "C_",
"GROUP_": "G_",
"NODE_": "N_",
"INSTANCE_": "I_",
}
def _NameToId(name):
"""Convert an opcode class name to an OP_ID.
......@@ -463,6 +470,20 @@ class OpCode(BaseOpCode):
txt = "%s(%s)" % (txt, field_value)
return txt
def TinySummary(self):
"""Generates a compact summary description of the opcode.
"""
assert self.OP_ID.startswith("OP_")
text = self.OP_ID[3:]
for (prefix, supplement) in _SUMMARY_PREFIX.items():
if text.startswith(prefix):
return supplement + text[len(prefix):]
return text
# cluster opcodes
......
......@@ -47,6 +47,8 @@ class TestOpcodes(unittest.TestCase):
self.assert_(len(cls.OP_ID) > 3)
self.assertEqual(cls.OP_ID, cls.OP_ID.upper())
self.assertEqual(cls.OP_ID, opcodes._NameToId(cls.__name__))
self.assertFalse(compat.any(cls.OP_ID.startswith(prefix)
for prefix in opcodes._SUMMARY_PREFIX.keys()))
self.assertRaises(TypeError, cls, unsupported_parameter="some value")
......@@ -102,6 +104,18 @@ class TestOpcodes(unittest.TestCase):
self.assertEqual(OpTest(data="node1.example.com").Summary(),
"TEST(node1.example.com)")
def testTinySummary(self):
self.assertFalse(utils.FindDuplicates(opcodes._SUMMARY_PREFIX.values()))
self.assertTrue(compat.all(prefix.endswith("_") and supplement.endswith("_")
for (prefix, supplement) in
opcodes._SUMMARY_PREFIX.items()))
self.assertEqual(opcodes.OpClusterPostInit().TinySummary(), "C_POST_INIT")
self.assertEqual(opcodes.OpNodeRemove().TinySummary(), "N_REMOVE")
self.assertEqual(opcodes.OpInstanceMigrate().TinySummary(), "I_MIGRATE")
self.assertEqual(opcodes.OpGroupQuery().TinySummary(), "G_QUERY")
self.assertEqual(opcodes.OpTestJqueue().TinySummary(), "TEST_JQUEUE")
def testListSummary(self):
class OpTest(opcodes.OpCode):
OP_DSC_FIELD = "data"
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment