minimal working version of alert_get_stats
This commit is contained in:
@@ -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.teams import coach_getcoachees
|
||||||
|
from rowers.dataprep import getsmallrowdata_db,getrowdata_db
|
||||||
|
import datetime
|
||||||
## BASIC operations
|
## BASIC operations
|
||||||
|
|
||||||
# create alert
|
# create alert
|
||||||
def create_alert(manager, rower, measured,period=7, emailalert=True,
|
def create_alert(manager, rower, measured,period=7, emailalert=True,
|
||||||
reststrokes=False, workouttype='water',
|
reststrokes=False, workouttype='water',
|
||||||
name=name,**kwargs):
|
name='',**kwargs):
|
||||||
|
|
||||||
# check if manager is coach of rower. If not return 0
|
# check if manager is coach of rower. If not return 0
|
||||||
if manager.rower != rower:
|
if manager.rower != rower:
|
||||||
@@ -26,7 +27,7 @@ def create_alert(manager, rower, measured,period=7, emailalert=True,
|
|||||||
manager=manager,
|
manager=manager,
|
||||||
rower=rower,
|
rower=rower,
|
||||||
measured=m,
|
measured=m,
|
||||||
restrokes=reststrokes,
|
reststrokes=reststrokes,
|
||||||
period=period,
|
period=period,
|
||||||
emailalert=emailalert,
|
emailalert=emailalert,
|
||||||
workouttype=workouttype
|
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 = 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
|
# nperiod = 1: 1 period ago , i.e. next_run -2n days to next_run -n days
|
||||||
def alert_get_stats(alert,nperiod=0):
|
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
|
# run alert report
|
||||||
|
|||||||
@@ -1023,7 +1023,7 @@ class Condition(models.Model):
|
|||||||
metric = models.CharField(max_length=50,choices=parchoicesy1,verbose_name='Metric')
|
metric = models.CharField(max_length=50,choices=parchoicesy1,verbose_name='Metric')
|
||||||
value1 = models.FloatField(default=0)
|
value1 = models.FloatField(default=0)
|
||||||
value2 = 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 = []
|
rowchoices = []
|
||||||
for key,value in mytypes.workouttypes:
|
for key,value in mytypes.workouttypes:
|
||||||
|
|||||||
Reference in New Issue
Block a user