Private
Public Access
1
0

adding data export

This commit is contained in:
Sander Roosendaal
2022-11-16 18:01:28 +01:00
parent 03c91df45b
commit 6325d3887e
2 changed files with 87 additions and 1 deletions

View File

@@ -1382,7 +1382,6 @@ class AnalysisChoiceForm(forms.Form):
cpoverlay = forms.BooleanField(initial=False,
label='Overlay Gold Medal Performance',
required=False)
piece = forms.IntegerField(initial=4, label='Ranking Piece (minutes)',
required=False)
@@ -1393,6 +1392,9 @@ class AnalysisChoiceForm(forms.Form):
trendline = forms.BooleanField(initial=False, required=False,
label='Trend Line')
savedata = forms.BooleanField(initial=False, required=False,
label='Export data as CSV')
def __init__(self, *args, **kwargs):
super(AnalysisChoiceForm, self).__init__(*args, **kwargs)

View File

@@ -4,6 +4,7 @@ from rowers.views.statements import *
from jinja2 import Environment, FileSystemLoader
from rowers.rower_rules import can_view_session
from django.forms.widgets import SelectDateWidget, HiddenInput
def floatformat(x, prec=2): # pragma: no cover
return '{x}'.format(x=round(x, prec))
@@ -267,6 +268,37 @@ def analysis_new(request,
request.session['enddate'] = enddatestring
request.session['options'] = options
savedata = options.get('savedata',False)
if savedata:
df = pd.DataFrame()
function = options.get('function','')
if function == 'boxplot':
df = boxplotdata(workouts, options)
elif function == 'trendflex': # pragma: no cover
df = trendflexdata(workouts, options, userid=userid)
elif function == 'histo': # pragma: no cover
df = histodata(workouts, options)
elif function == 'flexall': # pragma: no cover
df = flexalldata(workouts, options)
elif function == 'compare': # pragma: no cover
df = comparisondata(workouts, options)
elif function == 'cp': # pragma: no cover
df = cpdata(workouts, options)
options['savedata'] = False
request.session['options'] = options
response = HttpResponse(df.to_csv())
code = str(uuid4())
filename = code+'.csv'
chartform.fields['savedata'].initial = False
response['Content-Disposition'] = 'attachment; filename="%s"' % filename
response['Content-Type'] = 'application/octet-stream'
return response
if not request.user.is_superuser:
chartform.fields['savedata'].widget = HiddenInput()
chartform.fields['savedata'].initial = False
breadcrumbs = [
{
'url': '/rowers/analysis',
@@ -314,6 +346,7 @@ def trendflexdata(workouts, options, userid=0):
ploterrorbars = options['ploterrorbars']
ids = options['ids']
workstrokesonly = not includereststrokes
savedata = options['savedata']
fieldlist, fielddict = dataprep.getstatsfields()
@@ -466,6 +499,9 @@ def trendflexdata(workouts, options, userid=0):
u = User.objects.get(id=userid)
extratitle = ' '+u.first_name+' '+u.last_name
savedata = options.get('savedata',False)
if savedata:
return df
script, div = interactive_multiflex(df, xparam, yparam,
groupby,
@@ -500,6 +536,19 @@ def flexalldata(workouts, options):
u = User.objects.get(id=userid)
extratitle = ' '+u.first_name+' '+u.last_name
savedata = options.get('savedata',False)
if savedata:
workstrokesonly = not includereststrokes
columns = [xparam, yparam1, yparam2, 'spm', 'driveenergy', 'distance']
ids = [int(w.id) for w in workouts]
df = dataprep.getsmallrowdata_db(columns, ids=ids,
workstrokesonly=workstrokesonly,
doclean=True,
)
return df
res = interactive_cum_flex_chart2(workouts, xparam=xparam,
yparam1=yparam1,
yparam2=yparam2,
@@ -532,6 +581,16 @@ def histodata(workouts, options):
u = User.objects.get(id=userid)
extratitle = ' '+u.first_name+' '+u.last_name
savedata = options.get('savedata',False)
if savedata:
workstrokesonly = not includereststrokes
ids = [int(w.id) for w in workouts]
df = dataprep.getsmallrowdata_db([plotfield], ids=ids,
workstrokesonly=workstrokesonly,
doclean=True,
)
return df
script, div = interactive_histoall(workouts, plotfield, includereststrokes,
spmmin=spmmin, spmmax=spmmax,
@@ -562,9 +621,13 @@ def cpdata(workouts, options):
'url': urls,
})
if options['savedata']:
return powerdf
if powerdf.empty: # pragma: no cover
return('', '<p>No valid data found</p>')
powerdf = powerdf[powerdf['CP'] > 0]
powerdf.dropna(axis=0, inplace=True)
powerdf.sort_values(['Delta', 'CP'], ascending=[1, 0], inplace=True)
@@ -786,6 +849,22 @@ def comparisondata(workouts, options):
int(w.id): w.__str__() for w in workouts
}
savedata = options.get('savedata',False)
if savedata:
workstrokesonly = not includereststrokes
columns = [xparam, yparam1,
'ftime', 'distance', 'fpace',
'power', 'hr', 'spm',
'time', 'pace', 'workoutstate',
'workoutid']
df = dataprep.getsmallrowdata_db(columns, ids=ids,
workstrokesonly=workstrokesonly,
doclean=True,
)
return df
res = interactive_multiple_compare_chart(ids, xparam, yparam1,
promember=promember,
plottype=plottype,
@@ -852,6 +931,10 @@ def boxplotdata(workouts, options):
u = User.objects.get(id=userid)
extratitle = ' '+u.first_name+' '+u.last_name
savedata = options.get('savedata',False)
if savedata:
return datadf
script, div = interactive_boxchart(datadf, plotfield,
extratitle=extratitle,
spmmin=spmmin, spmmax=spmmax,
@@ -908,6 +991,7 @@ def analysis_view_data(request, userid=0):
except Workout.DoesNotExist: # pragma: no cover
pass
if function == 'boxplot':
script, div = boxplotdata(workouts, options)
elif function == 'trendflex': # pragma: no cover