def handle(self, *args, **options):
     if len(args) != 0:
         raise CommandError('Usage: manage.py add_clinic_ids_to_sync_logs')
     
     for sl in SyncLog.view("phone/sync_logs_by_chw", reduce=False, 
                             include_docs=True):
         try:
             chw = CommunityHealthWorker.get(sl.chw_id)
         except ResourceNotFound:
             chw = None
         if not chw:
             print "failed to update %s" % sl
             continue
         sl.clinic_id = chw.current_clinic_id
         sl.save()
                     
Esempio n. 2
0
def sync_logs_for_chw(chw_id):
    logs = SyncLog.view("phone/sync_logs_by_chw", reduce=False, startkey=[chw_id], endkey=[chw_id, {}], include_docs=True)
    return render_to_string("phone/partials/sync_log_for_chw_table.html", 
                            {"sync_data": logs})
    
Esempio n. 3
0
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