better parsing
This commit is contained in:
@ -778,24 +778,20 @@ 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,
|
||||||
'K': 1024,
|
'K': 1024,
|
||||||
'M': 1024**2,
|
'M': 1024**2,
|
||||||
'G': 1024**3,
|
'G': 1024**3,
|
||||||
'T': 1024**4,
|
'T': 1024**4,
|
||||||
'P': 1024**5
|
'P': 1024**5
|
||||||
@ -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
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user