def list_chws(request): """ List chws """ chws = CommunityHealthWorker.view("chw/by_clinic", include_docs=True) return render_to_response(request, "chw/chw_list.html", {"chws": chws})
def single_chw_summary(request): """Report for a single CHW""" chw_id = request.GET.get("chw", None) chws = CommunityHealthWorker.view("chw/by_clinic", include_docs=True) main_chw = CommunityHealthWorker.get(chw_id) if chw_id else None punchcard_url = "" if main_chw: punchcard_url = get_punchcard_url(get_data(main_chw.current_clinic_id, chw_id), width=910) # patch on extra data for display main_chw.last_submission = get_last_submission_date(main_chw.get_id) main_chw.first_submission = get_first_submission_date(main_chw.get_id) main_chw.forms_submitted = get_forms_submitted(main_chw.get_id) forms_breakdown = get_submission_breakdown(main_chw.get_id) main_chw.hh_surveys = forms_breakdown[config.CHW_HOUSEHOLD_SURVEY_NAMESPACE] main_chw.fus = forms_breakdown[config.CHW_FOLLOWUP_NAMESPACE] main_chw.refs = forms_breakdown[config.CHW_REFERRAL_NAMESPACE] main_chw.monthly_surveys = forms_breakdown[config.CHW_MONTHLY_SURVEY_NAMESPACE] # recent monthly surveys main_chw.recent_surveys = get_recent_forms(main_chw.get_id, config.CHW_MONTHLY_SURVEY_NAMESPACE) if not request.datespan.is_valid(): messages.error(request, request.datespan.get_validation_reason()) messages.warning(request, "Performance Indicators are not displayed. Please fix the other errors") report = {"name": "Partial CHW Summary for %s" % main_chw.formatted_name} else: report = get_chw_pi_report(main_chw, request.datespan.startdate, request.datespan.enddate) else: report = {"name": "CHW Summary"} fake_hh_data = [] now = datetime.now() for i in range(3): year, month = add_months(now.year, now.month, -i) fake_hh_data.append(["%s %s" % (year, month), 100, 200, "25%", "13%"]) return render_to_response(request, "reports/chw_summary.html", {"report": report, "chw_id": chw_id, "main_chw": main_chw, "chws": chws, "punchcard_url": punchcard_url, "show_dates": False, "hh_data": fake_hh_data, # TODO "fu_data": fake_hh_data, # TODO "ref_data": fake_hh_data # TODO })
def chw_pi(request): """ CHW Performance Indicator Report """ # This is currently defunct and combined with the single CHW report. chw_id = request.GET.get("chw", None) chws = CommunityHealthWorker.view("chw/by_clinic", include_docs=True) main_chw = CommunityHealthWorker.get(chw_id) if chw_id else None report = { "name": "CHW PI Report" } if main_chw: if not request.datespan.is_valid(): messages.error(request, request.datespan.get_validation_reason()) else: report = get_chw_pi_report(main_chw, request.datespan.startdate, request.datespan.enddate) return render_to_response(request, "reports/chw_pi.html", {"report": report, "chws": chws, "main_chw": main_chw, "view_slug": "chw_pi"})
def chw_dashboard_summary(clinic_dict): def _status_from_last_sync(last_sync): diff = datetime.utcnow() - last_sync.date if last_sync else None if not diff or diff > timedelta(days=5): return "bad" elif diff > timedelta(days=3): return "warn" else: return "good" def _fmt_hh_visits(num_visits, chw): ret = {} ret["count"] = num_visits zone = chw.get_zone() # > 100% of quota for the month in 30 days: green # 50 - 100% of quota for the month in 30 days: yellow # < 50% of quota for the month in 30 days: red # the quota is # of hh's in the zone / 3 if zone: ret["households"] = zone.households ret["percent"] = float(100) * float(num_visits) / float(zone.households) if num_visits > zone.households / 3: ret["status"] = "good" elif num_visits > zone.households / (2 * 3): ret["status"] = "warn" else: ret["status"] = "bad" else: ret["percent"] = "?" ret["households"] = "?" ret["status"] = "unknown" return ret def _status_from_ref_visits(ref_visits): if ref_visits > 2: return "good" elif ref_visits > 0: return "warn" else: return "bad" def _status_from_overdue_fus(fus): if fus > 2: return "bad" elif fus > 0: return "warn" else: return "good" chws = filter(lambda chw: chw.user and chw.user.is_active, CommunityHealthWorker.view("chw/by_clinic", key=clinic_dict["id"], include_docs=True).all()) if chws: clinic_dict["active"] = True clinic_dict["chws"] = [] for chw in chws: chw_dict = { "id": chw.get_id, "name": chw.formatted_name, "zone": chw.current_clinic_zone } # Metrics per CHW: # - date/time of last sync last_sync = SyncLog.view("phone/sync_logs_by_chw", reduce=False, startkey=[chw.get_id, {}], endkey=[chw.get_id], include_docs=True, limit=1, descending=True).one() chw_dict["last_sync"] = fmt_time(last_sync.date) if last_sync else None chw_dict["last_sync_status"] = _status_from_last_sync(last_sync) end = datetime.today() + timedelta(days=1) start = end - timedelta(days=30) # - current outstanding follow ups # Any follow up assigned to the CHW's clinic/zone that is past due # and not closed. This is different from the PI in that it won't # check whether or not the CHW has had that case sync down to their # phone or not. # This is much faster to calculate than anything else. res = get_db().view("centralreports/cases_due", startkey=[chw.current_clinic_id, chw.current_clinic_zone, 0], endkey=[chw.current_clinic_id, chw.current_clinic_zone, end.year, end.month - 1, end.day], reduce=True).one() chw_dict["overdue_fus"] = res["value"] if res else 0 chw_dict["overdue_fus_status"] = _status_from_overdue_fus(chw_dict["overdue_fus"]) # - visits performed chw_dict["hh_visits"] = _fmt_hh_visits(forms_submitted\ (chw.get_id, config.CHW_HOUSEHOLD_SURVEY_NAMESPACE, start, end), chw) # - referrals made chw_dict["ref_visits"] = forms_submitted\ (chw.get_id, config.CHW_REFERRAL_NAMESPACE, start, end) chw_dict["ref_visits_status"] = _status_from_ref_visits(chw_dict["ref_visits"]) clinic_dict["chws"].append(chw_dict) # sort clinic_dict["chws"].sort(key=lambda k: k['zone']) return clinic_dict