Skip to content

Home Information - Your home, your data, organized.

Python

FIxed bugs and clean-up of System Info page (#216)

Tony Cfe058fbc17f3
diff --git a/src/hi/apps/alert/monitors.py b/src/hi/apps/alert/monitors.py
index ea98a69c..d1b7a65c 100644
--- a/src/hi/apps/alert/monitors.py
+++ b/src/hi/apps/alert/monitors.py
@@ -10,13 +10,14 @@ logger = logging.getLogger(__name__)
class AlertMonitor( PeriodicMonitor, AlertMixin ):
+ MONITOR_ID = 'hi.apps.alert.monitor'
ALERT_POLLING_INTERVAL_SECS = 3
TRACE = False # for debugging
def __init__( self ):
super().__init__(
- id = 'alert-monitor',
+ id = self.MONITOR_ID,
interval_secs = self.ALERT_POLLING_INTERVAL_SECS,
)
return
@@ -24,7 +25,7 @@ class AlertMonitor( PeriodicMonitor, AlertMixin ):
@classmethod
def get_provider_info(cls) -> ProviderInfo:
return ProviderInfo(
- provider_id = 'hi.apps.alert',
+ provider_id = cls.MONITOR_ID,
provider_name = 'Alert Monitor',
description = 'Alert processing and notification management',
expected_heartbeat_interval_secs = cls.ALERT_POLLING_INTERVAL_SECS,
diff --git a/src/hi/apps/monitor/monitor_manager.py b/src/hi/apps/monitor/monitor_manager.py
index 624ef419..0ebb4a07 100644
--- a/src/hi/apps/monitor/monitor_manager.py
+++ b/src/hi/apps/monitor/monitor_manager.py
@@ -60,6 +60,15 @@ class AppMonitorManager( Singleton ):
continue
return
+ def get_health_status_by_monitor_id( self,
+ monitor_id : str ) -> HealthStatusProvider:
+ with self._data_lock:
+ for monitor in self._monitor_map.values():
+ if monitor.id == monitor_id:
+ return monitor
+ continue
+ raise KeyError( f'Unknown monitor id: "{monitor_id}".' )
+
def get_health_status_providers(self) -> List[HealthStatusProvider]:
"""Get health status providers for all registered monitors.
Each provider exposes get_provider_info() and health_status.
diff --git a/src/hi/apps/monitor/periodic_monitor.py b/src/hi/apps/monitor/periodic_monitor.py
index 0d574aae..5842643f 100644
--- a/src/hi/apps/monitor/periodic_monitor.py
+++ b/src/hi/apps/monitor/periodic_monitor.py
@@ -1,7 +1,6 @@
import asyncio
import logging
-from hi.apps.system.enums import HealthStatusType
from hi.apps.system.health_status_provider import HealthStatusProvider
@@ -36,14 +35,14 @@ class PeriodicMonitor( HealthStatusProvider ):
try:
await self.initialize()
- self.update_health_status( HealthStatusType.HEALTHY, 'initialized successfully' )
+ self.record_healthy( 'Initialized successfully' )
self._logger.info(f"{self.__class__.__name__} initialized successfully,"
f" entering monitoring loop")
while self._is_running:
try:
await self.run_query()
- self.update_heartbeat()
+ self.record_heartbeat()
except Exception as e:
self._logger.exception(f"Query execution failed in {self.__class__.__name__}: {e}")
@@ -59,13 +58,12 @@ class PeriodicMonitor( HealthStatusProvider ):
except asyncio.CancelledError as ce:
self._logger.info(f"{self.__class__.__name__} async task cancelled: {ce}")
- self.update_health_status( HealthStatusType.ERROR, "Monitor was cancelled" )
+ self.record_error( "Monitor was cancelled" )
raise # Re-raise to properly handle cancellation
except Exception as e:
self._logger.exception( f"{self.__class__.__name__} async task"
f" failed unexpectedly: {e}")
- self.update_health_status( HealthStatusType.ERROR,
- f"Monitor failed to start: {str(e)}" )
+ self.record_error( f"Monitor failed to start: {str(e)}" )
raise
finally:
self._logger.info(f"{self.__class__.__name__} async task cleaning up")
diff --git a/src/hi/apps/notify/monitors.py b/src/hi/apps/notify/monitors.py
index ab60e929..b329de32 100644
--- a/src/hi/apps/notify/monitors.py
+++ b/src/hi/apps/notify/monitors.py
@@ -10,11 +10,12 @@ logger = logging.getLogger(__name__)
class NotificationMonitor( PeriodicMonitor, NotificationMixin ):
+ MONITOR_ID = 'hi.apps.notify.monitor'
NOTIFICATION_POLLING_INTERVAL_SECS = 10
def __init__( self ):
super().__init__(
- id = 'notification-monitor',
+ id = self.MONITOR_ID,
interval_secs = self.NOTIFICATION_POLLING_INTERVAL_SECS,
)
return
@@ -22,9 +23,9 @@ class NotificationMonitor( PeriodicMonitor, NotificationMixin ):
@classmethod
def get_provider_info(cls) -> ProviderInfo:
return ProviderInfo(
- provider_id = 'hi.apps.notify',
+ provider_id = cls.MONITOR_ID,
provider_name = 'Notifications Monitor',
- description = 'Notification processing and delivery management',
+ description = 'Notification processing and delivery',
expected_heartbeat_interval_secs = cls.NOTIFICATION_POLLING_INTERVAL_SECS,
)
diff --git a/src/hi/apps/security/monitors.py b/src/hi/apps/security/monitors.py
index d381d08c..ddf33e9a 100644
--- a/src/hi/apps/security/monitors.py
+++ b/src/hi/apps/security/monitors.py
@@ -15,11 +15,12 @@ logger = logging.getLogger(__name__)
class SecurityMonitor( PeriodicMonitor, SettingsMixin, SecurityMixin ):
+ MONITOR_ID = 'hi.apps.security.monitor'
SECURITY_POLLING_INTERVAL_SECS = 5
def __init__( self ):
super().__init__(
- id = 'security-monitor',
+ id = self.MONITOR_ID,
interval_secs = self.SECURITY_POLLING_INTERVAL_SECS,
)
self._last_security_state_check_datetime = datetimeproxy.now()
@@ -28,9 +29,9 @@ class SecurityMonitor( PeriodicMonitor, SettingsMixin, SecurityMixin ):
@classmethod
def get_provider_info(cls) -> ProviderInfo:
return ProviderInfo(
- provider_id = 'hi.apps.security',
+ provider_id = cls.MONITOR_ID,
provider_name = 'Security Monitor',
- description = 'Security state monitoring and automated day/night transitions',
+ description = 'Security state monitoring',
expected_heartbeat_interval_secs = cls.SECURITY_POLLING_INTERVAL_SECS,
)
diff --git a/src/hi/apps/security/tests/test_security_monitor.py b/src/hi/apps/security/tests/test_security_monitor.py
index 3730bc2b..11928919 100644
--- a/src/hi/apps/security/tests/test_security_monitor.py
+++ b/src/hi/apps/security/tests/test_security_monitor.py
@@ -25,7 +25,7 @@ class TestSecurityMonitor(AsyncTaskTestCase):
monitor = SecurityMonitor()
# Should be properly configured as periodic monitor
- self.assertEqual(monitor.id, 'security-monitor')
+ self.assertEqual(monitor.id, monitor.MONITOR_ID)
self.assertEqual(monitor._query_interval_secs, SecurityMonitor.SECURITY_POLLING_INTERVAL_SECS)
self.assertIsNotNone(monitor._last_security_state_check_datetime)
@@ -74,25 +74,6 @@ class TestSecurityMonitor(AsyncTaskTestCase):
# Should exit gracefully without error
# (No exceptions should be raised)
- async def test_check_security_state_auto_change_not_allowed(self):
- """Test security state check blocked by auto_change_allowed - respects state restrictions."""
- monitor = SecurityMonitor()
-
- mock_settings_manager = Mock()
- mock_security_manager = Mock()
- mock_security_manager.security_state = SecurityState.DISABLED # auto_change_allowed = False
-
- with patch.object(monitor, 'settings_manager_async', new_callable=AsyncMock) as mock_settings:
- mock_settings.return_value = mock_settings_manager
-
- with patch.object(monitor, 'security_manager_async', new_callable=AsyncMock) as mock_security:
- mock_security.return_value = mock_security_manager
-
- await monitor._check_security_state()
-
- # Should not attempt state changes when auto change not allowed
- mock_security_manager.update_security_state_auto.assert_not_called()
-
@patch('hi.apps.security.monitors.datetimeproxy')
@patch('hi.apps.security.monitors.ConsoleSettingsHelper')
async def test_check_security_state_day_start_transition(self, mock_console_helper, mock_datetimeproxy):
diff --git a/src/hi/apps/system/aggregate_health_provider.py b/src/hi/apps/system/aggregate_health_provider.py
index 009a9679..df6824b9 100644
--- a/src/hi/apps/system/aggregate_health_provider.py
+++ b/src/hi/apps/system/aggregate_health_provider.py
@@ -1,71 +1,89 @@
-from abc import ABC, abstractmethod
import copy
import logging
-import threading
-from typing import Optional, Sequence
+from typing import Sequence
import hi.apps.common.datetimeproxy as datetimeproxy
from .aggregate_health_status import AggregateHealthStatus
from .api_health_status_provider import ApiHealthStatusProvider
+from .health_status import HealthStatus
+from .health_status_provider import HealthStatusProvider
from .enums import HealthStatusType, HealthAggregationRule
-from .provider_info import ProviderInfo
logger = logging.getLogger(__name__)
-class AggregateHealthProvider(ABC):
+class AggregateHealthProvider( HealthStatusProvider ):
"""
Provider for components that aggregate API health from one or more sources.
Provides health status tracking with API health aggregation capability.
This provider can be used alongside ApiHealthStatusProvider for components that both
make their own API calls and need to track/aggregate API health.
-
- This *should not* be used alongside HealthStatusProvider.
"""
- def _ensure_api_aggregator_setup(self):
- if hasattr(self, '_aggregated_health_status'):
- return
-
- self._health_lock = threading.Lock()
- self._api_health_status_providers = [] # Track API health status providers
-
+ @property
+ def initial_health_status(self) -> HealthStatus:
provider_info = self.get_provider_info()
- self._aggregated_health_status = AggregateHealthStatus(
+ return AggregateHealthStatus(
provider_id = provider_info.provider_id,
provider_name = provider_info.provider_name,
status = HealthStatusType.UNKNOWN,
- last_check = datetimeproxy.now(),
+ last_update = datetimeproxy.now(),
+ last_message = 'Initialization',
aggregation_rule = self._get_aggregation_rule()
)
-
- def _get_aggregation_rule(self) -> HealthAggregationRule:
- """Override to provide custom aggregation rule."""
- return HealthAggregationRule.ALL_SOURCES_HEALTHY
-
- @classmethod
- @abstractmethod
- def get_provider_info(cls) -> ProviderInfo:
- """Get the API service info for this class. Must be implemented by subclasses."""
- pass
+
+ def _ensure_health_status_provider_setup(self):
+ if hasattr( self, '_health_status' ):
+ return
+ super()._ensure_health_status_provider_setup()
+ self._api_health_status_providers = [] # Track API health status providers
+ return
@property
def health_status(self) -> AggregateHealthStatus:
"""Get aggregated health status (thread-safe, always fresh)."""
- self._ensure_api_aggregator_setup()
+ self._ensure_health_status_provider_setup()
with self._health_lock:
# Always refresh from API owners before returning
self._refresh_aggregated_health()
- return copy.deepcopy(self._aggregated_health_status)
+ return copy.deepcopy(self._health_status)
return
+
+ def refresh_aggregated_health(self) -> None:
+ self._ensure_health_status_provider_setup()
+ with self._health_lock:
+ self._refresh_aggregated_health()
+
+ def _refresh_aggregated_health(self) -> None:
+ """Refresh API status map from all tracked API health status providers.
+
+ Note: The aggregated health status is computed dynamically via the status property,
+ so this method only needs to update the API status map.
+ """
+ # Clear current sources
+ self._health_status.api_status_map.clear()
+
+ # Collect current health from all providers
+ for provider in self._api_health_status_providers:
+ provider_info = provider.get_api_provider_info()
+ api_health_status = provider.api_health_status
+ self._health_status.api_status_map[provider_info] = api_health_status
+
+ # Status is now computed dynamically via the status property in AggregateHealthStatus
+ # No need to manually update it here
+ return
+
+ def _get_aggregation_rule(self) -> HealthAggregationRule:
+ """Override to provide custom aggregation rule."""
+ return HealthAggregationRule.ALL_SOURCES_HEALTHY
def add_api_health_status_provider(
self,
api_health_status_provider : ApiHealthStatusProvider ) -> None:
"""Add an API health status provider to be tracked and aggregated."""
- self._ensure_api_aggregator_setup()
+ self._ensure_health_status_provider_setup()
with self._health_lock:
if api_health_status_provider not in self._api_health_status_providers:
self._api_health_status_providers.append( api_health_status_provider )
@@ -77,7 +95,7 @@ class AggregateHealthProvider(ABC):
api_health_status_provider_sequence : Sequence[ ApiHealthStatusProvider ]
) -> None:
"""Add API health status providers to be tracked and aggregated."""
- self._ensure_api_aggregator_setup()
+ self._ensure_health_status_provider_setup()
with self._health_lock:
for api_health_status_provider in api_health_status_provider_sequence:
if api_health_status_provider not in self._api_health_status_providers:
@@ -91,64 +109,9 @@ class AggregateHealthProvider(ABC):
api_health_status_provider : ApiHealthStatusProvider
) -> None:
"""Remove an API health status provider from tracking."""
- self._ensure_api_aggregator_setup()
+ self._ensure_health_status_provider_setup()
with self._health_lock:
if api_health_status_provider in self._api_health_status_providers:
self._api_health_status_providers.remove(api_health_status_provider)
self._refresh_aggregated_health()
return
-
- def _refresh_aggregated_health(self) -> None:
- """Refresh API status map from all tracked API health status providers.
-
- Note: The aggregated health status is computed dynamically via the status property,
- so this method only needs to update the API status map.
- """
- # Clear current sources
- self._aggregated_health_status.api_status_map.clear()
-
- # Collect current health from all providers
- for provider in self._api_health_status_providers:
- service_info = provider.get_api_provider_info()
- api_health = provider.api_health_status
- self._aggregated_health_status.api_status_map[service_info] = api_health
-
- # Status is now computed dynamically via the status property in AggregateHealthStatus
- # No need to manually update it here
- return
-
- def update_health_status( self,
- status : HealthStatusType,
- error_message : Optional[str] = None) -> None:
- """Update the base health status."""
- self._ensure_api_aggregator_setup()
- with self._health_lock:
- self._aggregated_health_status.status = status
- self._aggregated_health_status.last_check = datetimeproxy.now()
- self._aggregated_health_status.error_message = error_message
-
- if status.is_error:
- self._aggregated_health_status.error_count += 1
- else:
- # Reset error count on successful status
- self._aggregated_health_status.error_count = 0
-
- logger.debug( f'Health status updated to {status.label}:'
- f' {error_message or "No error"}')
-
- def record_warning(self, error_message: str) -> None:
- self.update_health_status(HealthStatusType.WARNING, error_message)
-
- def record_error(self, error_message: str) -> None:
- self.update_health_status(HealthStatusType.ERROR, error_message)
-
- def update_heartbeat(self) -> None:
- self._ensure_api_aggregator_setup()
- with self._health_lock:
- self._aggregated_health_status.heartbeat = datetimeproxy.now()
- logger.debug("Health heartbeat updated")
-
- def refresh_aggregated_health(self) -> None:
- self._ensure_api_aggregator_setup()
- with self._health_lock:
- self._refresh_aggregated_health()
diff --git a/src/hi/apps/system/aggregate_health_status.py b/src/hi/apps/system/aggregate_health_status.py
index ebdcf17c..2046f0b3 100644
--- a/src/hi/apps/system/aggregate_health_status.py
+++ b/src/hi/apps/system/aggregate_health_status.py
@@ -30,7 +30,7 @@ class AggregateHealthStatus( HealthStatus ):
# Return worst of base status and aggregated API status using priority
# Lower priority number = worse health
- return min([self._base_status, api_health], key=lambda s: s.priority)
+ return min([ self._base_status, api_health ], key=lambda s: s.priority )
@status.setter
def status(self, value: HealthStatusType) -> None:
diff --git a/src/hi/apps/system/api_health_status.py b/src/hi/apps/system/api_health_status.py
index d2c2b9e0..7aab0ddd 100644
--- a/src/hi/apps/system/api_health_status.py
+++ b/src/hi/apps/system/api_health_status.py
@@ -32,23 +32,15 @@ class ApiHealthStatus:
last_response_time : Optional[float] = None
cache_hits : int = 0
cache_misses : int = 0
-
+ last_error_message : str = None
+
def record_api_call( self, api_call_context : ApiCallContext ):
self.total_calls += 1
if api_call_context.status.is_success:
self.last_success = datetimeproxy.now()
- self.consecutive_failures = 0
- self.status = ApiHealthStatusType.HEALTHY
+ self.record_healthy()
else:
- self.total_failures += 1
- self.consecutive_failures += 1
-
- self.status = ApiHealthStatusType.from_metrics(
- consecutive_failures = self.consecutive_failures,
- total_failures = self.total_failures,
- total_requests = self.total_calls,
- avg_response_time = self.average_response_time
- )
+ self.record_error( 'API call failure' )
if api_call_context.duration is not None:
self.last_response_time = api_call_context.duration
@@ -60,6 +52,23 @@ class ApiHealthStatus:
+ ( api_call_context.duration * 0.2 ))
return
+ def record_healthy( self ):
+ self.status = ApiHealthStatusType.HEALTHY
+ self.consecutive_failures = 0
+ return
+
+ def record_error( self, message : str = None ):
+ self.total_failures += 1
+ self.consecutive_failures += 1
+ self.last_error_message = message
+ self.status = ApiHealthStatusType.from_metrics(
+ consecutive_failures = self.consecutive_failures,
+ total_failures = self.total_failures,
+ total_requests = self.total_calls,
+ avg_response_time = self.average_response_time
+ )
+ return
+
def record_cache_hit(self) -> int:
self.cache_hits += 1
self.status = ApiHealthStatusType.HEALTHY
diff --git a/src/hi/apps/system/api_health_status_provider.py b/src/hi/apps/system/api_health_status_provider.py
index 02c10f1f..95a69326 100644
--- a/src/hi/apps/system/api_health_status_provider.py
+++ b/src/hi/apps/system/api_health_status_provider.py
@@ -44,8 +44,7 @@ class ApiHealthStatusProvider(ABC):
def api_health_status(self) -> ApiHealthStatus:
self._ensure_api_health_status_provider_setup()
with self._api_health_lock:
- api_health_status = copy.deepcopy( self._api_health_status )
- return api_health_status
+ return copy.deepcopy( self._api_health_status )
@contextmanager
def api_call_context( self, operation_name : str ):
@@ -96,20 +95,25 @@ class ApiHealthStatusProvider(ABC):
self._api_health_status.record_cache_miss()
return
- def update_api_health_status( self, status_type : ApiHealthStatusType ) -> None:
+ def record_healthy( self ) -> None:
self._ensure_api_health_status_provider_setup()
with self._api_health_lock:
- self._api_health_status.status = status_type
+ self._api_health_status.record_healthy()
return
-
- def set_healthy( self ) -> None:
- self.update_api_health_status( status_type = ApiHealthStatusType.HEALTHY )
+
+ def record_error( self, message: str ) -> None:
+ self._ensure_api_health_status_provider_setup()
+ with self._api_health_lock:
+ self._api_health_status.record_error( message = message )
return
- def set_disabled( self ) -> None:
+ def record_disabled( self ) -> None:
self.update_api_health_status( status_type = ApiHealthStatusType.DISABLED )
return
-
-
-
+ def update_api_health_status( self, status_type : ApiHealthStatusType ) -> None:
+ self._ensure_api_health_status_provider_setup()
+ with self._api_health_lock:
+ self._api_health_status.status = status_type
+ return
+
diff --git a/src/hi/apps/system/asyncio_health_provider.py b/src/hi/apps/system/asyncio_health_provider.py
index 9e130775..d54de96c 100644
--- a/src/hi/apps/system/asyncio_health_provider.py
+++ b/src/hi/apps/system/asyncio_health_provider.py
@@ -38,16 +38,16 @@ class AsyncioHealthStatusProvider( HealthStatusProvider ):
async_diagnostics = BackgroundTaskMonitor.get_background_task_status()
status_type = HealthStatusType.HEALTHY
- error_message = None
+ message = None
if not async_diagnostics:
status_type = HealthStatusType.UNKNOWN
- error_message = "Unable to retrieve background task status"
+ message = "Unable to retrieve background task status"
else:
main_thread = async_diagnostics.get('main_thread', {})
if not main_thread.get('is_alive'):
status_type = HealthStatusType.ERROR
- error_message = "Main thread is not alive"
+ message = "Main thread is not alive"
if status_type == HealthStatusType.HEALTHY:
background_tasks = async_diagnostics.get('background_tasks', {})
@@ -65,19 +65,19 @@ class AsyncioHealthStatusProvider( HealthStatusProvider ):
if dead_tasks:
status_type = HealthStatusType.ERROR
- error_message = f"Dead threads: {', '.join(dead_tasks)}"
+ message = f"Dead threads: {', '.join(dead_tasks)}"
elif stopped_loops:
status_type = HealthStatusType.WARNING
- error_message = f"Stopped event loops: {', '.join(stopped_loops)}"
+ message = f"Stopped event loops: {', '.join(stopped_loops)}"
self.update_health_status(
status = status_type,
- error_message = error_message,
+ last_message = message,
)
except Exception as e:
logger.exception("Failed to get background task health status")
self.update_health_status(
status = HealthStatusType.ERROR,
- error_message = f"Failed to retrieve status: {str(e)}",
+ last_message = f"Failed to retrieve status: {str(e)}",
)
return
diff --git a/src/hi/apps/system/health_status.py b/src/hi/apps/system/health_status.py
index 4bb917a7..43ce9915 100644
--- a/src/hi/apps/system/health_status.py
+++ b/src/hi/apps/system/health_status.py
@@ -14,9 +14,9 @@ class HealthStatus:
provider_id : str # Technical identifier
status : HealthStatusType # When did we last check
- last_check : datetime # When did provider last report
+ last_update : datetime # When did provider last report
heartbeat : Optional[datetime] = None
- error_message : Optional[str] = None
+ last_message : Optional[str] = None
error_count : int = 0
expected_heartbeat_interval_secs : Optional[int] = None # Expected polling interval for dynamic heartbeat thresholds
@@ -168,10 +168,13 @@ class HealthStatus:
@property
def status_summary_message(self) -> str:
if self.is_healthy:
- return "Is operating normally and heartbeat is active."
+ if self.heartbeat:
+ return "Is operating normally and heartbeat is active."
+ else:
+ return "Is operating normally."
elif self.is_critical:
- return "Requires immediate attention. Please check the configuration settings and ensure external services are accessible."
+ return "Requires immediate attention."
else:
- return "Has encountered temporary issues. The problem may resolve automatically, but monitoring is recommended."
+ return "Has encountered temporary issues. The problem may resolve automatically."
diff --git a/src/hi/apps/system/health_status_provider.py b/src/hi/apps/system/health_status_provider.py
index 6e8ca579..9b2e7ce1 100644
--- a/src/hi/apps/system/health_status_provider.py
+++ b/src/hi/apps/system/health_status_provider.py
@@ -21,17 +21,22 @@ class HealthStatusProvider(ABC):
self._ensure_health_status_provider_setup()
return
- def _ensure_health_status_provider_setup(self):
- if hasattr( self, '_health_status' ):
- return
+ @property
+ def initial_health_status(self) -> HealthStatus:
provider_info = self.get_provider_info()
- self._health_status = HealthStatus(
+ return HealthStatus(
provider_id = provider_info.provider_id,
provider_name = provider_info.provider_name,
status = HealthStatusType.UNKNOWN,
- last_check = datetimeproxy.now(),
+ last_update = datetimeproxy.now(),
+ last_message = 'Initialization',
expected_heartbeat_interval_secs = provider_info.expected_heartbeat_interval_secs,
)
+
+ def _ensure_health_status_provider_setup(self):
+ if hasattr( self, '_health_status' ):
+ return
+ self._health_status = self.initial_health_status
self._health_lock = threading.Lock()
return
@@ -48,22 +53,37 @@ class HealthStatusProvider(ABC):
health_status = copy.deepcopy( self._health_status )
return health_status
- def record_warning( self, error_message: str ) -> None:
- self.update_health_status( HealthStatusType.WARNING, error_message )
+ def record_healthy( self, message: str ) -> None:
+ self.update_health_status( HealthStatusType.HEALTHY, message )
+ return
+
+ def record_warning( self, message: str ) -> None:
+ self.update_health_status( HealthStatusType.WARNING, message )
return
- def record_error( self, error_message: str ) -> None:
- self.update_health_status( HealthStatusType.ERROR, error_message )
+ def record_error( self, message: str ) -> None:
+ self.update_health_status( HealthStatusType.ERROR, message )
+ return
+
+ def record_disabled( self, message: str ) -> None:
+ self.update_health_status( HealthStatusType.DISABLED, message )
+ return
+
+ def record_heartbeat(self) -> None:
+ self._ensure_health_status_provider_setup()
+ with self._health_lock:
+ self._health_status.heartbeat = datetimeproxy.now()
+ logger.debug("Health heartbeat updated")
return
def update_health_status( self,
status : HealthStatusType,
- error_message : Optional[str] = None) -> None:
+ last_message : Optional[str] = None) -> None:
self._ensure_health_status_provider_setup()
with self._health_lock:
self._health_status.status = status
- self._health_status.last_check = datetimeproxy.now()
- self._health_status.error_message = error_message
+ self._health_status.last_update = datetimeproxy.now()
+ self._health_status.last_message = last_message
if status.is_error:
self._health_status.error_count += 1
@@ -72,14 +92,7 @@ class HealthStatusProvider(ABC):
self._health_status.error_count = 0
logger.debug( f'Health status updated to {status.label}:'
- f' {error_message or "No error"}')
- return
-
- def update_heartbeat(self) -> None:
- self._ensure_health_status_provider_setup()
- with self._health_lock:
- self._health_status.heartbeat = datetimeproxy.now()
- logger.debug("Health heartbeat updated")
+ f' {last_message or "No error"}')
return
diff --git a/src/hi/apps/system/templates/system/modals/health_status.html b/src/hi/apps/system/templates/system/modals/health_status.html
index c3244893..ef995c78 100644
--- a/src/hi/apps/system/templates/system/modals/health_status.html
+++ b/src/hi/apps/system/templates/system/modals/health_status.html
@@ -5,7 +5,7 @@
{% block title_text %}
{% icon "shield" size="sm" css_class="mr-2" %}
-{{ health_status.provider_name }} Health
+{{ health_status_provider.health_status.provider_name }} Health
{% endblock %}
{% block body %}
diff --git a/src/hi/apps/system/templates/system/panes/api_health_status_details.html b/src/hi/apps/system/templates/system/panes/api_health_status_details.html
index 6acf8921..9d0428ce 100644
--- a/src/hi/apps/system/templates/system/panes/api_health_status_details.html
+++ b/src/hi/apps/system/templates/system/panes/api_health_status_details.html
@@ -1,26 +1,34 @@
{% load icons %}
{% load tz %}
+{% load humanize %}
{% timezone USER_TIMEZONE %}
-<div class="api-source-section {{ api_health_status.border_color_class }} mb-4">
+<div class="{{ api_health_status.border_color_class }} mb-4">
<div class="d-flex justify-content-between align-items-center mb-2">
- <h6 class="mb-0">{{ api_health_status.provider_name }}</h6>
+ <h5 class="mb-0">{{ api_health_status.provider_name }}</h5>
<span class="badge {{ api_health_status.status_badge_class }}">
{% icon api_health_status.status_icon size="sm" css_class="mr-1" %}{{ api_health_status.status.label }}
</span>
</div>
+ {% if api_health_status.last_error_message %}
+ <div class="row">
+ <div class="col-sm-3"><strong>Last Error:</strong></div>
+ <div class="zzz col-sm-9">{{ api_health_status.last_error_message }}</div>
+ </div>
+ {% endif %}
+
{% if api_health_status.last_success %}
<div class="row">
<div class="col-sm-3"><strong>Last Success:</strong></div>
- <div class="col-sm-9">{{ api_health_status.last_success|localtime|date:"H:i:s" }}</div>
+ <div class="col-sm-9">{{ api_health_status.last_success|localtime|date:"M j, Y H:i:s" }}</div>
</div>
{% endif %}
<div class="api-metrics-grid mt-2">
<div class="metric-item {{ api_health_status.status_css_class }}">
<div class="small text-muted-custom">Total Calls</div>
- <div class="metric-value">{{ api_health_status.total_calls|floatformat:0 }}</div>
+ <div class="metric-value">{{ api_health_status.total_calls|intcomma }}</div>
</div>
<div class="metric-item {{ api_health_status.status_css_class }}">
<div class="small text-muted-custom">Success Rate</div>
@@ -54,7 +62,7 @@
{% if api_health_status.consecutive_failures > 0 %}Consecutive Failures{% else %}Total Failures{% endif %}
</div>
<div class="metric-value">
- {% if api_health_status.consecutive_failures > 0 %}{{ api_health_status.consecutive_failures }}{% else %}{{ api_health_status.total_failures }}{% endif %}
+ {% if api_health_status.consecutive_failures > 0 %}{{ api_health_status.consecutive_failures|intcomma }}{% else %}{{ api_health_status.total_failures|intcomma }}{% endif %}
</div>
</div>
{% endif %}
@@ -64,7 +72,7 @@
{% if api_health_status.cache_hits > 0 or api_health_status.cache_misses > 0 %}
<div class="cache-stats mt-2">
<small class="text-muted">
- Cache: {{ api_health_status.cache_hits }} hits, {{ api_health_status.cache_misses }} misses
+ Cache: {{ api_health_status.cache_hits|intcomma }} hits, {{ api_health_status.cache_misses|intcomma }} misses
{% if api_health_status.cache_hit_rate_percent %}
({{ api_health_status.cache_hit_rate_percent|floatformat:1 }}% hit rate)
{% endif %}
diff --git a/src/hi/apps/system/templates/system/panes/health_status_pane.html b/src/hi/apps/system/templates/system/panes/health_status_brief.html
similarity index 81%
rename from src/hi/apps/system/templates/system/panes/health_status_pane.html
rename to src/hi/apps/system/templates/system/panes/health_status_brief.html
index 9582ff23..1ed7cc63 100644
--- a/src/hi/apps/system/templates/system/panes/health_status_pane.html
+++ b/src/hi/apps/system/templates/system/panes/health_status_brief.html
@@ -12,11 +12,7 @@
<div class="d-flex justify-content-between align-items-center mb-3">
<div>
<h6 class="mb-0">{{ health_status.provider_name }}</h6>
-
- {% if health_status.api_status_map %}
- <small class="text-muted">{{ health_status.api_status_map|length }} API source{{ health_status.api_status_map|length|pluralize }} configured</small>
- {% endif %}
- </div>
+ </div>
<div>
<span class="badge monitor-status-badge {{ health_status.status_badge_class }}">
{% icon health_status.status_icon size="md" css_class="mr-1" %}{{ health_status.status_display }}
diff --git a/src/hi/apps/system/templates/system/panes/health_status_card.html b/src/hi/apps/system/templates/system/panes/health_status_card.html
index 393d614c..635a4e4c 100644
--- a/src/hi/apps/system/templates/system/panes/health_status_card.html
+++ b/src/hi/apps/system/templates/system/panes/health_status_card.html
@@ -21,19 +21,18 @@
<div class="row">
<div class="col-sm-6">
- <small class="text-muted">Last Check:</small><br>
- <span>{{ health_status.last_check|localtime|date:"H:i:s" }}</span>
+ {% if health_status.heartbeat %}
+ <small class="text-muted">Last Seen:</small><br>
+ <span>{{ health_status.heartbeat|localtime|date:"H:i:s" }}</span>
+ {% else %}
+ <small class="text-muted">Last Update:</small><br>
+ <span>{{ health_status.last_update|localtime|date:"H:i:s" }}</span>
+ {% endif %}
</div>
<div class="col-sm-6">
<small class="text-muted">Status:</small><br>
<span class="heartbeat-indicator {{ health_status.heartbeat_css_class }}"></span>
<span class="{{ health_status.heartbeat_text_class }}">{{ health_status.heartbeat_status_text }}</span><br>
- <small class="text-muted-custom">
- (last seen {{ health_status.heartbeat|localtime|date:"H:i:s" }})
- {% if health_status.expected_heartbeat_interval_secs %}
- <br>Expected: {{ health_status.expected_heartbeat_interval_secs }}s
- {% endif %}
- </small>
</div>
</div>
diff --git a/src/hi/apps/system/templates/system/panes/health_status_details.html b/src/hi/apps/system/templates/system/panes/health_status_details.html
index 50e37bef..99984787 100644
--- a/src/hi/apps/system/templates/system/panes/health_status_details.html
+++ b/src/hi/apps/system/templates/system/panes/health_status_details.html
@@ -2,6 +2,7 @@
{% load tz %}
{% timezone USER_TIMEZONE %}
+{% with health_status_provider.health_status as health_status %}
<div class="container-fluid px-0">
<!-- Overall Provider Status -->
<div class="row mb-4">
@@ -20,27 +21,32 @@
<div class="col-sm-9">
<span class="heartbeat-indicator {{ health_status.heartbeat_css_class }}"></span>
<span class="{{ health_status.heartbeat_text_class }}">{{ health_status.heartbeat_status_text }}</span>
- <small class="text-muted-custom">
- (last seen {{ health_status.heartbeat|localtime|date:"H:i:s" }})
- </small>
+ </div>
+ </div>
+ <div class="row mt-2">
+ <div class="col-sm-3"><strong>Last Seen:</strong></div>
+ <div class="col-sm-9">
+ {{ health_status.heartbeat|localtime|date:"H:i:s" }}
{% if health_status.expected_heartbeat_interval_secs %}
- <br>Expected: {{ health_status.expected_heartbeat_interval_secs }}s
+ <span class="text-muted">
+ (Expected every {{ health_status.expected_heartbeat_interval_secs }}s)
+ </span>
{% endif %}
</div>
</div>
{% endif %}
<div class="row mt-2">
- <div class="col-sm-3"><strong>Last Check:</strong></div>
- <div class="col-sm-9">{{ health_status.last_check|localtime|date:"M j, Y H:i:s" }}</div>
+ <div class="col-sm-3"><strong>Last Update:</strong></div>
+ <div class="col-sm-9">{{ health_status.last_update|localtime|date:"M j, Y H:i:s" }}</div>
</div>
- {% if health_status.error_message %}
+ {% if health_status.last_message %}
<div class="row mt-2">
- <div class="col-sm-3"><strong>Error Details:</strong></div>
+ <div class="col-sm-3"><strong>Last Message:</strong></div>
<div class="col-sm-9">
<div class="{{ health_status.status_alert_class }} p-2">
- {{ health_status.error_message }}
+ {{ health_status.last_message }}
</div>
</div>
</div>
@@ -77,11 +83,21 @@
</h6>
{% for api_health_status in health_status.api_status_map.values %}
+ <hr />
{% include "system/panes/api_health_status_details.html" with api_health_status=api_health_status %}
{% endfor %}
</div>
</div>
{% endif %}
+ {% if health_status_provider.get_provider_info.description %}
+ <div class="row">
+ <div class="col-12">
+ <small class="text-muted">{{ health_status_provider.get_provider_info.description }}</small>
+ </div>
+ </div>
+ {% endif %}
+
</div>
+{% endwith %}
{% endtimezone %}
diff --git a/src/hi/apps/system/templates/system/panes/system_info.html b/src/hi/apps/system/templates/system/panes/system_info.html
index 83afbe6f..ed6b26f7 100644
--- a/src/hi/apps/system/templates/system/panes/system_info.html
+++ b/src/hi/apps/system/templates/system/panes/system_info.html
@@ -8,7 +8,7 @@
<!-- System Health Page Header -->
<div class="d-flex justify-content-between align-items-center my-3">
<h3>System Health</h3>
- <a hrf="{% url 'system_info' %}"
+ <a href="{% url 'system_info' %}"
class="btn btn-outline-primary btn-sm" data-async="true">
{% icon "sync" size="sm" css_class="mr-1" %}Refresh Status
</a>
@@ -53,7 +53,7 @@
<div class="row">
<div class="col-12">
- {% include "system/panes/health_status_pane.html" with health_status_provider=weather_provider %}
+ {% include "system/panes/health_status_brief.html" with health_status_provider=weather_provider %}
</div>
</div>
@@ -66,7 +66,7 @@
<div class="row">
<div class="col-12">
- {% include "system/panes/health_status_pane.html" with health_status_provider=background_task_provider %}
+ {% include "system/panes/health_status_brief.html" with health_status_provider=background_task_provider %}
</div>
</div>
</div>
diff --git a/src/hi/apps/system/tests/synthetic_data.py b/src/hi/apps/system/tests/synthetic_data.py
index c5ff13e1..5b2b5383 100644
--- a/src/hi/apps/system/tests/synthetic_data.py
+++ b/src/hi/apps/system/tests/synthetic_data.py
@@ -58,14 +58,15 @@ class SystemSyntheticData:
'provider_id': 'provider-0',
'provider_name': 'Provider 0',
'status': status_enum,
- 'last_check': now,
+ 'last_update': now,
+ 'last_message': 'Test',
}
# Customize data based on status type
if status_type == 'unknown':
base_data.update({
'heartbeat': None,
- 'error_message': "No health data available - service not yet initialized",
+ 'last_message': "No health data available - service not yet initialized",
})
elif status_type == 'healthy':
base_data.update({
@@ -74,19 +75,19 @@ class SystemSyntheticData:
elif status_type == 'warning':
base_data.update({
'heartbeat': now - timedelta(minutes=2),
- 'error_message': "Temporary issue: Service experiencing intermittent connectivity issues",
+ 'last_message': "Temporary issue: Service experiencing intermittent connectivity issues",
'error_count': 3,
})
elif status_type == 'error':
base_data.update({
'heartbeat': now - timedelta(hours=1),
- 'error_message': "Critical error: Service has stopped responding - manual intervention required",
+ 'last_message': "Critical error: Service has stopped responding - manual intervention required",
'error_count': 8,
})
elif status_type == 'disabled':
base_data.update({
'heartbeat': now,
- 'error_message': "Service has been manually disabled for scheduled maintenance",
+ 'last_message': "Service has been manually disabled for scheduled maintenance",
})
if with_api_data:
@@ -115,6 +116,13 @@ class SystemSyntheticData:
}
return labels.get(status_type, 'Health Status')
+ @classmethod
+ def create_provider_info(cls):
+ return ProviderInfo(
+ provider_name = "Test Provider Name",
+ provider_id = "test_provider_id",
+ )
+
@classmethod
def _create_sample_api_status_map(cls, status_type: str, now: datetime) -> Dict[ProviderInfo, ApiHealthStatus]:
"""Create sample API sources based on status type."""
diff --git a/src/hi/apps/system/tests/test_aggregate_health_status.py b/src/hi/apps/system/tests/test_aggregate_health_status.py
index 95a7acc4..52dfc1c0 100644
--- a/src/hi/apps/system/tests/test_aggregate_health_status.py
+++ b/src/hi/apps/system/tests/test_aggregate_health_status.py
@@ -23,7 +23,8 @@ class TestAggregateHealthStatus(unittest.TestCase):
provider_id="test_provider",
provider_name="Test Provider",
status=HealthStatusType.HEALTHY,
- last_check=MagicMock(),
+ last_update=MagicMock(),
+ last_message='Setup',
aggregation_rule=HealthAggregationRule.ALL_SOURCES_HEALTHY
)
@@ -253,4 +254,4 @@ class TestAggregateHealthStatus(unittest.TestCase):
if __name__ == '__main__':
- unittest.main()
\ No newline at end of file
+ unittest.main()
diff --git a/src/hi/apps/system/tests/ui/views.py b/src/hi/apps/system/tests/ui/views.py
index 1dbd33e9..c2bc4a48 100644
--- a/src/hi/apps/system/tests/ui/views.py
+++ b/src/hi/apps/system/tests/ui/views.py
@@ -3,6 +3,8 @@ from django.shortcuts import render
from django.views.generic import View
from hi.apps.system.enums import HealthStatusType
+from hi.apps.system.health_status_provider import HealthStatusProvider
+from hi.apps.system.provider_info import ProviderInfo
from hi.apps.system.tests.synthetic_data import SystemSyntheticData
@@ -49,8 +51,17 @@ class SystemTestUiHealthStatusView(View):
status_type,
with_api_data = with_api_data,
)
+
+ class TestProvider( HealthStatusProvider ):
+ @classmethod
+ def get_provider_info(cls) -> ProviderInfo:
+ return SystemSyntheticData.create_provider_info()
+
+ health_status_provider = TestProvider()
+ health_status_provider._health_status = health_status
+
# Render template with scenario data (matching existing modal template expectations)
context = {
- 'health_status': health_status,
+ 'health_status_provider': health_status_provider,
}
return render(request, 'system/modals/health_status.html', context)
diff --git a/src/hi/apps/system/views.py b/src/hi/apps/system/views.py
index deffe319..59643810 100644
--- a/src/hi/apps/system/views.py
+++ b/src/hi/apps/system/views.py
@@ -76,40 +76,28 @@ class SystemHealthStatusView(HiModalView):
raise Http404( f'Unrecognized provider id "{provider_id}"')
def get_integration_status_response( self, request, provider_id : str ):
- integration_manager = IntegrationManager()
- providers = integration_manager.get_health_status_providers()
-
- # Find the monitor with matching provider_id
- target_integration = None
- for provider in providers:
- if provider.get_provider_info().provider_id == provider_id:
- target_integration = provider
- break
-
- if not target_integration:
- raise Http404(f"Integration with provider_id '{provider_id}' not found")
+ try:
+ health_status_provider = IntegrationManager().get_health_status_by_monitor_id(
+ monitor_id = provider_id,
+ )
+ except KeyError as e:
+ raise Http404( str(e) )
context = {
- 'health_status': target_integration.health_status,
+ 'health_status_provider': health_status_provider,
}
return self.modal_response(request, context)
def get_app_monitor_status_response( self, request, provider_id : str ):
- app_monitor_manager = AppMonitorManager()
- monitors = app_monitor_manager.get_health_status_providers()
-
- # Find the monitor with matching provider_id
- target_monitor = None
- for monitor in monitors:
- if monitor.get_provider_info().provider_id == provider_id:
- target_monitor = monitor
- break
-
- if not target_monitor:
- raise Http404(f"Monitor with provider_id '{provider_id}' not found")
+ try:
+ health_status_provider = AppMonitorManager().get_health_status_by_monitor_id(
+ monitor_id = provider_id,
+ )
+ except KeyError as e:
+ raise Http404( str(e) )
context = {
- 'health_status': target_monitor.health_status,
+ 'health_status_provider': health_status_provider,
}
return self.modal_response(request, context)
@@ -144,7 +132,7 @@ class WeatherHealthStatusDetailsView(HiModalView):
def get(self, request, *args, **kwargs):
context = {
- 'health_status': WeatherSourceManager().health_status,
+ 'health_status_provider': WeatherSourceManager(),
}
return self.modal_response(request, context)
diff --git a/src/hi/apps/weather/monitors.py b/src/hi/apps/weather/monitors.py
index c9db2197..456638e4 100644
--- a/src/hi/apps/weather/monitors.py
+++ b/src/hi/apps/weather/monitors.py
@@ -17,12 +17,13 @@ logger = logging.getLogger(__name__)
class WeatherMonitor( PeriodicMonitor, AlertMixin, SettingsMixin ):
+ MONITOR_ID = 'hi.apps.weather.monitor'
WEATHER_POLLING_INTERVAL_SECS = 150
STARTUP_SAFETY_SECS = 30
def __init__( self ):
super().__init__(
- id = 'weather-monitor',
+ id = self.MONITOR_ID,
interval_secs = self.WEATHER_POLLING_INTERVAL_SECS,
)
self._weather_data_source_instance_list = list()
@@ -45,16 +46,16 @@ class WeatherMonitor( PeriodicMonitor, AlertMixin, SettingsMixin ):
weather_data_source.id
)
if is_enabled:
- weather_data_source.set_healthy()
+ weather_data_source.record_healthy()
else:
- weather_data_source.set_disabled()
+ weather_data_source.record_disabled()
continue
return
@classmethod
def get_provider_info(cls) -> ProviderInfo:
return ProviderInfo(
- provider_id = 'hi.apps.weather',
+ provider_id = cls.MONITOR_ID,
provider_name = 'Weather Monitor',
description = 'Weather data collection and monitoring',
expected_heartbeat_interval_secs = cls.WEATHER_POLLING_INTERVAL_SECS,
@@ -80,7 +81,7 @@ class WeatherMonitor( PeriodicMonitor, AlertMixin, SettingsMixin ):
task = asyncio.create_task( weather_data_source.fetch() )
task_list.append( task )
else:
- weather_data_source.set_disabled()
+ weather_data_source.record_disabled()
logger.debug( f'Weather source {weather_data_source.id} is disabled, skipping' )
continue
diff --git a/src/hi/apps/weather/weather_data_source.py b/src/hi/apps/weather/weather_data_source.py
index 23ce2461..6cf75593 100644
--- a/src/hi/apps/weather/weather_data_source.py
+++ b/src/hi/apps/weather/weather_data_source.py
@@ -161,14 +161,17 @@ class WeatherDataSource( ApiHealthStatusProvider ):
if not can_fetch:
if self.TRACE:
logger.debug( f'Polling limits. Skipping weather data fetch for: {self.label}' )
+ self.record_warning( 'Polling limit reached.' )
return
logger.debug( f'Fetching weather data for: {self.label}' )
self.set_last_poll_time()
try:
await self.get_data()
- except Exception:
- logger.exception( f'Problem with weather source: {self.label}' )
+ except Exception as e:
+ message = f'Problem with weather source: {self.label}: {e}'
+ self.record_error( message )
+ logger.exception( message )
return
def can_fetch(self):
diff --git a/src/hi/apps/weather/weather_sources/openmeteo.py b/src/hi/apps/weather/weather_sources/openmeteo.py
index 621f680a..09deb4bb 100644
--- a/src/hi/apps/weather/weather_sources/openmeteo.py
+++ b/src/hi/apps/weather/weather_sources/openmeteo.py
@@ -78,11 +78,13 @@ class OpenMeteo(WeatherDataSource, WeatherMixin):
geographic_location = self.geographic_location
if not geographic_location:
+ self.record_error( 'No geographic data.' )
logger.warning('No geographic location setting. Skipping OpenMeteo weather fetch.')
return
weather_manager = await self.weather_manager_async()
if not weather_manager:
+ self.record_error( 'No weather weather manager.' )
logger.warning('Weather manager not available. Skipping OpenMeteo weather fetch.')
return
@@ -97,6 +99,7 @@ class OpenMeteo(WeatherDataSource, WeatherMixin):
weather_conditions_data = current_conditions_data,
)
except Exception as e:
+ self.record_error( 'Current conditions fetch error: {e}' )
logger.exception(f'Problem fetching OpenMeteo current conditions: {e}')
# Fetch hourly forecast data
@@ -110,6 +113,7 @@ class OpenMeteo(WeatherDataSource, WeatherMixin):
forecast_data_list = interval_hourly_forecast_list,
)
except Exception as e:
+ self.record_error( 'Hourly forecast fetch error: {e}' )
logger.exception(f'Problem fetching OpenMeteo hourly forecast: {e}')
# Fetch daily forecast data
@@ -123,6 +127,7 @@ class OpenMeteo(WeatherDataSource, WeatherMixin):
forecast_data_list = interval_daily_forecast_list,
)
except Exception as e:
+ self.record_error( 'Daily forecast fetch error: {e}' )
logger.exception(f'Problem fetching OpenMeteo daily forecast: {e}')
# Fetch historical weather data (last 7 days)
@@ -140,6 +145,7 @@ class OpenMeteo(WeatherDataSource, WeatherMixin):
else:
logger.warning('OpenMeteo returned no historical weather data')
except Exception as e:
+ self.record_error( 'Historical data fetch error: {e}' )
logger.exception(f'Problem fetching OpenMeteo historical data: {e}')
# Note: OpenMeteo does not provide astronomical data
diff --git a/src/hi/apps/weather/weather_sources/sunrise_sunset_org.py b/src/hi/apps/weather/weather_sources/sunrise_sunset_org.py
index 00b99340..7aa4451e 100644
--- a/src/hi/apps/weather/weather_sources/sunrise_sunset_org.py
+++ b/src/hi/apps/weather/weather_sources/sunrise_sunset_org.py
@@ -79,11 +79,13 @@ class SunriseSunsetOrg(WeatherDataSource, WeatherMixin):
geographic_location = self.geographic_location
if not geographic_location:
+ self.record_error( 'No geographic data.' )
logger.warning('No geographic location setting. Skipping Sunrise-Sunset.org fetch.')
return
weather_manager = await self.weather_manager_async()
if not weather_manager:
+ self.record_error( 'No weather weather manager.' )
logger.warning('Weather manager not available. Skipping Sunrise-Sunset.org fetch.')
return
@@ -99,6 +101,7 @@ class SunriseSunsetOrg(WeatherDataSource, WeatherMixin):
astronomical_data_list = astronomical_data_list,
)
except Exception as e:
+ self.record_error( 'Multi-data astronomical fetch error: {e}' )
logger.exception(f'Problem fetching Sunrise-Sunset.org multi-day astronomical data: {e}')
# Also update today's astronomical data for backwards compatibility
@@ -112,6 +115,7 @@ class SunriseSunsetOrg(WeatherDataSource, WeatherMixin):
astronomical_data = todays_astronomical_data,
)
except Exception as e:
+ self.record_error( 'Today\'s atronomical fetch error: {e}' )
logger.exception(f'Problem fetching Sunrise-Sunset.org today\'s astronomical data: {e}')
return
diff --git a/src/hi/apps/weather/weather_sources/usno.py b/src/hi/apps/weather/weather_sources/usno.py
index 3de3e6b1..859313e9 100644
--- a/src/hi/apps/weather/weather_sources/usno.py
+++ b/src/hi/apps/weather/weather_sources/usno.py
@@ -88,15 +88,17 @@ class USNO( WeatherDataSource, WeatherMixin ):
geographic_location = self.geographic_location
if not geographic_location:
+ self.record_error( 'No geographic data.' )
logger.warning('No geographic location setting. Skipping USNO fetch.')
return
weather_manager = await self.weather_manager_async()
if not weather_manager:
+ self.record_error( 'No weather weather manager.' )
logger.warning('Weather manager not available. Skipping USNO fetch.')
return
- # Fetch 10 days of astronomical data using the new multi-day method
+ # Fetch 10 days of astronomical data
try:
astronomical_data_list = self.get_astronomical_data_list(
geographic_location = geographic_location,
@@ -108,6 +110,7 @@ class USNO( WeatherDataSource, WeatherMixin ):
astronomical_data_list = astronomical_data_list,
)
except Exception as e:
+ self.record_error( 'Multi-data astronomical fetch error: {e}' )
logger.exception(f'Problem fetching USNO multi-day astronomical data: {e}')
# Also update today's astronomical data for backwards compatibility
@@ -121,6 +124,7 @@ class USNO( WeatherDataSource, WeatherMixin ):
astronomical_data = todays_astronomical_data,
)
except Exception as e:
+ self.record_error( 'Today\'s astronomical fetch error: {e}' )
logger.exception(f'Problem fetching USNO today\'s astronomical data: {e}')
return
@@ -149,15 +153,12 @@ class USNO( WeatherDataSource, WeatherMixin ):
astronomical_data_list = []
today = datetimeproxy.now().date()
-
- # Get local timezone from superclass
local_tz = pytz.timezone(self.tz_name)
for day_offset in range(days_count):
target_date = today + timedelta(days=day_offset)
try:
- # Get astronomical data for this date
astronomical_data = self.get_astronomical_data(
geographic_location=geographic_location,
target_date=target_date
@@ -165,10 +166,11 @@ class USNO( WeatherDataSource, WeatherMixin ):
if astronomical_data:
# Create local day boundaries (midnight to midnight in local time)
- local_start = local_tz.localize(datetime.combine(target_date, datetime.min.time()))
- local_end = local_tz.localize(datetime.combine(target_date, datetime.max.time()))
+ local_start = local_tz.localize(datetime.combine( target_date,
+ datetime.min.time()) )
+ local_end = local_tz.localize(datetime.combine( target_date,
+ datetime.max.time()) )
- # Convert to UTC for internal storage (following IntervalDataManager pattern)
interval_start = local_start.astimezone(pytz.UTC)
interval_end = local_end.astimezone(pytz.UTC)
diff --git a/src/hi/integrations/integration_gateway.py b/src/hi/integrations/integration_gateway.py
index dd464a95..f2ed8db7 100644
--- a/src/hi/integrations/integration_gateway.py
+++ b/src/hi/integrations/integration_gateway.py
@@ -4,7 +4,7 @@ from hi.apps.entity.models import Entity
from hi.apps.entity.transient_models import VideoStream
from hi.apps.monitor.periodic_monitor import PeriodicMonitor
from hi.apps.sense.transient_models import SensorResponse
-from hi.apps.system.health_status import HealthStatus
+from hi.apps.system.health_status_provider import HealthStatusProvider
from .integration_controller import IntegrationController
from .integration_manage_view_pane import IntegrationManageViewPane
@@ -40,7 +40,7 @@ class IntegrationGateway:
"""
raise NotImplementedError('Subclasses must override this method')
- def get_health_status(self) -> HealthStatus:
+ def get_health_status_provider(self) -> HealthStatusProvider:
raise NotImplementedError('Subclasses must override this method')
def validate_configuration(
diff --git a/src/hi/integrations/integration_manager.py b/src/hi/integrations/integration_manager.py
index 4e8792cd..87e7dc0f 100644
--- a/src/hi/integrations/integration_manager.py
+++ b/src/hi/integrations/integration_manager.py
@@ -73,6 +73,25 @@ class IntegrationManager( Singleton ):
return self._integration_data_map[integration_id].integration_gateway
raise KeyError( f'Unknown integration id "{integration_id}".' )
+ def get_health_status_by_provider_id( self,
+ provider_id : str ) -> HealthStatusProvider:
+ with self._data_lock:
+ for integration in self._integration_data_map.values():
+ provider = integration.integration_gateway.get_health_status_provider()
+ if provider.get_provider_info().provider_id == provider_id:
+ return provider
+ continue
+ raise KeyError( f'Unknown provider id: "{provider_id}".' )
+
+ def get_health_status_by_monitor_id( self,
+ monitor_id : str ) -> HealthStatusProvider:
+ with self._data_lock:
+ for monitor in self._monitor_map.values():
+ if monitor.id == monitor_id:
+ return monitor
+ continue
+ raise KeyError( f'Unknown monitor id: "{monitor_id}".' )
+
def get_health_status_providers(self) -> List[HealthStatusProvider]:
with self._data_lock:
return list( self._monitor_map.values() )
diff --git a/src/hi/integrations/templates/integrations/pages/integration_manage.html b/src/hi/integrations/templates/integrations/pages/integration_manage.html
index b0439a93..5b3ba35e 100644
--- a/src/hi/integrations/templates/integrations/pages/integration_manage.html
+++ b/src/hi/integrations/templates/integrations/pages/integration_manage.html
@@ -18,7 +18,7 @@
data-async="true">
<div class="d-flex justify-content-between align-items-center">
<span>{{ integration_data.label }}</span>
- {% with health_status=integration_data.integration_gateway.get_health_status %}
+ {% with health_status=integration_data.integration_gateway.get_health_status_provider.health_status %}
{% if health_status.is_error %}
<span class="text-danger">
&nbsp; {% icon "warning" size="lg" css_class="mr-1" %}
diff --git a/src/hi/integrations/views.py b/src/hi/integrations/views.py
index f39c17ff..5433977c 100644
--- a/src/hi/integrations/views.py
+++ b/src/hi/integrations/views.py
@@ -62,13 +62,9 @@ class IntegrationHealthStatusView( HiModalView, IntegrationViewMixin ):
integration_data = self.get_integration_data(
integration_id = integration_id,
)
-
- # Get health status from the integration gateway
- health_status = integration_data.integration_gateway.get_health_status()
-
+ health_status_provider = integration_data.integration_gateway.get_health_status_provider()
context = {
- 'integration_data': integration_data,
- 'health_status': health_status,
+ 'health_status_provider': health_status_provider,
}
return self.modal_response( request, context )
@@ -205,11 +201,11 @@ class IntegrationManageView( ConfigPageView, IntegrationViewMixin, AttributeEdit
raise BadRequest( f'{integration_data.label} integration is not enabled' )
# Get health status from the integration gateway
- health_status = integration_data.integration_gateway.get_health_status()
+ health_status_provider = integration_data.integration_gateway.get_health_status_provider()
attr_item_context = IntegrationAttributeItemEditContext(
integration_data = integration_data,
- health_status = health_status,
+ health_status = health_status_provider.health_status,
)
integration_data_list = self.get_integration_data_list( enabled_only = True )
@@ -232,7 +228,7 @@ class IntegrationManageView( ConfigPageView, IntegrationViewMixin, AttributeEdit
'integration_data_list': integration_data_list,
'integration_data': integration_data,
'manage_view_template_name': manage_template_name,
- 'health_status': health_status,
+ 'health_status': health_status_provider.health_status,
},
})
return template_context
@@ -252,11 +248,11 @@ class IntegrationManageView( ConfigPageView, IntegrationViewMixin, AttributeEdit
raise BadRequest( f'{integration_data.label} integration is not enabled' )
# Get health status from the integration gateway
- health_status = integration_data.integration_gateway.get_health_status()
+ health_status_provider = integration_data.integration_gateway.get_health_status_provider()
attr_item_context = IntegrationAttributeItemEditContext(
integration_data = integration_data,
- health_status = health_status,
+ health_status = health_status_provider.health_status,
)
return self.post_attribute_form(
diff --git a/src/hi/services/hass/hass_manager.py b/src/hi/services/hass/hass_manager.py
index 0520c396..016670c6 100644
--- a/src/hi/services/hass/hass_manager.py
+++ b/src/hi/services/hass/hass_manager.py
@@ -47,7 +47,7 @@ class HassManager( SingletonManager, AggregateHealthProvider, ApiHealthStatusPro
@classmethod
def get_provider_info(cls) -> ProviderInfo:
return ProviderInfo(
- provider_id = 'hass_integration',
+ provider_id = 'hi.services.hass.manager',
provider_name = 'Home Assistant Integration',
description = '',
)
@@ -56,7 +56,7 @@ class HassManager( SingletonManager, AggregateHealthProvider, ApiHealthStatusPro
def get_api_provider_info(cls) -> ProviderInfo:
"""Get the API service info for this manager."""
return ProviderInfo(
- provider_id = 'hass_api',
+ provider_id = 'hi.services.hass.api',
provider_name = 'Home Assistant API',
description = 'Home Assistant REST API for entity states'
)
@@ -93,27 +93,27 @@ class HassManager( SingletonManager, AggregateHealthProvider, ApiHealthStatusPro
self._hass_attr_type_to_attribute = self._load_attributes()
self._hass_client = self.create_hass_client( self._hass_attr_type_to_attribute )
self.clear_caches()
- self.update_health_status(HealthStatusType.HEALTHY)
+ self.record_healthy('Reloaded')
except IntegrationDisabledError:
msg = 'HASS integration disabled'
logger.info(msg)
- self.update_health_status( HealthStatusType.DISABLED, msg )
+ self.record_disabled( msg )
except IntegrationError as e:
error_msg = f'HASS integration configuration error: {e}'
logger.error(error_msg)
- self.update_health_status( HealthStatusType.ERROR, f"Configuration error: {error_msg}" )
+ self.record_error( f"Configuration error: {error_msg}" )
except IntegrationAttributeError as e:
error_msg = f'HASS integration attribute error: {e}'
logger.error(error_msg)
- self.update_health_status( HealthStatusType.ERROR, f"Configuration error: {error_msg}" )
+ self.record_error( f"Configuration error: {error_msg}" )
except Exception as e:
error_msg = f'Unexpected error loading HASS configuration: {e}'
logger.exception(error_msg)
- self.update_health_status( HealthStatusType.WARNING, f"Temporary issue: {error_msg}" )
+ self.record_warning( f"Temporary issue: {error_msg}" )
return
def clear_caches(self):
@@ -183,24 +183,22 @@ class HassManager( SingletonManager, AggregateHealthProvider, ApiHealthStatusPro
"""Test the connection to HASS API and update health status."""
try:
if not self.hass_client:
- self.update_health_status(HealthStatusType.ERROR,
- "HASS client not configured")
+ self.record_error("HASS client not configured")
return False
# Try to fetch states to test connection
states = self.fetch_hass_states_from_api(verbose=False)
if states is not None:
- self.update_health_status(HealthStatusType.HEALTHY)
+ self.record_healthy('OK')
return True
else:
- self.update_health_status(HealthStatusType.ERROR,
- "Failed to fetch states from HASS API")
+ self.record_error("Failed to fetch states from HASS API")
return False
except Exception as e:
error_msg = f'Connection test failed: {e}'
logger.debug(error_msg)
- self.update_health_status(HealthStatusType.ERROR, f"Connection error: {error_msg}")
+ self.record_error( f"Connection error: {error_msg}" )
return False
def test_client_with_attributes(
diff --git a/src/hi/services/hass/integration.py b/src/hi/services/hass/integration.py
index 0164d09d..8188d1b9 100644
--- a/src/hi/services/hass/integration.py
+++ b/src/hi/services/hass/integration.py
@@ -2,7 +2,7 @@ import logging
from typing import List
from hi.apps.system.enums import HealthStatusType
-from hi.apps.system.health_status import HealthStatus
+from hi.apps.system.health_status_provider import HealthStatusProvider
from hi.integrations.integration_controller import IntegrationController
from hi.integrations.integration_gateway import IntegrationGateway
@@ -46,8 +46,8 @@ class HassGateway( IntegrationGateway ):
except Exception as e:
logger.exception(f'Error notifying HASS integration of settings change: {e}')
- def get_health_status(self) -> HealthStatus:
- return HassManager().health_status
+ def get_health_status_provider(self) -> HealthStatusProvider:
+ return HassManager()
def validate_configuration(
self, integration_attributes: List[IntegrationAttribute]
diff --git a/src/hi/services/hass/monitors.py b/src/hi/services/hass/monitors.py
index 311b9c9d..2909c63b 100644
--- a/src/hi/services/hass/monitors.py
+++ b/src/hi/services/hass/monitors.py
@@ -14,12 +14,13 @@ logger = logging.getLogger(__name__)
class HassMonitor( PeriodicMonitor, HassMixin, SensorResponseMixin ):
+ MONITOR_ID = 'hi.services.hass.monitor'
HASS_POLLING_INTERVAL_SECS = 4
HASS_API_TIMEOUT_SECS = 10.0 # Shorter timeout appropriate for 2-second polling
def __init__( self ):
super().__init__(
- id = 'hass-monitor',
+ id = self.MONITOR_ID,
interval_secs = self.HASS_POLLING_INTERVAL_SECS,
)
self._was_initialized = False
@@ -53,7 +54,7 @@ class HassMonitor( PeriodicMonitor, HassMixin, SensorResponseMixin ):
@classmethod
def get_provider_info(cls) -> ProviderInfo:
return ProviderInfo(
- provider_id = 'hi.services.hass',
+ provider_id = cls.MONITOR_ID,
provider_name = 'Home Assistant Monitor',
description = 'Home Assistant device state monitoring',
expected_heartbeat_interval_secs = cls.HASS_POLLING_INTERVAL_SECS,
diff --git a/src/hi/services/hass/tests/test_hass_manager.py b/src/hi/services/hass/tests/test_hass_manager.py
index f0299909..9dc098ae 100644
--- a/src/hi/services/hass/tests/test_hass_manager.py
+++ b/src/hi/services/hass/tests/test_hass_manager.py
@@ -127,7 +127,6 @@ class TestHassManagerInitialization(TestCase):
# Health status should reflect the configuration error
health = self.manager.health_status
self.assertEqual(health.status, HealthStatusType.ERROR)
- self.assertIn('not implemented', health.error_message)
def test_reload_with_disabled_integration(self):
"""Test reload handles disabled integration gracefully"""
@@ -157,7 +156,6 @@ class TestHassManagerInitialization(TestCase):
# Health status should reflect the configuration error
health = self.manager.health_status
self.assertEqual(health.status, HealthStatusType.ERROR)
- self.assertIn('Missing HASS attribute', health.error_message)
def test_reload_with_empty_required_value(self):
"""Test reload handles empty required attribute values gracefully"""
@@ -188,7 +186,6 @@ class TestHassManagerInitialization(TestCase):
# Health status should reflect the configuration error
health = self.manager.health_status
self.assertEqual(health.status, HealthStatusType.ERROR)
- self.assertIn('Missing HASS attribute value', health.error_message)
def test_reload_success_with_all_required_attributes(self):
"""Test reload successfully configures manager with all required attributes"""
diff --git a/src/hi/services/zoneminder/integration.py b/src/hi/services/zoneminder/integration.py
index 2cce5547..065ed10c 100644
--- a/src/hi/services/zoneminder/integration.py
+++ b/src/hi/services/zoneminder/integration.py
@@ -7,7 +7,7 @@ from hi.apps.entity.enums import VideoStreamType, VideoStreamMode
from hi.apps.entity.constants import VideoStreamMetadataKeys
from hi.apps.sense.transient_models import SensorResponse
from hi.apps.system.enums import HealthStatusType
-from hi.apps.system.health_status import HealthStatus
+from hi.apps.system.health_status_provider import HealthStatusProvider
from hi.integrations.integration_controller import IntegrationController
from hi.integrations.integration_gateway import IntegrationGateway
@@ -56,8 +56,8 @@ class ZoneMinderGateway( IntegrationGateway, ZoneMinderMixin ):
except Exception as e:
logger.exception(f'Error notifying ZoneMinder integration of settings change: {e}')
- def get_health_status(self) -> HealthStatus:
- return ZoneMinderManager().health_status
+ def get_health_status_provider(self) -> HealthStatusProvider:
+ return ZoneMinderManager()
def validate_configuration(
self,
diff --git a/src/hi/services/zoneminder/monitors.py b/src/hi/services/zoneminder/monitors.py
index ca69260a..a6be0e25 100644
--- a/src/hi/services/zoneminder/monitors.py
+++ b/src/hi/services/zoneminder/monitors.py
@@ -21,6 +21,8 @@ logger = logging.getLogger(__name__)
class ZoneMinderMonitor( PeriodicMonitor, ZoneMinderMixin, SensorResponseMixin ):
+ MONITOR_ID = 'hi.services.zoneminder.monitor'
+
# TODO: Move this into the integrations attributes for users to set
ZONEMINDER_SERVER_TIMEZONE = 'America/Chicago'
@@ -32,7 +34,7 @@ class ZoneMinderMonitor( PeriodicMonitor, ZoneMinderMixin, SensorResponseMixin )
def __init__( self ):
super().__init__(
- id = 'zm-monitor',
+ id = self.MONITOR_ID,
interval_secs = self.ZONEMINDER_POLLING_INTERVAL_SECS,
)
self._fully_processed_event_ids = TTLCache( maxsize = 1000, ttl = 100000 )
@@ -60,7 +62,7 @@ class ZoneMinderMonitor( PeriodicMonitor, ZoneMinderMixin, SensorResponseMixin )
@classmethod
def get_provider_info(cls) -> ProviderInfo:
return ProviderInfo(
- provider_id = 'hi.services.zoneminder',
+ provider_id = cls.MONITOR_ID,
provider_name = 'ZoneMinder Monitor',
description = 'ZoneMinder camera motion detection',
expected_heartbeat_interval_secs = cls.ZONEMINDER_POLLING_INTERVAL_SECS,
diff --git a/src/hi/services/zoneminder/tests/test_zm_manager.py b/src/hi/services/zoneminder/tests/test_zm_manager.py
index 7fa224d4..dcc9f764 100644
--- a/src/hi/services/zoneminder/tests/test_zm_manager.py
+++ b/src/hi/services/zoneminder/tests/test_zm_manager.py
@@ -277,7 +277,7 @@ class ZoneMinderManagerHealthStatusTest(TestCase):
# Should have health status
self.assertIsNotNone(health)
- self.assertIsNotNone(health.last_check)
+ self.assertIsNotNone(health.last_update)
self.assertIsNotNone(health.status)
diff --git a/src/hi/services/zoneminder/zm_manager.py b/src/hi/services/zoneminder/zm_manager.py
index 6d9673b3..b114f994 100644
--- a/src/hi/services/zoneminder/zm_manager.py
+++ b/src/hi/services/zoneminder/zm_manager.py
@@ -77,7 +77,7 @@ class ZoneMinderManager( SingletonManager, AggregateHealthProvider, ApiHealthSta
@classmethod
def get_provider_info(cls) -> ProviderInfo:
return ProviderInfo(
- provider_id = 'zm_integration',
+ provider_id = 'hi.services.zoneminder.manager',
provider_name = 'ZoneMinder Integration',
description = '',
)
@@ -86,7 +86,7 @@ class ZoneMinderManager( SingletonManager, AggregateHealthProvider, ApiHealthSta
def get_api_provider_info(cls) -> ProviderInfo:
"""Get the API service info for this manager."""
return ProviderInfo(
- provider_id = 'zm_api',
+ provider_id = 'hi.services.zoneminder.api',
provider_name = 'ZoneMinder API',
description = 'ZoneMinder video surveillance system API'
)
@@ -142,28 +142,28 @@ class ZoneMinderManager( SingletonManager, AggregateHealthProvider, ApiHealthSta
# Clear all thread-local clients since configuration changed
self._clear_thread_local_clients()
self.clear_caches()
- self.update_health_status(HealthStatusType.HEALTHY)
+ self.record_healthy('Reloaded')
logger.debug( 'ZoneMinder manager loading completed.' )
except IntegrationDisabledError:
msg = 'ZoneMinder integration disabled'
logger.info(msg)
- self.update_health_status( HealthStatusType.DISABLED, msg )
+ self.record_disabled( msg )
except IntegrationError as e:
error_msg = f'ZoneMinder integration configuration error: {e}'
logger.error(error_msg)
- self.update_health_status( HealthStatusType.ERROR, f"Configuration error: {error_msg}" )
+ self.record_error( f"Configuration error: {error_msg}" )
except IntegrationAttributeError as e:
error_msg = f'ZoneMinder integration attribute error: {e}'
logger.error(error_msg)
- self.update_health_status( HealthStatusType.ERROR, f"Configuration error: {error_msg}" )
+ self.record_error( f"Configuration error: {error_msg}" )
except Exception as e:
error_msg = f'Unexpected error loading ZoneMinder configuration: {e}'
logger.exception(error_msg)
- self.update_health_status( HealthStatusType.WARNING, f"Temporary issue: {error_msg}" )
+ self.record_warning( f"Temporary issue: {error_msg}" )
return
def clear_caches(self):
@@ -365,24 +365,22 @@ class ZoneMinderManager( SingletonManager, AggregateHealthProvider, ApiHealthSta
"""Test the connection to ZoneMinder API and update health status."""
try:
if not self.zm_client:
- self.update_health_status( HealthStatusType.ERROR,
- "ZoneMinder client not configured")
+ self.record_error( "ZoneMinder client not configured")
return False
# Try to fetch states to test connection
states = self.get_zm_states(force_load=True)
if states:
- self.update_health_status(HealthStatusType.HEALTHY)
+ self.record_healthy('OK')
return True
else:
- self.update_health_status(HealthStatusType.ERROR,
- "Failed to fetch states from ZoneMinder API")
+ self.record_error("Failed to fetch states from ZoneMinder API")
return False
except Exception as e:
error_msg = f'Connection test failed: {e}'
logger.debug(error_msg)
- self.update_health_status(HealthStatusType.ERROR, f"Connection error: {error_msg}")
+ self.record_error(f"Connection error: {error_msg}")
return False
def test_client_with_attributes(