Add exceptions in /chat_activity
This commit is contained in:
parent
80fd3c7f79
commit
2afc19f05d
|
@ -41,33 +41,47 @@ def top_users():
|
||||||
def chat_activity():
|
def chat_activity():
|
||||||
from_date = request.args.get("from_date")
|
from_date = request.args.get("from_date")
|
||||||
to_date = request.args.get("to_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
|
||||||
|
|
||||||
date_format = request.args.get("date_format")
|
date_format = request.args.get("date_format")
|
||||||
group_by = request.args.get("group_by")
|
group_by = request.args.get("group_by")
|
||||||
|
|
||||||
if not date_format:
|
group_by = group_by if group_by else "day"
|
||||||
date_format = "%Y-%m-%d"
|
date_format = date_format if date_format else "%Y-%m-%d"
|
||||||
|
|
||||||
if not group_by:
|
|
||||||
group_by = "day"
|
|
||||||
|
|
||||||
dates = []
|
dates = []
|
||||||
counts = []
|
counts = []
|
||||||
|
|
||||||
if from_date and to_date:
|
try:
|
||||||
from_date = datetime.strptime(from_date, date_format)
|
from_date = datetime.strptime(from_date, date_format)
|
||||||
to_date = datetime.strptime(to_date, date_format)
|
to_date = datetime.strptime(to_date, date_format)
|
||||||
|
except ValueError:
|
||||||
query = (Message
|
return jsonify({
|
||||||
.select(fn.date_trunc(group_by, Message.timestamp).alias('range'), fn.count(Message.id).alias('count'))
|
"from_date": from_date,
|
||||||
.where((Message.timestamp >= from_date) & (Message.timestamp <= to_date))
|
"to_date": to_date,
|
||||||
.group_by(fn.date_trunc(group_by, Message.timestamp))
|
"date_format": date_format,
|
||||||
.order_by(SQL("range"))
|
"err":"can't format datatime obj"
|
||||||
)
|
}),422
|
||||||
|
|
||||||
|
if not group_by in ["day","week","month","year"]:
|
||||||
|
return jsonify({
|
||||||
|
"err":"invalid group_by"
|
||||||
|
}),422
|
||||||
|
|
||||||
for group in query:
|
query = (Message
|
||||||
dates.append(group.range.strftime("%Y-%m-%d"))
|
.select(fn.date_trunc(group_by, Message.timestamp).alias('range'), fn.count(Message.id).alias('count'))
|
||||||
counts.append(group.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(date_format))
|
||||||
|
counts.append(group.count)
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
"date": dates,
|
"date": dates,
|
||||||
|
|
Loading…
Reference in New Issue