2018-06-06 13:45:39 +02:00
|
|
|
from datetime import datetime, timedelta
|
2021-01-02 19:28:03 +01:00
|
|
|
from typing import Dict, Union
|
2018-06-06 00:22:24 +02:00
|
|
|
|
2021-01-20 16:47:00 +01:00
|
|
|
from flask import Blueprint, request
|
|
|
|
from sqlalchemy import func
|
|
|
|
|
2021-01-01 16:39:25 +01:00
|
|
|
from fittrackee import db
|
2022-05-27 15:51:40 +02:00
|
|
|
from fittrackee.oauth2.server import require_auth
|
2021-01-01 16:39:25 +01:00
|
|
|
from fittrackee.responses import (
|
2021-01-02 19:28:03 +01:00
|
|
|
HttpResponse,
|
2021-01-01 16:39:25 +01:00
|
|
|
InvalidPayloadErrorResponse,
|
|
|
|
NotFoundErrorResponse,
|
|
|
|
UserNotFoundErrorResponse,
|
|
|
|
handle_error_and_return_response,
|
|
|
|
)
|
2021-01-20 16:47:00 +01:00
|
|
|
from fittrackee.users.models import User
|
2018-06-06 00:22:24 +02:00
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
from .models import Sport, Workout
|
2022-02-16 17:46:22 +01:00
|
|
|
from .utils.convert import convert_timedelta_to_integer
|
|
|
|
from .utils.uploads import get_upload_dir_size
|
|
|
|
from .utils.workouts import get_average_speed, get_datetime_from_request_args
|
2018-06-06 00:22:24 +02:00
|
|
|
|
|
|
|
stats_blueprint = Blueprint('stats', __name__)
|
|
|
|
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
def get_workouts(
|
2021-01-02 19:28:03 +01:00
|
|
|
user_name: str, filter_type: str
|
|
|
|
) -> Union[Dict, HttpResponse]:
|
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
Return user workouts by sport or by time
|
2021-01-02 19:28:03 +01:00
|
|
|
"""
|
2018-06-06 00:22:24 +02:00
|
|
|
try:
|
2020-02-08 15:17:07 +01:00
|
|
|
user = User.query.filter_by(username=user_name).first()
|
2018-06-06 00:22:24 +02:00
|
|
|
if not user:
|
2021-01-01 16:39:25 +01:00
|
|
|
return UserNotFoundErrorResponse()
|
2018-06-06 00:22:24 +02:00
|
|
|
|
|
|
|
params = request.args.copy()
|
2021-05-22 17:14:24 +02:00
|
|
|
date_from, date_to = get_datetime_from_request_args(params, user)
|
2018-06-06 12:16:52 +02:00
|
|
|
sport_id = params.get('sport_id')
|
2018-06-06 12:09:09 +02:00
|
|
|
time = params.get('time')
|
2018-06-06 12:16:52 +02:00
|
|
|
|
2018-06-12 11:47:01 +02:00
|
|
|
if filter_type == 'by_sport':
|
2018-06-06 12:16:52 +02:00
|
|
|
if sport_id:
|
|
|
|
sport = Sport.query.filter_by(id=sport_id).first()
|
|
|
|
if not sport:
|
2021-11-01 09:44:10 +01:00
|
|
|
return NotFoundErrorResponse('sport does not exist')
|
2018-06-06 00:22:24 +02:00
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
workouts = (
|
|
|
|
Workout.query.filter(
|
|
|
|
Workout.user_id == user.id,
|
|
|
|
Workout.workout_date >= date_from if date_from else True,
|
|
|
|
Workout.workout_date < date_to + timedelta(seconds=1)
|
2019-08-28 13:25:39 +02:00
|
|
|
if date_to
|
|
|
|
else True,
|
2021-01-10 11:16:43 +01:00
|
|
|
Workout.sport_id == sport_id if sport_id else True,
|
2019-08-28 13:25:39 +02:00
|
|
|
)
|
2021-01-10 11:16:43 +01:00
|
|
|
.order_by(Workout.workout_date.asc())
|
2019-08-28 13:25:39 +02:00
|
|
|
.all()
|
|
|
|
)
|
2018-06-06 00:22:24 +02:00
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
workouts_list_by_sport = {}
|
|
|
|
workouts_list_by_time = {} # type: ignore
|
|
|
|
for workout in workouts:
|
2018-06-12 11:47:01 +02:00
|
|
|
if filter_type == 'by_sport':
|
2021-01-10 11:16:43 +01:00
|
|
|
sport_id = workout.sport_id
|
|
|
|
if sport_id not in workouts_list_by_sport:
|
|
|
|
workouts_list_by_sport[sport_id] = {
|
2021-11-24 17:42:27 +01:00
|
|
|
'average_speed': 0.0,
|
2021-01-10 11:16:43 +01:00
|
|
|
'nb_workouts': 0,
|
2019-08-28 13:25:39 +02:00
|
|
|
'total_distance': 0.0,
|
2018-06-06 12:16:52 +02:00
|
|
|
'total_duration': 0,
|
2021-08-20 07:35:50 +02:00
|
|
|
'total_ascent': 0.0,
|
|
|
|
'total_descent': 0.0,
|
2018-06-06 12:16:52 +02:00
|
|
|
}
|
2021-01-10 11:16:43 +01:00
|
|
|
workouts_list_by_sport[sport_id]['nb_workouts'] += 1
|
2021-11-24 17:42:27 +01:00
|
|
|
workouts_list_by_sport[sport_id][
|
|
|
|
'average_speed'
|
|
|
|
] = get_average_speed(
|
|
|
|
workouts_list_by_sport[sport_id]['nb_workouts'], # type: ignore # noqa
|
|
|
|
workouts_list_by_sport[sport_id]['average_speed'],
|
|
|
|
workout.ave_speed,
|
|
|
|
)
|
2021-01-10 11:16:43 +01:00
|
|
|
workouts_list_by_sport[sport_id]['total_distance'] += float(
|
|
|
|
workout.distance
|
2019-08-28 13:25:39 +02:00
|
|
|
)
|
2021-01-10 11:16:43 +01:00
|
|
|
workouts_list_by_sport[sport_id][
|
2019-08-28 13:25:39 +02:00
|
|
|
'total_duration'
|
2021-01-10 11:16:43 +01:00
|
|
|
] += convert_timedelta_to_integer(workout.moving)
|
2021-09-01 14:05:45 +02:00
|
|
|
if workout.ascent:
|
|
|
|
workouts_list_by_sport[sport_id]['total_ascent'] += float(
|
|
|
|
workout.ascent
|
|
|
|
)
|
|
|
|
if workout.descent:
|
|
|
|
workouts_list_by_sport[sport_id]['total_descent'] += float(
|
|
|
|
workout.descent
|
|
|
|
)
|
2018-06-06 12:16:52 +02:00
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
# filter_type == 'by_time'
|
2018-06-06 12:09:09 +02:00
|
|
|
else:
|
2018-06-06 12:16:52 +02:00
|
|
|
if time == 'week':
|
2021-01-10 11:16:43 +01:00
|
|
|
workout_date = workout.workout_date - timedelta(
|
2019-08-28 13:25:39 +02:00
|
|
|
days=(
|
2021-01-10 11:16:43 +01:00
|
|
|
workout.workout_date.isoweekday()
|
|
|
|
if workout.workout_date.isoweekday() < 7
|
2019-08-28 13:25:39 +02:00
|
|
|
else 0
|
|
|
|
)
|
2018-06-06 13:45:39 +02:00
|
|
|
)
|
2021-01-10 11:16:43 +01:00
|
|
|
time_period = datetime.strftime(workout_date, "%Y-%m-%d")
|
2018-06-06 12:16:52 +02:00
|
|
|
elif time == 'weekm': # week start Monday
|
2021-01-10 11:16:43 +01:00
|
|
|
workout_date = workout.workout_date - timedelta(
|
|
|
|
days=workout.workout_date.weekday()
|
2018-06-06 13:45:39 +02:00
|
|
|
)
|
2021-01-10 11:16:43 +01:00
|
|
|
time_period = datetime.strftime(workout_date, "%Y-%m-%d")
|
2018-06-06 12:16:52 +02:00
|
|
|
elif time == 'month':
|
2019-08-28 13:25:39 +02:00
|
|
|
time_period = datetime.strftime(
|
2021-01-10 11:16:43 +01:00
|
|
|
workout.workout_date, "%Y-%m"
|
2020-02-08 15:17:07 +01:00
|
|
|
)
|
2018-06-06 12:16:52 +02:00
|
|
|
elif time == 'year' or not time:
|
2021-01-10 11:16:43 +01:00
|
|
|
time_period = datetime.strftime(workout.workout_date, "%Y")
|
2018-06-06 12:16:52 +02:00
|
|
|
else:
|
2021-01-01 16:39:25 +01:00
|
|
|
return InvalidPayloadErrorResponse(
|
|
|
|
'Invalid time period.', 'fail'
|
|
|
|
)
|
2021-01-10 11:16:43 +01:00
|
|
|
sport_id = workout.sport_id
|
|
|
|
if time_period not in workouts_list_by_time:
|
|
|
|
workouts_list_by_time[time_period] = {}
|
|
|
|
if sport_id not in workouts_list_by_time[time_period]:
|
|
|
|
workouts_list_by_time[time_period][sport_id] = {
|
2021-11-24 17:42:27 +01:00
|
|
|
'average_speed': 0.0,
|
2021-01-10 11:16:43 +01:00
|
|
|
'nb_workouts': 0,
|
2019-08-28 13:25:39 +02:00
|
|
|
'total_distance': 0.0,
|
2018-06-06 12:16:52 +02:00
|
|
|
'total_duration': 0,
|
2021-08-20 07:35:50 +02:00
|
|
|
'total_ascent': 0.0,
|
|
|
|
'total_descent': 0.0,
|
2018-06-06 12:16:52 +02:00
|
|
|
}
|
2021-01-10 11:16:43 +01:00
|
|
|
workouts_list_by_time[time_period][sport_id][
|
|
|
|
'nb_workouts'
|
2021-01-02 19:28:03 +01:00
|
|
|
] += 1
|
2021-11-24 17:42:27 +01:00
|
|
|
workouts_list_by_time[time_period][sport_id][
|
|
|
|
'average_speed'
|
|
|
|
] = get_average_speed(
|
|
|
|
workouts_list_by_time[time_period][sport_id][
|
|
|
|
'nb_workouts'
|
|
|
|
],
|
|
|
|
workouts_list_by_time[time_period][sport_id][
|
|
|
|
'average_speed'
|
|
|
|
],
|
|
|
|
workout.ave_speed,
|
|
|
|
)
|
2021-01-10 11:16:43 +01:00
|
|
|
workouts_list_by_time[time_period][sport_id][
|
2019-08-28 13:25:39 +02:00
|
|
|
'total_distance'
|
2021-01-10 11:16:43 +01:00
|
|
|
] += float(workout.distance)
|
|
|
|
workouts_list_by_time[time_period][sport_id][
|
2019-08-28 13:25:39 +02:00
|
|
|
'total_duration'
|
2021-09-01 11:19:11 +02:00
|
|
|
] += convert_timedelta_to_integer(workout.moving)
|
2021-08-20 07:35:50 +02:00
|
|
|
if workout.ascent:
|
|
|
|
workouts_list_by_time[time_period][sport_id][
|
|
|
|
'total_ascent'
|
2021-09-01 14:05:45 +02:00
|
|
|
] += float(workout.ascent)
|
2021-08-20 07:35:50 +02:00
|
|
|
if workout.descent:
|
|
|
|
workouts_list_by_time[time_period][sport_id][
|
|
|
|
'total_descent'
|
2021-09-01 14:05:45 +02:00
|
|
|
] += float(workout.descent)
|
2021-01-01 16:39:25 +01:00
|
|
|
return {
|
2018-06-06 12:09:09 +02:00
|
|
|
'status': 'success',
|
2021-01-02 19:28:03 +01:00
|
|
|
'data': {
|
2021-01-10 11:16:43 +01:00
|
|
|
'statistics': workouts_list_by_sport
|
2021-01-02 19:28:03 +01:00
|
|
|
if filter_type == 'by_sport'
|
2021-01-10 11:16:43 +01:00
|
|
|
else workouts_list_by_time
|
2021-01-02 19:28:03 +01:00
|
|
|
},
|
2018-06-06 12:09:09 +02:00
|
|
|
}
|
|
|
|
except Exception as e:
|
2021-01-01 16:39:25 +01:00
|
|
|
return handle_error_and_return_response(e)
|
2018-06-06 12:09:09 +02:00
|
|
|
|
|
|
|
|
2020-02-08 15:17:07 +01:00
|
|
|
@stats_blueprint.route('/stats/<user_name>/by_time', methods=['GET'])
|
2022-06-15 19:16:14 +02:00
|
|
|
@require_auth(scopes=['workouts:read'])
|
2021-01-10 11:16:43 +01:00
|
|
|
def get_workouts_by_time(
|
2021-12-01 19:22:47 +01:00
|
|
|
auth_user: User, user_name: str
|
2021-01-02 19:28:03 +01:00
|
|
|
) -> Union[Dict, HttpResponse]:
|
2019-07-20 21:57:35 +02:00
|
|
|
"""
|
2022-07-14 18:36:19 +02:00
|
|
|
Get workouts statistics for a user by time.
|
|
|
|
|
|
|
|
**Scope**: ``workouts:read``
|
2019-07-20 21:57:35 +02:00
|
|
|
|
|
|
|
**Example requests**:
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
- without parameters:
|
2019-07-20 21:57:35 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2020-02-08 15:17:07 +01:00
|
|
|
GET /api/stats/admin/by_time HTTP/1.1
|
2019-07-20 21:57:35 +02:00
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
- with parameters:
|
2019-07-20 21:57:35 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
GET /api/stats/admin/by_time?from=2018-01-01&to=2018-06-30&time=week
|
|
|
|
HTTP/1.1
|
2019-07-20 21:57:35 +02:00
|
|
|
|
|
|
|
**Example responses**:
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
- success:
|
2019-07-20 21:57:35 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"statistics": {
|
|
|
|
"2017": {
|
|
|
|
"3": {
|
2021-11-24 17:42:27 +01:00
|
|
|
"average_speed": 4.48,
|
2021-01-10 11:16:43 +01:00
|
|
|
"nb_workouts": 2,
|
2021-09-01 14:05:45 +02:00
|
|
|
"total_ascent": 203.0,
|
|
|
|
"total_ascent": 156.0,
|
2019-07-20 21:57:35 +02:00
|
|
|
"total_distance": 15.282,
|
|
|
|
"total_duration": 12341
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"2019": {
|
|
|
|
"1": {
|
2021-11-24 17:42:27 +01:00
|
|
|
"average_speed": 16.99,
|
2021-01-10 11:16:43 +01:00
|
|
|
"nb_workouts": 3,
|
2021-09-01 14:05:45 +02:00
|
|
|
"total_ascent": 150.0,
|
|
|
|
"total_ascent": 178.0,
|
2019-07-20 21:57:35 +02:00
|
|
|
"total_distance": 47,
|
|
|
|
"total_duration": 9960
|
|
|
|
},
|
|
|
|
"2": {
|
2021-11-24 17:42:27 +01:00
|
|
|
"average_speed": 15.95,
|
2021-01-10 11:16:43 +01:00
|
|
|
"nb_workouts": 1,
|
2021-09-01 14:05:45 +02:00
|
|
|
"total_ascent": 46.0,
|
|
|
|
"total_ascent": 78.0,
|
2019-07-20 21:57:35 +02:00
|
|
|
"total_distance": 5.613,
|
|
|
|
"total_duration": 1267
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
- no workouts:
|
2019-07-20 21:57:35 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"statistics": {}
|
|
|
|
},
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
2022-07-14 18:36:19 +02:00
|
|
|
:param integer user_name: username
|
2019-07-20 21:57:35 +02:00
|
|
|
|
|
|
|
:query string from: start date (format: ``%Y-%m-%d``)
|
|
|
|
:query string to: end date (format: ``%Y-%m-%d``)
|
|
|
|
:query string time: time frame:
|
|
|
|
|
|
|
|
- ``week``: week starting Sunday
|
|
|
|
- ``weekm``: week starting Monday
|
|
|
|
- ``month``: month
|
|
|
|
- ``year``: year (default)
|
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
:statuscode 200: ``success``
|
2019-07-20 21:57:35 +02:00
|
|
|
:statuscode 401:
|
2023-06-18 20:45:39 +02:00
|
|
|
- ``provide a valid auth token``
|
|
|
|
- ``signature expired, please log in again``
|
|
|
|
- ``invalid token, please log in again``
|
|
|
|
:statuscode 404: ``user does not exist``
|
2019-07-20 21:57:35 +02:00
|
|
|
|
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
return get_workouts(user_name, 'by_time')
|
2018-06-06 12:09:09 +02:00
|
|
|
|
|
|
|
|
2020-02-08 15:17:07 +01:00
|
|
|
@stats_blueprint.route('/stats/<user_name>/by_sport', methods=['GET'])
|
2022-06-15 19:16:14 +02:00
|
|
|
@require_auth(scopes=['workouts:read'])
|
2021-01-10 11:16:43 +01:00
|
|
|
def get_workouts_by_sport(
|
2021-12-01 19:22:47 +01:00
|
|
|
auth_user: User, user_name: str
|
2021-01-02 19:28:03 +01:00
|
|
|
) -> Union[Dict, HttpResponse]:
|
2019-07-20 21:57:35 +02:00
|
|
|
"""
|
2022-07-14 18:36:19 +02:00
|
|
|
Get workouts statistics for a user by sport.
|
|
|
|
|
|
|
|
**Scope**: ``workouts:read``
|
2019-07-20 21:57:35 +02:00
|
|
|
|
|
|
|
**Example requests**:
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
- without parameters (get stats for all sports with workouts):
|
2019-07-20 21:57:35 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2020-02-08 15:17:07 +01:00
|
|
|
GET /api/stats/admin/by_sport HTTP/1.1
|
2019-07-20 21:57:35 +02:00
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
- with sport id:
|
2019-07-20 21:57:35 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
2020-02-08 15:17:07 +01:00
|
|
|
GET /api/stats/admin/by_sport?sport_id=1 HTTP/1.1
|
2019-07-20 21:57:35 +02:00
|
|
|
|
|
|
|
**Example responses**:
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
- success:
|
2019-07-20 21:57:35 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"statistics": {
|
|
|
|
"1": {
|
2021-11-24 17:42:27 +01:00
|
|
|
"average_speed": 16.99,
|
2021-01-10 11:16:43 +01:00
|
|
|
"nb_workouts": 3,
|
2021-09-01 14:05:45 +02:00
|
|
|
"total_ascent": 150.0,
|
|
|
|
"total_ascent": 178.0,
|
2019-07-20 21:57:35 +02:00
|
|
|
"total_distance": 47,
|
|
|
|
"total_duration": 9960
|
|
|
|
},
|
|
|
|
"2": {
|
2021-11-24 17:42:27 +01:00
|
|
|
"average_speed": 15.95,
|
2021-01-10 11:16:43 +01:00
|
|
|
"nb_workouts": 1,
|
2021-09-01 14:05:45 +02:00
|
|
|
"total_ascent": 46.0,
|
|
|
|
"total_ascent": 78.0,
|
2019-07-20 21:57:35 +02:00
|
|
|
"total_distance": 5.613,
|
|
|
|
"total_duration": 1267
|
|
|
|
},
|
|
|
|
"3": {
|
2021-11-24 17:42:27 +01:00
|
|
|
"average_speed": 4.46,
|
2021-01-10 11:16:43 +01:00
|
|
|
"nb_workouts": 2,
|
2021-09-01 14:05:45 +02:00
|
|
|
"total_ascent": 203.0,
|
|
|
|
"total_ascent": 156.0,
|
2019-07-20 21:57:35 +02:00
|
|
|
"total_distance": 15.282,
|
|
|
|
"total_duration": 12341
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
- no workouts:
|
2019-07-20 21:57:35 +02:00
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"statistics": {}
|
|
|
|
},
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
2022-07-14 18:36:19 +02:00
|
|
|
:param integer user_name: username
|
2019-07-20 21:57:35 +02:00
|
|
|
|
|
|
|
:query integer sport_id: sport id
|
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
:statuscode 200: ``success``
|
2019-07-20 21:57:35 +02:00
|
|
|
:statuscode 401:
|
2023-06-18 20:45:39 +02:00
|
|
|
- ``provide a valid auth token``
|
|
|
|
- ``signature expired, please log in again``
|
|
|
|
- ``invalid token, please log in again``
|
2019-07-20 21:57:35 +02:00
|
|
|
:statuscode 404:
|
2023-06-18 20:45:39 +02:00
|
|
|
- ``user does not exist``
|
|
|
|
- ``sport does not exist``
|
2019-07-20 21:57:35 +02:00
|
|
|
|
|
|
|
"""
|
2021-01-10 11:16:43 +01:00
|
|
|
return get_workouts(user_name, 'by_sport')
|
2019-09-23 20:30:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
@stats_blueprint.route('/stats/all', methods=['GET'])
|
2022-06-15 19:16:14 +02:00
|
|
|
@require_auth(scopes=['workouts:read'], as_admin=True)
|
2021-12-01 19:22:47 +01:00
|
|
|
def get_application_stats(auth_user: User) -> Dict:
|
2019-09-23 20:30:34 +02:00
|
|
|
"""
|
2022-07-14 18:36:19 +02:00
|
|
|
Get all application statistics.
|
|
|
|
|
|
|
|
**Scope**: ``workouts:read``
|
2019-09-23 20:30:34 +02:00
|
|
|
|
|
|
|
**Example requests**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
GET /api/stats/all HTTP/1.1
|
|
|
|
|
|
|
|
|
|
|
|
**Example responses**:
|
|
|
|
|
|
|
|
.. sourcecode:: http
|
|
|
|
|
|
|
|
HTTP/1.1 200 OK
|
|
|
|
Content-Type: application/json
|
|
|
|
|
|
|
|
{
|
|
|
|
"data": {
|
|
|
|
"sports": 3,
|
2021-01-10 11:16:43 +01:00
|
|
|
"uploads_dir_size": 1000,
|
2020-05-02 18:00:17 +02:00
|
|
|
"users": 2,
|
2021-01-10 11:16:43 +01:00
|
|
|
"workouts": 3,
|
2019-09-23 20:30:34 +02:00
|
|
|
},
|
|
|
|
"status": "success"
|
|
|
|
}
|
|
|
|
|
|
|
|
:reqheader Authorization: OAuth 2.0 Bearer Token
|
|
|
|
|
2023-06-18 20:45:39 +02:00
|
|
|
:statuscode 200: ``success``
|
2019-09-23 20:30:34 +02:00
|
|
|
:statuscode 401:
|
2023-06-18 20:45:39 +02:00
|
|
|
- ``provide a valid auth token``
|
|
|
|
- ``signature expired, please log in again``
|
|
|
|
- ``invalid token, please log in again``
|
|
|
|
:statuscode 403: ``you do not have permissions``
|
2019-09-23 20:30:34 +02:00
|
|
|
"""
|
|
|
|
|
2021-01-10 11:16:43 +01:00
|
|
|
nb_workouts = Workout.query.filter().count()
|
2019-09-23 20:30:34 +02:00
|
|
|
nb_users = User.query.filter().count()
|
|
|
|
nb_sports = (
|
2021-01-10 11:16:43 +01:00
|
|
|
db.session.query(func.count(Workout.sport_id))
|
|
|
|
.group_by(Workout.sport_id)
|
2019-09-23 20:30:34 +02:00
|
|
|
.count()
|
|
|
|
)
|
2021-01-01 16:39:25 +01:00
|
|
|
return {
|
2019-09-23 20:30:34 +02:00
|
|
|
'status': 'success',
|
|
|
|
'data': {
|
2021-01-10 11:16:43 +01:00
|
|
|
'workouts': nb_workouts,
|
2019-09-23 20:30:34 +02:00
|
|
|
'sports': nb_sports,
|
|
|
|
'users': nb_users,
|
2020-05-01 20:18:19 +02:00
|
|
|
'uploads_dir_size': get_upload_dir_size(),
|
2019-09-23 20:30:34 +02:00
|
|
|
},
|
|
|
|
}
|