Skip to content

Commit

Permalink
Event Counters CLI (#2449)
Browse files Browse the repository at this point in the history
Add CLI for event_counters
  • Loading branch information
zbud-msft authored Oct 23, 2022
1 parent 2dab0d0 commit 254cafc
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
26 changes: 26 additions & 0 deletions show/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,32 @@ def vrf(vrf_name):
body.append(["", intf])
click.echo(tabulate(body, header))

#
# 'events' command ("show event-counters")
#

@cli.command()
def event_counters():
"""Show events counter"""
# dump keys as formatted
counters_db = SonicV2Connector(host='127.0.0.1')
counters_db.connect(counters_db.COUNTERS_DB, retry_on=False)

header = ['name', 'count']
keys = counters_db.keys(counters_db.COUNTERS_DB, 'COUNTERS_EVENTS*')
table = []

for key in natsorted(keys):
key_list = key.split(':')
data_dict = counters_db.get_all(counters_db.COUNTERS_DB, key)
table.append((key_list[1], data_dict["value"]))

if table:
click.echo(tabulate(table, header, tablefmt='simple', stralign='right'))
else:
click.echo('No data available in COUNTERS_EVENTS\n')


#
# 'arp' command ("show arp")
#
Expand Down
18 changes: 18 additions & 0 deletions tests/event_counters_input/counters_db.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"COUNTERS_EVENTS:latency_in_ms": {
"value": 2
},
"COUNTERS_EVENTS:missed_to_cache": {
"value": 0
},
"COUNTERS_EVENTS:missed_internal": {
"value": 0
},
"COUNTERS_EVENTS:missed_by_slow_receiver": {
"value": 0
},
"COUNTERS_EVENTS:published": {
"value": 40147
}
}

37 changes: 37 additions & 0 deletions tests/show_event_counters_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import os
import sys
from click.testing import CliRunner
from utilities_common.db import Db
import show.main as show

class TestShowEventCounters(object):
@classmethod
def setup_class(cls):
print("SETUP")
os.environ["UTILITIES_UNIT_TESTING"] = "1"

def test_event_counters_show(self):
from .mock_tables import dbconnector
test_path = os.path.dirname(os.path.abspath(__file__))
mock_db_path = os.path.join(test_path, "event_counters_input")
jsonfile_counters = os.path.join(mock_db_path, "counters_db")
dbconnector.dedicated_dbs['COUNTERS_DB'] = jsonfile_counters
runner = CliRunner()
db = Db()
expected_output = """\
name count
----------------------- -------
latency_in_ms 2
missed_by_slow_receiver 0
missed_internal 0
missed_to_cache 0
published 40147
"""

result = runner.invoke(show.cli.commands['event-counters'], [], obj=db)
print(result.exit_code)
print(result.output)
dbconnector.dedicated_dbs['COUNTERS_DB'] = None
assert result.exit_code == 0
assert result.output == expected_output

0 comments on commit 254cafc

Please sign in to comment.