def _pi_results(view_slug, startdate, enddate, clinic_id): # keys is a list of start/end key dicts, for all of them, # get the results and chain into one big single list return itertools.chain(*[get_db().view\ (const.get_view_name(view_slug), group=True, group_level=4, **keys).all() \ for keys in _get_keys(startdate, enddate, clinic_id)])
def testHivPi(self): folder_name = os.path.join(os.path.dirname(__file__), "testpatients", "hiv_pitest") patient = export.import_patient_json_file(os.path.join(folder_name, "patient.json")) # first visit has a recent test so it doesn't count to denominator updated_patient, form_doc1 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "001_general.xml")) [res] = get_db().view(const.get_view_name("adult_pi"), group=True, group_level=4, key=[2011,4, "9999999", "hiv_test"]).all() num, denom = res["value"] self.assertEqual(0, num) self.assertEqual(0, denom) updated_patient, form_doc2 = export.add_form_file_to_patient(patient.get_id, os.path.join(folder_name, "002_general.xml")) # second visit has no recent test so it counts [res] = get_db().view(const.get_view_name("adult_pi"), group=True, group_level=4, key=[2011,5, "9999999", "hiv_test"]).all() num, denom = res["value"] self.assertEqual(0, num) self.assertEqual(1, denom)
def pi_details(request): year = int(request.GET["year"]) month = int(request.GET["month"]) clinic = request.GET["clinic"] report_slug = request.GET["report"] col_slug = request.GET["col"] results = get_db().view(const.get_view_name(report_slug), reduce=False, key=[year, month -1, clinic, col_slug], include_docs=True) forms = [] for row in results: num, denom = row["value"] # only count forms for now, and make sure they have a patient id # and contributed to the report denominator if row["doc"]["doc_type"] == "CXFormInstance" and denom > 0: form = CXFormInstance.wrap(row["doc"]) try: form.patient_id = form.xpath("case/patient_id") form.bhoma_patient_id = CPatient.get(form.patient_id).formatted_id except ResourceNotFound: form.patient = form.patient_id = form.bhoma_patient_id = None form.num = num form.denom = denom form.good = num == denom forms.append(form) elif row["doc"]["doc_type"] == "PregnancyReportRecord" and denom > 0: # for the pregnancy PI force the aggregated pregnancy docs # to look like forms preg = PregnancyReportRecord.wrap(row["doc"]) try: preg.bhoma_patient_id = CPatient.get(preg.patient_id).formatted_id except ResourceNotFound: form.patient = form.patient_id = form.bhoma_patient_id = None preg.num = num preg.denom = denom preg.good = num == denom preg.encounter_date = preg.first_visit_date forms.append(preg) title = "PI Details - %s: %s (%s, %s)" % (const.get_name(report_slug), const.get_display_name(report_slug, col_slug), datetime(year, month, 1).strftime("%B %Y"), clinic_display_name(clinic)) return render_to_response(request, "reports/pi_details.html", {"report": {"name": title}, "forms": forms})
def disease_details(request): year = int(request.GET["year"]) month = int(request.GET["month"]) clinic = request.GET["clinic"] report_slug = request.GET["report"] col_slug = request.GET["col"] results = get_db().view(const.get_view_name(report_slug), reduce=True, startkey=[year, month -1, clinic, col_slug], endkey=[year, month -1, clinic, col_slug, {}], group=True, group_level=5) results_dict = dict([(row["key"][4], row["value"]) for row in results]) title = "Clinic: %s, Category: %s, %s" % \ (clinic_display_name(clinic), const.get_display_name(report_slug, col_slug), datetime(year, month, 1).strftime("%B, %Y")) return render_to_response(request, "reports/disease_details.html", {"report": {"name": title}, "values": results_dict})