submit race result now starts async process
This commit is contained in:
139
rowers/tasks.py
139
rowers/tasks.py
@@ -12,6 +12,8 @@ from scipy import optimize
|
||||
import rowingdata
|
||||
|
||||
from rowingdata import rowingdata as rdata
|
||||
from datetime import timedelta
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
from celery import app
|
||||
import datetime
|
||||
@@ -21,6 +23,7 @@ import iso8601
|
||||
from matplotlib.backends.backend_agg import FigureCanvas
|
||||
#from matplotlib.backends.backend_cairo import FigureCanvasCairo as FigureCanvas
|
||||
import matplotlib.pyplot as plt
|
||||
from matplotlib import path
|
||||
|
||||
from rowsandall_app.settings import SITE_URL
|
||||
from rowsandall_app.settings_dev import SITE_URL as SITE_URL_DEV
|
||||
@@ -41,7 +44,8 @@ from rowers.dataprepnodjango import (
|
||||
getsmallrowdata_db, updatecpdata_sql,
|
||||
update_agegroup_db,fitnessmetric_to_sql,
|
||||
add_c2_stroke_data_db,totaltime_sec_to_string,
|
||||
create_c2_stroke_data_db,update_empower
|
||||
create_c2_stroke_data_db,update_empower,
|
||||
database_url_debug,database_url,
|
||||
)
|
||||
|
||||
|
||||
@@ -66,6 +70,7 @@ siteurl = SITE_URL
|
||||
# testing task
|
||||
|
||||
from rowers.emails import send_template_email
|
||||
from rowers.courseutils import coursetime_paths, coursetime_first
|
||||
|
||||
@app.task
|
||||
def add(x, y):
|
||||
@@ -171,6 +176,136 @@ def getagegrouprecord(age,sex='male',weightcategory='hwt',
|
||||
|
||||
return power
|
||||
|
||||
def polygon_to_path(polygon,debug=True):
|
||||
pid = polygon[0]
|
||||
query = 'SELECT "rowers_geopoint"."id", "rowers_geopoint"."latitude", "rowers_geopoint"."longitude" FROM "rowers_geopoint" WHERE "rowers_geopoint"."polygon_id" = {pid} ORDER BY "rowers_geopoint"."order_in_poly" ASC'.format(
|
||||
pid=pid
|
||||
)
|
||||
if debug:
|
||||
engine = create_engine(database_url_debug, echo=False)
|
||||
else:
|
||||
engine = create_engine(database_url, echo=False)
|
||||
with engine.connect() as conn, conn.begin():
|
||||
result = conn.execute(query)
|
||||
points = result.fetchall()
|
||||
|
||||
conn.close()
|
||||
engine.dispose()
|
||||
s = []
|
||||
|
||||
for point in points:
|
||||
s.append([point[1],point[2]])
|
||||
|
||||
p = path.Path(s[:-1])
|
||||
|
||||
return p
|
||||
|
||||
@app.task(bind=True)
|
||||
def handle_check_race_course(self,
|
||||
f1,workoutid,courseid,
|
||||
recordid,**kwargs):
|
||||
|
||||
if 'debug' in kwargs:
|
||||
debug = kwargs['debug']
|
||||
else:
|
||||
debug = False
|
||||
|
||||
columns = ['time',' latitude',' longitude','cum_dist']
|
||||
|
||||
try:
|
||||
row = rdata(csvfile=f1)
|
||||
except IOError:
|
||||
try:
|
||||
row = rdata(f1 + '.csv')
|
||||
except IOError:
|
||||
try:
|
||||
row = rdata(f1 + '.gz')
|
||||
except IOError:
|
||||
return 0
|
||||
|
||||
|
||||
rowdata = row.df
|
||||
|
||||
rowdata.rename(columns = {
|
||||
' latitude':'latitude',
|
||||
' longitude':'longitude',
|
||||
' ElapsedTime (sec)': 'time',
|
||||
}, inplace=True)
|
||||
|
||||
|
||||
rowdata.fillna(method='backfill',inplace=True)
|
||||
|
||||
rowdata['time'] = rowdata['time']-rowdata.ix[0,'time']
|
||||
# we may want to expand the time (interpolate)
|
||||
rowdata['dt'] = rowdata['time'].apply(
|
||||
lambda x: timedelta(seconds=x)
|
||||
)
|
||||
rowdata = rowdata.resample('100ms',on='dt').mean()
|
||||
rowdata = rowdata.interpolate()
|
||||
|
||||
# initiate database engine
|
||||
|
||||
if debug:
|
||||
engine = create_engine(database_url_debug, echo=False)
|
||||
else:
|
||||
engine = create_engine(database_url, echo=False)
|
||||
|
||||
# get polygons
|
||||
query = 'SELECT "rowers_geopolygon"."id" FROM "rowers_geopolygon" WHERE "rowers_geopolygon"."course_id" = {courseid} ORDER BY "rowers_geopolygon"."order_in_course" ASC'.format(
|
||||
courseid=courseid
|
||||
)
|
||||
|
||||
with engine.connect() as conn, conn.begin():
|
||||
try:
|
||||
result = conn.execute(query)
|
||||
polygons = result.fetchall()
|
||||
except:
|
||||
print "Database locked"
|
||||
conn.close()
|
||||
engine.dispose()
|
||||
|
||||
paths = []
|
||||
for polygon in polygons:
|
||||
path = polygon_to_path(polygon,debug=debug)
|
||||
paths.append(path)
|
||||
|
||||
(
|
||||
coursetimeseconds,
|
||||
coursemeters,
|
||||
coursecompleted,
|
||||
|
||||
) = coursetime_paths(rowdata,paths)
|
||||
(
|
||||
coursetimefirst,
|
||||
coursemetersfirst,
|
||||
firstcompleted
|
||||
) = coursetime_first(
|
||||
rowdata,paths)
|
||||
|
||||
coursetimeseconds = coursetimeseconds-coursetimefirst
|
||||
coursemeters = coursemeters-coursemetersfirst
|
||||
|
||||
if coursecompleted:
|
||||
query = 'UPDATE "rowers_virtualraceresult" SET "coursecompleted" = 1, "duration" = "{duration}", "distance" = {distance}, "workoutid" = {workoutid} WHERE "id"="{recordid}"'.format(
|
||||
recordid=recordid,
|
||||
duration=totaltime_sec_to_string(coursetimeseconds),
|
||||
distance=int(coursemeters),
|
||||
workoutid=workoutid,
|
||||
)
|
||||
|
||||
with engine.connect() as conn, conn.begin():
|
||||
result = conn.execute(query)
|
||||
|
||||
conn.close()
|
||||
engine.dispose()
|
||||
|
||||
return 1
|
||||
|
||||
else:
|
||||
return 2
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
@app.task(bind=True)
|
||||
def handle_getagegrouprecords(self,
|
||||
@@ -287,8 +422,6 @@ def handle_update_empower(self,
|
||||
boattype = workoutdict['boattype']
|
||||
f1 = workoutdict['filename']
|
||||
|
||||
print wid
|
||||
|
||||
# oarlength consistency checks will be done in view
|
||||
|
||||
havedata = 1
|
||||
|
||||
Reference in New Issue
Block a user