niming_backend/blueprints/log.py

43 lines
1.3 KiB
Python
Raw Normal View History

2024-11-19 21:22:01 +08:00
from flask import Blueprint, request, jsonify
2024-11-14 13:03:00 +08:00
from sqlalchemy import desc
2024-11-19 21:22:01 +08:00
2024-12-09 03:24:22 +08:00
from utils import pgclass, dbhelper
2024-11-19 21:22:01 +08:00
from utils.misc import error
2024-11-14 13:03:00 +08:00
log = Blueprint('log', __name__)
# 列出log
@log.route("/list", methods = ["GET"])
def listlog():
# variables
2024-11-19 02:19:25 +08:00
if request.args.get("start") is None or request.args.get("count") is None or \
2024-11-19 23:29:01 +08:00
not request.args.get("start").isdigit() or not request.args.get("count").isdigit():
2024-11-19 21:22:01 +08:00
return error("Arguments error"), 400
2024-11-14 13:03:00 +08:00
rst = int(request.args.get("start"))
count = int(request.args.get("count"))
2024-11-19 21:22:01 +08:00
# getctx
2024-12-09 03:24:22 +08:00
with dbhelper.db.getsession() as session:
2024-11-19 02:19:25 +08:00
table = pgclass.SQLlog
res = session.query(table).order_by(desc(table.id)).offset(rst).limit(count).all()
2024-11-14 13:03:00 +08:00
2024-11-19 21:22:01 +08:00
# mapping
res = [ {"id":r.id, "created_at":r.created_at, "source":r.source, "message":r.message} for r in res ]
2024-11-14 13:03:00 +08:00
2024-11-19 21:22:01 +08:00
return jsonify(res), 200
2024-11-14 13:03:00 +08:00
2024-12-09 14:10:58 +08:00
2024-11-14 13:03:00 +08:00
# 指定顯示特定一條log
2024-11-19 21:22:01 +08:00
@log.route("/<int:id>", methods = ["GET"])
2024-11-18 02:47:25 +08:00
def getlog(id:int):
2024-11-14 13:03:00 +08:00
# db
2024-12-09 03:24:22 +08:00
with dbhelper.db.getsession() as session:
2024-11-19 02:19:25 +08:00
table = pgclass.SQLlog
2024-11-19 21:22:01 +08:00
res = session.query(table).filter(table.id == id).first()
if res is None:
return error("Log not Found"), 404
2024-11-14 13:03:00 +08:00
# mapping
2024-11-19 21:22:01 +08:00
res = {"id":res.id, "created_at":res.created_at, "source":res.source, "message":res.message}
2024-11-14 13:03:00 +08:00
2024-11-19 21:22:01 +08:00
return jsonify(res), 200