Skip to content

Commit

Permalink
fixes / optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
akpw committed Aug 11, 2024
1 parent 5401636 commit 3811e71
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
6 changes: 3 additions & 3 deletions mktxp/collector/health_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from mktxp.collector.base_collector import BaseCollector
from mktxp.datasource.health_ds import HealthMetricsDataSource

from mktxp.utils.utils import str2bool

class HealthCollector(BaseCollector):
''' System Health Metrics collector
Expand All @@ -26,8 +26,8 @@ def collect(router_entry):
'psu1_voltage', 'psu2_voltage', 'psu1_current', 'psu2_current', 'psu1_state', 'psu2_state',
'poe_out_consumption', 'jack_voltage', '2pin_voltage', 'poe_in_voltage']
translation_table = {
'psu1_state': lambda value: '1' if value=='ok' else '0',
'psu2_state': lambda value: '1' if value=='ok' else '0'}
'psu1_state': lambda value: '1' if str2bool(value, default = False) else '0',
'psu2_state': lambda value: '1' if str2bool(value, default = False) else '0'}
health_records = HealthMetricsDataSource.metric_records(router_entry, metric_labels = health_labels, translation_table = translation_table)
if health_records:
for record in health_records:
Expand Down
8 changes: 5 additions & 3 deletions mktxp/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,16 @@ def parse_mkt_uptime(time):
delta = timedelta(**{key: int(value) for key, value in time_dict.items() if value}).total_seconds()
return int(delta) if delta else 0

def str2bool(str_value):
def str2bool(str_value, default = None):
if not str_value or not type(str_value) is str:
return False
str_value = str_value.lower()
if str_value in ('y', 'yes', 't', 'true', 'on', '1'):
if str_value in ('y', 'yes', 't', 'true', 'on', 'ok', '1'):
return True
elif str_value in ('n', 'no', 'f', 'false', 'off', '0'):
elif str_value in ('n', 'no', 'f', 'false', 'off', 'fail', '0'):
return False
elif default is not None:
return default
else:
raise ValueError(f'Invalid truth value: {str_value}')

Expand Down

0 comments on commit 3811e71

Please sign in to comment.