@@ -83,6 +83,11 @@ class ConnectionHealth:
8383 # Count consecutive unhealthy evaluations to apply grace period
8484 consecutive_unhealthy : int = 0
8585
86+ # Health scoring weights (configurable)
87+ latency_weight : float = 0.4
88+ success_rate_weight : float = 0.4
89+ stability_weight : float = 0.2
90+
8691 def __post_init__ (self ) -> None :
8792 """Initialize default values and validate data."""
8893 current_time = time .time ()
@@ -103,14 +108,16 @@ def __post_init__(self) -> None:
103108 self .connection_stability = max (0.0 , min (1.0 , float (self .connection_stability )))
104109
105110 def update_health_score (self ) -> None :
106- """Calculate overall health score based on metrics."""
111+ """Calculate overall health score based on metrics with configurable weights ."""
107112 # Weighted scoring algorithm
108113 latency_score = max (0.0 , 1.0 - (self .ping_latency / 1000.0 )) # Normalize to 1s
109114 success_score = self .ping_success_rate
110115 stability_score = self .connection_stability
111116
112117 self .health_score = (
113- latency_score * 0.4 + success_score * 0.4 + stability_score * 0.2
118+ latency_score * self .latency_weight
119+ + success_score * self .success_rate_weight
120+ + stability_score * self .stability_weight
114121 )
115122
116123 def update_ping_metrics (self , latency : float , success : bool ) -> None :
@@ -257,8 +264,30 @@ def get_health_summary(self) -> dict[str, Any]:
257264
258265def create_default_connection_health (
259266 established_at : float | None = None ,
267+ latency_weight : float = 0.4 ,
268+ success_rate_weight : float = 0.4 ,
269+ stability_weight : float = 0.2 ,
260270) -> ConnectionHealth :
261- """Create a new ConnectionHealth instance with default values."""
271+ """
272+ Create a new ConnectionHealth instance with default values.
273+
274+ Parameters
275+ ----------
276+ established_at : float | None
277+ Timestamp when the connection was established. Defaults to current time.
278+ latency_weight : float
279+ Weight for latency in health scoring. Defaults to 0.4.
280+ success_rate_weight : float
281+ Weight for success rate in health scoring. Defaults to 0.4.
282+ stability_weight : float
283+ Weight for stability in health scoring. Defaults to 0.2.
284+
285+ Returns
286+ -------
287+ ConnectionHealth
288+ New ConnectionHealth instance with provided or default values.
289+
290+ """
262291 current_time = time .time ()
263292 established_at = established_at or current_time
264293
@@ -283,4 +312,7 @@ def create_default_connection_health(
283312 last_bandwidth_check = current_time ,
284313 peak_bandwidth = 0.0 ,
285314 average_bandwidth = 0.0 ,
315+ latency_weight = latency_weight ,
316+ success_rate_weight = success_rate_weight ,
317+ stability_weight = stability_weight ,
286318 )
0 commit comments