This repository has been archived on 2026-08-27. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
RequestCounter/app/api/views.py
T

134 lines
3.0 KiB
Python
Raw Permalink Normal View History

2022-04-09 10:36:36 +08:00
#!/usr/bin/env python3
# -- coding:utf-8 --
# @Author: markushammered@gmail.com
# @Development Tool: PyCharm
# @Create Time: 2022/4/9
2022-04-09 10:55:27 +08:00
# @File Name: views.py
from flask import jsonify
from flask import request
2022-04-10 14:31:24 +08:00
from flask import send_file
2022-04-09 10:55:27 +08:00
from ..db.db import SQLite as db
2022-04-11 21:49:36 +08:00
from ..decorators import permission_required
2022-04-09 10:55:27 +08:00
from . import api
@api.route('/overall/', methods=['GET', 'POST'])
def overall():
2022-04-10 14:31:24 +08:00
"""
查询ReqCount表内所有的源数据
:return:
"""
2022-04-09 10:55:27 +08:00
limit = request.args.get('limit', type=int)
try:
data = db().exec('select * from reqcount;')
format_data = []
for i in data:
format_data.append({'name': i[0], 'times': i[1]})
return jsonify(
{
'code': 200,
'msg': 'Success. Total: %s row(s)' % len(data),
'data': format_data[:limit]
}
), 200
except Exception as e:
return jsonify(
{
'code': -200,
'msg': 'Error',
'data': e
}
), 500
@api.route('/query/', methods=['GET', 'POST'])
def query():
2022-04-10 14:31:24 +08:00
"""
查询指定名称的计数数据
:return:
"""
2022-04-09 10:55:27 +08:00
name = request.args.get('name', type=str)
nochange = request.args.get('nochange', type=bool)
if nochange:
nochange = True
if not db().exists_name(name):
return jsonify(
{
'code': -200,
'msg': 'Failed. No such name',
'data': None
}
), 404
else:
data = db().fetch(name, nochange)
return jsonify(
{
'code': 200,
'msg': 'Success',
'data': data
}
), 200
@api.route('/theme/', methods=['GET', 'POST'])
def theme():
2022-04-10 14:31:24 +08:00
"""
查询数据库内主题的源数据
:return:
"""
2022-04-09 10:55:27 +08:00
_theme = request.args.get('name', type=str)
if not db().exists_table(_theme):
return jsonify(
{
'code': 404,
'msg': 'Failed. No such theme',
'data': None
}
), 404
else:
data = db().fetching_table(_theme)
return jsonify(
{
'code': 200,
'msg': 'Success',
'data': data
}
), 200
@api.route('/alltables/', methods=['GET', 'POST'])
def all_tables():
2022-04-10 14:31:24 +08:00
"""
查询所有已有表
:return:
"""
2022-04-09 10:55:27 +08:00
data = db().show_tables
return jsonify(
{
'code': 200,
'msg': 'Success',
'data': data
}
), 200
2022-04-10 14:31:24 +08:00
@api.route('/export/', methods=['GET', 'POST'])
@permission_required
2022-04-14 22:21:24 +08:00
def export():
2022-04-10 14:31:24 +08:00
"""
导出数据库文件
:return:
"""
return send_file('./db/data.sqlite', as_attachment=True)
2022-04-14 22:21:24 +08:00
@api.route('/test', methods=['GET', 'POST'])
def test_api():
"""
测试专用接口
:return:
"""
data = db().fetch('test-example')
return str(data[1])