From 977c8b9a799d20fce6bc4e1b93b4bd77f7b2377d Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Tue, 10 Oct 2017 14:41:23 +0200 Subject: [PATCH] added announcement for hard workout --- rowers/dataprep.py | 26 ++++++++++++++++++++++- rowers/tasks.py | 34 ++++++++++++++++++++++++++++++ rowers/templates/show_graph.html | 4 +--- rowers/templates/workout_view.html | 2 -- rowers/utils.py | 7 +++++- 5 files changed, 66 insertions(+), 7 deletions(-) diff --git a/rowers/dataprep.py b/rowers/dataprep.py index 4cab2758..6d784d73 100644 --- a/rowers/dataprep.py +++ b/rowers/dataprep.py @@ -654,6 +654,7 @@ def save_workout_database(f2,r,dosmooth=True,workouttype='rower', w.save() isbreakthrough = False + ishard = False if workouttype == 'water': df = getsmallrowdata_db(['power','workoutid','time'],ids=[w.id]) # delta,cpvalues,avgpower = datautils.getsinglecp(row.df) @@ -664,12 +665,15 @@ def save_workout_database(f2,r,dosmooth=True,workouttype='rower', dfgrouped = df.groupby(['workoutid']) delta,cpvalues,avgpower = datautils.getcp(dfgrouped,logarr) - res,btvalues = utils.isbreakthrough(delta,cpvalues,r.p0,r.p1,r.p2,r.p3,r.cpratio) + res,btvalues,res2 = utils.isbreakthrough(delta,cpvalues,r.p0,r.p1,r.p2,r.p3,r.cpratio) else: res = 0 + res2 = 0 if res: isbreakthrough = True res = datautils.updatecp(delta,cpvalues,r) + if res2: + ishard = True # submit email task to send email about breakthrough workout if isbreakthrough: @@ -691,6 +695,26 @@ def save_workout_database(f2,r,dosmooth=True,workouttype='rower', pass else: pass + # submit email task to send email about breakthrough workout + if ishard: + a_messages.info(r.user,'That was a pretty hard workout') + if settings.DEBUG and r.getemailnotifications: + res = handle_sendemail_hard.delay(w.id,r.user.email, + r.user.first_name, + r.user.last_name, + btvalues=btvalues.to_json()) + elif r.getemailnotifications: + try: + res = queuehigh.enqueue( + handle_sendemail_hard(w.id, + r.user.email, + r.user.first_name, + r.user.last_name, + btvalues=btvalues.to_json())) + except AttributeError: + pass + else: + pass if privacy == 'visible': ts = Team.objects.filter(rower=r) diff --git a/rowers/tasks.py b/rowers/tasks.py index 71399b50..ce7ff02c 100644 --- a/rowers/tasks.py +++ b/rowers/tasks.py @@ -139,6 +139,40 @@ def handle_sendemail_breakthrough(workoutid,useremail, # remove tcx file return 1 +# send email when a breakthrough workout is uploaded +@app.task +def handle_sendemail_hard(workoutid,useremail, + userfirstname,userlastname, + btvalues = pd.DataFrame().to_json()): + + # send email with attachment + subject = "That was a pretty hard workout on rowsandall.com" + message = "Dear "+userfirstname+",\n" + message += "Congratulations! Your recent workout has been analyzed" + message += " by Rowsandall.com and it appears that it was pretty hard work." + message += " You were working pretty close to your Critical Power\n\n" + message += " Critical Power (CP) is the power that you can " + message += "sustain for a given duration. For more, see this " + message += " article in the analytics blog:\n\n" + message += " http://analytics.rowsandall.com/2017/06/17/how-do-we-calculate-critical-power/ \n\n" + message += "Link to the workout http://rowsandall.com/rowers/workout/" + message += str(workoutid) + message +="/edit\n\n" + + message += "To opt out of these email notifications, deselect the checkbox on your Profile page under Account Information.\n\n" + + message += "Best Regards, the Rowsandall Team" + + email = EmailMessage(subject, message, + 'Rowsandall ', + [useremail]) + + + res = email.send() + + # remove tcx file + return 1 + # send email to me when an unrecognized file is uploaded @app.task diff --git a/rowers/templates/show_graph.html b/rowers/templates/show_graph.html index e7a1706f..c14bd873 100644 --- a/rowers/templates/show_graph.html +++ b/rowers/templates/show_graph.html @@ -10,9 +10,7 @@ {{ workout.date }} - {{ workout.distance }}m - {{ workout.duration |durationprint:"%H:%M:%S.%f" }}{% endblock %} {% block og_image %} - - - + {% endblock %} diff --git a/rowers/templates/workout_view.html b/rowers/templates/workout_view.html index d0f0c1a1..ad3069c1 100644 --- a/rowers/templates/workout_view.html +++ b/rowers/templates/workout_view.html @@ -15,8 +15,6 @@ {% if graphs1 %} {% for graph in graphs1 %} - - diff --git a/rowers/utils.py b/rowers/utils.py index 036969f3..209d06e3 100644 --- a/rowers/utils.py +++ b/rowers/utils.py @@ -139,12 +139,17 @@ def isbreakthrough(delta,cpvalues,p0,p1,p2,p3,ratio): pwr = abs(p0)/(1+(delta/abs(p2))) pwr += abs(p1)/(1+(delta/abs(p3))) + dd = 0.25*(ratio-1) + pwr2 = pwr*(1+dd) + pwr *= ratio + delta = delta.values cpvalues = cpvalues.values res = np.sum(cpvalues>pwr) + res2 = np.sum(cpvalues>pwr2) btdf = pd.DataFrame( { @@ -158,4 +163,4 @@ def isbreakthrough(delta,cpvalues,p0,p1,p2,p3,ratio): btdf.sort_values('delta',axis=0,inplace=True) - return res>1,btdf + return res>1,btdf,res2>1