def get_schedule_tally(username, total_interval, override_date=None): """ Main entry point For a given username and interval, get a simple array of the username and scheduled visit (whether a submission is there or not) exists. returns (schedule_tally_array, patient_array, total_scheduled (int), total_visited(int)) schedul_tally_array = [visit_date, [(patient1, visit1), (patient2, visit2), (patient3, None), (patient4, visit4), ...]] where visit = XFormInstance """ if override_date is None: nowdate = datetime.utcnow() chw_schedule = CHWPatientSchedule.get_schedule(username) else: nowdate = override_date chw_schedule = CHWPatientSchedule.get_schedule(username, override_date=nowdate) patient_case_ids = set([x['case_id'] for x in chw_schedule.raw_schedule]) patient_cache = get_patient_display_cache(list(patient_case_ids)) #got the chw schedule #now let's walk through the date range, and get the scheduled CHWs per this date.visit_dates = [] ret = [] #where it's going to be an array of tuples: #(date, scheduled[], submissions[] - that line up with the scheduled) total_scheduled = 0 total_visited = 0 for n in range(0, total_interval): td = timedelta(days=n) visit_date = nowdate - td scheduled_case_ids = chw_schedule.scheduled_for_date(visit_date) patient_case_ids = set([x for x in scheduled_case_ids if x is not None]) dereferenced_patient_info = [patient_cache.get(x, {}) for x in patient_case_ids] visited = [] #inefficient, but we need to get the patients in alpha order #patients = sorted(patients, key=lambda x: x.last_name) dp = parser() for case_id in patient_case_ids: total_scheduled += 1 search_results = dots_submissions_by_case(case_id, visit_date, username=username) submissions = search_results['hits']['hits'] if len(submissions) > 0: #calculate if pillbox checked pillbox_check_str = submissions[0]['fields']['pillbox_check'] if len(pillbox_check_str) > 0: pillbox_check_data = json.loads(pillbox_check_str) anchor_date = dp.parse(pillbox_check_data.get('anchor')) else: pillbox_check_data = {} anchor_date = datetime.min encounter_date = dp.parse(submissions[0]['fields']['encounter_date']) submissions[0]['fields']['has_pillbox_check'] = 'Yes' if anchor_date.date() == encounter_date.date() else 'No' visited.append(submissions[0]['fields']) total_visited += 1 else: #ok, so no submission from this chw, let's see if there's ANY from anyone on this day. search_results = dots_submissions_by_case(case_id, visit_date) other_submissions = search_results['hits']['hits'] if len(other_submissions) > 0: visited.append(other_submissions[0]['fields']) total_visited += 1 else: visited.append(None) ret.append((visit_date, list(zip(dereferenced_patient_info, visited)))) return ret, patient_case_ids, total_scheduled, total_visited
def get_schedule_tally(username, total_interval, override_date=None): """ Main entry point For a given username and interval, get a simple array of the username and scheduled visit (whether a submission is there or not) exists. returns (schedule_tally_array, patient_array, total_scheduled (int), total_visited(int)) schedul_tally_array = [visit_date, [(patient1, visit1), (patient2, visit2), (patient3, None), (patient4, visit4), ...]] where visit = XFormInstance """ if override_date is None: nowdate = datetime.now() chw_schedule = CHWPatientSchedule.get_schedule(username) else: nowdate = override_date chw_schedule = CHWPatientSchedule.get_schedule(username, override_date=nowdate) patient_case_ids = set([x['case_id'] for x in chw_schedule.raw_schedule]) patient_cache = get_patient_display_cache(list(patient_case_ids)) #got the chw schedule #now let's walk through the date range, and get the scheduled CHWs per this date.visit_dates = [] ret = [] #where it's going to be an array of tuples: #(date, scheduled[], submissions[] - that line up with the scheduled) total_scheduled = 0 total_visited = 0 for n in range(0, total_interval): td = timedelta(days=n) visit_date = nowdate - td scheduled_case_ids = chw_schedule.scheduled_for_date(visit_date) patient_case_ids = set(filter(lambda x: x is not None, scheduled_case_ids)) dereferenced_patient_info = [patient_cache.get(x, {}) for x in patient_case_ids] visited = [] #inefficient, but we need to get the patients in alpha order #patients = sorted(patients, key=lambda x: x.last_name) dp = parser() for case_id in patient_case_ids: total_scheduled += 1 search_results = dots_submissions_by_case(case_id, visit_date, username=username) submissions = search_results['hits']['hits'] if len(submissions) > 0: #calculate if pillbox checked pillbox_check_str = submissions[0]['fields']['pillbox_check'] if len(pillbox_check_str) > 0: pillbox_check_data = simplejson.loads(pillbox_check_str) anchor_date = dp.parse(pillbox_check_data.get('anchor')) else: pillbox_check_data = {} anchor_date = datetime.min encounter_date = dp.parse(submissions[0]['fields']['encounter_date']) submissions[0]['fields']['has_pillbox_check'] = 'Yes' if anchor_date.date() == encounter_date.date() else 'No' visited.append(submissions[0]['fields']) total_visited += 1 else: #ok, so no submission from this chw, let's see if there's ANY from anyone on this day. search_results = dots_submissions_by_case(case_id, visit_date) other_submissions = search_results['hits']['hits'] if len(other_submissions) > 0: visited.append(other_submissions[0]['fields']) total_visited += 1 else: visited.append(None) ret.append((visit_date, zip(dereferenced_patient_info, visited))) return ret, patient_case_ids, total_scheduled, total_visited