Add exceptions in /chat_activity
This commit is contained in:
parent
80fd3c7f79
commit
2afc19f05d
|
@ -42,32 +42,46 @@ def chat_activity():
|
|||
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
|
||||
|
||||
date_format = request.args.get("date_format")
|
||||
group_by = request.args.get("group_by")
|
||||
|
||||
if not date_format:
|
||||
date_format = "%Y-%m-%d"
|
||||
|
||||
if not group_by:
|
||||
group_by = "day"
|
||||
group_by = group_by if group_by else "day"
|
||||
date_format = date_format if date_format else "%Y-%m-%d"
|
||||
|
||||
dates = []
|
||||
counts = []
|
||||
|
||||
if from_date and to_date:
|
||||
try:
|
||||
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
|
||||
|
||||
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"))
|
||||
)
|
||||
if not group_by in ["day","week","month","year"]:
|
||||
return jsonify({
|
||||
"err":"invalid group_by"
|
||||
}),422
|
||||
|
||||
for group in query:
|
||||
dates.append(group.range.strftime("%Y-%m-%d"))
|
||||
counts.append(group.count)
|
||||
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(date_format))
|
||||
counts.append(group.count)
|
||||
|
||||
return jsonify({
|
||||
"date": dates,
|
||||
|
|
Loading…
Reference in New Issue