Private
Public Access
1
0

Merge branch 'feature/activitychart' into develop

This commit is contained in:
Sander Roosendaal
2020-05-10 17:56:48 +02:00
3 changed files with 73 additions and 17 deletions

View File

@@ -26,7 +26,7 @@ from bokeh.models import CustomJS,Slider, TextInput,BoxAnnotation
from bokeh.resources import CDN,INLINE
from bokeh.embed import components
from bokeh.layouts import layout,widgetbox
from bokeh.palettes import Category20c
from bokeh.palettes import Category20c,Category10
from bokeh.layouts import row as layoutrow
from bokeh.layouts import column as layoutcolumn
from bokeh.models import LinearAxis,LogAxis,Range1d,DatetimeTickFormatter,HoverTool
@@ -178,7 +178,7 @@ def interactive_hr_piechart(df,rower,title,totalseconds=0):
if df.empty:
return "","Not enough data to make a chart"
df.sort_values(by='hr',inplace=True)
df['timehr'] = df['deltat']*df['hr']
@@ -278,7 +278,7 @@ def interactive_workouttype_piechart(workouts):
data = pd.Series(datadict).reset_index(name='value').rename(columns={'index':'type'})
data['angle'] = data['value']/data['value'].sum() * 2*pi
if len(datadict)>2:
data['color'] = Category20c[len(datadict)]
data['color'] = Category10[len(datadict)]
elif len(datadict)==2:
data['color'] = ['orange','lightblue']
else:
@@ -401,7 +401,7 @@ def interactive_planchart(data,startdate,enddate):
return script,div
def interactive_activitychart(workouts,startdate,enddate,stack='type'):
def interactive_activitychart(workouts,startdate,enddate,stack='type',toolbar_location=None):
dates = []
dates_sorting = []
@@ -414,6 +414,13 @@ def interactive_activitychart(workouts,startdate,enddate,stack='type'):
seen = ['seen']
idseen = []
startdate = datetime.datetime(year=startdate.year,month=startdate.month,day=startdate.day)
enddate = datetime.datetime(year=enddate.year,month=enddate.month,day=enddate.day)
duration = enddate-startdate
totaldays = duration.total_seconds()/(24*3600)
for w in workouts:
aantal=1
@@ -432,9 +439,15 @@ def interactive_activitychart(workouts,startdate,enddate,stack='type'):
for w in workouts:
dd = w.date.strftime('%m/%d')
dd2 = w.date.strftime('%Y/%m/%d')
dd3 = w.date.strftime('%Y/%m')
du = w.duration.hour*60+w.duration.minute
dates.append(dd)
dates_sorting.append(dd2)
if totaldays<30:
dates.append(dd)
dates_sorting.append(dd2)
else:
dates.append(dd3)
dates_sorting.append(dd3)
durations.append(du)
links.append(
"{siteurl}/rowers/workout/{code}/".format(
@@ -451,21 +464,25 @@ def interactive_activitychart(workouts,startdate,enddate,stack='type'):
try:
d = utc.localize(startdate)
except ValueError:
except (ValueError,AttributeError):
d = startdate
try:
enddate = utc.localize(enddate)
except ValueError:
except (ValueError,AttributeError):
pass
# add dates with no activity
while d<=enddate:
dd = d.strftime('%d')
dates.append(d.strftime('%m/%d'))
dates_sorting.append(d.strftime('%Y/%m/%d'))
if totaldays<30:
dates.append(d.strftime('%m/%d'))
dates_sorting.append(d.strftime('%Y/%m/%d'))
else:
dates.append(d.strftime('%Y/%m'))
dates_sorting.append(d.strftime('%Y/%m'))
durations.append(0)
links.append('')
types.append('rower')
@@ -489,6 +506,9 @@ def interactive_activitychart(workouts,startdate,enddate,stack='type'):
'link':links,
}
types_ind = list(set(types))
df = pd.DataFrame(thedict)
source = ColumnDataSource(df)
@@ -521,7 +541,7 @@ def interactive_activitychart(workouts,startdate,enddate,stack='type'):
p.plot_width=550
p.plot_height=350
p.toolbar_location = 'above'
p.toolbar_location = toolbar_location
p.sizing_mode = 'scale_width'
url = "http://rowsandall.com/rowers/workout/@duration/"
taptool = p.select(type=TapTool)

View File

@@ -23,11 +23,14 @@
<div id="t_script">
{{ tscript|safe}}
</div>
</div>
<div id="activities_script">
</div>
<ul class="main-content">
<li class="grid_2">
<h1>History for {{ rower.user.first_name }} {{ rower.user.last_name }}</h1>
<h1>History for {{ rower.user.first_name }} {{ rower.user.last_name }} from {{ startdate }} to {{ enddate }}</h1>
</li>
<li class="grid_2">
<a href="/rowers/history/?enddate={{ today|date:"Y-m-d" }}&startdate={{ lastseven|date:"Y-m-d" }}&workouttype={{ workouttype }}">Past 7 days</a>
@@ -81,6 +84,10 @@
</table>
</p>
</li>
<li class="grid_2">
<div id="id_activities">
</div>
</li>
<li class="grid_2">
{{ tdiv|safe }}
</li>
@@ -136,18 +143,23 @@
<script>
$(function($) {
console.log('loading script for chart');
$.getJSON(window.location.protocol + '//'+window.location.host + '/rowers/history/user/{{ rower.user.id }}/data/?startdate={{ startdate|date:"Y-m-d" }}&{{ enddate|date:"Y-m-d" }}&workouttype={{ workouttype }}', function(json) {
var ed = '{{ senddate|date:"Y-m-d" }}'
console.log('End',ed);
console.log(window.location.protocol + '//'+window.location.host + '/rowers/history/user/{{ rower.user.id }}/data/?startdate={{ sstartdate|date:"Y-m-d" }}&enddate={{ senddate|date:"Y-m-d" }}&workouttype={{ workouttype }}')
$.getJSON(window.location.protocol + '//'+window.location.host + '/rowers/history/user/{{ rower.user.id }}/data/?startdate={{ sstartdate|date:"Y-m-d" }}&enddate={{ senddate|date:"Y-m-d" }}&workouttype={{ workouttype }}', function(json) {
var script = json.script;
var div = json.div;
var totalsdict = json.totalsdict
var listofdicts = json.listofdicts
var activities_script = json.activities_script
var activities_chart = json.activities_chart
$("#id_sitready").remove();
$("#id_chart").append(div);
console.log(div);
// $("#id_script").append("<s"+"cript>"+script+"</s"+"cript>");
$("#id_script").append(script);
$("#id_activities").append(activities_chart)
$("#activities_script").append(activities_script)
$("#total_hr").append(totalsdict.hrmean);
$("#total_maxhr").append(totalsdict.hrmax);
$("#total_power").append(totalsdict.powermean);

View File

@@ -4662,6 +4662,8 @@ def history_view(request,userid=0):
if request.GET.get('startdate'):
startdate,enddate = get_dates_timeperiod(request)
sstartdate = startdate
senddate = enddate
activity_startdate = datetime.datetime(
startdate.year,startdate.month,startdate.day
).replace(hour=0,minute=0,second=0).astimezone(usertimezone)
@@ -4673,6 +4675,12 @@ def history_view(request,userid=0):
activity_enddate = activity_enddate.replace(hour=23,minute=59,second=59).astimezone(usertimezone)
activity_startdate = activity_enddate-datetime.timedelta(days=15)
activity_startdate = activity_startdate.replace(hour=0,minute=0,second=0).astimezone(usertimezone)
sstartdate = datetime.date(year=activity_startdate.year,month=activity_startdate.month,
day=activity_startdate.day)
senddate = datetime.date(year=activity_enddate.year,month=activity_enddate.month,
day=activity_enddate.day)
typeselect = 'All'
if request.GET.get('workouttype'):
@@ -4685,6 +4693,8 @@ def history_view(request,userid=0):
'enddate':activity_enddate,
'workouttype':typeselect})
g_workouts = Workout.objects.filter(
user=r,
startdatetime__gte=activity_startdate,
@@ -4693,6 +4703,7 @@ def history_view(request,userid=0):
privacy='visible'
).order_by("-startdatetime")
ids = [w.id for w in g_workouts]
columns = ['hr','power','time']
@@ -4772,6 +4783,8 @@ def history_view(request,userid=0):
if firstmay>today:
firstmay = datetime.datetime(year=today.year-1,month=5,day=1)
print(sstartdate,senddate,'history')
return render(request,'history.html',
{
'tscript':tscript,
@@ -4793,6 +4806,8 @@ def history_view(request,userid=0):
'today':today,
'workouttype':typeselect,
'firstmay':firstmay,
'sstartdate':sstartdate,
'senddate':senddate,
})
@login_required()
@@ -4801,8 +4816,10 @@ def history_view_data(request,userid=0):
usertimezone = pytz.timezone(r.defaulttimezone)
print(request.GET)
if request.GET.get('startdate'):
startdate,enddate = get_dates_timeperiod(request)
startdate = datetime.datetime.strptime(request.GET.get('startdate'),"%Y-%m-%d")
enddate = datetime.datetime.strptime(request.GET.get('enddate'),"%Y-%m-%d")
activity_startdate = datetime.datetime(
startdate.year,startdate.month,startdate.day
).replace(hour=0,minute=0,second=0).astimezone(usertimezone)
@@ -4815,6 +4832,8 @@ def history_view_data(request,userid=0):
activity_startdate = activity_enddate-datetime.timedelta(days=15)
activity_startdate = activity_startdate.replace(hour=0,minute=0,second=0).astimezone(usertimezone)
typeselect = 'All'
if request.GET.get('workouttype'):
typeselect = request.GET.get('workouttype')
@@ -4822,6 +4841,7 @@ def history_view_data(request,userid=0):
if typeselect not in mytypes.checktypes:
typeselect = 'All'
g_workouts = Workout.objects.filter(
user=r,
startdatetime__gte=activity_startdate,
@@ -4906,6 +4926,8 @@ def history_view_data(request,userid=0):
totalsdict['nrworkouts'] = g_workouts.count()
# activity chart
activity_script, activity_div = interactive_activitychart(g_workouts,startdate,enddate)
# interactive hr pie chart
if typeselect == 'All':
@@ -4932,4 +4954,6 @@ def history_view_data(request,userid=0):
'div':totaldiv,
'totalsdict':totalsdict,
'listofdicts':listofdicts,
'activities_script':activity_script,
'activities_chart':activity_div,
})