Esempio n. 1
0
    def process_indicators(self, doc_dict, domain, namespaces):
        case_type = doc_dict.get('type')
        if not case_type:
            return

        case_indicators = []
        for namespace in namespaces:
            case_indicators.extend(CaseIndicatorDefinition.get_all(namespace, domain, case_type=case_type))

        if case_indicators:
            case_doc = CommCareCase.get(doc_dict['_id'])
            case_doc.update_indicators_in_bulk(case_indicators, logger=pillow_logging)

        xform_ids = doc_dict.get('xform_ids', [])
        for namespace in namespaces:
            for xform_id in xform_ids:
                try:
                    xform_doc = XFormInstance.get(xform_id)
                    if not xform_doc.xmlns:
                        continue
                    related_xform_indicators = CaseDataInFormIndicatorDefinition.get_all(namespace, domain,
                                                                                         xmlns=xform_doc.xmlns)
                    xform_doc.update_indicators_in_bulk(related_xform_indicators, logger=pillow_logging)
                except ResourceNotFound:
                    pillow_logging.error("[INDICATOR %(namespace)s %(domain)s] Tried to form indicator %(xform_id)s "
                                         "from case %(case_id)s and failed." % {
                                             'namespace': namespace,
                                             'domain': domain,
                                             'xform_id': xform_id,
                                             'case_id': doc_dict['_id'],
                                         })
    def update_indicators_for_case_type(self, case_type, domain):
        print "\n\n\nFetching %s cases in domain %s...." % (case_type, domain)
        key = ["all type", domain, case_type]
        relevant_indicators = CaseIndicatorDefinition.get_all(
            namespace=MVP.NAMESPACE,
            domain=domain,
            case_type=case_type
        )

        if relevant_indicators:
            all_cases = get_db().view("case/all_cases",
                reduce=True,
                startkey=key,
                endkey=key+[{}]
            ).first()
            num_cases = all_cases['value'] if all_cases else 0

            print "\nFound the following Case Indicator Definitions for Case Type %s in Domain %s" % (case_type, domain)
            print "--%s\n" % "\n--".join([i.slug for i in relevant_indicators])

            print "Found %d possible cases for update." % num_cases
            get_cases = lambda skip, limit: CommCareCase.view("case/all_cases",
                reduce=False,
                include_docs=True,
                startkey=key,
                endkey=key+[{}],
                skip=skip,
                limit=limit
            ).all()
            self._throttle_updates("Cases of type %s in %s" % (case_type, domain),
                                   relevant_indicators, num_cases, domain, get_cases)
Esempio n. 3
0
    def get_indicators_by_doc(self, doc_dict):
        indicators = []

        case_type = doc_dict.get('type')
        domain = doc_dict.get('domain')
        form_ids = doc_dict.get('xform_ids') or []

        if case_type and domain:
            # relevant namespaces
            namespaces = INDICATOR_CONFIG[domain]

            for namespace in namespaces:
                # need all the relevant Case Indicator Defs
                indicators.extend(CaseIndicatorDefinition.get_all(namespace, domain, case_type=case_type))

                # we also need to update forms that are related to this case and might use data which
                # may have just been updated
                for form_id in form_ids:
                    try:
                        xform = XFormInstance.get(form_id)
                        if xform.xmlns and isinstance(xform, XFormInstance):
                            case_related_indicators = CaseDataInFormIndicatorDefinition.get_all(namespace, domain,
                                xmlns=xform.xmlns)
                            logging.info("Grabbed Related XForm %s and now processing %d indicators." %
                                         (form_id, len(case_related_indicators)))
                            FormIndicatorPillow.update_indicators_in_doc_instance(xform, case_related_indicators)
                            logging.info("Done processing indicators.")
                    except Exception as e:
                        logging.error("Error grabbing related xform for CommCareCase %s: %s" % (doc_dict['_id'], e))

        return indicators
    def update_indicators_for_case_type(self, case_type, domain):
        print "\n\n\nFetching %s cases in domain %s...." % (case_type, domain)
        relevant_indicators = CaseIndicatorDefinition.get_all(
            namespace=MVP.NAMESPACE, domain=domain, case_type=case_type
        )

        if relevant_indicators:
            num_cases = get_number_of_cases_in_domain(domain, type=case_type)

            print ("\nFound the following Case Indicator Definitions " "for Case Type %s in Domain %s") % (
                case_type,
                domain,
            )
            print "--%s\n" % "\n--".join([i.slug for i in relevant_indicators])

            print "Found %d possible cases for update." % num_cases
            case_ids = get_case_ids_in_domain(domain, type=case_type)

            self._throttle_updates(
                "Cases of type %s in %s" % (case_type, domain),
                relevant_indicators,
                num_cases,
                domain,
                case_ids,
                CommCareCase,
            )
