niming_backend/blueprints/log.py
2024-11-14 05:03:00 +00:00

44 lines
1.2 KiB
Python

from flask import current_app, Blueprint, request, jsonify
from sqlalchemy.orm import sessionmaker
from sqlalchemy import desc
from utils import pgclass
log = Blueprint('log', __name__)
# 列出log
@log.route("/list", methods = ["GET"])
def listlog():
# variables
rst = int(request.args.get("start"))
count = int(request.args.get("count"))
# db
db = current_app.shared_resource.engine
Session = sessionmaker(bind=db)
session = Session()
# get ctx
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)
# 指定顯示特定一條log
@log.route("/get/<int:id>", methods = ["GET"])
def getlog(id):
# db
db = current_app.shared_resource.engine
Session = sessionmaker(bind=db)
session = Session()
# get ctx
table = pgclass.SQLlog
res = session.query(table).filter(table.id == id).all()
# mapping
res = [ {"id":r.id, "created_at":r.created_at, "source":r.source, "message":r.message} for r in res ]
return jsonify(res)