Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions scripts/portstat
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ try:
except KeyError:
pass

from swsscommon.swsscommon import CounterTable, PortCounter
from utilities_common import constants
from utilities_common.intf_filter import parse_interface_in_filter
import utilities_common.multi_asic as multi_asic_util
Expand Down Expand Up @@ -157,20 +158,20 @@ class Portstat(object):
"""
Get the counters info from database.
"""
def get_counters(table_id):
def get_counters(port):
"""
Get the counters from specific table.
"""
fields = ["0"]*BUCKET_NUM

_, fvs = counter_table.get(PortCounter(), port)
fvs = dict(fvs)
for pos, cntr_list in counter_bucket_dict.items():
for counter_name in cntr_list:
full_table_id = COUNTER_TABLE_PREFIX + table_id
counter_data = self.db.get(self.db.COUNTERS_DB, full_table_id, counter_name)
if counter_data is None:
if counter_name not in fvs:
fields[pos] = STATUS_NA
elif fields[pos] != STATUS_NA:
fields[pos] = str(int(fields[pos]) + int(counter_data))
fields[pos] = str(int(fields[pos]) + int(fvs[counter_name]))

cntr = NStats._make(fields)
return cntr
Expand All @@ -196,13 +197,14 @@ class Portstat(object):
cnstat_dict = OrderedDict()
cnstat_dict['time'] = datetime.datetime.now()
ratestat_dict = OrderedDict()
counter_table = CounterTable(self.db.get_redis_client(self.db.COUNTERS_DB))
if counter_port_name_map is None:
return cnstat_dict, ratestat_dict
for port in natsorted(counter_port_name_map):
port_name = port.split(":")[0]
if self.multi_asic.skip_display(constants.PORT_OBJ, port_name):
continue
cnstat_dict[port] = get_counters(counter_port_name_map[port])
cnstat_dict[port] = get_counters(port)
ratestat_dict[port] = get_rates(counter_port_name_map[port])
return cnstat_dict, ratestat_dict

Expand Down
20 changes: 20 additions & 0 deletions tests/mock_tables/dbconnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,30 @@ def keys(self, pattern='*'):
return [key for key in self.redis if regex.match(key)]


class PortCounter:
pass


class CounterTable:
def __init__(self, db):
self.db = db

def get(self, counter, name):
if isinstance(counter, PortCounter):
name_map = "COUNTERS_PORT_NAME_MAP"
else:
return False, ()

key = self.db.hget(name_map, name)
return True, tuple(self.db.get("COUNTERS:" + key).items())


swsssdk.interface.DBInterface._subscribe_keyspace_notification = _subscribe_keyspace_notification
mockredis.MockRedis.config_set = config_set
redis.StrictRedis = SwssSyncClient
SonicV2Connector.connect = connect_SonicV2Connector
swsscommon.SonicV2Connector = SonicV2Connector
swsscommon.ConfigDBConnector = ConfigDBConnector
swsscommon.ConfigDBPipeConnector = ConfigDBPipeConnector
swsscommon.CounterTable = CounterTable
swsscommon.PortCounter = PortCounter