Esempio n. 5
0
    def process_indicators(self, doc_dict, domain, namespaces):
        case_type = doc_dict.get('type')
        if not case_type:
            pillow_eval_logging.info("CaseIndicatorPillow did not find a case type for %(domain)s, "
                                     "doc id: %(doc_id)s" % {
                                         'domain': domain,
                                         'doc_id': doc_dict['_id'],
                                     })
            return

        case_indicators = []
        for namespace in namespaces:
            case_indicators.extend(CaseIndicatorDefinition.get_all(namespace, domain, case_type=case_type))

        if case_indicators:
            case_doc = CommCareCase.get(doc_dict['_id'])
            case_doc.update_indicators_in_bulk(case_indicators, logger=pillow_logging)
        else:
            pillow_eval_logging.info("CaseIndicatorPillow could not find case indicators for %(domain)s, "
                                     "doc id: %(doc_id)s" % {
                                         'domain': domain,
                                         'doc_id': doc_dict['_id'],
                                     })

        xform_ids = doc_dict.get('xform_ids', [])
        if not xform_ids:
            pillow_eval_logging.info("CaseIndicatorPillow could not find related xforms for case in %(domain)s, "
                                     "doc id: %(doc_id)s" % {
                                         'domain': domain,
                                         'doc_id': doc_dict['_id'],
                                     })

        for xform_id in xform_ids:
            try:
                xform_doc = XFormInstance.get(xform_id)
                if not xform_doc.xmlns:
                    continue
                related_xform_indicators = []
                for namespace in namespaces:
                    related_xform_indicators.extend(CaseDataInFormIndicatorDefinition.get_all(
                        namespace, domain, xmlns=xform_doc.xmlns))
                xform_doc.update_indicators_in_bulk(related_xform_indicators, logger=pillow_eval_logging)
            except ResourceNotFound:
                pillow_logging.error("[INDICATOR %(domain)s] Tried to form grab indicators for %(xform_id)s "
                                     "from case %(case_id)s and failed." % {
                                         'domain': domain,
                                         'xform_id': xform_id,
                                         'case_id': doc_dict['_id'],
                                     })
Esempio n. 6
0
    def update_indicators_for_case_type(self, case_type, domain):
        print "\n\n\nFetching %s cases in domain %s...." % (case_type, domain)
        relevant_indicators = CaseIndicatorDefinition.get_all(
            namespace=MVP.NAMESPACE, domain=domain, case_type=case_type)

        if relevant_indicators:
            num_cases = get_number_of_cases_in_domain(domain, type=case_type)

            print(
                "\nFound the following Case Indicator Definitions "
                "for Case Type %s in Domain %s") % (case_type, domain)
            print "--%s\n" % "\n--".join([i.slug for i in relevant_indicators])

            print "Found %d possible cases for update." % num_cases
            case_ids = get_case_ids_in_domain(domain, type=case_type)

            self._throttle_updates(
                "Cases of type %s in %s" % (case_type, domain),
                relevant_indicators, num_cases, domain, case_ids, CommCareCase)
Esempio n. 7
0
    def get_indicators_by_doc(self, doc_dict):
        indicators = []

        case_type = doc_dict.get('type')
        domain = doc_dict.get('domain')
        form_ids = doc_dict.get('xform_ids') or []

        if case_type and domain:
            # relevant namespaces
            namespaces = INDICATOR_CONFIG[domain]

            for namespace in namespaces:
                # need all the relevant Case Indicator Defs
                indicators.extend(
                    CaseIndicatorDefinition.get_all(namespace,
                                                    domain,
                                                    case_type=case_type))

                # we also need to update forms that are related to this case and might use data which
                # may have just been updated
                for form_id in form_ids:
                    try:
                        xform = XFormInstance.get(form_id)
                        if xform.xmlns and isinstance(xform, XFormInstance):
                            case_related_indicators = CaseDataInFormIndicatorDefinition.get_all(
                                namespace, domain, xmlns=xform.xmlns)
                            logging.info(
                                "Grabbed Related XForm %s and now processing %d indicators."
                                % (form_id, len(case_related_indicators)))
                            FormIndicatorPillow.update_indicators_in_doc_instance(
                                xform, case_related_indicators)
                            logging.info("Done processing indicators.")
                    except Exception as e:
                        logging.error(
                            "Error grabbing related xform for CommCareCase %s: %s"
                            % (doc_dict['_id'], e))

        return indicators
