2022-12-14 09:12:57 -05:00
|
|
|
from flask import Blueprint, jsonify, request
|
2022-12-17 04:58:53 -05:00
|
|
|
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 04:58:53 -05:00
|
|
|
@group_stat_api.route("/chat_activity", methods=["GET"])
|
|
|
|
def chat_activity():
|
|
|
|
from_date = request.args.get("from_date")
|
|
|
|
to_date = request.args.get("to_date")
|
2022-12-11 08:51:44 -05:00
|
|
|
|
2022-12-17 04:58:53 -05:00
|
|
|
date_format = request.args.get("date_format")
|
|
|
|
group_by = request.args.get("group_by")
|
2022-12-14 09:12:57 -05:00
|
|
|
|
2022-12-17 04:58:53 -05:00
|
|
|
if not date_format:
|
|
|
|
date_format = "%Y-%m-%d"
|
|
|
|
|
|
|
|
if not group_by:
|
|
|
|
group_by = "day"
|
|
|
|
|
|
|
|
dates = []
|
2022-12-14 09:12:57 -05:00
|
|
|
counts = []
|
|
|
|
|
2022-12-17 04:58:53 -05:00
|
|
|
if from_date and to_date:
|
|
|
|
from_date = datetime.strptime(from_date, date_format)
|
|
|
|
to_date = datetime.strptime(to_date, date_format)
|
|
|
|
|
|
|
|
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"))
|
|
|
|
)
|
|
|
|
|
|
|
|
for group in query:
|
|
|
|
dates.append(group.range.strftime("%Y-%m-%d"))
|
|
|
|
counts.append(group.count)
|
2022-12-14 09:12:57 -05:00
|
|
|
|
2022-12-11 08:51:44 -05:00
|
|
|
return jsonify({
|
2022-12-17 04:58:53 -05:00
|
|
|
"date": dates,
|
|
|
|
"counts": counts
|
2022-12-11 08:51:44 -05:00
|
|
|
})
|