Offline CP calculations for OTW
A new table in the database with precalculated CP values. The data are updated through RQ/Celery asynchronous functions
This commit is contained in:
@@ -17,6 +17,7 @@ from django.utils import timezone
|
||||
from time import strftime, strptime, mktime, time, daylight
|
||||
import arrow
|
||||
from django.utils.timezone import get_current_timezone
|
||||
|
||||
thetimezone = get_current_timezone()
|
||||
from rowingdata import (
|
||||
TCXParser, RowProParser, ErgDataParser,
|
||||
@@ -40,7 +41,7 @@ import itertools
|
||||
import math
|
||||
from tasks import (
|
||||
handle_sendemail_unrecognized, handle_sendemail_breakthrough,
|
||||
handle_sendemail_hard
|
||||
handle_sendemail_hard, handle_updatecp
|
||||
)
|
||||
|
||||
from django.conf import settings
|
||||
@@ -425,6 +426,112 @@ def paceformatsecs(values):
|
||||
|
||||
return out
|
||||
|
||||
def getcpdata_sql(rower_id):
|
||||
engine = create_engine(database_url, echo=False)
|
||||
query = sa.text('SELECT delta,cp from cpdata WHERE user={rower_id};'.format(
|
||||
rower_id=rower_id
|
||||
))
|
||||
connection = engine.raw_connection()
|
||||
df = pd.read_sql_query(query, engine)
|
||||
|
||||
return df
|
||||
|
||||
def deletecpdata_sql(rower_id):
|
||||
engine = create_engine(database_url, echo=False)
|
||||
query = sa.text('DELETE from cpdata WHERE user={rower_id};'.format(
|
||||
rower_id=rower_id
|
||||
))
|
||||
with engine.connect() as conn, conn.begin():
|
||||
try:
|
||||
result = conn.execute(query)
|
||||
except:
|
||||
print "Database locked"
|
||||
conn.close()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
|
||||
def updatecpdata_sql(rower_id,delta,cp):
|
||||
deletecpdata_sql(rower_id)
|
||||
df = pd.DataFrame(
|
||||
{
|
||||
'delta':delta,
|
||||
'cp':cp,
|
||||
'user':rower_id
|
||||
}
|
||||
)
|
||||
|
||||
engine = create_engine(database_url, echo=False)
|
||||
with engine.connect() as conn, conn.begin():
|
||||
df.to_sql('cpdata', engine, if_exists='append', index=False)
|
||||
conn.close()
|
||||
engine.dispose()
|
||||
|
||||
|
||||
def runcpupdate(rower):
|
||||
startdate = timezone.now()-datetime.timedelta(days=365)
|
||||
enddate = timezone.now()+datetime.timedelta(days=5)
|
||||
theworkouts = Workout.objects.filter(user=rower,rankingpiece=True,
|
||||
workouttype='water',
|
||||
startdatetime__gte=startdate,
|
||||
startdatetime__lte=enddate)
|
||||
|
||||
theids = [w.id for w in theworkouts]
|
||||
|
||||
|
||||
if settings.DEBUG:
|
||||
res = handle_updatecp.delay(rower.id,theids,debug=True)
|
||||
else:
|
||||
res = queue.enqueue(handle_updatecp,rower.id,theids)
|
||||
|
||||
|
||||
def fetchcp(rower,theworkouts):
|
||||
# get all power data from database (plus workoutid)
|
||||
theids = [int(w.id) for w in theworkouts]
|
||||
columns = ['power','workoutid','time']
|
||||
df = getsmallrowdata_db(columns,ids=theids)
|
||||
|
||||
dfgrouped = df.groupby(['workoutid'])
|
||||
avgpower2 = dict(dfgrouped.mean()['power'].astype(int))
|
||||
|
||||
cpdf = getcpdata_sql(rower.id)
|
||||
|
||||
if not cpdf.empty:
|
||||
return cpdf['delta'],cpdf['cp'],avgpower2
|
||||
else:
|
||||
if settings.DEBUG:
|
||||
res = handle_updatecp.delay(rower.id,theids,debug=True)
|
||||
else:
|
||||
res = queue.enqueue(handle_updatecp,rower.id,theids)
|
||||
return [],[],avgpower2
|
||||
|
||||
# below is redundant
|
||||
thesecs = []
|
||||
|
||||
for w in theworkouts:
|
||||
timesecs = 3600*w.duration.hour
|
||||
timesecs += 60*w.duration.minute
|
||||
timesecs += w.duration.second
|
||||
timesecs += 1.e-5*w.duration.microsecond
|
||||
|
||||
thesecs.append(timesecs)
|
||||
|
||||
if len(thesecs) != 0:
|
||||
maxt = 1.05*pd.Series(thesecs).max()
|
||||
else:
|
||||
maxt = 1000.
|
||||
|
||||
|
||||
logarr = datautils.getlogarr(maxt)
|
||||
|
||||
|
||||
delta,cpvalue,avgpower = datautils.getcp(dfgrouped,logarr)
|
||||
|
||||
updatecpdata_sql(rower.id,delta,cpvalue)
|
||||
|
||||
return delta,cpvalue,avgpower2
|
||||
|
||||
|
||||
# Processes painsled CSV file to database
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user