Esempio n. 8
0
    def process_indicators(self, doc_dict, domain, namespaces):
        case_type = doc_dict.get('type')
        if not case_type:
            pillow_eval_logging.info(
                "CaseIndicatorPillow did not find a case type for %(domain)s, "
                "doc id: %(doc_id)s" % {
                    'domain': domain,
                    'doc_id': doc_dict['_id'],
                })
            return

        case_indicators = []
        for namespace in namespaces:
            case_indicators.extend(
                CaseIndicatorDefinition.get_all(namespace,
                                                domain,
                                                case_type=case_type))

        if case_indicators:
            case_doc = CommCareCase.get(doc_dict['_id'])
            case_doc.update_indicators_in_bulk(case_indicators,
                                               logger=pillow_logging)
        else:
            pillow_eval_logging.info("CaseIndicatorPillow could not find "
                                     "case indicators for %(domain)s, "
                                     "doc id: %(doc_id)s" % {
                                         'domain': domain,
                                         'doc_id': doc_dict['_id'],
                                     })

        xform_ids = doc_dict.get('xform_ids', [])
        if not xform_ids:
            pillow_eval_logging.info("CaseIndicatorPillow could not find "
                                     "related xforms for case in %(domain)s, "
                                     "doc id: %(doc_id)s" % {
                                         'domain': domain,
                                         'doc_id': doc_dict['_id'],
                                     })

        for xform_id in xform_ids:
            try:
                with XFormInstance.get_locked_obj(_id=xform_id) as xform_doc:
                    if not xform_doc.xmlns:
                        continue
                    related_xform_indicators = []
                    for namespace in namespaces:
                        related_xform_indicators.extend(
                            CaseDataInFormIndicatorDefinition.get_all(
                                namespace, domain, xmlns=xform_doc.xmlns))
                    xform_doc.update_indicators_in_bulk(
                        related_xform_indicators,
                        logger=pillow_eval_logging,
                    )
            except ResourceNotFound:
                pillow_logging.error(
                    "[INDICATOR %(domain)s] Tried to form grab indicators "
                    "for %(xform_id)s "
                    "from case %(case_id)s and failed." % {
                        'domain': domain,
                        'xform_id': xform_id,
                        'case_id': doc_dict['_id'],
                    })
Esempio n. 9
0
def process_indicators_for_case(namespaces, domain, doc_dict):
    case_type = doc_dict.get('type')
    if not case_type:
        return

    case_indicator_defs = []
    for namespace in namespaces:
        case_indicator_defs.extend(CaseIndicatorDefinition.get_all(
            namespace,
            domain,
            case_type=case_type
        ))

    try:
        indicator_case = IndicatorCase.wrap_for_indicator_db(doc_dict)
        indicator_case.update_indicators_in_bulk(
            case_indicator_defs, logger=pillow_logging,
            save_on_update=False
        )
        indicator_case.save()
    except Exception as e:
        pillow_logging.error(
            "Error creating for MVP Indicator for form %(form_id)s: "
            "%(error)s" % {
                'form_id': doc_dict['_id'],
                'error': e,
            },
        )
        raise

    # Now Update Data From Case to All Related Xforms (ewwww)
    xform_ids = doc_dict.get('xform_ids', [])
    if not xform_ids:
        return

    for xform_id in xform_ids:
        related_xform_indicators = []
        try:
            # first try to get the doc from the indicator DB
            xform_doc = IndicatorXForm.get(xform_id)
        except ResourceNotFound:
            # if that fails fall back to the main DB
            try:
                xform_dict = XFormInstance.get_db().get(xform_id)
                xform_doc = IndicatorXForm.wrap_for_indicator_db(xform_dict)
                related_xform_indicators = get_form_indicators(namespaces, domain, xform_doc.xmlns)
            except ResourceNotFound:
                pillow_logging.error(
                    "Could not find an XFormInstance with id %(xform_id)s "
                    "related to Case %(case_id)s" % {
                        'xform_id': xform_id,
                        'case_id': doc_dict['_id'],
                    }
                )
                continue

        if not xform_doc.xmlns:
            continue

        for namespace in namespaces:
            related_xform_indicators.extend(
                CaseDataInFormIndicatorDefinition.get_all(
                    namespace,
                    domain,
                    xmlns=xform_doc.xmlns
                )
            )
        xform_doc.update_indicators_in_bulk(
            related_xform_indicators,
            logger=pillow_logging,
            save_on_update=False
        )
        xform_doc.save()
