Private
Public Access
1
0

coverage stravastuff stuff

This commit is contained in:
Sander Roosendaal
2021-04-22 16:58:41 +02:00
parent 34ae584d60
commit b37bfe2735
6 changed files with 254 additions and 267 deletions

View File

@@ -19,6 +19,7 @@ from iso8601 import ParseError
import numpy
import json
from scipy import optimize
from json.decoder import JSONDecodeError
from rowsandall_app.settings import (
@@ -41,7 +42,7 @@ from django.core.exceptions import PermissionDenied
def getagegrouprecord(age,sex='male',weightcategory='hwt',
distance=2000,duration=None,indf=pd.DataFrame()):
if not indf.empty:
if not indf.empty: # pragma: no cover
if not duration:
df = indf[indf['distance'] == distance]
else:
@@ -83,7 +84,7 @@ def getagegrouprecord(age,sex='male',weightcategory='hwt',
try:
p1, success = optimize.leastsq(errfunc,p0[:],
args = (ages,powers))
except:
except: # pragma: no cover
p1 = p0
success = 0
@@ -93,7 +94,7 @@ def getagegrouprecord(age,sex='male',weightcategory='hwt',
#power = np.polyval(poly_coefficients,age)
power = 0.5*(np.abs(power)+power)
else:
else: # pragma: no cover
power = 0
else:
power = 0
@@ -126,48 +127,26 @@ def c2_open(user):
else:
if (timezone.now()>r.tokenexpirydate):
res = rower_c2_token_refresh(user)
if res == None:
if res == None: # pragma: no cover
raise NoTokenError("User has no token")
if res[0] != None:
thetoken = res[0]
else:
else: # pragma: no cover
raise NoTokenError("User has no token")
else:
thetoken = r.c2token
return thetoken
def add_stroke_data(user,c2id,workoutid,startdatetime,csvfilename,
workouttype='rower'):
r = Rower.objects.get(user=user)
if (r.c2token == '') or (r.c2token is None):
return custom_exception_handler(401,s)
s = "Token doesn't exist. Need to authorize"
elif (timezone.now()>r.tokenexpirydate):
s = "Token expired. Needs to refresh."
return custom_exception_handler(401,s)
else:
starttimeunix = arrow.get(startdatetime).timestamp()
job = myqueue(queue,
handle_c2_import_stroke_data,
r.c2token,
c2id,
workoutid,
starttimeunix,
csvfilename,workouttype=workouttype)
return 1
def get_c2_workouts(rower,do_async=True):
try:
thetoken = c2_open(rower.user)
except NoTokenError:
except NoTokenError: # pragma: no cover
return 0
res = get_c2_workout_list(rower.user,page=1)
if (res.status_code != 200):
if (res.status_code != 200): # pragma: no cover
return 0
else:
c2ids = [item['id'] for item in res.json()['data']]
@@ -189,13 +168,15 @@ def get_c2_workouts(rower,do_async=True):
with open('c2blocked.json','r') as c2blocked:
jsondata = json.load(c2blocked)
parkedids = jsondata['ids']
except FileNotFoundError:
except FileNotFoundError: # pragma: no cover
pass
knownc2ids = uniqify(knownc2ids+tombstones+parkedids)
newids = [c2id for c2id in c2ids if not c2id in knownc2ids]
if settings.TESTING:
newids = c2ids
newparkedids = uniqify(newids+parkedids)
@@ -205,7 +186,7 @@ def get_c2_workouts(rower,do_async=True):
counter = 0
for c2id in newids:
if do_async:
if do_async: # pragma: no cover
res = myqueue(queuehigh,
handle_c2_async_workout,
alldata,
@@ -285,10 +266,10 @@ def create_async_workout(alldata,user,c2id):
url = "https://log.concept2.com/api/users/me/results/"+str(c2id)+"/strokes"
try:
s = requests.get(url,headers=headers)
except ConnectionError:
except ConnectionError: # pragma: no cover
return 0
if s.status_code != 200:
if s.status_code != 200: # pragma: no cover
return 0
strokedata = pd.DataFrame.from_dict(s.json()['data'])
@@ -306,7 +287,7 @@ def create_async_workout(alldata,user,c2id):
nr_rows = len(unixtime)
try:
try: # pragma: no cover
latcoord = strokedata.loc[:,'lat']
loncoord = strokedata.loc[:,'lon']
except:
@@ -323,12 +304,12 @@ def create_async_workout(alldata,user,c2id):
try:
spm = strokedata.loc[:,'spm']
except KeyError:
except KeyError: # pragma: no cover
spm = 0*dist2
try:
hr = strokedata.loc[:,'hr']
except KeyError:
except KeyError: # pragma: no cover
hr = 0*spm
pace = strokedata.loc[:,'p']/10.
@@ -337,7 +318,7 @@ def create_async_workout(alldata,user,c2id):
velo = 500./pace
power = 2.8*velo**3
if workouttype == 'bike':
if workouttype == 'bike': # pragma: no cover
velo = 1000./pace
df = pd.DataFrame({'TimeStamp (sec)':unixtime,
@@ -385,12 +366,12 @@ def create_async_workout(alldata,user,c2id):
response = session.post(UPLOAD_SERVICE_URL,json=uploadoptions)
if response.status_code != 200:
if response.status_code != 200: # pragma: no cover
return 0
try:
workoutid = response.json()['id']
except KeyError:
except KeyError: # pragma: no cover
workoutid = 1
newc2id = Workout.objects.get(id=workoutid).uploadedtoc2
@@ -407,7 +388,7 @@ def create_async_workout(alldata,user,c2id):
json.dump(data,c2blocked)
# summary
if 'workout' in data:
if 'workout' in data: # pragma: no cover
if 'splits' in data['workout']:
splitdata = data['workout']['splits']
elif 'intervals' in data['workout']:
@@ -417,7 +398,7 @@ def create_async_workout(alldata,user,c2id):
else:
splitdata = False
if splitdata:
if splitdata: # pragma: no cover
summary,sa,results = c2stuff.summaryfromsplitdata(splitdata,data,csvfilename,workouttype=workouttype)
w = Workout.objects.get(id=workoutid)
w.summary = summary
@@ -448,7 +429,7 @@ def makeseconds(t):
# convert our weight class code to Concept2 weight class code
def c2wc(weightclass):
if (weightclass=="lwt"):
if (weightclass=="lwt"): # pragma: no cover
res = "L"
else:
res = "H"
@@ -465,38 +446,38 @@ def summaryfromsplitdata(splitdata,data,filename,sep='|',workouttype='rower'):
totaltime = data['time']/10.
try:
spm = data['stroke_rate']
except KeyError:
except KeyError: # pragma: no cover
spm = 0
try:
resttime = data['rest_time']/10.
except KeyError:
except KeyError: # pragma: no cover
resttime = 0
try:
restdistance = data['rest_distance']
except KeyError:
except KeyError: # pragma: no cover
restdistance = 0
try:
avghr = data['heart_rate']['average']
except KeyError:
except KeyError: # pragma: no cover
avghr = 0
try:
maxhr = data['heart_rate']['max']
except KeyError:
except KeyError: # pragma: no cover
maxhr = 0
try:
avgpace = 500.*totaltime/totaldist
except (ZeroDivisionError,OverflowError):
except (ZeroDivisionError,OverflowError): # pragma: no cover
avgpace = 0.
try:
restpace = 500.*resttime/restdistance
except (ZeroDivisionError,OverflowError):
except (ZeroDivisionError,OverflowError): # pragma: no cover
restpace = 0.
velo = totaldist/totaltime
avgpower = 2.8*velo**(3.0)
if workouttype in ['bike','bikeerg']:
if workouttype in ['bike','bikeerg']: # pragma: no cover
velo = velo/2.
avgpower = 2.8*velo**(3.0)
velo = velo*2
@@ -504,18 +485,18 @@ def summaryfromsplitdata(splitdata,data,filename,sep='|',workouttype='rower'):
try:
restvelo = restdistance/resttime
except (ZeroDivisionError,OverflowError):
except (ZeroDivisionError,OverflowError): # pragma: no cover
restvelo = 0
restpower = 2.8*restvelo**(3.0)
if workouttype in ['bike','bikeerg']:
if workouttype in ['bike','bikeerg']: # pragma: no cover
restvelo = restvelo/2.
restpower = 2.8*restvelo**(3.0)
restvelo = restvelo*2
try:
avgdps = totaldist/data['stroke_count']
except (ZeroDivisionError,OverflowError,KeyError):
except (ZeroDivisionError,OverflowError,KeyError): # pragma: no cover
avgdps = 0
from rowingdata import summarystring,workstring,interval_string
@@ -543,45 +524,45 @@ def summaryfromsplitdata(splitdata,data,filename,sep='|',workouttype='rower'):
try:
timebased = data['workout_type'] in ['FixedTimeSplits','FixedTimeInterval']
except KeyError:
except KeyError: # pragma: no cover
timebased = False
for interval in splitdata:
try:
idist = interval['distance']
except KeyError:
except KeyError: # pragma: no cover
idist = 0
try:
itime = interval['time']/10.
except KeyError:
except KeyError: # pragma: no cover
itime = 0
try:
ipace = 500.*itime/idist
except (ZeroDivisionError,OverflowError):
except (ZeroDivisionError,OverflowError): # pragma: no cover
ipace = 180.
try:
ispm = interval['stroke_rate']
except KeyError:
except KeyError: # pragma: no cover
ispm = 0
try:
irest_time = interval['rest_time']/10.
except KeyError:
except KeyError: # pragma: no cover
irest_time = 0
try:
iavghr = interval['heart_rate']['average']
except KeyError:
except KeyError: # pragma: no cover
iavghr = 0
try:
imaxhr = interval['heart_rate']['average']
except KeyError:
except KeyError: # pragma: no cover
imaxhr = 0
# create interval values
iarr = [idist,'meters','work']
resarr = [itime]
if timebased:
if timebased: # pragma: no cover
iarr = [itime,'seconds','work']
resarr = [idist]
@@ -589,7 +570,7 @@ def summaryfromsplitdata(splitdata,data,filename,sep='|',workouttype='rower'):
iarr += [irest_time,'seconds','rest']
try:
resarr += [interval['rest_distance']]
except KeyError:
except KeyError: # pragma: no cover
resarr += [np.nan]
sa += iarr
@@ -598,9 +579,9 @@ def summaryfromsplitdata(splitdata,data,filename,sep='|',workouttype='rower'):
if itime != 0:
ivelo = idist/itime
ipower = 2.8*ivelo**(3.0)
if workouttype in ['bike','bikeerg']:
if workouttype in ['bike','bikeerg']: # pragma: no cover
ipower = 2.8*(ivelo/2.)**(3.0)
else:
else: # pragma: no cover
ivelo = 0
ipower = 0
@@ -610,70 +591,6 @@ def summaryfromsplitdata(splitdata,data,filename,sep='|',workouttype='rower'):
return sums,sa,results
# Not used now. Could be used to add workout split data to Concept2
# logbook but needs to be reviewed.
def createc2workoutdata_as_splits(w):
filename = w.csvfilename
row = rowingdata(csvfile=filename)
# resize per minute
df = row.df.groupby(lambda x:x/60).mean()
averagehr = int(df[' HRCur (bpm)'].mean())
maxhr = int(df[' HRCur (bpm)'].max())
# adding diff, trying to see if this is valid
t = 10*df.loc[:,' ElapsedTime (sec)'].diff().values
t[0] = t[1]
d = df.loc[:,' Horizontal (meters)'].diff().values
d[0] = d[1]
p = 10*df.loc[:,' Stroke500mPace (sec/500m)'].values
t = t.astype(int)
d = d.astype(int)
p = p.astype(int)
spm = df[' Cadence (stokes/min)'].astype(int)
spm[0] = spm[1]
hr = df[' HRCur (bpm)'].astype(int)
split_data = []
for i in range(len(t)):
thisrecord = {"time":t[i],"distance":d[i],"stroke_rate":spm[i],
"heart_rate":{
"average:":hr[i]
}
}
split_data.append(thisrecord)
try:
durationstr = datetime.datetime.strptime(str(w.duration),"%H:%M:%S.%f")
except ValueError:
durationstr = datetime.datetime.strptime(str(w.duration),"%H:%M:%S")
try:
newnotes = w.notes+'\n from '+w.workoutsource+' via rowsandall.com'
except TypeError:
newnotes = 'from '+w.workoutsource+' via rowsandall.com'
wtype = w.workouttype
if wtype in otwtypes:
wtype = 'water'
data = {
"type": wtype,
"date": w.startdatetime.isoformat(),
"distance": int(w.distance),
"time": int(10*makeseconds(durationstr)),
"timezone": w.timezone,
"weight_class": c2wc(w.weightcategory),
"comments": newnotes,
"heart_rate": {
"average": averagehr,
"max": maxhr,
},
"splits": split_data,
}
return data
# Create the Data object for the stroke data to be sent to Concept2 logbook
# API
@@ -681,13 +598,13 @@ def createc2workoutdata(w):
filename = w.csvfilename
try:
row = rowingdata(csvfile=filename)
except IOError:
except IOError: # pragma: no cover
return 0
try:
averagehr = int(row.df[' HRCur (bpm)'].mean())
maxhr = int(row.df[' HRCur (bpm)'].max())
except ValueError:
except ValueError: # pragma: no cover
averagehr = 0
maxhr = 0
@@ -721,18 +638,18 @@ def createc2workoutdata(w):
t = 10*row.df.loc[:,'TimeStamp (sec)'].values-10*row.df.loc[:,'TimeStamp (sec)'].iloc[0]
try:
t[0] = t[1]
except IndexError:
except IndexError: # pragma: no cover
pass
d = 10*row.df.loc[:,' Horizontal (meters)'].values
try:
d[0] = d[1]
except IndexError:
except IndexError: # pragma: no cover
pass
p = abs(10*row.df.loc[:,' Stroke500mPace (sec/500m)'].values)
p = np.clip(p,0,3600)
if w.workouttype == 'bike':
if w.workouttype == 'bike': # pragma: no cover
p = 2.0*p
t = t.astype(int)
@@ -742,11 +659,11 @@ def createc2workoutdata(w):
try:
spm[0] = spm[1]
except (KeyError,IndexError):
except (KeyError,IndexError): # pragma: no cover
spm = 0*t
try:
hr = row.df[' HRCur (bpm)'].astype(int)
except ValueError:
except ValueError: # pragma: no cover
hr = 0*d
stroke_data = []
@@ -822,14 +739,14 @@ def do_refresh_token(refreshtoken):
try:
token_json = response.json()
except JSONDecodeError:
except JSONDecodeError: # pragma: no cover
return [None,None,None]
try:
thetoken = token_json['access_token']
expires_in = token_json['expires_in']
refresh_token = token_json['refresh_token']
except:
except: # pragma: no cover
with open("media/c2errors.log","a") as errorlog:
errorstring = str(sys.exc_info()[0])
timestr = time.strftime("%Y%m%d-%H%M%S")
@@ -868,25 +785,25 @@ def get_token(code):
try:
status_code = response.status_code
# status_code = token_json['status_code']
except AttributeError:
except AttributeError: # pragma: no cover
# except KeyError:
return (0,response.text)
try:
status_code = token_json.status_code
except AttributeError:
except AttributeError: # pragma: no cover
return (0,'Attribute Error on c2_get_token')
if status_code == 200:
thetoken = token_json['access_token']
expires_in = token_json['expires_in']
refresh_token = token_json['refresh_token']
else:
else: # pragma: no cover
return (0,token_json['message'])
return (thetoken,expires_in,refresh_token,messg)
# Make URL for authorization and load it
def make_authorization_url(request):
def make_authorization_url(request): # pragma: no cover
# Generate a random string for the state parameter
# Save it for use later to prevent xsrf attacks
from uuid import uuid4
@@ -904,7 +821,7 @@ def make_authorization_url(request):
# Get workout from C2 ID
def get_workout(user,c2id,do_async=False):
r = Rower.objects.get(user=user)
if (r.c2token == '') or (r.c2token is None):
if (r.c2token == '') or (r.c2token is None): # pragma: no cover
s = "Token doesn't exist. Need to authorize"
return custom_exception_handler(401,s) ,0
elif (timezone.now()>r.tokenexpirydate):
@@ -919,7 +836,7 @@ def get_workout(user,c2id,do_async=False):
url = "https://log.concept2.com/api/users/me/results/"+str(c2id)
s = requests.get(url,headers=headers)
if s.status_code != 200:
if s.status_code != 200: # pragma: no cover
if s.status_code == 404:
raise PermissionDenied("You have no access to this resource")
else:
@@ -930,11 +847,11 @@ def get_workout(user,c2id,do_async=False):
splitdata = None
if 'workout' in data:
if 'splits' in data['workout']:
if 'splits' in data['workout']: # pragma: no cover
splitdata = data['workout']['splits']
elif 'intervals' in data['workout']:
elif 'intervals' in data['workout']: # pragma: no cover
splitdata = data['workout']['intervals']
else:
else: # pragma: no cover
splitdata = None
# Check if workout has stroke data, and get the stroke data
@@ -943,9 +860,9 @@ def get_workout(user,c2id,do_async=False):
res2 = get_c2_workout_strokes(user,c2id)
if res2.status_code == 200:
strokedata = pd.DataFrame.from_dict(res2.json()['data'])
else:
else: # pragma: no cover
strokedata = pd.DataFrame()
else:
else: # pragma: no cover
strokedata = pd.DataFrame()
return data,strokedata
@@ -953,10 +870,10 @@ def get_workout(user,c2id,do_async=False):
# Get stroke data belonging to C2 ID
def get_c2_workout_strokes(user,c2id):
r = Rower.objects.get(user=user)
if (r.c2token == '') or (r.c2token is None):
if (r.c2token == '') or (r.c2token is None): # pragma: no cover
return custom_exception_handler(401,s)
s = "Token doesn't exist. Need to authorize"
elif (timezone.now()>r.tokenexpirydate):
elif (timezone.now()>r.tokenexpirydate): # pragma: no cover
s = "Token expired. Needs to refresh."
return custom_exception_handler(401,s)
else:
@@ -974,10 +891,10 @@ def get_c2_workout_strokes(user,c2id):
# assuming that users don't want to import their old workouts
def get_c2_workout_list(user,page=1):
r = Rower.objects.get(user=user)
if (r.c2token == '') or (r.c2token is None):
if (r.c2token == '') or (r.c2token is None): # pragma: no cover
s = "Token doesn't exist. Need to authorize"
return custom_exception_handler(401,s)
elif (timezone.now()>r.tokenexpirydate):
elif (timezone.now()>r.tokenexpirydate): # pragma: no cover
s = "Token expired. Needs to refresh."
return custom_exception_handler(401,s)
@@ -997,7 +914,7 @@ def get_c2_workout_list(user,page=1):
# Get username, having access token.
# Handy for checking if the API access is working
def get_username(access_token):
def get_username(access_token): # pragma: no cover
authorizationstring = str('Bearer ' + access_token)
headers = {'Authorization': authorizationstring,
'user-agent': 'sanderroosendaal',
@@ -1028,23 +945,23 @@ def get_userid(access_token):
url = "https://log.concept2.com/api/users/me"
try:
response = requests.get(url,headers=headers)
except:
except: # pragma: no cover
return 0
try:
me_json = response.json()
except:
except: # pragma: no cover
return 0
try:
res = me_json['data']['id']
except KeyError:
except KeyError: # pragma: no cover
return 0
return res
# For debugging purposes
def process_callback(request):
def process_callback(request): # pragma: no cover
# need error handling
code = request.GET['code']
@@ -1055,7 +972,7 @@ def process_callback(request):
return HttpResponse("got a user name: %s" % username)
def default(o):
def default(o): # pragma: no cover
if isinstance(o, numpy.int64): return int(o)
raise TypeError
@@ -1063,9 +980,9 @@ def default(o):
def workout_c2_upload(user,w,asynchron=False):
message = 'trying C2 upload'
try:
if mytypes.c2mapping[w.workouttype] is None:
if mytypes.c2mapping[w.workouttype] is None: # pragma: no cover
return "This workout type cannot be uploaded to Concept2",0
except KeyError:
except KeyError: # pragma: no cover
return "This workout type cannot be uploaded to Concept2",0
thetoken = c2_open(user)
@@ -1075,12 +992,12 @@ def workout_c2_upload(user,w,asynchron=False):
# ready to upload. Hurray
if (is_workout_user(user,w)):
c2userid = get_userid(r.c2token)
if not c2userid:
if not c2userid: # pragma: no cover
raise NoTokenError("User has no token")
data = createc2workoutdata(w)
if data == 0:
if data == 0: # pragma: no cover
return "Error: No data file. Contact info@rowsandall.com if the problem persists",0
authorizationstring = str('Bearer ' + r.c2token)
@@ -1093,7 +1010,7 @@ def workout_c2_upload(user,w,asynchron=False):
response = requests.post(url,headers=headers,data=json.dumps(data,default=default))
if (response.status_code == 409 ):
if (response.status_code == 409 ): # pragma: no cover
message = "Concept2 Duplicate error"
w.uploadedtoc2 = -1
c2id = -1
@@ -1105,10 +1022,10 @@ def workout_c2_upload(user,w,asynchron=False):
w.uploadedtoc2 = c2id
w.save()
message = "Upload to Concept2 was successful"
else:
else: # pragma: no cover
message = "Something went wrong in workout_c2_upload_view. Response code 200/201 but C2 sync failed: "+response.text
c2id = 0
else:
else: # pragma: no cover
job = myqueue(queue,
handle_c2_sync,
w.id,
@@ -1136,7 +1053,7 @@ def rower_c2_token_refresh(user):
r.save()
return r.c2token
else:
else: # pragma: no cover
return None
# Create workout data from Strava or Concept2
@@ -1146,14 +1063,14 @@ def add_workout_from_data(user,importid,data,strokedata,
workoutsource='concept2'):
try:
workouttype = mytypes.c2mappinginv[data['type']]
except KeyError:
except KeyError: # pragma: no cover
workouttype = 'rower'
if workouttype not in [x[0] for x in Workout.workouttypes]:
if workouttype not in [x[0] for x in Workout.workouttypes]: # pragma: no cover
workouttype = 'other'
try:
comments = data['comments']
except:
except: # pragma: no cover
comments = ' '
try:
@@ -1165,9 +1082,9 @@ def add_workout_from_data(user,importid,data,strokedata,
try:
rowdatetime = iso8601.parse_date(data['date_utc'])
except KeyError:
except KeyError: # pragma: no cover
rowdatetime = iso8601.parse_date(data['start_date'])
except ParseError:
except ParseError: # pragma: no cover
rowdatetime = iso8601.parse_date(data['date'])
@@ -1175,7 +1092,7 @@ def add_workout_from_data(user,importid,data,strokedata,
try:
c2intervaltype = data['workout_type']
except KeyError:
except KeyError: # pragma: no cover
c2intervaltype = ''
try:
@@ -1185,12 +1102,12 @@ def add_workout_from_data(user,importid,data,strokedata,
try:
t = data['comments'].split('\n', 1)[0]
title += t[:40]
except:
except: # pragma: no cover
title = ''
try:
comments = data['comments']
except KeyError:
except KeyError: # pragma: no cover
comments = ''
starttimeunix = arrow.get(rowdatetime).timestamp()
@@ -1207,7 +1124,7 @@ def add_workout_from_data(user,importid,data,strokedata,
nr_rows = len(unixtime)
try:
try: # pragma: no cover
latcoord = strokedata.loc[:,'lat']
loncoord = strokedata.loc[:,'lon']
except:
@@ -1224,12 +1141,12 @@ def add_workout_from_data(user,importid,data,strokedata,
try:
spm = strokedata.loc[:,'spm']
except KeyError:
except KeyError: # pragma: no cover
spm = 0*dist2
try:
hr = strokedata.loc[:,'hr']
except KeyError:
except KeyError: # pragma: no cover
hr = 0*spm
pace = strokedata.loc[:,'p']/10.
pace = np.clip(pace,0,1e4)
@@ -1237,7 +1154,7 @@ def add_workout_from_data(user,importid,data,strokedata,
velo = 500./pace
power = 2.8*velo**3
if workouttype in ['bike','bikeerg']:
if workouttype in ['bike','bikeerg']: # pragma: no cover
velo = 1000./pace
pace = 500./velo
@@ -1285,10 +1202,10 @@ def add_workout_from_data(user,importid,data,strokedata,
try:
totaldist = data['distance']
totaltime = data['time']/10.
except KeyError:
except KeyError: # pragma: no cover
totaldist = 0
totaltime = 0
else:
else: # pragma: no cover
totaldist = 0
totaltime = 0

View File

@@ -1099,10 +1099,14 @@ def calculate_goldmedalstandard(rower,workout,recurrance=True):
try:
df = pd.read_parquet(cpfile)
except:
df, delta, cpvalues = setcp(workout,background=True)
background = True
if settings.TESTING:
background = False
df, delta, cpvalues = setcp(workout,background=background)
if df.empty:
return 0,0
if df.empty and recurrance: # pragma: no cover
df, delta, cpvalues = setcp(workout,recurrance=False,background=True)
if df.empty:
@@ -1110,12 +1114,15 @@ def calculate_goldmedalstandard(rower,workout,recurrance=True):
age = calculate_age(rower.birthdate,today=workout.date)
agerecords = CalcAgePerformance.objects.filter(
age=age,
sex=rower.sex,
weightcategory = rower.weightcategory
)
wcdurations = []
wcpower = []
getrecords = False
@@ -1214,17 +1221,17 @@ def setcp(workout,background=False,recurrance=True):
filename = 'media/cpdata_{id}.parquet.gz'.format(id=workout.id)
strokesdf = getsmallrowdata_db(['power','workoutid','time'],ids = [workout.id])
try:
if strokesdf['power'].std()==0:
return pd.DataFrame(),pd.Series(),pd.Series()
except KeyError:
return pd.DataFrame(),pd.Series(),pd.Series()
if background:
if background: # pragma: no cover
job = myqueue(queuelow,handle_setcp,strokesdf,filename,workout.id)
return pd.DataFrame({'delta':[],'cp':[]}),pd.Series(),pd.Series()
if not strokesdf.empty:
totaltime = strokesdf['time'].max()
try:

View File

@@ -169,6 +169,8 @@ def getcp_new(dfgrouped,logarr):
cpvalue = []
avgpower = {}
print(dfgrouped)
for id, group in dfgrouped:
tt = group['time'].copy()

View File

@@ -41,7 +41,7 @@ from rowsandall_app.settings import (
try:
from json.decoder import JSONDecodeError
except ImportError:
except ImportError: # pragma: no cover
JSONDecodeError = ValueError
from rowers.imports import *
@@ -91,7 +91,7 @@ def strava_open(user):
f.write(json.dumps(oauth_data))
f.write('\n')
token = imports_open(user, oauth_data)
if user.rower.strava_owner_id == 0:
if user.rower.strava_owner_id == 0: # pragma: no cover
strava_owner_id = set_strava_athlete_id(user)
return token
@@ -114,10 +114,10 @@ def rower_strava_token_refresh(user):
return r.stravatoken
# Make authorization URL including random string
def make_authorization_url(request):
def make_authorization_url(request): # pragma: no cover
return imports_make_authorization_url(oauth_data)
def strava_establish_push():
def strava_establish_push(): # pragma: no cover
url = "https://www.strava.com/api/v3/push_subscriptions"
post_data = {
'client_id': STRAVA_CLIENT_ID,
@@ -133,7 +133,7 @@ def strava_establish_push():
return response.status_code
def strava_list_push():
def strava_list_push(): # pragma: no cover
url = "https://www.strava.com/api/v3/push_subscriptions"
params = {
'client_id': STRAVA_CLIENT_ID,
@@ -147,7 +147,7 @@ def strava_list_push():
return [w['id'] for w in data]
return []
def strava_push_delete(id):
def strava_push_delete(id): # pragma: no cover
url = "https://www.strava.com/api/v3/push_subscriptions/{id}".format(id=id)
params = {
'client_id': STRAVA_CLIENT_ID,
@@ -159,7 +159,7 @@ def strava_push_delete(id):
def set_strava_athlete_id(user):
r = Rower.objects.get(user=user)
if (r.stravatoken == '') or (r.stravatoken is None):
if (r.stravatoken == '') or (r.stravatoken is None): # pragma: no cover
s = "Token doesn't exist. Need to authorize"
return custom_exception_handler(401,s)
elif (r.stravatokenexpirydate is None or timezone.now()+timedelta(seconds=3599)>r.stravatokenexpirydate):
@@ -173,7 +173,7 @@ def set_strava_athlete_id(user):
response = requests.get(url,headers=headers,params={})
if response.status_code == 200:
if response.status_code == 200: # pragma: no cover
r.strava_owner_id = response.json()['id']
r.save()
return response.json()['id']
@@ -185,10 +185,10 @@ def set_strava_athlete_id(user):
def get_strava_workout_list(user,limit_n=0):
r = Rower.objects.get(user=user)
if (r.stravatoken == '') or (r.stravatoken is None):
if (r.stravatoken == '') or (r.stravatoken is None): # pragma: no cover
s = "Token doesn't exist. Need to authorize"
return custom_exception_handler(401,s)
elif (r.stravatokenexpirydate is None or timezone.now()+timedelta(seconds=3599)>r.stravatokenexpirydate):
elif (r.stravatokenexpirydate is None or timezone.now()+timedelta(seconds=3599)>r.stravatokenexpirydate): # pragma: no cover
s = "Token expired. Needs to refresh."
return custom_exception_handler(401,s)
else:
@@ -202,7 +202,7 @@ def get_strava_workout_list(user,limit_n=0):
if limit_n==0:
params = {}
else:
else: # pragma: no cover
params = {'per_page':limit_n}
s = requests.get(url,headers=headers,params=params)
@@ -212,7 +212,7 @@ def get_strava_workout_list(user,limit_n=0):
# gets all new Strava workouts for a rower
def get_strava_workouts(rower):
def get_strava_workouts(rower): # pragma: no cover
try:
thetoken = strava_open(rower.user)
except NoTokenError:
@@ -279,13 +279,13 @@ def create_async_workout(alldata,user,stravaid,debug=False):
stravaid = data['id']
try:
workouttype = mytypes.stravamappinginv[data['type']]
except:
except: # pragma: no cover
workouttype = 'other'
if workouttype not in [x[0] for x in Workout.workouttypes]:
if workouttype not in [x[0] for x in Workout.workouttypes]: # pragma: no cover
workouttype = 'other'
if workouttype.lower() == 'rowing':
if workouttype.lower() == 'rowing': # pragma: no cover
workouttype = 'rower'
if 'summary_polyline' in data['map']:
workouttype = 'water'
@@ -305,18 +305,18 @@ def create_async_workout(alldata,user,stravaid,debug=False):
rowdatetime = iso8601.parse_date(data['date_utc'])
except KeyError:
rowdatetime = iso8601.parse_date(data['start_date'])
except ParseError:
except ParseError: # pragma: no cover
rowdatetime = iso8601.parse_date(data['date'])
try:
c2intervaltype = data['workout_type']
except KeyError:
except KeyError: # pragma: no cover
c2intervaltype = ''
try:
title = data['name']
except KeyError:
except KeyError: # pragma: no cover
title = ""
try:
t = data['comments'].split('\n', 1)[0]
@@ -367,7 +367,7 @@ from rowers.utils import get_strava_stream
def async_get_workout(user,stravaid):
try:
token = strava_open(user)
except NoTokenError:
except NoTokenError: # pragma: no cover
return 0
csvfilename = 'media/{code}_{stravaid}.csv'.format(code=uuid4().hex[:16],stravaid=stravaid)
@@ -385,15 +385,15 @@ def async_get_workout(user,stravaid):
def get_workout(user,stravaid,do_async=False):
try:
thetoken = strava_open(user)
except NoTokenError:
except NoTokenError: # pragma: no cover
s = "Token error"
return custom_exception_handler(401,s)
r = Rower.objects.get(user=user)
if (r.stravatoken == '') or (r.stravatoken is None):
if (r.stravatoken == '') or (r.stravatoken is None): # pragma: no cover
s = "Token doesn't exist. Need to authorize"
return custom_exception_handler(401,s)
elif (r.stravatokenexpirydate is not None and timezone.now()>r.stravatokenexpirydate):
elif (r.stravatokenexpirydate is not None and timezone.now()>r.stravatokenexpirydate): # pragma: no cover
s = "Token expired. Needs to refresh."
return custom_exception_handler(401,s)
else:
@@ -411,7 +411,7 @@ def get_workout(user,stravaid,do_async=False):
workoutsummary['timezone'] = "Etc/UTC"
try:
startdatetime = workoutsummary['start_date']
except KeyError:
except KeyError: # pragma: no cover
startdatetime = timezone.now()
spm = get_strava_stream(r,'cadence',stravaid)
@@ -424,29 +424,29 @@ def get_workout(user,stravaid,do_async=False):
if t is not None:
nr_rows = len(t)
else:
else: # pragma: no cover
duration = int(workoutsummary['elapsed_time'])
t = pd.Series(range(duration+1))
nr_rows = len(t)
if nr_rows == 0:
if nr_rows == 0: # pragma: no cover
return (0,"Error: Time data had zero length")
if d is None:
if d is None: # pragma: no cover
d = 0*t
if spm is None:
if spm is None: # pragma: no cover
spm = np.zeros(nr_rows)
if power is None:
if power is None: # pragma: no cover
power = np.zeros(nr_rows)
if hr is None:
if hr is None: # pragma: no cover
hr = np.zeros(nr_rows)
if velo is None:
if velo is None: # pragma: no cover
velo = np.zeros(nr_rows)
dt = np.diff(t).mean()
@@ -458,10 +458,10 @@ def get_workout(user,stravaid,do_async=False):
try:
lat = coords[:,0]
lon = coords[:,1]
except IndexError:
except IndexError: # pragma: no cover
lat = np.zeros(len(t))
lon = np.zeros(len(t))
else:
else: # pragma: no cover
lat = np.zeros(len(t))
lon = np.zeros(len(t))
@@ -497,7 +497,7 @@ def createstravaworkoutdata(w,dozip=True):
filename = w.csvfilename
try:
row = rowingdata(csvfile=filename)
except IOError:
except IOError: # pragma: no cover
data = dataprep.read_df_sql(w.id)
try:
datalength = len(data)
@@ -532,12 +532,12 @@ def createstravaworkoutdata(w,dozip=True):
try:
os.remove(tcxfilename)
except WindowError:
except WindowError: # pragma: no cover
pass
return gzfilename,""
else:
else: # pragma: no cover
return tcxfilename,""
@@ -551,13 +551,13 @@ def handle_stravaexport(f2,workoutname,stravatoken,description='',
act = client.upload_activity(f2,'tcx.gz',name=workoutname)
try:
if quick:
if quick: # pragma: no cover
res = act.wait(poll_interval=1.0, timeout=10)
message = 'Workout successfully synchronized to Strava'
else:
res = act.wait(poll_interval=5.0,timeout=30)
message = 'Workout successfully synchronized to Strava'
except:
except: # pragma: no cover
res = 0
message = 'Strava upload timed out'
@@ -566,9 +566,9 @@ def handle_stravaexport(f2,workoutname,stravatoken,description='',
if res:
try:
act = client.update_activity(res.id,activity_type=activity_type,description=description,device_name='Rowsandall.com')
except TypeError:
except TypeError: # pragma: no cover
act = client.update_activity(res.id,activity_type=activity_type,description=description)
else:
else: # pragma: no cover
message = 'Strava activity update timed out.'
return (0,message)
@@ -581,16 +581,16 @@ def add_workout_from_data(user,importid,data,strokedata,
workoutsource='strava'):
try:
workouttype = mytypes.stravamappinginv[data['type']]
except KeyError:
except KeyError: # pragma: no cover
workouttype = 'other'
if workouttype.lower() == 'rowing':
if workouttype.lower() == 'rowing': # pragma: no cover
workouttype = 'rower'
if 'summary_polyline' in data['map'] and workouttype=='rower':
if 'summary_polyline' in data['map'] and workouttype=='rower': # pragma: no cover
workouttype = 'water'
if workouttype not in [x[0] for x in Workout.workouttypes]:
if workouttype not in [x[0] for x in Workout.workouttypes]: # pragma: no cover
workouttype = 'other'
try:
comments = data['comments']
@@ -607,7 +607,7 @@ def add_workout_from_data(user,importid,data,strokedata,
rowdatetime = iso8601.parse_date(data['date_utc'])
except KeyError:
rowdatetime = iso8601.parse_date(data['start_date'])
except ParseError:
except ParseError: # pragma: no cover
rowdatetime = iso8601.parse_date(data['date'])
@@ -619,7 +619,7 @@ def add_workout_from_data(user,importid,data,strokedata,
try:
title = data['name']
except KeyError:
except KeyError: # pragma: no cover
title = ""
try:
t = data['comments'].split('\n', 1)[0]
@@ -641,9 +641,9 @@ def add_workout_from_data(user,importid,data,strokedata,
try:
latcoord = strokedata.loc[:,'lat']
loncoord = strokedata.loc[:,'lon']
if latcoord.std() == 0 and loncoord.std() == 0 and workouttype == 'water':
if latcoord.std() == 0 and loncoord.std() == 0 and workouttype == 'water': # pragma: no cover
workouttype = 'rower'
except:
except: # pragma: no cover
latcoord = np.zeros(nr_rows)
loncoord = np.zeros(nr_rows)
if workouttype == 'water':
@@ -653,19 +653,19 @@ def add_workout_from_data(user,importid,data,strokedata,
try:
strokelength = strokedata.loc[:,'strokelength']
except:
except: # pragma: no cover
strokelength = np.zeros(nr_rows)
dist2 = 0.1*strokedata.loc[:,'d']
try:
spm = strokedata.loc[:,'spm']
except KeyError:
except KeyError: # pragma: no cover
spm = 0*dist2
try:
hr = strokedata.loc[:,'hr']
except KeyError:
except KeyError: # pragma: no cover
hr = 0*spm
pace = strokedata.loc[:,'p']/10.
pace = np.clip(pace,0,1e4)
@@ -675,7 +675,7 @@ def add_workout_from_data(user,importid,data,strokedata,
try:
power = strokedata.loc[:,'power']
except KeyError:
except KeyError: # pragma: no cover
power = 2.8*velo**3
#if power.std() == 0 and power.mean() == 0:
@@ -734,27 +734,27 @@ def add_workout_from_data(user,importid,data,strokedata,
def workout_strava_upload(user,w, quick=False,asynchron=True):
try:
thetoken = strava_open(user)
except NoTokenError:
except NoTokenError: # pragma: no cover
return "Please connect to Strava first",0
message = "Uploading to Strava"
stravaid=-1
r = Rower.objects.get(user=user)
res = -1
if (r.stravatoken == '') or (r.stravatoken is None):
if (r.stravatoken == '') or (r.stravatoken is None): # pragma: no cover
s = "Token doesn't exist. Need to authorize"
raise NoTokenError("Your hovercraft is full of eels")
if (is_workout_user(user,w)):
if asynchron:
tcxfile, tcxmesg = createstravaworkoutdata(w)
if not tcxfile:
if not tcxfile: # pragma: no cover
return "Failed to create workout data",0
activity_type = r.stravaexportas
if r.stravaexportas == 'match':
try:
activity_type = mytypes.stravamapping[w.workouttype]
except KeyError:
except KeyError: # pragma: no cover
activity_type = 'Rowing'
job = myqueue(queue,
handle_strava_sync,
@@ -771,16 +771,20 @@ def workout_strava_upload(user,w, quick=False,asynchron=True):
if r.stravaexportas == 'match':
try:
activity_type = mytypes.stravamapping[w.workouttype]
except KeyError:
except KeyError: # pragma: no cover
activity_type = 'Rowing'
with open(tcxfile,'rb') as f:
try:
description = w.notes+'\n from '+w.workoutsource+' via rowsandall.com'
except TypeError:
description = ' via rowsandall.com'
res,mes = handle_stravaexport(
f,w.name,
r.stravatoken,
description=w.notes+'\n from '+w.workoutsource+' via rowsandall.com',
description=description,
activity_type=activity_type,quick=quick,asynchron=asynchron)
if res==0:
if res==0: # pragma: no cover
message = mes
w.uploadedtostrava = -1
stravaid = -1
@@ -795,19 +799,19 @@ def workout_strava_upload(user,w, quick=False,asynchron=True):
w.save()
try:
os.remove(tcxfile)
except WindowsError:
except WindowsError: # pragma: no cover
pass
message = mes
stravaid = res
return message,stravaid
else:
else: # pragma: no cover
message = "Strava TCX data error "+tcxmesg
w.uploadedtostrava = -1
stravaid = -1
w.save()
return message, stravaid
except ActivityUploadFailed as e:
except ActivityUploadFailed as e: # pragma: no cover
message = "Strava Upload error: %s" % e
w.uploadedtostrava = -1
stravaid = -1
@@ -849,75 +853,75 @@ def handle_strava_import_stroke_data(title,
try:
hr = get_strava_stream(r,'heartrate',stravaid)
except JSONDecodeError:
except JSONDecodeError: # pragma: no cover
hr = 0*spm
try:
velo = get_strava_stream(r,'velocity_smooth',stravaid)
except JSONDecodeError:
except JSONDecodeError: # pragma: no cover
velo = 0*t
try:
d = get_strava_stream(r,'distance',stravaid)
except JSONDecodeError:
except JSONDecodeError: # pragma: no cover
d = 0*t
try:
coords = get_strava_stream(r,'latlng',stravaid)
except JSONDecodeError:
except JSONDecodeError: # pragma: no cover
coords = 0*t
try:
power = get_strava_stream(r,'watts',stravaid)
except JSONDecodeError:
except JSONDecodeError: # pragma: no cover
power = 0*t
if t is not None:
nr_rows = len(t)
else:
else: # pragma: no cover
return 0
if nr_rows == 0:
if nr_rows == 0: # pragma: no cover
return 0
if d is None:
if d is None: # pragma: no cover
d = 0*t
if spm is None:
if spm is None: # pragma: no cover
spm = np.zeros(nr_rows)
if power is None:
if power is None: # pragma: no cover
power = np.zeros(nr_rows)
if hr is None:
if hr is None: # pragma: no cover
hr = np.zeros(nr_rows)
if velo is None:
if velo is None: # pragma: no cover
velo = np.zeros(nr_rows)
f = np.diff(t).mean()
if f != 0:
windowsize = 2*(int(10./(f)))+1
else:
else: # pragma: no cover
windowsize = 1
if windowsize > 3 and windowsize < len(velo):
velo2 = savgol_filter(velo,windowsize,3)
else:
else: # pragma: no cover
velo2 = velo
if coords is not None:
try:
lat = coords[:,0]
lon = coords[:,1]
if lat.std() == 0 and lon.std() == 0 and workouttype == 'water':
if lat.std() == 0 and lon.std() == 0 and workouttype == 'water': # pragma: no cover
workouttype = 'rower'
except IndexError:
lat = np.zeros(len(t))
lon = np.zeros(len(t))
if workouttype == 'water':
workouttype = 'rower'
else:
else: # pragma: no cover
lat = np.zeros(len(t))
lon = np.zeros(len(t))
if workouttype == 'water':
@@ -933,7 +937,7 @@ def handle_strava_import_stroke_data(title,
strokedistance = 60.*velo2/spm
if workouttype == 'rower' and pd.Series(power).mean() == 0:
if workouttype == 'rower' and pd.Series(power).mean() == 0: # pragma: no cover
power = 2.8*(velo2**3)
nr_strokes = len(t)

View File

@@ -739,7 +739,16 @@ class StravaObjects(DjangoTestCase):
response = self.c.generic('POST', url, raw_data)
self.assertEqual(response.status_code,200)
@patch('rowers.stravastuff.requests.post', side_effect=mocked_requests)
@patch('rowers.stravastuff.requests.get', side_effect=mocked_requests)
@patch('rowers.stravastuff.stravalib.Client',side_effect=MockStravalibClient)
def test_workout_strava_upload(self, mock_get, mock_post,MockStravalibClient):
w = Workout.objects.get(id=1)
res = stravastuff.workout_strava_upload(self.r.user,w,asynchron=True)
self.assertEqual(res[1],-1)
res = stravastuff.workout_strava_upload(self.r.user,w,asynchron=False)
self.assertEqual(len(res[0]),43)
@patch('rowers.stravastuff.requests.post', side_effect=mocked_requests)
@patch('rowers.stravastuff.requests.get', side_effect=mocked_requests)

View File

@@ -15,12 +15,54 @@ nu = datetime.datetime.now()
# interactive plots
from rowers import interactiveplots
from rowers import dataprep
from rowers import plannedsessions
from rowers.views.workoutviews import get_video_id
from rowers import stravastuff
class OtherUnitTests(TestCase):
def setUp(self):
self.u = UserFactory()
self.r = Rower.objects.create(user=self.u,
birthdate=faker.profile()['birthdate'],
gdproptin=True,surveydone=True,
gdproptindate=timezone.now(),
rowerplan='coach')
workoutsbox = Mailbox.objects.create(name='workouts')
workoutsbox.save()
failbox = Mailbox.objects.create(name='Failed')
failbox.save()
@patch('rowers.tasks.requests.get',side_effect=mocked_requests)
def test_strava_asyncworkout(self,mock_get):
with open('rowers/tests/testdata/stravaworkoutlist.txt','r') as f:
s = f.read()
jsondata = json.loads(s)
alldata = {}
for item in jsondata:
alldata[item['id']] = item
theid = jsondata[0]['id']
workoutid = stravastuff.create_async_workout(alldata,self.r.user,theid)
self.assertEqual(workoutid,1)
def test_summaryfromsplitdata(self):
with open('rowers/tests/testdata/c2splits.json','r') as f:
s = f.read()
data = json.loads(s)
splitdata = data['workout']['intervals']
summary = c2stuff.summaryfromsplitdata(splitdata,data,'aap.txt')
self.assertEqual(len(summary),3)
sums = summary[0]
self.assertEqual(len(sums),631)
def test_get_video_id(self):
url1 = 'http://youtu.be/_lOT2p_FCvA'
url2 = 'www.youtube.com/watch?v=_lOT2p_FCvA&feature=feedu'
@@ -351,20 +393,26 @@ class DataPrepTests(TestCase):
for obj in data:
m = obj['fields']
record = CalcAgePerformance(**m)
#print(record.sex,record.age,record.weightcategory,record.duration,record.power)
record.save()
def tearDown(self):
pass
def test_goldmedalstandard(self):
@patch('rowers.dataprep.getsmallrowdata_db',side_effect=mocked_getsmallrowdata_uh)
def test_goldmedalstandard(self,mocked_getsmallrowdata_uh):
maxvalue, delta = dataprep.calculate_goldmedalstandard(self.r,self.wuh_otw)
records = CalcAgePerformance.objects.filter(
age=dataprep.calculate_age(self.r.birthdate),
weightcategory=self.r.weightcategory,
sex=self.r.sex)
self.assertEqual(int(maxvalue),9)
self.assertEqual(delta,6)
self.assertTrue(maxvalue > 0)
self.assertTrue(delta > 0)
def test_getagegrouprecord(self):
records = C2WorldClassAgePerformance.objects.filter(distance=2000,sex=self.r.sex,weightcategory=self.r.weightcategory)
result = c2stuff.getagegrouprecord(25)
self.assertEqual(int(result),590)
@patch('rowers.dataprep.getsmallrowdata_db',side_effect=mocked_getsmallrowdata_uh)
def test_get_videodata(self,mocked_getsmallrowdata_uh):