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.
27 lines
887 B
Python
27 lines
887 B
Python
"""Alert endpoints."""
|
|
from datetime import datetime, timedelta, UTC
|
|
from flask import request
|
|
from . import bp, paginate
|
|
from ..models import Alert
|
|
from ..extensions import db
|
|
|
|
|
|
@bp.route('/alerts')
|
|
def list_alerts():
|
|
"""List alerts with filters."""
|
|
alert_type = request.args.get('type')
|
|
sensor_id = request.args.get('sensor_id', type=int)
|
|
hours = request.args.get('hours', 24, type=int)
|
|
|
|
since = datetime.now(UTC) - timedelta(hours=hours)
|
|
query = db.select(Alert).where(Alert.timestamp >= since).order_by(Alert.timestamp.desc())
|
|
|
|
if alert_type:
|
|
query = query.where(Alert.alert_type == alert_type)
|
|
if sensor_id:
|
|
query = query.where(Alert.sensor_id == sensor_id)
|
|
|
|
result = paginate(query, Alert.to_dict)
|
|
return {'alerts': result['items'], 'total': result['total'],
|
|
'limit': result['limit'], 'offset': result['offset']}
|