slightly working prototype
only boxplot implemented, doesn't change y param
This commit is contained in:
@@ -5,6 +5,341 @@ from __future__ import unicode_literals
|
||||
from __future__ import unicode_literals, absolute_import
|
||||
from rowers.views.statements import *
|
||||
|
||||
# generic Analysis view -
|
||||
|
||||
defaultoptions = {
|
||||
'includereststrokes': False,
|
||||
'workouttypes':['rower','dynamic','slides'],
|
||||
'waterboattype': mytypes.waterboattype,
|
||||
'rankingonly': False,
|
||||
'function':'boxplot'
|
||||
}
|
||||
|
||||
|
||||
@user_passes_test(ispromember, login_url="/rowers/paidplans",
|
||||
message="This functionality requires a Pro plan or higher",
|
||||
redirect_field_name=None)
|
||||
def analysis_new(request,userid=0,function='boxplot'):
|
||||
r = getrequestrower(request, userid=userid)
|
||||
user = r.user
|
||||
userid = user.id
|
||||
|
||||
if 'options' in request.session:
|
||||
options = request.session['options']
|
||||
else:
|
||||
options=defaultoptions
|
||||
|
||||
try:
|
||||
workouttypes = options['workouttypes']
|
||||
except KeyError:
|
||||
workouttypes = ['rower','dynamic','slides']
|
||||
|
||||
try:
|
||||
rankingonly = options['rankingonly']
|
||||
except KeyError:
|
||||
rankingonly = False
|
||||
|
||||
try:
|
||||
includereststrokes = options['includereststrokes']
|
||||
except KeyError:
|
||||
includereststrokes = False
|
||||
|
||||
if 'startdate' in request.session:
|
||||
startdate = iso8601.parse_date(request.session['startdate'])
|
||||
|
||||
|
||||
if 'enddate' in request.session:
|
||||
enddate = iso8601.parse_date(request.session['enddate'])
|
||||
|
||||
workstrokesonly = not includereststrokes
|
||||
|
||||
waterboattype = mytypes.waterboattype
|
||||
|
||||
if request.method == 'POST':
|
||||
dateform = DateRangeForm(request.POST)
|
||||
if dateform.is_valid():
|
||||
startdate = dateform.cleaned_data['startdate']
|
||||
enddate = dateform.cleaned_data['enddate']
|
||||
startdatestring = startdate.strftime('%Y-%m-%d')
|
||||
enddatestring = enddate.strftime('%Y-%m-%d')
|
||||
request.session['startdate'] = startdatestring
|
||||
request.session['enddate'] = enddatestring
|
||||
optionsform = AnalysisOptionsForm(request.POST)
|
||||
if optionsform.is_valid():
|
||||
for key, value in optionsform.cleaned_data.items():
|
||||
options[key] = value
|
||||
|
||||
modality = optionsform.cleaned_data['modality']
|
||||
waterboattype = optionsform.cleaned_data['waterboattype']
|
||||
if modality == 'all':
|
||||
modalities = [m[0] for m in mytypes.workouttypes]
|
||||
else:
|
||||
modalities = [modality]
|
||||
if modality != 'water':
|
||||
waterboattype = [b[0] for b in mytypes.boattypes]
|
||||
|
||||
|
||||
if 'rankingonly' in optionsform.cleaned_data:
|
||||
rankingonly = optionsform.cleaned_data['rankingonly']
|
||||
else:
|
||||
rankingonly = False
|
||||
|
||||
options['modalities'] = modalities
|
||||
options['waterboattype'] = waterboattype
|
||||
|
||||
chartform = AnalysisChoiceForm(request.POST)
|
||||
if chartform.is_valid():
|
||||
for key, value in chartform.cleaned_data.items():
|
||||
options[key] = value
|
||||
|
||||
form = WorkoutMultipleCompareForm(request.POST)
|
||||
if form.is_valid():
|
||||
cd = form.cleaned_data
|
||||
selectedworkouts = cd['workouts']
|
||||
ids = [int(w.id) for w in selectedworkouts]
|
||||
options['ids'] = ids
|
||||
else:
|
||||
ids = []
|
||||
options['ids'] = ids
|
||||
else:
|
||||
dateform = DateRangeForm(initial={
|
||||
'startdate':startdate,
|
||||
'enddate':enddate,
|
||||
})
|
||||
|
||||
if 'modalities' in request.session:
|
||||
modalities = request.session['modalities']
|
||||
if len(modalities) > 1:
|
||||
modality = 'all'
|
||||
else:
|
||||
modality = modalities[0]
|
||||
else:
|
||||
modalities = [m[0] for m in mytypes.workouttypes]
|
||||
modality = 'all'
|
||||
|
||||
|
||||
|
||||
|
||||
negtypes = []
|
||||
for b in mytypes.boattypes:
|
||||
if b[0] not in waterboattype:
|
||||
negtypes.append(b[0])
|
||||
|
||||
|
||||
startdate = datetime.datetime.combine(startdate,datetime.time())
|
||||
enddate = datetime.datetime.combine(enddate,datetime.time(23,59,59))
|
||||
|
||||
if enddate < startdate:
|
||||
s = enddate
|
||||
enddate = startdate
|
||||
startdate = s
|
||||
|
||||
negtypes = []
|
||||
for b in mytypes.boattypes:
|
||||
if b[0] not in waterboattype:
|
||||
negtypes.append(b[0])
|
||||
|
||||
|
||||
workouts = Workout.objects.filter(user=r,
|
||||
startdatetime__gte=startdate,
|
||||
startdatetime__lte=enddate,
|
||||
workouttype__in=modalities,
|
||||
).order_by(
|
||||
"-date", "-starttime"
|
||||
).exclude(boattype__in=negtypes)
|
||||
if rankingonly:
|
||||
workouts = workouts.exclude(rankingpiece=False)
|
||||
|
||||
query = request.GET.get('q')
|
||||
if query:
|
||||
query_list = query.split()
|
||||
workouts = workouts.filter(
|
||||
reduce(operator.and_,
|
||||
(Q(name__icontains=q) for q in query_list)) |
|
||||
reduce(operator.and_,
|
||||
(Q(notes__icontains=q) for q in query_list))
|
||||
)
|
||||
searchform = SearchForm(initial={'q':query})
|
||||
else:
|
||||
searchform = SearchForm()
|
||||
|
||||
if request.method != 'POST':
|
||||
form = WorkoutMultipleCompareForm()
|
||||
chartform = AnalysisChoiceForm()
|
||||
selectedworkouts = Workout.objects.none()
|
||||
else:
|
||||
selectedworkouts = Workout.objects.filter(id__in=ids)
|
||||
|
||||
form.fields["workouts"].queryset = workouts | selectedworkouts
|
||||
|
||||
|
||||
optionsform = AnalysisOptionsForm(initial={
|
||||
'modality':modality,
|
||||
'waterboattype':waterboattype,
|
||||
'rankingonly':rankingonly,
|
||||
})
|
||||
|
||||
|
||||
|
||||
startdatestring = startdate.strftime('%Y-%m-%d')
|
||||
enddatestring = enddate.strftime('%Y-%m-%d')
|
||||
request.session['startdate'] = startdatestring
|
||||
request.session['enddate'] = enddatestring
|
||||
request.session['options'] = options
|
||||
|
||||
|
||||
breadcrumbs = [
|
||||
{
|
||||
'url':'/rowers/analysis',
|
||||
'name':'Analysis'
|
||||
},
|
||||
{
|
||||
'url':reverse('analysis_new',kwargs={'userid':userid}),
|
||||
'name': 'Analysis Select'
|
||||
},
|
||||
]
|
||||
return render(request, 'user_analysis_select.html',
|
||||
{'workouts': workouts,
|
||||
'dateform':dateform,
|
||||
'startdate':startdate,
|
||||
'enddate':enddate,
|
||||
'rower':r,
|
||||
'breadcrumbs':breadcrumbs,
|
||||
'theuser':user,
|
||||
'form':form,
|
||||
'active':'nav-analysis',
|
||||
'chartform':chartform,
|
||||
'searchform':searchform,
|
||||
'optionsform':optionsform,
|
||||
'teams':get_my_teams(request.user),
|
||||
})
|
||||
|
||||
|
||||
def boxplotdata(workouts,options):
|
||||
try:
|
||||
includereststrokes = options['includereststrokes']
|
||||
spmmin = options['spmmin']
|
||||
spmmax = options['spmmax']
|
||||
workmin = options['workmin']
|
||||
workmax = options['workmax']
|
||||
ids = options['ids']
|
||||
userid = options['userid']
|
||||
plotfield = options['plotfield']
|
||||
function = options['function']
|
||||
except KeyError:
|
||||
includereststrokes = False
|
||||
spmmin = 15
|
||||
spmmax = 55
|
||||
workmin = 0
|
||||
workmax = 55
|
||||
ids = []
|
||||
userid = 0
|
||||
plotfield = 'spm'
|
||||
function = 'boxplot'
|
||||
|
||||
|
||||
workstrokesonly = not includereststrokes
|
||||
labeldict = {
|
||||
int(w.id): w.__str__() for w in workouts
|
||||
}
|
||||
|
||||
|
||||
datemapping = {
|
||||
w.id:w.date for w in workouts
|
||||
}
|
||||
|
||||
|
||||
|
||||
fieldlist,fielddict = dataprep.getstatsfields()
|
||||
fieldlist = [plotfield,'workoutid','spm','driveenergy',
|
||||
'workoutstate']
|
||||
|
||||
ids = [w.id for w in workouts]
|
||||
|
||||
# prepare data frame
|
||||
datadf,extracols = dataprep.read_cols_df_sql(ids,fieldlist)
|
||||
|
||||
|
||||
|
||||
datadf = dataprep.clean_df_stats(datadf,workstrokesonly=workstrokesonly)
|
||||
|
||||
datadf = dataprep.filter_df(datadf,'spm',spmmin,
|
||||
largerthan=True)
|
||||
datadf = dataprep.filter_df(datadf,'spm',spmmax,
|
||||
largerthan=False)
|
||||
datadf = dataprep.filter_df(datadf,'driveenergy',workmin,
|
||||
largerthan=True)
|
||||
datadf = dataprep.filter_df(datadf,'driveneergy',workmax,
|
||||
largerthan=False)
|
||||
|
||||
datadf.dropna(axis=0,how='any',inplace=True)
|
||||
|
||||
|
||||
datadf['workoutid'].replace(datemapping,inplace=True)
|
||||
datadf.rename(columns={"workoutid":"date"},inplace=True)
|
||||
datadf = datadf.sort_values(['date'])
|
||||
|
||||
if userid == 0:
|
||||
extratitle = ''
|
||||
else:
|
||||
u = User.objects.get(id=userid)
|
||||
extratitle = ' '+u.first_name+' '+u.last_name
|
||||
|
||||
|
||||
|
||||
script,div = interactive_boxchart(datadf,plotfield,
|
||||
extratitle=extratitle,
|
||||
spmmin=spmmin,spmmax=spmmax,workmin=workmin,workmax=workmax)
|
||||
|
||||
scripta = script.split('\n')[2:-1]
|
||||
script = ''.join(scripta)
|
||||
|
||||
return(script,div)
|
||||
|
||||
@user_passes_test(ispromember,login_url="/rowers/paidplans",
|
||||
message="This functionality requires a Pro plan or higher",
|
||||
redirect_field_name=None)
|
||||
def analysis_view_data(request,userid=0):
|
||||
|
||||
if 'options' in request.session:
|
||||
options = request.session['options']
|
||||
else:
|
||||
options = defaultoptions
|
||||
|
||||
|
||||
if userid==0:
|
||||
userid = request.user.id
|
||||
|
||||
workouts = []
|
||||
|
||||
ids = options['ids']
|
||||
function = options['function']
|
||||
|
||||
if not ids:
|
||||
return JSONResponse({
|
||||
"script":'',
|
||||
"div":'No data found'
|
||||
})
|
||||
|
||||
for id in ids:
|
||||
try:
|
||||
workouts.append(Workout.objects.get(id=id))
|
||||
except Workout.DoesNotExist:
|
||||
pass
|
||||
|
||||
if function == 'boxplot':
|
||||
script, div = boxplotdata(workouts,options)
|
||||
else:
|
||||
script = ''
|
||||
div = 'Unknown analysis functions'
|
||||
|
||||
|
||||
return JSONResponse({
|
||||
"script":script,
|
||||
"div":div,
|
||||
})
|
||||
|
||||
|
||||
# Histogram for a date/time range
|
||||
@user_passes_test(ispromember,login_url="/rowers/paidplans",
|
||||
message="This functionality requires a Pro plan or higher",
|
||||
@@ -2769,7 +3104,7 @@ def multiflex_view(request,userid=0,
|
||||
options['spmmax'] = spmmax
|
||||
options['workmin'] = workmin
|
||||
options['workmax'] = workmax
|
||||
options['ids'] = ids
|
||||
options['idso'] = ids
|
||||
|
||||
|
||||
request.session['options'] = options
|
||||
|
||||
Reference in New Issue
Block a user