Esempio n. 1
0
def doc_in_es(request):
    doc_id = request.GET.get("id")
    if not doc_id:
        return render(request, "hqadmin/doc_in_es.html", {})
    try:
        couch_doc = get_db().get(doc_id)
    except ResourceNotFound:
        couch_doc = {}
    query = {"filter":
                {"ids": {
                    "values": [doc_id]}}}
    es_doc = {}
    index_found = ''
    for index, url in ES_URLS.items():
        res = run_query(url, query)
        if res['hits']['total'] == 1:
            es_doc = res['hits']['hits'][0]['_source']
            index_found = index
            break
    doc_type = couch_doc.get('doc_type') or es_doc.get('doc_type', "Unknown")
    def to_json(doc):
        return json.dumps(doc, indent=4, sort_keys=True) if doc else "NOT FOUND!"
    context = {
        "doc_id": doc_id,
        "status": "found" if es_doc else "NOT FOUND!",
        "doc_type": doc_type,
        "couch_doc": to_json(couch_doc),
        "es_doc": to_json(es_doc),
        "index": index_found
    }
    return render(request, "hqadmin/doc_in_es.html", context)
Esempio n. 2
0
def doc_in_es(request):
    doc_id = request.GET.get("id")
    if not doc_id:
        return render(request, "hqadmin/doc_in_es.html", {})
    try:
        couch_doc = get_db().get(doc_id)
    except ResourceNotFound:
        couch_doc = {}
    query = {"filter":
                {"ids": {
                    "values": [doc_id]}}}

    def to_json(doc):
        return json.dumps(doc, indent=4, sort_keys=True) if doc else "NOT FOUND!"

    found_indices = {}
    doc_type = couch_doc.get('doc_type')
    es_doc_type = None
    for index, url in ES_URLS.items():
        res = run_query(url, query)
        if 'hits' in res and res['hits']['total'] == 1:
            es_doc = res['hits']['hits'][0]['_source']
            found_indices[index] = to_json(es_doc)
            es_doc_type = es_doc_type or es_doc.get('doc_type')

    doc_type = doc_type or es_doc_type or 'Unknown'

    context = {
        "doc_id": doc_id,
        "status": "found" if found_indices else "NOT FOUND!",
        "doc_type": doc_type,
        "couch_doc": to_json(couch_doc),
        "found_indices": found_indices,
    }
    return render(request, "hqadmin/doc_in_es.html", context)
Esempio n. 3
0
def doc_in_es(request):
    doc_id = request.GET.get("id")
    if not doc_id:
        return render(request, "hqadmin/doc_in_es.html", {})
    try:
        couch_doc = get_db().get(doc_id)
    except ResourceNotFound:
        couch_doc = {}
    query = {"filter":
                {"ids": {
                    "values": [doc_id]}}}
    es_doc = {}
    for url in ES_URLS.values():
        res = run_query(url, query)
        if res['hits']['total'] == 1:
            es_doc = res['hits']['hits'][0]['_source']
            break
    doc_type = couch_doc.get('doc_type') or es_doc.get('doc_type', "Unknown")
    def to_json(doc):
        return json.dumps(doc, indent=4, sort_keys=True) if doc else "NOT FOUND!"
    context = {
        "doc_id": doc_id,
        "status": "found" if es_doc else "NOT FOUND!",
        "doc_type": doc_type,
        "couch_doc": to_json(couch_doc),
        "es_doc": to_json(es_doc),
    }
    return render(request, "hqadmin/doc_in_es.html", context)