Esempio n. 10
0
def process_indicators_for_case(namespaces, domain, doc_dict):
    case_type = doc_dict.get('type')
    if not case_type:
        return

    case_indicator_defs = []
    for namespace in namespaces:
        case_indicator_defs.extend(
            CaseIndicatorDefinition.get_all(namespace,
                                            domain,
                                            case_type=case_type))

    try:
        indicator_case = IndicatorCase.wrap_for_indicator_db(doc_dict)
        indicator_case.update_indicators_in_bulk(case_indicator_defs,
                                                 logger=pillow_logging,
                                                 save_on_update=False)
        indicator_case.save()
    except Exception as e:
        pillow_logging.error(
            "Error creating for MVP Indicator for form %(form_id)s: "
            "%(error)s" % {
                'form_id': doc_dict['_id'],
                'error': e,
            }, )
        raise

    # Now Update Data From Case to All Related Xforms (ewwww)
    xform_ids = doc_dict.get('xform_ids', [])
    if not xform_ids:
        return

    for xform_id in xform_ids:
        related_xform_indicators = []
        try:
            # first try to get the doc from the indicator DB
            xform_doc = IndicatorXForm.get(xform_id)
        except ResourceNotFound:
            # if that fails fall back to the main DB
            try:
                xform_dict = XFormInstance.get_db().get(xform_id)
                xform_doc = IndicatorXForm.wrap_for_indicator_db(xform_dict)
                related_xform_indicators = get_form_indicators(
                    namespaces, domain, xform_doc.xmlns)
            except ResourceNotFound:
                pillow_logging.error(
                    "Could not find an XFormInstance with id %(xform_id)s "
                    "related to Case %(case_id)s" % {
                        'xform_id': xform_id,
                        'case_id': doc_dict['_id'],
                    })
                continue

        if not xform_doc.xmlns:
            continue

        for namespace in namespaces:
            related_xform_indicators.extend(
                CaseDataInFormIndicatorDefinition.get_all(
                    namespace, domain, xmlns=xform_doc.xmlns))
        xform_doc.update_indicators_in_bulk(related_xform_indicators,
                                            logger=pillow_logging,
                                            save_on_update=False)
        xform_doc.save()
Esempio n. 11
0
    def process_indicators(self, namespaces, domain, doc_dict):
        case_type = doc_dict.get('type')
        if not case_type:
            return

        case_indicator_defs = []
        for namespace in namespaces:
            case_indicator_defs.extend(CaseIndicatorDefinition.get_all(
                namespace,
                domain,
                case_type=case_type
            ))
        if not case_indicator_defs:
            return

        try:
            indicator_case = IndicatorCase.get_or_create_from_dict(doc_dict)[0]
            indicator_case.update_indicators_in_bulk(
                case_indicator_defs, logger=pillow_eval_logging
            )
        except Exception as e:
            pillow_eval_logging.error(
                "Error creating for MVP Indicator for form %(form_id)s: "
                "%(error)s" % {
                    'form_id': doc_dict['_id'],
                    'error': e,
                },
            )

        # Now Update Data From Case to All Related Xforms (ewwww)
        xform_ids = doc_dict.get('xform_ids', [])
        if not xform_ids:
            return

        for xform_id in xform_ids:
            try:
                xform_dict = XFormInstance.get_db().get(xform_id)
                xform_doc = IndicatorXForm.get_or_create_from_dict(xform_dict)[0]
            except ResourceNotFound:
                pillow_eval_logging.error(
                    "Could not find an XFormInstance with id %(xform_id)s "
                    "related to Case %(case_id)s" % {
                        'xform_id': xform_id,
                        'case_id': doc_dict['_id'],
                    }
                )
                continue

            if not xform_doc.xmlns:
                continue
            related_xform_defs = []
            for namespace in namespaces:
                related_xform_defs.extend(
                    CaseDataInFormIndicatorDefinition.get_all(
                        namespace,
                        domain,
                        xmlns=xform_doc.xmlns
                    )
                )
            xform_doc.update_indicators_in_bulk(
                related_xform_defs,
                logger=pillow_eval_logging,
            )