niming_backend/blueprints/log.py
2024-11-19 13:22:01 +00:00

43 lines
1.3 KiB
Python

from flask import Blueprint, request, jsonify
from sqlalchemy import desc
from utils import pgclass
from utils.dbhelper import db
from utils.misc import error
log = Blueprint('log', __name__)
# 列出log
@log.route("/list", methods = ["GET"])
def listlog():
# variables
if request.args.get("start") is None or request.args.get("count") is None or \
request.args.get("start").isdigit()==False or request.args.get("count").isdigit()==False:
return error("Arguments error"), 400
rst = int(request.args.get("start"))
count = int(request.args.get("count"))
# getctx
with db.getsession() as session:
table = pgclass.SQLlog
res = session.query(table).order_by(desc(table.id)).offset(rst).limit(count).all()
# mapping
res = [ {"id":r.id, "created_at":r.created_at, "source":r.source, "message":r.message} for r in res ]
return jsonify(res), 200
# 指定顯示特定一條log
@log.route("/<int:id>", methods = ["GET"])
def getlog(id:int):
# db
with db.getsession() as session:
table = pgclass.SQLlog
res = session.query(table).filter(table.id == id).first()
if res is None:
return error("Log not Found"), 404
# mapping
res = {"id":res.id, "created_at":res.created_at, "source":res.source, "message":res.message}
return jsonify(res), 200