def create_task_period(**kwargs): arguments = { 'compute_index': 0, 'date': datetime.datetime.utcnow(), 'user_id': 0, 'task_index': 1, 'concern_index': 2, 'start': datetime.datetime(2000, 1, 1, 12, 0, 0, 0), 'end': datetime.datetime(2000, 1, 1, 12, 0, 3, 0), } arguments.update(kwargs) return TaskPeriod.create(**arguments)
def compute_task_periods(discard_periods=DISCARD_TASK_PERIODS, extra_periods=EXTRA_TASK_PERIODS): # Create a new index for this computation last_compute_index = TaskPeriod.select(fn.Max( TaskPeriod.compute_index)).scalar() or 0 compute_index = last_compute_index + 1 # Compute the ID of the last user to complete the study max_user_id = QuestionEvent.select(fn.Max( QuestionEvent.user_id)).scalar() or 0 # Compute the time that each user spends in each question for user_id in range(0, max_user_id + 1): question_events = (QuestionEvent.select().where( QuestionEvent.user_id == user_id).order_by( QuestionEvent.time.asc())) start_task_event = None for question_event in question_events: # If the 'task' page has been loaded, store the question event that started it. if question_event.event_type == 'get task': start_task_event = question_event elif question_event.event_type == 'post task': if start_task_event is not None: # Save an event if the index of task for a 'post' event that comes # after a task starts matches the task index of the event that started it. if question_event.question_index == start_task_event.question_index: # Only save a task period if its user and index are not in the discard list. task_discard_specification = { 'user_id': user_id, 'task_index': question_event.question_index, } if task_discard_specification not in discard_periods: TaskPeriod.create( compute_index=compute_index, user_id=user_id, task_index=question_event.question_index, concern_index=_get_concern_index( user_id, question_event.question_index), start=start_task_event.time, end=question_event.time, ) # As long as we have seen an event for the end of a task, reset # state such that no "start task" event has been seen start_task_event = None # The caller may have provided a list of extra task periods to append to the computed results. # Add these records in one by one. for period_data in extra_periods: TaskPeriod.create( compute_index=compute_index, user_id=period_data['user_id'], task_index=period_data['task_index'], concern_index=_get_concern_index(period_data['user_id'], period_data['task_index']), start=period_data['start'], end=period_data['end'], )
def compute_task_periods(discard_periods=DISCARD_TASK_PERIODS, extra_periods=EXTRA_TASK_PERIODS): # Create a new index for this computation last_compute_index = TaskPeriod.select(fn.Max(TaskPeriod.compute_index)).scalar() or 0 compute_index = last_compute_index + 1 # Compute the ID of the last user to complete the study max_user_id = QuestionEvent.select(fn.Max(QuestionEvent.user_id)).scalar() or 0 # Compute the time that each user spends in each question for user_id in range(0, max_user_id + 1): question_events = ( QuestionEvent .select() .where(QuestionEvent.user_id == user_id) .order_by(QuestionEvent.time.asc()) ) start_task_event = None for question_event in question_events: # If the 'task' page has been loaded, store the question event that started it. if question_event.event_type == 'get task': start_task_event = question_event elif question_event.event_type == 'post task': if start_task_event is not None: # Save an event if the index of task for a 'post' event that comes # after a task starts matches the task index of the event that started it. if question_event.question_index == start_task_event.question_index: # Only save a task period if its user and index are not in the discard list. task_discard_specification = { 'user_id': user_id, 'task_index': question_event.question_index, } if task_discard_specification not in discard_periods: TaskPeriod.create( compute_index=compute_index, user_id=user_id, task_index=question_event.question_index, concern_index=_get_concern_index( user_id, question_event.question_index), start=start_task_event.time, end=question_event.time, ) # As long as we have seen an event for the end of a task, reset # state such that no "start task" event has been seen start_task_event = None # The caller may have provided a list of extra task periods to append to the computed results. # Add these records in one by one. for period_data in extra_periods: TaskPeriod.create( compute_index=compute_index, user_id=period_data['user_id'], task_index=period_data['task_index'], concern_index=_get_concern_index(period_data['user_id'], period_data['task_index']), start=period_data['start'], end=period_data['end'], )