fix: login issue
This commit is contained in:
@@ -14,25 +14,13 @@ import json
|
|||||||
import logging
|
import logging
|
||||||
from datetime import date, datetime, timedelta, timezone
|
from datetime import date, datetime, timedelta, timezone
|
||||||
|
|
||||||
from dateutil.relativedelta import relativedelta
|
|
||||||
from sqlalchemy import text
|
from sqlalchemy import text
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
|
||||||
|
|
||||||
from config import settings
|
from config import settings
|
||||||
from database.postgres import AsyncSessionLocal
|
from database.postgres import AsyncSessionLocal
|
||||||
|
|
||||||
logger = logging.getLogger("database.pg_mqtt")
|
logger = logging.getLogger("database.pg_mqtt")
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
# Internal session helper — used by fire-and-forget insert paths that are
|
|
||||||
# called from the MQTT ingestion thread (not inside a FastAPI request).
|
|
||||||
# ---------------------------------------------------------------------------
|
|
||||||
|
|
||||||
async def _session() -> AsyncSession:
|
|
||||||
return AsyncSessionLocal()
|
|
||||||
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
# Insert operations
|
# Insert operations
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
@@ -302,14 +290,21 @@ async def get_alerts(device_serial: str) -> list:
|
|||||||
# Partition management
|
# Partition management
|
||||||
# ---------------------------------------------------------------------------
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def _add_months(d: date, months: int) -> date:
|
||||||
|
month = d.month - 1 + months
|
||||||
|
year = d.year + month // 12
|
||||||
|
month = month % 12 + 1
|
||||||
|
return d.replace(year=year, month=month, day=1)
|
||||||
|
|
||||||
|
|
||||||
async def ensure_current_partitions():
|
async def ensure_current_partitions():
|
||||||
"""Create device_logs partitions for the current and next month if missing."""
|
"""Create device_logs partitions for the current and next month if missing."""
|
||||||
async with AsyncSessionLocal() as session:
|
async with AsyncSessionLocal() as session:
|
||||||
for month_offset in (0, 1):
|
for month_offset in (0, 1):
|
||||||
d = date.today().replace(day=1) + relativedelta(months=month_offset)
|
d = _add_months(date.today().replace(day=1), month_offset)
|
||||||
partition_name = f"device_logs_{d.strftime('%Y_%m')}"
|
partition_name = f"device_logs_{d.strftime('%Y_%m')}"
|
||||||
start = d.isoformat()
|
start = d.isoformat()
|
||||||
end = (d + relativedelta(months=1)).isoformat()
|
end = _add_months(d, 1).isoformat()
|
||||||
await session.execute(text(f"""
|
await session.execute(text(f"""
|
||||||
CREATE TABLE IF NOT EXISTS {partition_name}
|
CREATE TABLE IF NOT EXISTS {partition_name}
|
||||||
PARTITION OF device_logs
|
PARTITION OF device_logs
|
||||||
@@ -321,7 +316,7 @@ async def ensure_current_partitions():
|
|||||||
|
|
||||||
async def drop_old_partitions(keep_months: int = 6):
|
async def drop_old_partitions(keep_months: int = 6):
|
||||||
"""Drop device_logs partitions older than keep_months."""
|
"""Drop device_logs partitions older than keep_months."""
|
||||||
cutoff = date.today().replace(day=1) - relativedelta(months=keep_months)
|
cutoff = _add_months(date.today().replace(day=1), -keep_months)
|
||||||
async with AsyncSessionLocal() as session:
|
async with AsyncSessionLocal() as session:
|
||||||
result = await session.execute(text("""
|
result = await session.execute(text("""
|
||||||
SELECT tablename FROM pg_tables
|
SELECT tablename FROM pg_tables
|
||||||
|
|||||||
Reference in New Issue
Block a user