style: ruff format 10 python files
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -43,8 +43,7 @@ async def _evaluate_condition(session, rule) -> bool: # noqa: ANN001
|
||||
|
||||
offline_result = await session.execute(
|
||||
text(
|
||||
"SELECT count(*) AS cnt FROM devices "
|
||||
"WHERE site_id = :site_id AND is_online = false"
|
||||
"SELECT count(*) AS cnt FROM devices WHERE site_id = :site_id AND is_online = false"
|
||||
),
|
||||
{"site_id": site_id},
|
||||
)
|
||||
@@ -55,8 +54,7 @@ async def _evaluate_condition(session, rule) -> bool: # noqa: ANN001
|
||||
elif rule_type == "device_offline_count":
|
||||
offline_result = await session.execute(
|
||||
text(
|
||||
"SELECT count(*) AS cnt FROM devices "
|
||||
"WHERE site_id = :site_id AND is_online = false"
|
||||
"SELECT count(*) AS cnt FROM devices WHERE site_id = :site_id AND is_online = false"
|
||||
),
|
||||
{"site_id": site_id},
|
||||
)
|
||||
@@ -171,9 +169,11 @@ async def _evaluate_rules() -> None:
|
||||
# Events with consecutive_hits < 2 are considered "pending"
|
||||
# (not yet confirmed). On next evaluation if still met,
|
||||
# consecutive_hits increments to 2 (confirmed alert).
|
||||
severity = "critical" if rule.rule_type in (
|
||||
"device_offline_percent", "device_offline_count"
|
||||
) else "warning"
|
||||
severity = (
|
||||
"critical"
|
||||
if rule.rule_type in ("device_offline_percent", "device_offline_count")
|
||||
else "warning"
|
||||
)
|
||||
|
||||
await session.execute(
|
||||
text("""
|
||||
|
||||
@@ -122,9 +122,7 @@ async def _subscribe_with_retry(js: JetStreamContext) -> None:
|
||||
durable="api-interface-consumer",
|
||||
stream="DEVICE_EVENTS",
|
||||
)
|
||||
logger.info(
|
||||
"NATS: subscribed to device.interfaces.> (durable: api-interface-consumer)"
|
||||
)
|
||||
logger.info("NATS: subscribed to device.interfaces.> (durable: api-interface-consumer)")
|
||||
return
|
||||
except Exception as exc:
|
||||
if attempt < max_attempts:
|
||||
|
||||
@@ -31,8 +31,8 @@ _link_discovery_client: Optional[NATSClient] = None
|
||||
|
||||
# Configurable thresholds for link state transitions
|
||||
DEGRADED_SIGNAL_THRESHOLD = -80 # dBm — signals weaker than this mark link as degraded
|
||||
CONSECUTIVE_MISS_THRESHOLD = 3 # Missed polls before marking link as down
|
||||
STALE_HOURS = 24 # Hours after down before marking link as stale
|
||||
CONSECUTIVE_MISS_THRESHOLD = 3 # Missed polls before marking link as down
|
||||
STALE_HOURS = 24 # Hours after down before marking link as stale
|
||||
|
||||
|
||||
# =============================================================================
|
||||
@@ -187,14 +187,16 @@ async def on_wireless_registration_for_links(msg) -> None:
|
||||
|
||||
# Mark stale: any links in 'down' state where last_seen > STALE_HOURS ago
|
||||
await session.execute(
|
||||
text("""
|
||||
text(
|
||||
"""
|
||||
UPDATE wireless_links
|
||||
SET state = 'stale', updated_at = NOW()
|
||||
WHERE ap_device_id = :ap_device_id
|
||||
AND tenant_id = :tenant_id
|
||||
AND state = 'down'
|
||||
AND last_seen < NOW() - INTERVAL ':stale_hours hours'
|
||||
""".replace(":stale_hours", str(STALE_HOURS))),
|
||||
""".replace(":stale_hours", str(STALE_HOURS))
|
||||
),
|
||||
{
|
||||
"ap_device_id": device_id,
|
||||
"tenant_id": tenant_id,
|
||||
|
||||
@@ -29,7 +29,9 @@ logger = structlog.get_logger("site_service")
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
def _site_response(site: Site, device_count: int = 0, online_count: int = 0, alert_count: int = 0) -> SiteResponse:
|
||||
def _site_response(
|
||||
site: Site, device_count: int = 0, online_count: int = 0, alert_count: int = 0
|
||||
) -> SiteResponse:
|
||||
"""Build a SiteResponse from an ORM Site instance with health stats."""
|
||||
online_percent = (online_count / device_count * 100) if device_count > 0 else 0.0
|
||||
return SiteResponse(
|
||||
@@ -51,9 +53,7 @@ def _site_response(site: Site, device_count: int = 0, online_count: int = 0, ale
|
||||
|
||||
async def _get_site_or_404(db: AsyncSession, tenant_id: uuid.UUID, site_id: uuid.UUID) -> Site:
|
||||
"""Fetch a site by id and tenant, or raise 404."""
|
||||
result = await db.execute(
|
||||
select(Site).where(Site.id == site_id, Site.tenant_id == tenant_id)
|
||||
)
|
||||
result = await db.execute(select(Site).where(Site.id == site_id, Site.tenant_id == tenant_id))
|
||||
site = result.scalar_one_or_none()
|
||||
if not site:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Site not found")
|
||||
|
||||
Reference in New Issue
Block a user