WebAdminPanel/apps/group_stat_api.py

34 lines
847 B
Python
Raw Normal View History

2022-12-14 09:12:57 -05:00
from flask import Blueprint, jsonify, request
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-14 09:12:57 -05:00
@group_stat_api.route("/top_users",methods=["GET"])
2022-12-11 08:51:44 -05:00
def top_users():
2022-12-14 09:12:57 -05:00
number = request.args.get("number")
if not number: number = 3
number = int(number)
members = []
counts = []
for user in Member.select():
members.append(user.first_name)
counts.append(Message.select().where(Message.user_id == user.user_id).count())
# Sorting
counts, members = (list(t) for t in zip(*sorted(zip(counts,members),reverse=True)))
members.insert(number, "Others")
counts.insert(number, sum(counts[number:]))
2022-12-11 08:51:44 -05:00
return jsonify({
2022-12-14 09:12:57 -05:00
"members":members[:number+1],
"counts":counts[:number+1]
2022-12-11 08:51:44 -05:00
})