WebAdminPanel/apps/group_stat_api.py

147 lines
3.9 KiB
Python
Raw Normal View History

2022-12-21 15:37:54 -05:00
from flask import Blueprint
from flask import request, jsonify
from peewee import SQL, fn
2022-12-14 09:12:57 -05:00
from database import Message, Member
from datetime import datetime
2022-12-11 08:51:44 -05:00
group_stat_api = Blueprint('group_stat_api', __name__)
2022-12-17 07:30:04 -05:00
@group_stat_api.route("/top_users", methods=["GET"])
def top_users():
number = request.args.get("number")
if not number: number = 3
number = int(number)
members = []
counts = []
query = (Member
.select(fn.count(Message.id).alias("count"), Member)
.join(Message)
.group_by(Member)
.order_by(SQL("count").desc())
)
for group in query:
members.append(group.first_name)
counts.append(group.count)
members.insert(0, "Others")
counts.insert(0, sum(counts[number:]))
return jsonify({
"members": members[:number + 1],
"counts": counts[:number + 1]
})
@group_stat_api.route("/chat_activity", methods=["GET"])
def chat_activity():
2022-12-21 15:41:28 -05:00
dates = []
counts = []
from_date = request.args.get("from_date")
to_date = request.args.get("to_date")
2022-12-20 14:27:48 -05:00
if not from_date or not to_date:
return jsonify({
"err": "from_date/to_date empty"
}),422
2022-12-11 08:51:44 -05:00
date_format = request.args.get("date_format")
2022-12-21 15:41:28 -05:00
date_format = date_format if date_format else "%Y-%m-%d"
2022-12-20 14:27:48 -05:00
2022-12-21 15:41:28 -05:00
group_by = request.args.get("group_by")
2022-12-20 14:27:48 -05:00
group_by = group_by if group_by else "day"
try:
from_date = datetime.strptime(from_date, date_format)
2022-12-20 14:27:48 -05:00
to_date = datetime.strptime(to_date, date_format)
except ValueError:
return jsonify({
"from_date": from_date,
"to_date": to_date,
"date_format": date_format,
"err":"can't format datatime obj"
}),422
if not group_by in ["day","week","month","year"]:
return jsonify({
"err":"invalid group_by"
}),422
query = (Message
.select(fn.date_trunc(group_by, Message.timestamp).alias('range'), fn.count(Message.id).alias('count'))
.where((Message.timestamp >= from_date) & (Message.timestamp <= to_date))
.group_by(fn.date_trunc(group_by, Message.timestamp))
.order_by(SQL("range"))
)
2022-12-21 15:41:28 -05:00
for row in query:
dates.append(row.range.strftime(date_format))
counts.append(row.count)
2022-12-14 09:12:57 -05:00
2022-12-11 08:51:44 -05:00
return jsonify({
"date": dates,
"counts": counts
2022-12-11 08:51:44 -05:00
})
2022-12-21 15:37:54 -05:00
@group_stat_api.route("/user_activity", methods=["GET"])
def user_activity():
dates = []
counts = []
user_id = request.args.get("user_id")
user = Member.get_or_none(Member.user_id == user_id)
if not user:
return jsonify({
"err":"invalid user_id"
}),422
from_date = request.args.get("from_date")
to_date = request.args.get("to_date")
if not from_date or not to_date:
return jsonify({
"err": "from_date/to_date empty"
}),422
group_by = request.args.get("group_by")
group_by = group_by if group_by else "day"
date_format = request.args.get("date_format")
date_format = date_format if date_format else "%Y-%m-%d"
try:
from_date = datetime.strptime(from_date,date_format)
to_date = datetime.strptime(to_date, date_format)
except ValueError:
return jsonify({
"from_date": from_date,
"to_date": to_date,
"date_format": date_format,
"err":"can't format datatime obj"
}),422
query = (Message
.select( fn.date_trunc(group_by, Message.timestamp).alias("range"), fn.count(Message.id).alias("count") )
.where((Message.user == user) & (Message.timestamp >= from_date) & (Message.timestamp <= to_date))
.group_by(fn.date_trunc(group_by, Message.timestamp))
.order_by(SQL("range"))
)
for row in query:
dates.append(row.range.strftime(date_format))
counts.append(row.count)
return jsonify({
"date": dates,
"counts": counts
})