Esempio n. 4
0
def doc_in_es(request):
    doc_id = request.GET.get("id")
    if not doc_id:
        return render(request, "hqadmin/doc_in_es.html", {})

    def to_json(doc):
        return json.dumps(doc, indent=4, sort_keys=True) if doc else "NOT FOUND!"

    query = {"filter": {"ids": {"values": [doc_id]}}}
    found_indices = {}
    es_doc_type = None
    for index, url in ES_URLS.items():
        res = run_query(url, query)
        if 'hits' in res and res['hits']['total'] == 1:
            es_doc = res['hits']['hits'][0]['_source']
            found_indices[index] = to_json(es_doc)
            es_doc_type = es_doc_type or es_doc.get('doc_type')

    context = {
        "doc_id": doc_id,
        "es_info": {
            "status": "found" if found_indices else "NOT FOUND IN ELASTICSEARCH!",
            "doc_type": es_doc_type,
            "found_indices": found_indices,
        },
        "couch_info": _lookup_id_in_couch(doc_id),
    }
    return render(request, "hqadmin/doc_in_es.html", context)
    def es_query(self, paginated):
        reviewed_form_ids = get_form_ids_by_status(
            self.domain, getattr(self, 'display_status', None))
        if len(reviewed_form_ids) > 0:
            if not getattr(self, 'es_response', None):
                date_tuple = _get_date_range(
                    self.request_params.get('range', None))
                filtered_case_ids = self._get_filtered_cases(
                    date_tuple[0], date_tuple[1])
                location_ids = get_location_hierarchy_by_id(
                    self.request_params.get("location_id", None),
                    self.domain,
                    CCT_only=True)
                q = _get_report_query(date_tuple[0], date_tuple[1],
                                      filtered_case_ids, location_ids)

                if len(reviewed_form_ids) > 0:
                    q["filter"]["and"].append(
                        {"ids": {
                            "values": reviewed_form_ids
                        }})

                q["sort"] = self.get_sorting_block() if self.get_sorting_block(
                ) else [{
                    "form.meta.timeEnd": {
                        "order": "desc"
                    }
                }]
                if paginated:
                    self.es_response = es_query(
                        params={"domain.exact": self.domain},
                        q=q,
                        es_url=ES_URLS.get('forms'),
                        start_at=self.pagination.start,
                        size=self.pagination.count)
                else:
                    self.es_response = es_query(
                        params={"domain.exact": self.domain},
                        q=q,
                        es_url=ES_URLS.get('forms'))
        else:
            self.es_response = {'hits': {'total': 0}}

        return self.es_response
    def es_query(self, paginated):
        if not getattr(self, 'es_response', None):
            date_tuple = _get_date_range(self.request_params.get(
                'range', None))
            filtered_case_ids = self._get_filtered_cases(
                date_tuple[0], date_tuple[1])
            location_ids = get_location_hierarchy_by_id(
                self.request_params.get("location_id", None),
                self.domain,
                CCT_only=True)
            q = _get_report_query(date_tuple[0], date_tuple[1],
                                  filtered_case_ids, location_ids)

            xmlnss = _get_relevant_xmlnss_for_service_type(
                self.request.GET.get("service_type_filter"))
            if xmlnss:
                q["filter"]["and"].append({"terms": {"xmlns.exact": xmlnss}})

            modify_close = filter(None, [u'Modify/Close Client'])
            q["filter"]["and"].append(
                {"not": {
                    "terms": {
                        "form.@name": modify_close
                    }
                }})

            q["sort"] = self.get_sorting_block() \
                if self.get_sorting_block() else [{"form.meta.timeEnd": {"order": "desc"}}]
            if paginated:
                self.es_response = es_query(
                    params={"domain.exact": self.domain},
                    q=q,
                    es_url=ES_URLS.get('forms'),
                    start_at=self.pagination.start,
                    size=self.pagination.count)
            else:
                self.es_response = es_query(
                    params={"domain.exact": self.domain},
                    q=q,
                    es_url=ES_URLS.get('forms'))
        return self.es_response
Esempio n. 7
0
 def __init__(self, index=None):
     self.index = index if index is not None else self.index
     if self.index not in ES_URLS:
         msg = "%s is not a valid ES index.  Available options are: %s" % (
             index, ', '.join(ES_URLS.keys()))
         raise IndexError(msg)
     self._default_filters = deepcopy(self.default_filters)
     self.es_query = {"query": {
         "filtered": {
             "filter": {"and": []},
             "query": {'match_all': {}}
         }
     }}
Esempio n. 8
0
def doc_in_es(request):
    doc_id = request.GET.get("id")
    if not doc_id:
        return render(request, "hqadmin/doc_in_es.html", {})

    couch_doc = {}
    db_urls = [settings.COUCH_DATABASE] + settings.EXTRA_COUCHDB_DATABASES.values()
    for url in db_urls:
        try:
            couch_doc = Database(url).get(doc_id)
            break
        except ResourceNotFound:
            pass
    query = {"filter":
                {"ids": {
                    "values": [doc_id]}}}

    def to_json(doc):
        return json.dumps(doc, indent=4, sort_keys=True) if doc else "NOT FOUND!"

    found_indices = {}
    doc_type = couch_doc.get('doc_type')
    es_doc_type = None
    for index, url in ES_URLS.items():
        res = run_query(url, query)
        if 'hits' in res and res['hits']['total'] == 1:
            es_doc = res['hits']['hits'][0]['_source']
            found_indices[index] = to_json(es_doc)
            es_doc_type = es_doc_type or es_doc.get('doc_type')

    doc_type = doc_type or es_doc_type or 'Unknown'

    context = {
        "doc_id": doc_id,
        "status": "found" if found_indices else "NOT FOUND!",
        "doc_type": doc_type,
        "couch_doc": to_json(couch_doc),
        "found_indices": found_indices,
    }
    return render(request, "hqadmin/doc_in_es.html", context)
    def _get_filtered_cases(self, start_date, end_date):
        query = {
            "query": {
                "bool": {
                    "must_not": [{
                        "range": {
                            "modified_on.date": {
                                "lt": start_date
                            }
                        }
                    }, {
                        "range": {
                            "opened_on.date": {
                                "gt": end_date
                            }
                        }
                    }]
                }
            }
        }

        case_search = self.request.GET.get("case_search", "")
        if len(case_search) > 0:
            query["filter"] = {
                "and": [{
                    "regexp": {
                        "name.exact": ".*?%s.*?" % case_search
                    }
                }]
            }

        es_response = es_query(params={"domain.exact": self.domain},
                               q=query,
                               es_url=ES_URLS.get('cases'))
        return [
            res['_source']['_id']
            for res in es_response.get('hits', {}).get('hits', [])
        ]
Esempio n. 10
0
    def es_query(self, paginated):
        if not getattr(self, 'es_response', None):
            date_tuple = _get_date_range(self.request_params.get('range', None))
            filtered_case_ids = self._get_filtered_cases(date_tuple[0], date_tuple[1])
            location_ids = get_location_hierarchy_by_id(self.request_params.get("location_id", None), self.domain,
                                                        CCT_only=True)
            q = _get_report_query(date_tuple[0], date_tuple[1], filtered_case_ids, location_ids)

            xmlnss = _get_relevant_xmlnss_for_service_type(self.request.GET.get("service_type_filter"))
            if xmlnss:
                q["filter"]["and"].append({"terms": {"xmlns.exact": xmlnss}})

            modify_close = filter(None, [u'Modify/Close Client'])
            q["filter"]["and"].append({"not": {"terms": {"form.@name": modify_close}}})

            q["sort"] = self.get_sorting_block() \
                if self.get_sorting_block() else [{"form.meta.timeEnd": {"order": "desc"}}]
            if paginated:
                self.es_response = es_query(params={"domain.exact": self.domain}, q=q, es_url=ES_URLS.get('forms'),
                                            start_at=self.pagination.start, size=self.pagination.count)
            else:
                self.es_response = es_query(params={"domain.exact": self.domain}, q=q, es_url=ES_URLS.get('forms'))
        return self.es_response
Esempio n. 11
0
    def es_query(self, paginated):
        reviewed_form_ids = get_form_ids_by_status(self.domain, getattr(self, 'display_status', None))
        if len(reviewed_form_ids) > 0:
            if not getattr(self, 'es_response', None):
                date_tuple = _get_date_range(self.request_params.get('range', None))
                filtered_case_ids = self._get_filtered_cases(date_tuple[0], date_tuple[1])
                location_ids = get_location_hierarchy_by_id(self.request_params.get("location_id", None), self.domain,
                                                            CCT_only=True)
                q = _get_report_query(date_tuple[0], date_tuple[1], filtered_case_ids, location_ids)

                if len(reviewed_form_ids) > 0:
                    q["filter"]["and"].append({"ids": {"values": reviewed_form_ids}})

                q["sort"] = self.get_sorting_block() if self.get_sorting_block() else [{"form.meta.timeEnd" : {"order": "desc"}}]
                if paginated:
                    self.es_response = es_query(params={"domain.exact": self.domain}, q=q, es_url=ES_URLS.get('forms'),
                                                start_at=self.pagination.start, size=self.pagination.count)
                else:
                    self.es_response = es_query(params={"domain.exact": self.domain}, q=q, es_url=ES_URLS.get('forms'))
        else:
            self.es_response = {'hits': {'total': 0}}

        return self.es_response
Esempio n. 12
0
    def es_query(self, paginated):
        if not getattr(self, 'es_response', None):
            range = self.request_params.get('range', None)
            start_date = None
            end_date = None
            if range is not None:
                dates = str(range).split(_(" to "))
                start_date = dates[0]
                end_date = dates[1]
            filtered_case_ids = self._get_filtered_cases(start_date, end_date)
            exclude_form_ids = [mcct_status.form_id for mcct_status in McctStatus.objects.filter(
                domain=self.domain, received_on__range=(start_date, end_date))
                                if (mcct_status.status != "eligible" or
                                    (mcct_status.immunized == False and
                                    (date.today() - mcct_status.registration_date).days < 272 and
                                     mcct_status.is_booking == False))]
            location_ids = get_location_hierarchy_by_id(self.request_params.get("location_id", None), self.domain,
                                                        CCT_only=True)
            q = _get_report_query(start_date, end_date, filtered_case_ids, location_ids)

            if len(exclude_form_ids) > 0:
                q["filter"]["and"].append({"not": {"ids": {"values": exclude_form_ids}}})

            xmlnss = _get_relevant_xmlnss_for_service_type(self.request.GET.get("service_type_filter"))
            if xmlnss:
                q["filter"]["and"].append({"terms": {"xmlns.exact": xmlnss}})

            modify_close = filter(None, [u'Modify/Close Client'])
            q["filter"]["and"].append({"not": {"terms": {"form.@name": modify_close}}})

            q["sort"] = self.get_sorting_block() \
                if self.get_sorting_block() else [{"form.meta.timeEnd" : {"order": "desc"}}]

            if paginated:
                self.es_response = es_query(params={"domain.exact": self.domain}, q=q, es_url=ES_URLS.get('forms'),
                                            start_at=self.pagination.start, size=self.pagination.count)
            else:
                self.es_response = es_query(params={"domain.exact": self.domain}, q=q, es_url=ES_URLS.get('forms'))
        return self.es_response
Esempio n. 13
0
        def _get_filtered_cases(self, start_date, end_date):
            query = {
                "query": {
                    "bool": {
                        "must_not": [
                            {"range": {"modified_on.date": {"lt": start_date}}},
                            {"range": {"opened_on.date": {"gt": end_date}}}
                        ]
                    }
                }
            }

            case_search = self.request.GET.get("case_search", "")
            if len(case_search) > 0:
                query["filter"] = {
                    "and": [
                        {"regexp": {"name.exact": ".*?%s.*?" % case_search}}
                    ]
                }

            es_response = es_query(params={"domain.exact": self.domain}, q=query, es_url=ES_URLS.get('cases'))
            return [res['_source']['_id'] for res in es_response.get('hits', {}).get('hits', [])]
    def es_query(self, paginated):
        if not getattr(self, 'es_response', None):
            range = self.request_params.get('range', None)
            start_date = None
            end_date = None
            if range is not None:
                dates = str(range).split(_(" to "))
                start_date = dates[0]
                end_date = dates[1]
            filtered_case_ids = self._get_filtered_cases(start_date, end_date)
            exclude_form_ids = [
                mcct_status.form_id
                for mcct_status in McctStatus.objects.filter(
                    domain=self.domain,
                    received_on__range=(start_date, end_date))
                if (mcct_status.status != "eligible" or (
                    mcct_status.immunized == False and
                    (date.today() - mcct_status.registration_date).days < 272
                    and mcct_status.is_booking == False))
            ]
            location_ids = get_location_hierarchy_by_id(
                self.request_params.get("location_id", None),
                self.domain,
                CCT_only=True)
            q = _get_report_query(start_date, end_date, filtered_case_ids,
                                  location_ids)
            if len(exclude_form_ids) > 0:
                q["filter"]["and"].append(
                    {"not": {
                        "ids": {
                            "values": exclude_form_ids
                        }
                    }})

            xmlnss = _get_relevant_xmlnss_for_service_type(
                self.request.GET.get("service_type_filter"))
            if xmlnss:
                q["filter"]["and"].append({"terms": {"xmlns.exact": xmlnss}})

            modify_close = filter(None, [u'Modify/Close Client'])
            q["filter"]["and"].append(
                {"not": {
                    "terms": {
                        "form.@name": modify_close
                    }
                }})

            q["sort"] = self.get_sorting_block() \
                if self.get_sorting_block() else [{"form.meta.timeEnd" : {"order": "desc"}}]

            if paginated:
                self.es_response = es_query(
                    params={"domain.exact": self.domain},
                    q=q,
                    es_url=ES_URLS.get('forms'),
                    start_at=self.pagination.start,
                    size=self.pagination.count)
            else:
                self.es_response = es_query(
                    params={"domain.exact": self.domain},
                    q=q,
                    es_url=ES_URLS.get('forms'))
        return self.es_response