feat: pagination totals, request logging, data retention
Add shared paginate() helper with total count to all list endpoints. Add request logging middleware (method, path, status, duration, IP). Add data retention service with configurable thresholds and CLI command.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"""Alert endpoints."""
|
||||
from datetime import datetime, timedelta, UTC
|
||||
from flask import request
|
||||
from . import bp
|
||||
from . import bp, paginate
|
||||
from ..models import Alert
|
||||
from ..extensions import db
|
||||
|
||||
@@ -12,8 +12,6 @@ def list_alerts():
|
||||
alert_type = request.args.get('type')
|
||||
sensor_id = request.args.get('sensor_id', type=int)
|
||||
hours = request.args.get('hours', 24, type=int)
|
||||
limit = min(int(request.args.get('limit', 100)), 1000)
|
||||
offset = int(request.args.get('offset', 0))
|
||||
|
||||
since = datetime.now(UTC) - timedelta(hours=hours)
|
||||
query = db.select(Alert).where(Alert.timestamp >= since).order_by(Alert.timestamp.desc())
|
||||
@@ -23,7 +21,6 @@ def list_alerts():
|
||||
if sensor_id:
|
||||
query = query.where(Alert.sensor_id == sensor_id)
|
||||
|
||||
query = query.limit(limit).offset(offset)
|
||||
alerts = db.session.scalars(query).all()
|
||||
|
||||
return {'alerts': [a.to_dict() for a in alerts], 'limit': limit, 'offset': offset}
|
||||
result = paginate(query, Alert.to_dict)
|
||||
return {'alerts': result['items'], 'total': result['total'],
|
||||
'limit': result['limit'], 'offset': result['offset']}
|
||||
|
||||
Reference in New Issue
Block a user