From 49a2a552067a7d959d929902a1baafbde1ba1049 Mon Sep 17 00:00:00 2001 From: Sander Roosendaal Date: Sat, 20 Jun 2020 11:05:02 +0200 Subject: [PATCH] logging of validaitons --- rowers/courseutils.py | 37 +++++++++++++++----- rowers/tasks.py | 41 ++++++++++++++++++----- rowers/templates/trajectoryfailemail.html | 4 +++ 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/rowers/courseutils.py b/rowers/courseutils.py index 6ff83fc7..3bdeefd8 100644 --- a/rowers/courseutils.py +++ b/rowers/courseutils.py @@ -2,7 +2,7 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function from __future__ import unicode_literals - +import time @@ -19,7 +19,7 @@ class InvalidTrajectoryError(Exception): def __str__(self): return repr(self.value) -def time_in_path(df,p,maxmin='max',getall=False): +def time_in_path(df,p,maxmin='max',getall=False,name='unknown',logfile=None): if df.empty: return 0 @@ -43,20 +43,38 @@ def time_in_path(df,p,maxmin='max',getall=False): else: return df[b==2]['time'].min(),df[b==2]['cum_dist'].min() + if logfile is not None: + t = time.localtime() + timestamp = time.strftime('%b-%d-%Y_%H%M', t) + with open(logfile,'a') as f: + f.write('\n') + f.write(timestamp) + f.write(' ') + f.write(name) + f.write(' ') + f.write(maxmin) + f.write(' ') + f.write(str(getall)) + f.write(' ') + f.write(str(len(df[b==2]))) raise InvalidTrajectoryError("Trajectory doesn't go through path") return 0 -def coursetime_first(data,paths): +def coursetime_first(data,paths,polygons=[],logfile=None): entrytime = data['time'].max() entrydistance = data['cum_dist'].max() coursecompleted = False + if len(polygons) == 0: + polygons = [(0,str(i)) for i in range(len(paths))] + + try: - entrytime,entrydistance = time_in_path(data,paths[0],maxmin='max') + entrytime,entrydistance = time_in_path(data,paths[0],maxmin='max',name=polygons[0][1],logfile=logfile) coursecompleted = True except InvalidTrajectoryError: entrytime = data['time'].max() @@ -64,12 +82,15 @@ def coursetime_first(data,paths): coursecompleted = False return entrytime, entrydistance, coursecompleted -def coursetime_paths(data,paths,finalmaxmin='min'): +def coursetime_paths(data,paths,finalmaxmin='min',polygons=[],logfile=None): entrytime = data['time'].max() entrydistance = data['cum_dist'].max() coursecompleted = False + if len(polygons) == 0: + polygons = [(0,str(i)) for i in range(len(paths))] + # corner case - empty list of paths if len(paths) == 0: return 0,True @@ -80,7 +101,7 @@ def coursetime_paths(data,paths,finalmaxmin='min'): ( entrytime, entrydistance - ) = time_in_path(data,paths[0],maxmin=finalmaxmin) + ) = time_in_path(data,paths[0],maxmin=finalmaxmin,name = polygons[0][1],logfile=logfile) coursecompleted = True except InvalidTrajectoryError: entrytime = data['time'].max() @@ -90,7 +111,7 @@ def coursetime_paths(data,paths,finalmaxmin='min'): if len(paths) > 1: try: - time,dist = time_in_path(data, paths[0]) + time,dist = time_in_path(data, paths[0],name=polygons[0][1],logfile=logfile) data = data[data['time']>time] data['time'] = data['time']-time data['cum_dist'] = data['cum_dist']-dist @@ -98,7 +119,7 @@ def coursetime_paths(data,paths,finalmaxmin='min'): timenext, distnext, coursecompleted - ) = coursetime_paths(data,paths[1:]) + ) = coursetime_paths(data,paths[1:],polygons=polygons[1:],logfile=logfile) return time+timenext, dist+distnext,coursecompleted except InvalidTrajectoryError: entrytime = data['time'].max() diff --git a/rowers/tasks.py b/rowers/tasks.py index 2a5365fc..d0d5844b 100644 --- a/rowers/tasks.py +++ b/rowers/tasks.py @@ -349,6 +349,8 @@ def handle_check_race_course(self, recordid,useremail,userfirstname, **kwargs): + logfile = 'courselog_{workoutid}_{courseid}.log'.format(workoutid=workoutid,courseid=courseid) + if 'debug' in kwargs: debug = kwargs['debug'] else: @@ -420,10 +422,11 @@ def handle_check_race_course(self, engine = create_engine(database_url, echo=False) # get polygons - query = "SELECT id FROM rowers_geopolygon WHERE course_id = {courseid} ORDER BY order_in_course ASC".format( + query = "SELECT id,name FROM rowers_geopolygon WHERE course_id = {courseid} ORDER BY order_in_course ASC".format( courseid=courseid ) + with engine.connect() as conn, conn.begin(): result = conn.execute(query) polygons = result.fetchall() @@ -441,7 +444,16 @@ def handle_check_race_course(self, # check how many times went through start polygon try: - entrytimes,entrydistances = time_in_path(rowdata,paths[0],maxmin='max',getall=True) + entrytimes,entrydistances = time_in_path(rowdata,paths[0],maxmin='max',getall=True, + name=polygons[0].name,logfile=logfile) + with open(logfile,'a') as f: + t = time.localtime() + timestamp = time.strftime('%b-%d-%Y_%H%M', t) + f.write('\n') + f.write(timestamp) + f.write(' ') + f.write('Found {n} entrytimes'.format(n=len(entrytimes))) + except InvalidTrajectoryError: entrytimes = [] entrydistances = [] @@ -457,7 +469,13 @@ def handle_check_race_course(self, endseconds = [] for startt in entrytimes: - + with open(logfile,'a') as f: + t = time.localtime() + timestamp = time.strftime('%b-%d-%Y_%H%M', t) + f.write('\n') + f.write(timestamp) + f.write(' ') + f.write('Path starting at {t}'.format(t=startt)) rowdata2 = rowdata[rowdata['time']>(startt-10.)] ( @@ -465,13 +483,13 @@ def handle_check_race_course(self, coursemeters, coursecompleted, - ) = coursetime_paths(rowdata2,paths) + ) = coursetime_paths(rowdata2,paths,polygons=polygons,logfile=logfile) ( coursetimefirst, coursemetersfirst, firstcompleted ) = coursetime_first( - rowdata2,paths) + rowdata2,paths,polygons=polygons,logfile=logfile) @@ -541,6 +559,8 @@ def handle_check_race_course(self, conn.close() engine.dispose() + os.remove(logfile) + return 1 else: @@ -574,9 +594,11 @@ def handle_check_race_course(self, # send email handle_sendemail_coursefail( - useremail,userfirstname, + useremail,userfirstname,logfile ) + os.remove(logfile) + return 2 return 0 @@ -1168,7 +1190,7 @@ def handle_sendemail_raceregistration( return 1 def handle_sendemail_coursefail( - useremail, username, **kwargs): + useremail, username, logfile, **kwargs): if 'debug' in kwargs: debug = kwargs['debug'] @@ -1186,7 +1208,10 @@ def handle_sendemail_coursefail( res = send_template_email(from_email,[useremail], subject, 'trajectoryfailemail.html', - d,**kwargs) + d, + cc=['info@rowsandall.com'], + attach_file=logfile, + **kwargs) return 1 diff --git a/rowers/templates/trajectoryfailemail.html b/rowers/templates/trajectoryfailemail.html index 3022682b..10593994 100644 --- a/rowers/templates/trajectoryfailemail.html +++ b/rowers/templates/trajectoryfailemail.html @@ -16,6 +16,10 @@ contact me by reply to this email.

+

+ The attachment contains debugging information for the site owners. +

+

Best Regards, the Rowsandall Team