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")
|
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
|
||||||
|
|
||||||
query = (Message
|
if not group_by in ["day","week","month","year"]:
|
||||||
.select(fn.date_trunc(group_by, Message.timestamp).alias('range'), fn.count(Message.id).alias('count'))
|
return jsonify({
|
||||||
.where((Message.timestamp >= from_date) & (Message.timestamp <= to_date))
|
"err":"invalid group_by"
|
||||||
.group_by(fn.date_trunc(group_by, Message.timestamp))
|
}),422
|
||||||
.order_by(SQL("range"))
|
|
||||||
)
|
|
||||||
|
|
||||||
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