fix: preserve legacy monitoring migration data

This commit is contained in:
2026-08-04 11:22:21 +08:00
parent 80a31b8dd0
commit fee707f42d
4 changed files with 270 additions and 35 deletions
+47 -15
View File
@@ -36,6 +36,23 @@ class DeviceMonitoringPolicy:
degraded_loss_percent: float
recovery_consecutive_clean_rounds: int
@staticmethod
def _resolve_override(
override: object,
default: int | float,
minimum: int | float,
maximum: int | float,
) -> int | float:
"""Use a bounded persisted override or retain the validated default."""
if (
isinstance(override, bool)
or not isinstance(override, (int, float))
or override < minimum
or override > maximum
):
return default
return override
@classmethod
def from_device(
cls,
@@ -45,29 +62,44 @@ class DeviceMonitoringPolicy:
"""Resolve device overrides without changing the stored device."""
return cls(
probe_packets_per_round=(
device.probe_packets_per_round
if device.probe_packets_per_round is not None
else settings.probe_packets_per_round
cls._resolve_override(
device.probe_packets_per_round,
settings.probe_packets_per_round,
1,
10,
)
),
offline_consecutive_rounds=(
device.offline_consecutive_rounds
if device.offline_consecutive_rounds is not None
else settings.offline_consecutive_rounds
cls._resolve_override(
device.offline_consecutive_rounds,
settings.offline_consecutive_rounds,
1,
10,
)
),
degraded_window_rounds=(
device.degraded_window_rounds
if device.degraded_window_rounds is not None
else settings.degraded_window_rounds
cls._resolve_override(
device.degraded_window_rounds,
settings.degraded_window_rounds,
2,
60,
)
),
degraded_loss_percent=(
device.degraded_loss_percent
if device.degraded_loss_percent is not None
else settings.degraded_loss_percent
cls._resolve_override(
device.degraded_loss_percent,
settings.degraded_loss_percent,
1,
100,
)
),
recovery_consecutive_clean_rounds=(
device.recovery_consecutive_clean_rounds
if device.recovery_consecutive_clean_rounds is not None
else settings.recovery_consecutive_clean_rounds
cls._resolve_override(
device.recovery_consecutive_clean_rounds,
settings.recovery_consecutive_clean_rounds,
1,
20,
)
),
)