kin: BATON-ARCH-002-backend_dev
This commit is contained in:
parent
057e500d5f
commit
a1279b92e6
3 changed files with 36 additions and 29 deletions
|
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
|||
import asyncio
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
from datetime import datetime, timezone
|
||||
from typing import Any
|
||||
|
||||
from fastapi import Depends, FastAPI, Request
|
||||
|
|
@ -21,7 +22,7 @@ from backend.models import (
|
|||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
aggregator = telegram.SignalAggregator(interval=10)
|
||||
# aggregator = telegram.SignalAggregator(interval=10) # v2.0 feature — отключено в v1 (ADR-004)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
|
|
@ -33,20 +34,21 @@ async def lifespan(app: FastAPI):
|
|||
await telegram.set_webhook(url=config.WEBHOOK_URL, secret=config.WEBHOOK_SECRET)
|
||||
logger.info("Webhook registered")
|
||||
|
||||
task = asyncio.create_task(aggregator.run())
|
||||
logger.info("Aggregator started")
|
||||
# v2.0 feature — агрегатор отключён в v1 (ADR-004)
|
||||
# task = asyncio.create_task(aggregator.run())
|
||||
# logger.info("Aggregator started")
|
||||
|
||||
yield
|
||||
|
||||
# Shutdown
|
||||
aggregator.stop()
|
||||
await aggregator.flush()
|
||||
task.cancel()
|
||||
try:
|
||||
await task
|
||||
except asyncio.CancelledError:
|
||||
pass
|
||||
logger.info("Aggregator stopped, final flush done")
|
||||
# aggregator.stop()
|
||||
# await aggregator.flush()
|
||||
# task.cancel()
|
||||
# try:
|
||||
# await task
|
||||
# except asyncio.CancelledError:
|
||||
# pass
|
||||
# logger.info("Aggregator stopped, final flush done")
|
||||
|
||||
|
||||
app = FastAPI(lifespan=lifespan)
|
||||
|
|
@ -81,13 +83,19 @@ async def signal(body: SignalRequest) -> SignalResponse:
|
|||
)
|
||||
|
||||
user_name = await db.get_user_name(body.user_id)
|
||||
await aggregator.add_signal(
|
||||
user_uuid=body.user_id,
|
||||
user_name=user_name,
|
||||
timestamp=body.timestamp,
|
||||
geo={"lat": lat, "lon": lon, "accuracy": accuracy} if geo else None,
|
||||
signal_id=signal_id,
|
||||
ts = datetime.fromtimestamp(body.timestamp / 1000, tz=timezone.utc)
|
||||
name = user_name or body.user_id[:8]
|
||||
geo_info = (
|
||||
f"📍 {lat}, {lon} (±{accuracy}м)"
|
||||
if geo
|
||||
else "Без геолокации"
|
||||
)
|
||||
text = (
|
||||
f"🚨 Сигнал от {name}\n"
|
||||
f"⏰ {ts.strftime('%H:%M:%S')} UTC\n"
|
||||
f"{geo_info}"
|
||||
)
|
||||
await telegram.send_message(text)
|
||||
|
||||
return SignalResponse(status="ok", signal_id=signal_id)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue