diff --git a/db.py b/db.py index 9fd0bb4..788ba31 100644 --- a/db.py +++ b/db.py @@ -33,6 +33,15 @@ def init(): userkey TEXT, surplus INT); ''') + cursor.execute( + ''' + CREATE TABLE log ( + ip TEXT, + time INT, + tokens INT, + model TEXT, + userkey TEXT); + ''') # 提交事务 db.commit() @@ -149,3 +158,35 @@ def createKey(quota,number=1,key="null"): return output + +def newLog(ip:str, time:int, tokens:int, model:str, userkey:str): + #打开数据库连接 + db = getconn() + # 使用 cursor() 方法创建一个游标对象 cursor + cursor = db.cursor() + + # 使用 execute() 方法执行 SQL 查询 + + cursor.execute(f"INSERT INTO log (ip, time, tokens, model, userkey) VALUES (?, ?, ?, ?, ?);", [ip, time, tokens, model, userkey]) + + # 提交事务 + db.commit() + + db.close() + + +def getlog(num:int): + #打开数据库连接 + db = getconn() + # 使用 cursor() 方法创建一个游标对象 cursor + cursor = db.cursor() + + # 使用 execute() 方法执行 SQL 查询 + cursor.execute(f"SELECT * FROM log order by time desc limit ?;", [num]) + # 使用 fetchall() 方法获取结果集 + data = cursor.fetchall() + + # 关闭连接 + db.close() + + return data diff --git a/log.py b/log.py new file mode 100644 index 0000000..a54dc60 --- /dev/null +++ b/log.py @@ -0,0 +1,19 @@ +import time as times +import db + +def newLog(ip:str,tokens:int, model:str, userkey:str): + db.newLog(ip, int(times.time()), tokens, model, userkey) + +def getlog(num:int): + if num < 0: + num = 10 + rawdata = db.getlog(num) + data = [] + for i in rawdata: + item = list(i) + item[1] = times.strftime("%Y-%m-%d %H:%M:%S",times.localtime(i[1])) + data.append(item) + + return data + + \ No newline at end of file diff --git a/main.py b/main.py index 99c8e4f..a67185b 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,12 @@ import flask from flask_cors import CORS -import db , config +import db , config , log from apiModule import qwenTurbo , chatglmTurbo , gpt35Turbo , gpt4Turbo app = flask.Flask(__name__) CORS(app,origins="*") app.secret_key = b'SQ-{kJE;m(jEBi|{yq]v' +app.config['TRUSTED_PROXIES'] = ['proxy_ip'] @app.route('/api/user', methods=['POST']) def post_data(): @@ -48,6 +49,7 @@ def post_data(): code , output , tokenUsed = gpt4Turbo.service(userRequest['prompt']) db.reduce_value(userRequest['userkey'], tokenUsed) + log.newLog(flask.request.headers.get('X-Forwarded-For'), tokenUsed, userRequest["model"], userRequest['userkey']) return {"code":code,"output":output,"surplus":surplusToken} @@ -83,6 +85,20 @@ def adminList(): return flask.render_template("keylist.html",data=data) return flask.redirect('login') +@app.route('/admin/log', methods=['GET']) +def adminListLog(): + if "admin" in flask.session : + if 'show' in flask.request.args: + try: + shownum = int(flask.request.values.get('show')) + except: + return flask.abort(400) + else: + return flask.abort(400) + data = log.getlog(shownum) + return flask.render_template("loglist.html",data=data) + return flask.redirect('login') + @app.route('/admin/createkey', methods=['POST','GET']) def createkey(): if "admin" in flask.session : diff --git a/templates/loglist.html b/templates/loglist.html new file mode 100644 index 0000000..412fce3 --- /dev/null +++ b/templates/loglist.html @@ -0,0 +1,72 @@ + + + + + + 列出日志 + + + + +
+

列出日志

+ + + + + + + + + + + + {% for item in data %} + + + + + + + + {% endfor %} + +
IP时间token用量使用模型UserKey
{{item[0]}}{{item[1]}}{{item[2]}}{{item[3]}}{{item[4]}}
+
+
+ + + + + \ No newline at end of file diff --git a/templates/status.html b/templates/status.html index 278f627..a63c9af 100644 --- a/templates/status.html +++ b/templates/status.html @@ -97,6 +97,9 @@ 创建密钥 + + 查看日志 + 阿巴阿巴