[API] statistics - add average speed

This commit is contained in:
Sam 2021-11-24 17:42:27 +01:00
parent bdf556bf27
commit fb127b22c4
6 changed files with 134 additions and 2 deletions

View File

@ -157,6 +157,7 @@
<span class="nt">&quot;statistics&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="nt">&quot;2017&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="nt">&quot;3&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="nt">&quot;average_speed&quot;</span><span class="p">:</span> <span class="mf">4.48</span><span class="p">,</span>
<span class="nt">&quot;nb_workouts&quot;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span>
<span class="nt">&quot;total_ascent&quot;</span><span class="p">:</span> <span class="mf">203.0</span><span class="p">,</span>
<span class="nt">&quot;total_ascent&quot;</span><span class="p">:</span> <span class="mf">156.0</span><span class="p">,</span>
@ -166,6 +167,7 @@
<span class="p">},</span>
<span class="nt">&quot;2019&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="nt">&quot;1&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="nt">&quot;average_speed&quot;</span><span class="p">:</span> <span class="mf">16.99</span><span class="p">,</span>
<span class="nt">&quot;nb_workouts&quot;</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
<span class="nt">&quot;total_ascent&quot;</span><span class="p">:</span> <span class="mf">150.0</span><span class="p">,</span>
<span class="nt">&quot;total_ascent&quot;</span><span class="p">:</span> <span class="mf">178.0</span><span class="p">,</span>
@ -173,6 +175,7 @@
<span class="nt">&quot;total_duration&quot;</span><span class="p">:</span> <span class="mi">9960</span>
<span class="p">},</span>
<span class="nt">&quot;2&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="nt">&quot;average_speed&quot;</span><span class="p">:</span> <span class="mf">15.95</span><span class="p">,</span>
<span class="nt">&quot;nb_workouts&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
<span class="nt">&quot;total_ascent&quot;</span><span class="p">:</span> <span class="mf">46.0</span><span class="p">,</span>
<span class="nt">&quot;total_ascent&quot;</span><span class="p">:</span> <span class="mf">78.0</span><span class="p">,</span>
@ -272,6 +275,7 @@
<span class="nt">&quot;data&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="nt">&quot;statistics&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="nt">&quot;1&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="nt">&quot;average_speed&quot;</span><span class="p">:</span> <span class="mf">16.99</span><span class="p">,</span>
<span class="nt">&quot;nb_workouts&quot;</span><span class="p">:</span> <span class="mi">3</span><span class="p">,</span>
<span class="nt">&quot;total_ascent&quot;</span><span class="p">:</span> <span class="mf">150.0</span><span class="p">,</span>
<span class="nt">&quot;total_ascent&quot;</span><span class="p">:</span> <span class="mf">178.0</span><span class="p">,</span>
@ -279,6 +283,7 @@
<span class="nt">&quot;total_duration&quot;</span><span class="p">:</span> <span class="mi">9960</span>
<span class="p">},</span>
<span class="nt">&quot;2&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="nt">&quot;average_speed&quot;</span><span class="p">:</span> <span class="mf">15.95</span><span class="p">,</span>
<span class="nt">&quot;nb_workouts&quot;</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span>
<span class="nt">&quot;total_ascent&quot;</span><span class="p">:</span> <span class="mf">46.0</span><span class="p">,</span>
<span class="nt">&quot;total_ascent&quot;</span><span class="p">:</span> <span class="mf">78.0</span><span class="p">,</span>
@ -286,6 +291,7 @@
<span class="nt">&quot;total_duration&quot;</span><span class="p">:</span> <span class="mi">1267</span>
<span class="p">},</span>
<span class="nt">&quot;3&quot;</span><span class="p">:</span> <span class="p">{</span>
<span class="nt">&quot;average_speed&quot;</span><span class="p">:</span> <span class="mf">4.46</span><span class="p">,</span>
<span class="nt">&quot;nb_workouts&quot;</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span>
<span class="nt">&quot;total_ascent&quot;</span><span class="p">:</span> <span class="mf">203.0</span><span class="p">,</span>
<span class="nt">&quot;total_ascent&quot;</span><span class="p">:</span> <span class="mf">156.0</span><span class="p">,</span>

File diff suppressed because one or more lines are too long

View File

@ -109,6 +109,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
assert data['data']['statistics'] == {
'2017': {
'1': {
'average_speed': 14.0,
'nb_workouts': 2,
'total_ascent': 220.0,
'total_descent': 280.0,
@ -118,6 +119,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2018': {
'1': {
'average_speed': 18.79,
'nb_workouts': 5,
'total_ascent': 340.0,
'total_descent': 500.0,
@ -125,6 +127,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
'total_duration': 11624,
},
'2': {
'average_speed': 7.2,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -156,6 +159,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
assert data['data']['statistics'] == {
'2018': {
'1': {
'average_speed': 4.8,
'nb_workouts': 1,
'total_ascent': 40.0,
'total_descent': 20.0,
@ -163,6 +167,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
'total_duration': 6000,
},
'2': {
'average_speed': 7.2,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -195,6 +200,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
assert data['data']['statistics'] == {
'2018': {
'1': {
'average_speed': 4.8,
'nb_workouts': 1,
'total_ascent': 40.0,
'total_descent': 20.0,
@ -202,6 +208,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
'total_duration': 6000,
},
'2': {
'average_speed': 7.2,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -233,6 +240,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
assert data['data']['statistics'] == {
'2017': {
'1': {
'average_speed': 14.0,
'nb_workouts': 2,
'total_ascent': 220.0,
'total_descent': 280.0,
@ -242,6 +250,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2018': {
'1': {
'average_speed': 18.79,
'nb_workouts': 5,
'total_ascent': 340.0,
'total_descent': 500.0,
@ -249,6 +258,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
'total_duration': 11624,
},
'2': {
'average_speed': 7.2,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -280,6 +290,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
assert data['data']['statistics'] == {
'2018': {
'1': {
'average_speed': 4.8,
'nb_workouts': 1,
'total_ascent': 40.0,
'total_descent': 20.0,
@ -287,6 +298,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
'total_duration': 6000,
},
'2': {
'average_speed': 7.2,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -319,6 +331,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
assert data['data']['statistics'] == {
'2018': {
'1': {
'average_speed': 4.8,
'nb_workouts': 1,
'total_ascent': 40.0,
'total_descent': 20.0,
@ -326,6 +339,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
'total_duration': 6000,
},
'2': {
'average_speed': 7.2,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -357,6 +371,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
assert data['data']['statistics'] == {
'2017-03': {
'1': {
'average_speed': 17.58,
'nb_workouts': 1,
'total_ascent': 120.0,
'total_descent': 200.0,
@ -366,6 +381,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2017-06': {
'1': {
'average_speed': 10.42,
'nb_workouts': 1,
'total_ascent': 100.0,
'total_descent': 80.0,
@ -375,6 +391,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2018-01': {
'1': {
'average_speed': 35.16,
'nb_workouts': 1,
'total_ascent': 80.0,
'total_descent': 100.0,
@ -384,6 +401,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2018-02': {
'1': {
'average_speed': 21.0,
'nb_workouts': 2,
'total_ascent': 220.0,
'total_descent': 380.0,
@ -393,6 +411,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2018-04': {
'1': {
'average_speed': 4.8,
'nb_workouts': 1,
'total_ascent': 40.0,
'total_descent': 20.0,
@ -400,6 +419,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
'total_duration': 6000,
},
'2': {
'average_speed': 7.2,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -409,6 +429,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2018-05': {
'1': {
'average_speed': 12.0,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -440,6 +461,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
assert data['data']['statistics'] == {
'2017-03': {
'1': {
'average_speed': 17.58,
'nb_workouts': 1,
'total_ascent': 120.0,
'total_descent': 200.0,
@ -449,6 +471,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2017-06': {
'1': {
'average_speed': 10.42,
'nb_workouts': 1,
'total_ascent': 100.0,
'total_descent': 80.0,
@ -458,6 +481,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2018-01': {
'1': {
'average_speed': 35.16,
'nb_workouts': 1,
'total_ascent': 80.0,
'total_descent': 100.0,
@ -467,6 +491,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2018-02': {
'1': {
'average_speed': 21.0,
'nb_workouts': 2,
'total_ascent': 220.0,
'total_descent': 380.0,
@ -476,6 +501,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2018-04': {
'1': {
'average_speed': 4.8,
'nb_workouts': 1,
'total_ascent': 40.0,
'total_descent': 20.0,
@ -483,6 +509,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
'total_duration': 6000,
},
'2': {
'average_speed': 7.2,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -492,6 +519,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2018-05': {
'1': {
'average_speed': 12.0,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -523,6 +551,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
assert data['data']['statistics'] == {
'2018-04': {
'1': {
'average_speed': 4.8,
'nb_workouts': 1,
'total_ascent': 40.0,
'total_descent': 20.0,
@ -530,6 +559,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
'total_duration': 6000,
},
'2': {
'average_speed': 7.2,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -561,6 +591,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
assert data['data']['statistics'] == {
'2017-03-19': {
'1': {
'average_speed': 17.58,
'nb_workouts': 1,
'total_ascent': 120.0,
'total_descent': 200.0,
@ -570,6 +601,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2017-05-28': {
'1': {
'average_speed': 10.42,
'nb_workouts': 1,
'total_ascent': 100.0,
'total_descent': 80.0,
@ -579,6 +611,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2017-12-31': {
'1': {
'average_speed': 35.16,
'nb_workouts': 1,
'total_ascent': 80.0,
'total_descent': 100.0,
@ -588,6 +621,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2018-02-18': {
'1': {
'average_speed': 21.0,
'nb_workouts': 2,
'total_ascent': 220.0,
'total_descent': 380.0,
@ -597,6 +631,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2018-04-01': {
'1': {
'average_speed': 4.8,
'nb_workouts': 1,
'total_ascent': 40.0,
'total_descent': 20.0,
@ -604,6 +639,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
'total_duration': 6000,
},
'2': {
'average_speed': 7.2,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -613,6 +649,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2018-05-06': {
'1': {
'average_speed': 12.0,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -644,6 +681,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
assert data['data']['statistics'] == {
'2018-04-01': {
'1': {
'average_speed': 4.8,
'nb_workouts': 1,
'total_ascent': 40.0,
'total_descent': 20.0,
@ -651,6 +689,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
'total_duration': 6000,
},
'2': {
'average_speed': 7.2,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -682,6 +721,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
assert data['data']['statistics'] == {
'2017-03-20': {
'1': {
'average_speed': 17.58,
'nb_workouts': 1,
'total_ascent': 120.0,
'total_descent': 200.0,
@ -691,6 +731,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2017-05-29': {
'1': {
'average_speed': 10.42,
'nb_workouts': 1,
'total_ascent': 100.0,
'total_descent': 80.0,
@ -700,6 +741,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2018-01-01': {
'1': {
'average_speed': 35.16,
'nb_workouts': 1,
'total_ascent': 80.0,
'total_descent': 100.0,
@ -709,6 +751,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2018-02-19': {
'1': {
'average_speed': 21.0,
'nb_workouts': 2,
'total_ascent': 220.0,
'total_descent': 380.0,
@ -718,6 +761,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2018-03-26': {
'1': {
'average_speed': 4.8,
'nb_workouts': 1,
'total_ascent': 40.0,
'total_descent': 20.0,
@ -725,6 +769,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
'total_duration': 6000,
},
'2': {
'average_speed': 7.2,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -734,6 +779,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
},
'2018-05-07': {
'1': {
'average_speed': 12.0,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -765,6 +811,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
assert data['data']['statistics'] == {
'2018-03-26': {
'1': {
'average_speed': 4.8,
'nb_workouts': 1,
'total_ascent': 40.0,
'total_descent': 20.0,
@ -772,6 +819,7 @@ class TestGetStatsByTime(ApiTestCaseMixin):
'total_duration': 6000,
},
'2': {
'average_speed': 7.2,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -804,6 +852,7 @@ class TestGetStatsBySport(ApiTestCaseMixin):
assert 'success' in data['status']
assert data['data']['statistics'] == {
'1': {
'average_speed': 17.42,
'nb_workouts': 7,
'total_ascent': 560.0,
'total_descent': 780.0,
@ -811,6 +860,7 @@ class TestGetStatsBySport(ApiTestCaseMixin):
'total_duration': 16104,
},
'2': {
'average_speed': 7.2,
'nb_workouts': 1,
'total_ascent': 0.0,
'total_descent': 0.0,
@ -840,6 +890,7 @@ class TestGetStatsBySport(ApiTestCaseMixin):
assert 'success' in data['status']
assert data['data']['statistics'] == {
'1': {
'average_speed': 17.42,
'nb_workouts': 7,
'total_ascent': 560.0,
'total_descent': 780.0,

View File

@ -0,0 +1,32 @@
from statistics import mean
from typing import List
import pytest
from fittrackee.workouts.utils import get_average_speed
class TestWorkoutAverageSpeed:
@pytest.mark.parametrize(
'ave_speeds_list',
[
([0]),
([10]),
([0, 0]),
([10, 20]),
([10, 0, 20, 10]),
([1.5, 2, 3.7, 4.2]),
],
)
def test_it_calculates_average_speed(self, ave_speeds_list: List) -> None:
nb_workouts = len(ave_speeds_list)
total_average_speed = (
sum(ave_speeds_list[:-1]) / (len(ave_speeds_list) - 1)
if len(ave_speeds_list) > 1
else ave_speeds_list[0]
)
workout_average_speed = ave_speeds_list[-1]
assert get_average_speed(
nb_workouts, total_average_speed, workout_average_speed
) == mean(ave_speeds_list)

View File

@ -16,7 +16,11 @@ from fittrackee.users.decorators import authenticate, authenticate_as_admin
from fittrackee.users.models import User
from .models import Sport, Workout
from .utils import get_datetime_from_request_args, get_upload_dir_size
from .utils import (
get_average_speed,
get_datetime_from_request_args,
get_upload_dir_size,
)
from .utils_format import convert_timedelta_to_integer
stats_blueprint = Blueprint('stats', __name__)
@ -64,6 +68,7 @@ def get_workouts(
sport_id = workout.sport_id
if sport_id not in workouts_list_by_sport:
workouts_list_by_sport[sport_id] = {
'average_speed': 0.0,
'nb_workouts': 0,
'total_distance': 0.0,
'total_duration': 0,
@ -71,6 +76,13 @@ def get_workouts(
'total_descent': 0.0,
}
workouts_list_by_sport[sport_id]['nb_workouts'] += 1
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,
)
workouts_list_by_sport[sport_id]['total_distance'] += float(
workout.distance
)
@ -117,6 +129,7 @@ def get_workouts(
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] = {
'average_speed': 0.0,
'nb_workouts': 0,
'total_distance': 0.0,
'total_duration': 0,
@ -126,6 +139,17 @@ def get_workouts(
workouts_list_by_time[time_period][sport_id][
'nb_workouts'
] += 1
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,
)
workouts_list_by_time[time_period][sport_id][
'total_distance'
] += float(workout.distance)
@ -189,6 +213,7 @@ def get_workouts_by_time(
"statistics": {
"2017": {
"3": {
"average_speed": 4.48,
"nb_workouts": 2,
"total_ascent": 203.0,
"total_ascent": 156.0,
@ -198,6 +223,7 @@ def get_workouts_by_time(
},
"2019": {
"1": {
"average_speed": 16.99,
"nb_workouts": 3,
"total_ascent": 150.0,
"total_ascent": 178.0,
@ -205,6 +231,7 @@ def get_workouts_by_time(
"total_duration": 9960
},
"2": {
"average_speed": 15.95,
"nb_workouts": 1,
"total_ascent": 46.0,
"total_ascent": 78.0,
@ -292,6 +319,7 @@ def get_workouts_by_sport(
"data": {
"statistics": {
"1": {
"average_speed": 16.99,
"nb_workouts": 3,
"total_ascent": 150.0,
"total_ascent": 178.0,
@ -299,6 +327,7 @@ def get_workouts_by_sport(
"total_duration": 9960
},
"2": {
"average_speed": 15.95,
"nb_workouts": 1,
"total_ascent": 46.0,
"total_ascent": 78.0,
@ -306,6 +335,7 @@ def get_workouts_by_sport(
"total_duration": 1267
},
"3": {
"average_speed": 4.46,
"nb_workouts": 2,
"total_ascent": 203.0,
"total_ascent": 156.0,

View File

@ -448,3 +448,16 @@ def get_upload_dir_size() -> int:
fp = os.path.join(dir_path, f)
total_size += os.path.getsize(fp)
return total_size
def get_average_speed(
nb_workouts: int, total_average_speed: float, workout_average_speed: float
) -> float:
return round(
(
(total_average_speed * (nb_workouts - 1))
+ float(workout_average_speed)
)
/ nb_workouts,
2,
)