Private
Public Access
1
0

mailprocessing using dataprep (not tested)

This commit is contained in:
Sander Roosendaal
2017-02-11 17:46:15 +01:00
parent 8d9a8b0d2b
commit aeef0e3a2c
3 changed files with 92 additions and 199 deletions

View File

@@ -327,54 +327,7 @@ def save_workout_database(f2,r,dosmooth=True,workouttype='rower',
return (w.id,message) return (w.id,message)
# Create new workout from file and store it in the database def handle_nonpainsled(f2,fileformat,summary=''):
# This routine should be used everywhere in views.py and mailprocessing.py
# Currently there is code duplication
def new_workout_from_file(r,f2,
workouttype='rower',
title='Workout',
notes=''):
message = None
fileformat = get_file_type(f2)
summary = ''
if len(fileformat)==3 and fileformat[0]=='zip':
f_to_be_deleted = f2
with zipfile.ZipFile(f2) as z:
# for now, we're getting only the first file
# from the NK zip file (issue #69 on bitbucket)
f2 = z.extract(z.namelist()[0],path='media/')
fileformat = fileformat[2]
os.remove(f_to_be_deleted)
# Some people try to upload Concept2 logbook summaries
if fileformat == 'c2log':
os.remove(f2)
message = "This C2 logbook summary does not contain stroke data. Please download the Export Stroke Data file from the workout details on the C2 logbook."
return (0,message)
# Some people try to upload RowPro summary logs
if fileformat == 'rowprolog':
os.remove(f2)
message = "This RowPro logbook summary does not contain stroke data. Please use the Stroke Data CSV file for the individual workout in your log."
return (0,message)
# Sometimes people try an unsupported file type.
# Send an email to info@rowsandall.com with the file attached
# for me to check if it is a bug, or a new file type
# worth supporting
if fileformat == 'unknown':
message = "We couldn't recognize the file type"
if settings.DEBUG:
res = handle_sendemail_unrecognized.delay(f2,
request.user.email)
else:
res = queuehigh.enqueue(handle_sendemail_unrecognized,
f2,request.user.email)
return (0,'message')
# handle non-Painsled by converting it to painsled compatible CSV
if (fileformat != 'csv'):
# handle RowPro: # handle RowPro:
if (fileformat == 'rp'): if (fileformat == 'rp'):
row = RowProParser(f2) row = RowProParser(f2)
@@ -446,6 +399,58 @@ def new_workout_from_file(r,f2,
except: except:
os.remove(f_to_be_deleted+'.gz') os.remove(f_to_be_deleted+'.gz')
return (f2,summary)
# Create new workout from file and store it in the database
# This routine should be used everywhere in views.py and mailprocessing.py
# Currently there is code duplication
def new_workout_from_file(r,f2,
workouttype='rower',
title='Workout',
notes=''):
message = None
fileformat = get_file_type(f2)
summary = ''
if len(fileformat)==3 and fileformat[0]=='zip':
f_to_be_deleted = f2
with zipfile.ZipFile(f2) as z:
# for now, we're getting only the first file
# from the NK zip file (issue #69 on bitbucket)
f2 = z.extract(z.namelist()[0],path='media/')
fileformat = fileformat[2]
os.remove(f_to_be_deleted)
# Some people try to upload Concept2 logbook summaries
if fileformat == 'c2log':
os.remove(f2)
message = "This C2 logbook summary does not contain stroke data. Please download the Export Stroke Data file from the workout details on the C2 logbook."
return (0,message)
# Some people try to upload RowPro summary logs
if fileformat == 'rowprolog':
os.remove(f2)
message = "This RowPro logbook summary does not contain stroke data. Please use the Stroke Data CSV file for the individual workout in your log."
return (0,message)
# Sometimes people try an unsupported file type.
# Send an email to info@rowsandall.com with the file attached
# for me to check if it is a bug, or a new file type
# worth supporting
if fileformat == 'unknown':
message = "We couldn't recognize the file type"
if settings.DEBUG:
res = handle_sendemail_unrecognized.delay(f2,
request.user.email)
else:
res = queuehigh.enqueue(handle_sendemail_unrecognized,
f2,request.user.email)
return (0,'message')
# handle non-Painsled by converting it to painsled compatible CSV
if (fileformat != 'csv'):
f2,summary = handle_nonpainsled(f2,fileformat,summary=summary)
dosummary = (fileformat != 'fit') dosummary = (fileformat != 'fit')
id,message = save_workout_database(f2,r, id,message = save_workout_database(f2,r,

View File

@@ -159,15 +159,19 @@ def processattachments_debug():
# Need to move the code to a subroutine used both in views.py and here # Need to move the code to a subroutine used both in views.py and here
def make_new_workout_from_email(rr,f2,name,cntr=0): def make_new_workout_from_email(rr,f2,name,cntr=0):
workouttype = 'rower' workouttype = 'rower'
try:
f2 = f2.name f2 = f2.name
fileformat = get_file_type('media/'+f2) fileformat = get_file_type('media/'+f2)
except IOError:
f2 = f2.name+'.gz'
fileformat = get_file_type('media/'+f2)
if len(fileformat)==3 and fileformat[0]=='zip': if len(fileformat)==3 and fileformat[0]=='zip':
f_to_be_deleted = f2 f_to_be_deleted = f2
with zipfile.ZipFile('media/'+f2) as z: with zipfile.ZipFile('media/'+f2) as z:
f2 = z.extract(z.namelist()[0],path='media/')[6:] f2 = z.extract(z.namelist()[0],path='media/')[6:]
fileformat = fileformat[2] fileformat = fileformat[2]
print f2
if fileformat == 'unknown': if fileformat == 'unknown':
if settings.DEBUG: if settings.DEBUG:
@@ -182,64 +186,13 @@ def make_new_workout_from_email(rr,f2,name,cntr=0):
summary = '' summary = ''
# handle non-Painsled # handle non-Painsled
if (fileformat != 'csv'): f3,summary = dataprep.handle_nonpainsled('media/'+f2,fileformat,summary)
# handle RowPro:
if (fileformat == 'rp'):
row = RowProParser('media/'+f2)
# handle TCX
if (fileformat == 'tcx'):
row = TCXParser('media/'+f2)
# handle Mystery
if (fileformat == 'mystery'):
row = MysteryParser('media/'+f2)
# handle TCX no HR
if (fileformat == 'tcxnohr'):
row = TCXParserNoHR('media/'+f2)
# handle ErgData
if (fileformat == 'ergdata'):
row = ErgDataParser('media/'+f2)
# handle BoatCoach
if (fileformat == 'boatcoach'):
row = BoatCoachParser('media/'+f2)
# handle painsled desktop
if (fileformat == 'painsleddesktop'):
row = painsledDesktopParser('media/'+f2)
# handle speed coach GPS
if (fileformat == 'speedcoach'):
row = speedcoachParser('media/'+f2)
# handle speed coach GPS 2
if (fileformat == 'speedcoach2'):
row = SpeedCoach2Parser('media/'+f2)
# handle ErgStick
if (fileformat == 'ergstick'):
row = ErgStickParser('media/'+f2)
# handle FIT
if (fileformat == 'fit'):
row = FITParser('media/'+f2)
s = fitsummarydata('media/'+f2)
s.setsummary()
summary = s.summarytext
timestr = time.strftime("%Y%m%d-%H%M%S")
filename = timestr+str(cntr)+'o.csv'
row.write_csv('media/'+filename,gzip=True)
f2 = filename
# make workout and put in database # make workout and put in database
#r = rrower(hrmax=rr.max,hrut2=rr.ut2, #r = rrower(hrmax=rr.max,hrut2=rr.ut2,
# hrut1=rr.ut1,hrat=rr.at, # hrut1=rr.ut1,hrat=rr.at,
# hrtr=rr.tr,hran=rr.an,ftp=r.ftp) # hrtr=rr.tr,hran=rr.an,ftp=r.ftp)
row = rdata('media/'+f2) #,rower=r) row = rdata(f3) #,rower=r)
if row == 0: if row == 0:
return 0 return 0
@@ -248,83 +201,21 @@ def make_new_workout_from_email(rr,f2,name,cntr=0):
timestr = time.strftime("%Y%m%d-%H%M%S") timestr = time.strftime("%Y%m%d-%H%M%S")
f2 = 'media/'+timestr+str(cntr)+'o.csv' f2 = 'media/'+timestr+str(cntr)+'o.csv'
# auto smoothing
pace = row.df[' Stroke500mPace (sec/500m)'].values
velo = 500./pace
f = row.df['TimeStamp (sec)'].diff().mean()
windowsize = 2*(int(10./(f)))+1
if not 'originalvelo' in row.df:
row.df['originalvelo'] = velo
if windowsize > 3:
velo2 = savgol_filter(velo,windowsize,3)
else:
velo2 = velo
pace2 = 500./abs(velo2)
row.df[' Stroke500mPace (sec/500m)'] = pace2
row.df = row.df.fillna(0)
row.write_csv(f2,gzip=True) row.write_csv(f2,gzip=True)
dosummary = (fileformat != 'fit')
# recalculate power data
if workouttype == 'rower' or workouttype == 'dynamic' or workouttype == 'slides':
try:
row.erg_recalculatepower()
# row.spm_fromtimestamps()
row.write_csv(f2,gzip=True)
except:
pass
if fileformat != 'fit':
summary = row.summary()
summary += '\n'
summary += row.intervalstats_painsled()
averagehr = row.df[' HRCur (bpm)'].mean()
maxhr = row.df[' HRCur (bpm)'].max()
totaldist = row.df['cum_dist'].max()
totaltime = row.df['TimeStamp (sec)'].max()-row.df['TimeStamp (sec)'].min()
totaltime = totaltime+row.df.ix[0,' ElapsedTime (sec)']
hours = int(totaltime/3600.)
minutes = int((totaltime - 3600.*hours)/60.)
seconds = int(totaltime - 3600.*hours - 60.*minutes)
tenths = int(10*(totaltime - 3600.*hours - 60.*minutes - seconds))
duration = "%s:%s:%s.%s" % (hours,minutes,seconds,tenths)
workoutdate = row.rowdatetime.strftime('%Y-%m-%d')
workoutstarttime = row.rowdatetime.strftime('%H:%M:%S')
notes = 'imported through email'
if name == '': if name == '':
name = 'imported through email' name = 'imported through email'
w = Workout(user=rr,name=name,date=workoutdate, id,message = dataprep.save_workout_database(f2,rr,
workouttype=workouttype, workouttype=workouttype,
duration=duration,distance=totaldist, dosummary=dosummary,
weightcategory=rr.weightcategory, title=name,
starttime=workoutstarttime, notes='imported through email')
csvfilename=f2,notes=notes,summary=summary,
maxhr=maxhr,averagehr=averagehr,
startdatetime=row.rowdatetime)
w.save()
# put stroke data in database
res = dataprep.dataprep(row.df,id=w.id,
bands=True,barchart=True,
otwpower=True,empower=True)
return w.id return id

View File

@@ -22,10 +22,7 @@ from rowsandall_app.settings import BASE_DIR
from rowingdata import rower as rrower from rowingdata import rower as rrower
from rowingdata import main as rmain from rowingdata import main as rmain
from rowingdata import rowingdata as rrdata from rowingdata import rowingdata as rrdata
from rowingdata import TCXParser,RowProParser,ErgDataParser,TCXParserNoHR
from rowingdata import MysteryParser
from rowingdata import painsledDesktopParser,speedcoachParser,ErgStickParser
from rowingdata import SpeedCoach2Parser,FITParser,fitsummarydata
from rowingdata import make_cumvalues from rowingdata import make_cumvalues
from rowingdata import summarydata,get_file_type from rowingdata import summarydata,get_file_type