better parsing

This commit is contained in:
2025-03-09 20:29:04 -04:00
parent d9b3e62bb5
commit 34f9327afd

View File

@ -778,19 +778,15 @@ class SystemHealthMonitor:
def _parse_size(self, size_str: str) -> float: def _parse_size(self, size_str: str) -> float:
""" """
Parse size string with units to bytes. Parse size string with units to float value
Examples: '1.5G', '500M', '2.3T', etc. Examples: '44.9G', '1.9G', '192.2M', '5.3T'
""" """
if not size_str or not isinstance(size_str, str): if not size_str or not isinstance(size_str, str):
return 0.0 return 0.0
# Remove any spaces and convert to uppercase for consistency # Remove any spaces and normalize format
size_str = size_str.strip().upper() size_str = size_str.strip().upper()
# Handle special case for MB notation
if 'MB' in size_str:
size_str = size_str.replace('MB', 'M')
# Define unit multipliers # Define unit multipliers
units = { units = {
'B': 1, 'B': 1,
@ -803,13 +799,13 @@ class SystemHealthMonitor:
try: try:
# Extract numeric value and unit # Extract numeric value and unit
if size_str[-1] in units: match = re.match(r'^([\d.]+)([BKMGTP])$', size_str)
number = float(size_str[:-1]) if match:
unit = size_str[-1] value = float(match.group(1))
return number * units[unit] unit = match.group(2)
# Handle case where the input is just a number return value * units[unit]
return float(size_str) return float(size_str)
except (ValueError, IndexError): except (ValueError, AttributeError):
logger.debug(f"Could not parse size: {size_str}") logger.debug(f"Could not parse size: {size_str}")
return 0.0 return 0.0