848f804169
- FastAPI 后端 + Vue 3 前端 - Docker Compose 一键部署 - Casdoor OAuth 认证集成 - LogHive 集中式日志 - 设备批量 CSV 导入/导出 - WebSocket 实时状态推送 - 企业微信告警通知 - fping 高性能并发 Ping 检测 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
"""数据清理服务"""
|
|
import logging
|
|
from datetime import datetime, timedelta
|
|
|
|
from sqlalchemy import delete
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.config import settings
|
|
from app.models.ping_record import PingRecord
|
|
from app.models.alert_event import AlertEvent
|
|
from app.core.deps import async_session
|
|
|
|
logger = logging.getLogger("pingwatch.cleanup")
|
|
|
|
|
|
async def cleanup_old_data():
|
|
"""清理超过保留期限的旧数据"""
|
|
now = datetime.now()
|
|
|
|
# 清理 ping_records
|
|
ping_cutoff = now - timedelta(days=settings.PING_RECORD_RETENTION_DAYS)
|
|
async with async_session() as db:
|
|
result = await db.execute(
|
|
delete(PingRecord).where(PingRecord.created_at < ping_cutoff)
|
|
)
|
|
await db.commit()
|
|
if result.rowcount > 0:
|
|
logger.info(f"清理 ping_records: {result.rowcount} 条")
|
|
|
|
# 清理 alert_events
|
|
alert_cutoff = now - timedelta(days=settings.ALERT_RETENTION_DAYS)
|
|
async with async_session() as db:
|
|
result = await db.execute(
|
|
delete(AlertEvent).where(AlertEvent.created_at < alert_cutoff)
|
|
)
|
|
await db.commit()
|
|
if result.rowcount > 0:
|
|
logger.info(f"清理 alert_events: {result.rowcount} 条")
|