Added null safety checks

This commit is contained in:
2025-05-29 11:44:07 -04:00
parent 147947b8ca
commit f8784eddd2

View File

@ -567,23 +567,24 @@ class SystemHealthMonitor:
last_test_date = smart_data.get('last_test_date', 'N/A') last_test_date = smart_data.get('last_test_date', 'N/A')
age = f"{int(power_on_hours/24/365) if isinstance(power_on_hours, (int, float)) else 'N/A'} years" if power_on_hours != 'N/A' else 'N/A' age = f"{int(power_on_hours/24/365) if isinstance(power_on_hours, (int, float)) else 'N/A'} years" if power_on_hours != 'N/A' else 'N/A'
description += """ # Fix the formatting issue by ensuring all values are strings and not None
device_safe = device or 'N/A'
model_safe = drive_details.get('model') or 'N/A'
serial_safe = drive_details.get('serial') or 'N/A'
capacity_safe = drive_details.get('capacity') or 'N/A'
type_safe = drive_details.get('type') or 'N/A'
firmware_safe = drive_details.get('firmware') or 'N/A'
description += f"""
┏━ DRIVE SPECIFICATIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┏━ DRIVE SPECIFICATIONS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Device Path │ {:<60} ┃ Device Path │ {device_safe:<60}
┃ Model │ {:<60} ┃ Model │ {model_safe:<60}
┃ Serial │ {:<60} ┃ Serial │ {serial_safe:<60}
┃ Capacity │ {:<60} ┃ Capacity │ {capacity_safe:<60}
┃ Type │ {:<60} ┃ Type │ {type_safe:<60}
┃ Firmware │ {:<60} ┃ Firmware │ {firmware_safe:<60}
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
""".format( """
device,
drive_details.get('model', 'N/A'),
drive_details.get('serial', 'N/A'),
drive_details.get('capacity', 'N/A'),
drive_details.get('type', 'N/A'),
drive_details.get('firmware', 'N/A')
)
if drive_info: if drive_info:
perf_metrics = { perf_metrics = {
@ -593,34 +594,34 @@ class SystemHealthMonitor:
'iops': drive_info.get('performance_metrics', {}).get('iops', 'N/A') 'iops': drive_info.get('performance_metrics', {}).get('iops', 'N/A')
} }
description += """ power_on_safe = f"{power_on_hours} hours" if power_on_hours != 'N/A' else 'N/A'
┏━ DRIVE TIMELINE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ last_test_safe = last_test_date or 'N/A'
Power-On Hours │ {:<56} age_safe = age or 'N/A'
┃ Last SMART Test │ {:<56}
┃ Drive Age │ {:<56}
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
""".format(
f"{power_on_hours} hours" if power_on_hours != 'N/A' else 'N/A',
last_test_date,
age
)
description += """ description += f"""
┏━ SMART STATUS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ ┏━ DRIVE TIMELINE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Status │ {:<60} Power-On Hours │ {power_on_safe:<56}
┃ Temperature │ {:<60} ┃ Last SMART Test │ {last_test_safe:<56}
┃ Drive Age │ {age_safe:<56}
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
""".format( """
drive_info.get('smart_status', 'N/A'),
f"{drive_info.get('temperature')}°C" if drive_info.get('temperature') else 'N/A' smart_status_safe = drive_info.get('smart_status') or 'N/A'
) temp_safe = f"{drive_info.get('temperature')}°C" if drive_info.get('temperature') else 'N/A'
description += f"""
┏━ SMART STATUS ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Status │ {smart_status_safe:<60}
┃ Temperature │ {temp_safe:<60}
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
"""
if drive_info.get('smart_attributes'): if drive_info.get('smart_attributes'):
description += "\n┏━ SMART ATTRIBUTES " + "" * 48 + "\n" description += "\n┏━ SMART ATTRIBUTES " + "" * 48 + "\n"
for attr, value in drive_info['smart_attributes'].items(): for attr, value in drive_info['smart_attributes'].items():
description += "{:<25}{:<37}\n".format( attr_safe = str(attr).replace('_', ' ') if attr else 'Unknown'
attr.replace('_', ' '), value value_safe = str(value) if value is not None else 'N/A'
) description += f"{attr_safe:<25}{value_safe:<37}\n"
description += "" + "" * 71 + "\n" description += "" + "" * 71 + "\n"
if drive_info.get('partitions'): if drive_info.get('partitions'):
@ -629,30 +630,29 @@ class SystemHealthMonitor:
blocks = int(usage_percent / 5) # 20 blocks total = 100% blocks = int(usage_percent / 5) # 20 blocks total = 100%
usage_meter = '' * blocks + '' * (20 - blocks) usage_meter = '' * blocks + '' * (20 - blocks)
description += """ mountpoint_safe = partition.get('mountpoint') or 'N/A'
┏━ PARTITION [{:<60}] ━┓ fstype_safe = partition.get('fstype') or 'N/A'
Filesystem │ {:<60} total_space_safe = partition.get('total_space') or 'N/A'
Usage Meter │ [{:<58}] ┃ used_space_safe = partition.get('used_space') or 'N/A'
Total Space │ {:<60} free_space_safe = partition.get('free_space') or 'N/A'
Used Space │ {:<60}
Free Space │ {:<60} description += f"""
┃ Usage │ {:<60} ┏━ PARTITION [{mountpoint_safe:<60}] ━┓
┃ Filesystem │ {fstype_safe:<60}
┃ Usage Meter │ [{usage_meter:<58}] ┃
┃ Total Space │ {total_space_safe:<60}
┃ Used Space │ {used_space_safe:<60}
┃ Free Space │ {free_space_safe:<60}
┃ Usage │ {usage_percent}%{'':<57}
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
""".format( """
partition.get('mountpoint', 'N/A'),
partition.get('fstype', 'N/A'),
usage_meter,
partition.get('total_space', 'N/A'),
partition.get('used_space', 'N/A'),
partition.get('free_space', 'N/A'),
f"{usage_percent}%"
)
firmware_info = self._check_disk_firmware(device) firmware_info = self._check_disk_firmware(device)
if firmware_info['is_problematic']: if firmware_info['is_problematic']:
description += "\n┏━ FIRMWARE ALERTS " + "" * 48 + "\n" description += "\n┏━ FIRMWARE ALERTS " + "" * 48 + "\n"
for issue in firmware_info['known_issues']: for issue_item in firmware_info['known_issues']:
description += "┃ ⚠ {:<67}\n".format(issue) issue_safe = str(issue_item) if issue_item else 'Unknown issue'
description += f"┃ ⚠ {issue_safe:<67}\n"
description += "" + "" * 71 + "\n" description += "" + "" * 71 + "\n"
except Exception as e: except Exception as e:
description += f"\nError generating drive details: {str(e)}\n" description += f"\nError generating drive details: {str(e)}\n"