Add exceptions in /chat_activity
This commit is contained in:
parent
80fd3c7f79
commit
2afc19f05d
|
@ -42,21 +42,35 @@ 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:
|
||||||
|
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
|
query = (Message
|
||||||
.select(fn.date_trunc(group_by, Message.timestamp).alias('range'), fn.count(Message.id).alias('count'))
|
.select(fn.date_trunc(group_by, Message.timestamp).alias('range'), fn.count(Message.id).alias('count'))
|
||||||
|
@ -66,7 +80,7 @@ def chat_activity():
|
||||||
)
|
)
|
||||||
|
|
||||||
for group in query:
|
for group in query:
|
||||||
dates.append(group.range.strftime("%Y-%m-%d"))
|
dates.append(group.range.strftime(date_format))
|
||||||
counts.append(group.count)
|
counts.append(group.count)
|
||||||
|
|
||||||
return jsonify({
|
return jsonify({
|
||||||
|
|
Loading…
Reference in New Issue