add --no-traits for event-list

Currently we print traits list for each event when run event-list
command, but that is a lot of information. After system run for a
few days, the event-list command prints so much that it is not
convenient to know the whole event-list even with less command.

This patch adds a simple argument called --no-traits, it is False
by default, event-list will not print traits if this argument is
specified. Since this argument is boolean type, no need to assign
a value, if it is specified, then it is actived.

Change-Id: Ib69209fef5967a18f094f2d4665fa189e4ca3e6a
This commit is contained in:
ZhiQiang Fan
2015-01-16 12:42:47 +08:00
parent 945f9a392a
commit f5fd6afbd1
2 changed files with 66 additions and 12 deletions

View File

@@ -919,12 +919,22 @@ class ShellEventListCommandTest(utils.BaseTestCase):
"generated": "2015-01-12T04:03:25.741471",
"message_id": "fb2bef58-88af-4380-8698-e0f18fcf452d",
"event_type": "compute.instance.create.start",
"traits": [{
"name": "state",
"type": "string",
"value": "building",
}],
},
{
"traits": [],
"generated": "2015-01-12T04:03:28.452495",
"message_id": "9b20509a-576b-4995-acfa-1a24ee5cf49f",
"event_type": "compute.instance.create.end",
"traits": [{
"name": "state",
"type": "string",
"value": "active",
}],
},
]
@@ -933,6 +943,7 @@ class ShellEventListCommandTest(utils.BaseTestCase):
self.cc = mock.Mock()
self.args = mock.Mock()
self.args.query = None
self.args.no_traits = None
@mock.patch('sys.stdout', new=six.StringIO())
def test_event_list(self):
@@ -941,16 +952,54 @@ class ShellEventListCommandTest(utils.BaseTestCase):
self.cc.events.list.return_value = ret_events
ceilometer_shell.do_event_list(self.cc, self.args)
self.assertEqual('''\
+--------------------------------------+-------------------------------\
+----------------------------+--------+
| Message ID | Event Type \
| Generated | Traits |
+--------------------------------------+-------------------------------\
+----------------------------+--------+
| fb2bef58-88af-4380-8698-e0f18fcf452d | compute.instance.create.start \
| 2015-01-12T04:03:25.741471 | |
| 9b20509a-576b-4995-acfa-1a24ee5cf49f | compute.instance.create.end \
| 2015-01-12T04:03:28.452495 | |
+--------------------------------------+-------------------------------\
+----------------------------+--------+
+--------------------------------------+-------------------------------+\
----------------------------+-------------------------------+
| Message ID | Event Type |\
Generated | Traits |
+--------------------------------------+-------------------------------+\
----------------------------+-------------------------------+
| fb2bef58-88af-4380-8698-e0f18fcf452d | compute.instance.create.start |\
2015-01-12T04:03:25.741471 | +-------+--------+----------+ |
| | |\
| | name | type | value | |
| | |\
| +-------+--------+----------+ |
| | |\
| | state | string | building | |
| | |\
| +-------+--------+----------+ |
| 9b20509a-576b-4995-acfa-1a24ee5cf49f | compute.instance.create.end |\
2015-01-12T04:03:28.452495 | +-------+--------+--------+ |
| | |\
| | name | type | value | |
| | |\
| +-------+--------+--------+ |
| | |\
| | state | string | active | |
| | |\
| +-------+--------+--------+ |
+--------------------------------------+-------------------------------+\
----------------------------+-------------------------------+
''', sys.stdout.getvalue())
@mock.patch('sys.stdout', new=six.StringIO())
def test_event_list_no_traits(self):
self.args.no_traits = True
ret_events = [events.Event(mock.Mock(), event)
for event in self.EVENTS]
self.cc.events.list.return_value = ret_events
ceilometer_shell.do_event_list(self.cc, self.args)
self.assertEqual('''\
+--------------------------------------+-------------------------------\
+----------------------------+
| Message ID | Event Type \
| Generated |
+--------------------------------------+-------------------------------\
+----------------------------+
| fb2bef58-88af-4380-8698-e0f18fcf452d | compute.instance.create.start \
| 2015-01-12T04:03:25.741471 |
| 9b20509a-576b-4995-acfa-1a24ee5cf49f | compute.instance.create.end \
| 2015-01-12T04:03:28.452495 |
+--------------------------------------+-------------------------------\
+----------------------------+
''', sys.stdout.getvalue())

View File

@@ -744,11 +744,16 @@ def do_resource_show(cc, args={}):
help='key[op]data_type::value; list. data_type is optional, '
'but if supplied must be string, integer, float'
'or datetime.')
@utils.arg('--no-traits', dest='no_traits', action='store_true',
help='If specified, traits will not be printed.')
def do_event_list(cc, args={}):
"""List events."""
events = cc.events.list(q=options.cli_to_array(args.query))
field_labels = ['Message ID', 'Event Type', 'Generated', 'Traits']
fields = ['message_id', 'event_type', 'generated', 'traits']
if args.no_traits:
field_labels.pop()
fields.pop()
utils.print_list(events, fields, field_labels,
formatters={
'traits': utils.nested_list_of_dict_formatter(