API: refactor
This commit is contained in:
parent
fa4a094cf3
commit
3621bf87d2
@ -10,10 +10,7 @@ from .models import Activity, Sport, convert_timedelta_to_integer
|
|||||||
stats_blueprint = Blueprint('stats', __name__)
|
stats_blueprint = Blueprint('stats', __name__)
|
||||||
|
|
||||||
|
|
||||||
@stats_blueprint.route('/stats/<int:user_id>/by_time', methods=['GET'])
|
def get_activities(user_id, type):
|
||||||
@authenticate
|
|
||||||
def get_activities_by_time(auth_user_id, user_id):
|
|
||||||
"""Get activities statistics for a user"""
|
|
||||||
try:
|
try:
|
||||||
user = User.query.filter_by(id=user_id).first()
|
user = User.query.filter_by(id=user_id).first()
|
||||||
if not user:
|
if not user:
|
||||||
@ -26,93 +23,20 @@ def get_activities_by_time(auth_user_id, user_id):
|
|||||||
params = request.args.copy()
|
params = request.args.copy()
|
||||||
date_from = params.get('from')
|
date_from = params.get('from')
|
||||||
date_to = params.get('to')
|
date_to = params.get('to')
|
||||||
time = params.get('time')
|
|
||||||
activities_list = {}
|
|
||||||
|
|
||||||
activities = Activity.query.filter(
|
|
||||||
Activity.user_id == user_id,
|
|
||||||
Activity.activity_date >= datetime.strptime(date_from, '%Y-%m-%d')
|
|
||||||
if date_from else True,
|
|
||||||
Activity.activity_date <= datetime.strptime(date_to, '%Y-%m-%d')
|
|
||||||
if date_to else True,
|
|
||||||
).order_by(
|
|
||||||
Activity.activity_date.asc()
|
|
||||||
).all()
|
|
||||||
|
|
||||||
for activity in activities:
|
|
||||||
if time == 'week':
|
|
||||||
time_period = datetime.strftime(activity.activity_date, "%Y-W%U") # noqa
|
|
||||||
elif time == 'weekm': # week start Monday
|
|
||||||
time_period = datetime.strftime(activity.activity_date, "%Y-W%W") # noqa
|
|
||||||
elif time == 'month':
|
|
||||||
time_period = datetime.strftime(activity.activity_date, "%Y-%m") # noqa
|
|
||||||
elif time == 'year' or not time:
|
|
||||||
time_period = datetime.strftime(activity.activity_date, "%Y")
|
|
||||||
else:
|
|
||||||
response_object = {
|
|
||||||
'status': 'fail',
|
|
||||||
'message': 'Invalid time period.'
|
|
||||||
}
|
|
||||||
return jsonify(response_object), 400
|
|
||||||
sport_id = activity.sport_id
|
|
||||||
if time_period not in activities_list:
|
|
||||||
activities_list[time_period] = {}
|
|
||||||
if sport_id not in activities_list[time_period]:
|
|
||||||
activities_list[time_period][sport_id] = {
|
|
||||||
'nb_activities': 0,
|
|
||||||
'total_distance': 0.,
|
|
||||||
'total_duration': 0,
|
|
||||||
}
|
|
||||||
activities_list[time_period][sport_id]['nb_activities'] += 1
|
|
||||||
activities_list[time_period][sport_id]['total_distance'] += \
|
|
||||||
float(activity.distance)
|
|
||||||
activities_list[time_period][sport_id]['total_duration'] += \
|
|
||||||
convert_timedelta_to_integer(activity.duration)
|
|
||||||
|
|
||||||
response_object = {
|
|
||||||
'status': 'success',
|
|
||||||
'data': {
|
|
||||||
'statistics': activities_list
|
|
||||||
}
|
|
||||||
}
|
|
||||||
code = 200
|
|
||||||
except Exception as e:
|
|
||||||
appLog.error(e)
|
|
||||||
response_object = {
|
|
||||||
'status': 'error',
|
|
||||||
'message': 'Error. Please try again or contact the administrator.'
|
|
||||||
}
|
|
||||||
code = 500
|
|
||||||
return jsonify(response_object), code
|
|
||||||
|
|
||||||
|
|
||||||
@stats_blueprint.route('/stats/<int:user_id>/by_sport', methods=['GET'])
|
|
||||||
@authenticate
|
|
||||||
def get_activities_by_sport(auth_user_id, user_id):
|
|
||||||
"""Get activities statistics for a user"""
|
|
||||||
try:
|
|
||||||
user = User.query.filter_by(id=user_id).first()
|
|
||||||
if not user:
|
|
||||||
response_object = {
|
|
||||||
'status': 'not found',
|
|
||||||
'message': 'User does not exist.'
|
|
||||||
}
|
|
||||||
return jsonify(response_object), 404
|
|
||||||
|
|
||||||
params = request.args.copy()
|
|
||||||
date_from = params.get('from')
|
|
||||||
date_to = params.get('to')
|
|
||||||
|
|
||||||
sport_id = params.get('sport_id')
|
sport_id = params.get('sport_id')
|
||||||
if sport_id:
|
time = params.get('time')
|
||||||
sport = Sport.query.filter_by(id=sport_id).first()
|
|
||||||
if not sport:
|
if type == 'by_sport':
|
||||||
print('not sport')
|
sport_id = params.get('sport_id')
|
||||||
response_object = {
|
if sport_id:
|
||||||
'status': 'not found',
|
sport = Sport.query.filter_by(id=sport_id).first()
|
||||||
'message': 'Sport does not exist.'
|
if not sport:
|
||||||
}
|
print('not sport')
|
||||||
return jsonify(response_object), 404
|
response_object = {
|
||||||
|
'status': 'not found',
|
||||||
|
'message': 'Sport does not exist.'
|
||||||
|
}
|
||||||
|
return jsonify(response_object), 404
|
||||||
|
|
||||||
activities = Activity.query.filter(
|
activities = Activity.query.filter(
|
||||||
Activity.user_id == user_id,
|
Activity.user_id == user_id,
|
||||||
@ -127,18 +51,49 @@ def get_activities_by_sport(auth_user_id, user_id):
|
|||||||
|
|
||||||
activities_list = {}
|
activities_list = {}
|
||||||
for activity in activities:
|
for activity in activities:
|
||||||
sport_id = activity.sport_id
|
if type == 'by_sport':
|
||||||
if sport_id not in activities_list:
|
sport_id = activity.sport_id
|
||||||
activities_list[sport_id] = {
|
if sport_id not in activities_list:
|
||||||
'nb_activities': 0,
|
activities_list[sport_id] = {
|
||||||
'total_distance': 0.,
|
'nb_activities': 0,
|
||||||
'total_duration': 0,
|
'total_distance': 0.,
|
||||||
}
|
'total_duration': 0,
|
||||||
activities_list[sport_id]['nb_activities'] += 1
|
}
|
||||||
activities_list[sport_id]['total_distance'] += \
|
activities_list[sport_id]['nb_activities'] += 1
|
||||||
float(activity.distance)
|
activities_list[sport_id]['total_distance'] += \
|
||||||
activities_list[sport_id]['total_duration'] += \
|
float(activity.distance)
|
||||||
convert_timedelta_to_integer(activity.duration)
|
activities_list[sport_id]['total_duration'] += \
|
||||||
|
convert_timedelta_to_integer(activity.duration)
|
||||||
|
|
||||||
|
else:
|
||||||
|
if time == 'week':
|
||||||
|
time_period = datetime.strftime(activity.activity_date, "%Y-W%U") # noqa
|
||||||
|
elif time == 'weekm': # week start Monday
|
||||||
|
time_period = datetime.strftime(activity.activity_date, "%Y-W%W") # noqa
|
||||||
|
elif time == 'month':
|
||||||
|
time_period = datetime.strftime(activity.activity_date, "%Y-%m") # noqa
|
||||||
|
elif time == 'year' or not time:
|
||||||
|
time_period = datetime.strftime(activity.activity_date, "%Y") # noqa
|
||||||
|
else:
|
||||||
|
response_object = {
|
||||||
|
'status': 'fail',
|
||||||
|
'message': 'Invalid time period.'
|
||||||
|
}
|
||||||
|
return jsonify(response_object), 400
|
||||||
|
sport_id = activity.sport_id
|
||||||
|
if time_period not in activities_list:
|
||||||
|
activities_list[time_period] = {}
|
||||||
|
if sport_id not in activities_list[time_period]:
|
||||||
|
activities_list[time_period][sport_id] = {
|
||||||
|
'nb_activities': 0,
|
||||||
|
'total_distance': 0.,
|
||||||
|
'total_duration': 0,
|
||||||
|
}
|
||||||
|
activities_list[time_period][sport_id]['nb_activities'] += 1
|
||||||
|
activities_list[time_period][sport_id]['total_distance'] += \
|
||||||
|
float(activity.distance)
|
||||||
|
activities_list[time_period][sport_id]['total_duration'] += \
|
||||||
|
convert_timedelta_to_integer(activity.duration)
|
||||||
|
|
||||||
response_object = {
|
response_object = {
|
||||||
'status': 'success',
|
'status': 'success',
|
||||||
@ -155,3 +110,16 @@ def get_activities_by_sport(auth_user_id, user_id):
|
|||||||
}
|
}
|
||||||
code = 500
|
code = 500
|
||||||
return jsonify(response_object), code
|
return jsonify(response_object), code
|
||||||
|
|
||||||
|
|
||||||
|
@stats_blueprint.route('/stats/<int:user_id>/by_time', methods=['GET'])
|
||||||
|
@authenticate
|
||||||
|
def get_activities_by_time(auth_user_id, user_id):
|
||||||
|
"""Get activities statistics for a user"""
|
||||||
|
return get_activities(user_id, 'by_time')
|
||||||
|
|
||||||
|
|
||||||
|
@stats_blueprint.route('/stats/<int:user_id>/by_sport', methods=['GET'])
|
||||||
|
@authenticate
|
||||||
|
def get_activities_by_sport(auth_user_id, user_id):
|
||||||
|
return get_activities(user_id, 'by_sport')
|
||||||
|
Loading…
Reference in New Issue
Block a user