diff --git a/rowers/alerts.py b/rowers/alerts.py index 96101d3b..2bbb0e81 100644 --- a/rowers/alerts.py +++ b/rowers/alerts.py @@ -1,12 +1,13 @@ -from rowers.models import Alert, Condition, User, Rower +from rowers.models import Alert, Condition, User, Rower, Workout from rowers.teams import coach_getcoachees - +from rowers.dataprep import getsmallrowdata_db,getrowdata_db +import datetime ## BASIC operations # create alert def create_alert(manager, rower, measured,period=7, emailalert=True, reststrokes=False, workouttype='water', - name=name,**kwargs): + name='',**kwargs): # check if manager is coach of rower. If not return 0 if manager.rower != rower: @@ -26,7 +27,7 @@ def create_alert(manager, rower, measured,period=7, emailalert=True, manager=manager, rower=rower, measured=m, - restrokes=reststrokes, + reststrokes=reststrokes, period=period, emailalert=emailalert, workouttype=workouttype @@ -76,6 +77,75 @@ def alert_add_filters(alert,filter): # nperiod = 0: current period, i.e. next_run - n days to today # nperiod = 1: 1 period ago , i.e. next_run -2n days to next_run -n days def alert_get_stats(alert,nperiod=0): - return {} + # get strokes + workstrokesonly = not alert.reststrokes + startdate = (alert.next_run - datetime.timedelta(days=(nperiod+1)*alert.period-1)) + enddate = alert.next_run - datetime.timedelta(days=(nperiod)*alert.period) + columns = [alert.measured.metric] + + for condition in alert.filter.all(): + columns += condition.metric + + workouts = Workout.objects.filter(date__gte=startdate,date__lte=enddate,user=alert.rower, + workouttype=alert.workouttype) + ids = [w.id for w in workouts] + + df = getsmallrowdata_db(columns,ids=ids,doclean=True,workstrokesonly=workstrokesonly) + if df.empty: + return { + 'workouts':len(workouts), + 'startdate':startdate, + 'enddate':enddate, + 'nr_strokes':0, + 'nr_strokes_qualifying':0, + } + + + # drop strokes through filter + for condition in alert.filter.all(): + if condition.condition == '>': + mask = df[condition.metric] > condition.value1 + df.loc[mask,alert.measured.metric] = np.nan + elif condition.condition == '<': + mask = df[condition.metric] < condition.value1 + df.loc[mask,alert.measured.metric] = np.nan + elif condition.condition == 'between': + mask = df[condition.metric] > condition.value1 + mask2 = df[condition.metric] < condition.value2 + df.loc[mask & mask2,alert.measured.metric] = np.nan + elif condition.condition == '=': + mask = df[condition.metric] == condition.value1 + df.loc[mask,alert.measured.metric] = np.nan + + df.dropna(inplace=True,axis=0) + + # count strokes + nr_strokes = len(df) + + + # count qualifying + if alert.measured.condition == '>': + mask = df[alert.measured.metric] > alert.measured.value1 + df2 = df[mask].copy() + elif alert.measured.condition == '<': + mask = df[alert.measured.metric] > alert.measured.value1 + df2 = df[mask].copy() + elif alert.measured.condition == 'between': + mask = df[alert.measured.metric] > alert.measured.value1 + mask2 = df[alert.measured.metric] < alert.measured.value2 + df2 = df[mask & mask2].copy() + else: + mask = df[alert.measured.metric] == alert.measured.value1 + df2 = df[mask].copy() + + nr_strokes_qualifying = len(df2) + + return { + 'workouts':len(workouts), + 'startdate':startdate, + 'enddate':enddate, + 'nr_strokes':nr_strokes, + 'nr_strokes_qualifying':nr_strokes_qualifying + } # run alert report diff --git a/rowers/models.py b/rowers/models.py index e97e5d47..dfc5f807 100644 --- a/rowers/models.py +++ b/rowers/models.py @@ -1023,7 +1023,7 @@ class Condition(models.Model): metric = models.CharField(max_length=50,choices=parchoicesy1,verbose_name='Metric') value1 = models.FloatField(default=0) value2 = models.FloatField(default=0) - condition = models.CharField(max_length=2,choices=conditionchoices,null=True) + condition = models.CharField(max_length=20,choices=conditionchoices,null=True) rowchoices = [] for key,value in mytypes.workouttypes: