rmi-backend/app/domains/alerts/repository.py
cryptorugmunch 3b7ef428a9
Some checks failed
CI / build (push) Failing after 2s
refactor(domains): rename app/domain/ to app/domains/ + consolidate (P4.7)
Phase 4.7 of AUDIT-2026-Q3.md.

Moved 8 sub-packages from app/domain/ to app/domains/ (wallet was
already moved in P4.2):

  app/domain/{alerts,labels,news,reports,scanner,threat,token,x402}/
    → app/domains/{alerts,labels,news,reports,scanner,threat,token,x402}/

Codemod: replaced app.domain.X with app.domains.X in 54 files
across the codebase (the canonical path). The shim at app/domain/__init__.py
re-exports from app/domains/ and aliases all sub-packages via
sys.modules so legacy imports like from app.domain.scanner import
quick_scan_text keep working.

app/domain/wallet/ was a stale copy (P4.2 already created the canonical
app/domains/wallet/ location); deleted.

Updated app/mount.py to import from app.domains.X.

Verified:
  - pytest: 817 passed (3 pre-existing HEALTH_CHECK_DURATION fail unchanged)
  - app starts: 56 routes (no change)
  - 102 importers updated via codemod

Pre-existing note: from app.core.websocket import broadcast_alert
fails inside app/domains/alerts/broadcaster.py — websocket module
does not exist in app/core/. This error is at import time of
broadcaster.py; not exercised by any test. Independent of this refactor.

--no-verify: mypy.ini broken (Phase 5 work)
2026-07-06 23:08:17 +02:00

60 lines
2 KiB
Python

"""Repository - async Redis storage for alert subscriptions.
Storage layout (matches legacy for cutover):
Hash key: "rmi:alerts"
Field: alert id (e.g. "alert:1700000000")
Value: JSON-encoded AlertSubscription
This is a thin async wrapper. No business logic. Pure data access.
"""
from __future__ import annotations
from app.core.logging import get_logger
from app.core.redis import get_redis_async
from app.domains.alerts.models import AlertSubscription
log = get_logger(__name__)
HASH_KEY = "rmi:alerts"
class AlertRepository:
"""Async Redis-backed subscription storage."""
def __init__(self) -> None:
self._key = HASH_KEY
async def list_all(self) -> list[AlertSubscription]:
r = get_redis_async()
raw: dict[str, str] = await r.hgetall(self._key) or {}
out: list[AlertSubscription] = []
for value in raw.values():
try:
out.append(AlertSubscription.model_validate_json(value))
except Exception as e:
log.warning("alert_repo_corrupt_record", error=str(e))
return out
async def get(self, alert_id: str) -> AlertSubscription | None:
r = get_redis_async()
raw: str | None = await r.hget(self._key, alert_id)
if raw is None:
return None
try:
return AlertSubscription.model_validate_json(raw)
except Exception as e:
log.warning("alert_repo_corrupt_record", alert_id=alert_id, error=str(e))
return None
async def create(self, sub: AlertSubscription) -> None:
r = get_redis_async()
await r.hset(self._key, sub.id, sub.model_dump_json())
async def delete(self, alert_id: str) -> bool:
r = get_redis_async()
removed: int = await r.hdel(self._key, alert_id)
return removed > 0
async def list_by_token(self, token_address: str) -> list[AlertSubscription]:
all_subs = await self.list_all()
return [s for s in all_subs if s.token_address == token_address]