Esempio n. 1
0
def delete_all_locations():
    ids = [
        doc['id'] for doc in
        SupplyPointCase.get_db().view('supply_point_by_loc/view', reduce=False).all()
    ]
    iter_bulk_delete(SupplyPointCase.get_db(), ids)
    delete_all_docs_by_doc_type(Location.get_db(), ['Location'])
    SQLLocation.objects.all().delete()
Esempio n. 2
0
def delete_all_locations():
    ids = [
        doc['id'] for doc in
        SupplyPointCase.get_db().view('supply_point_by_loc/view', reduce=False).all()
    ]
    iter_bulk_delete(SupplyPointCase.get_db(), ids)
    SQLLocation.objects.all().delete()
    LocationType.objects.all().delete()
Esempio n. 3
0
def delete_all_locations():
    ids = [
        doc['id'] for doc in
        SupplyPointCase.get_db().view('commtrack/supply_point_by_loc', reduce=False).all()
    ]
    iter_bulk_delete(SupplyPointCase.get_db(), ids)

    iter_bulk_delete(Location.get_db(), SQLLocation.objects.location_ids())

    SQLLocation.objects.all().delete()
Esempio n. 4
0
def supply_point_ids(locations):
    keys = [[loc.domain, loc._id] for loc in locations]
    rows = SupplyPointCase.get_db().view(
        'commtrack/supply_point_by_loc',
        keys=keys,
        include_docs=False,
    )
    return [row['id'] for row in rows]
Esempio n. 5
0
def supply_point_ids(locations):
    keys = [[loc.domain, loc._id] for loc in locations]
    rows = SupplyPointCase.get_db().view(
        'commtrack/supply_point_by_loc',
        keys=keys,
        include_docs=False,
    )
    return [row['id'] for row in rows]
Esempio n. 6
0
    def get_data(self):
        # todo: this will probably have to paginate eventually
        if self.all_relevant_forms:
            sp_ids = get_relevant_supply_point_ids(
                self.domain,
                self.active_location,
            )

            form_xmlnses = [form['xmlns'] for form in self.all_relevant_forms.values()]
            spoint_loc_map = {
                doc['_id']: doc['location_id']
                for doc in iter_docs(SupplyPointCase.get_db(), sp_ids)
            }
            locations = {
                doc['_id']: Location.wrap(doc)
                for doc in iter_docs(Location.get_db(), spoint_loc_map.values())
            }

            for spoint_id, loc_id in spoint_loc_map.items():
                loc = locations[loc_id]

                form_ids = StockReport.objects.filter(
                    stocktransaction__case_id=spoint_id
                ).exclude(
                    date__lte=self.start_date
                ).exclude(
                    date__gte=self.end_date
                ).values_list(
                    'form_id', flat=True
                ).order_by('-date').distinct()  # not truly distinct due to ordering
                matched = False
                for form_id in form_ids:
                    try:
                        if XFormInstance.get(form_id).xmlns in form_xmlnses:
                            yield {
                                'loc_id': loc._id,
                                'loc_path': loc.path,
                                'name': loc.name,
                                'type': loc.location_type,
                                'reporting_status': 'reporting',
                                'geo': loc._geopoint,
                            }
                            matched = True
                            break
                    except ResourceNotFound:
                        logging.error('Stock report for location {} in {} references non-existent form {}'.format(
                            loc._id, loc.domain, form_id
                        ))
                if not matched:
                    yield {
                        'loc_id': loc._id,
                        'loc_path': loc.path,
                        'name': loc.name,
                        'type': loc.location_type,
                        'reporting_status': 'nonreporting',
                        'geo': loc._geopoint,
                    }
Esempio n. 7
0
 def get_ids(self):
     supply_points_ids = SQLLocation.objects.filter(
         domain=self.domain,
         location_type__administrative=False
     ).order_by('created_at').values_list('supply_point_id', flat=True)
     return [
         doc['external_id']
         for doc in iter_docs(SupplyPointCase.get_db(), supply_points_ids)
         if doc['external_id']
     ]
Esempio n. 8
0
 def get_ids(self):
     supply_points_ids = SQLLocation.objects.filter(
         domain=self.domain,
         location_type__administrative=False
     ).order_by('created_at').values_list('supply_point_id', flat=True)
     return [
         doc['external_id']
         for doc in iter_docs(SupplyPointCase.get_db(), supply_points_ids)
         if doc['external_id']
     ]
Esempio n. 9
0
def get_supply_points_json_in_domain_by_location(domain):
    from corehq.apps.commtrack.models import SupplyPointCase
    results = SupplyPointCase.get_db().view(
        'commtrack/supply_point_by_loc',
        startkey=[domain],
        endkey=[domain, {}],
        include_docs=True,
    )

    for result in results:
        location_id = result['key'][-1]
        case = result['doc']
        yield location_id, case
Esempio n. 10
0
def get_supply_points_json_in_domain_by_location(domain):
    from corehq.apps.commtrack.models import SupplyPointCase
    results = SupplyPointCase.get_db().view(
        'commtrack/supply_point_by_loc',
        startkey=[domain],
        endkey=[domain, {}],
        include_docs=True,
    )

    for result in results:
        location_id = result['key'][-1]
        case = result['doc']
        yield location_id, case
Esempio n. 11
0
def get_supply_point_ids_in_domain_by_location(domain):
    """
    Returns a dict that maps from associated location id's
    to supply point id's for all supply point cases in the passed
    domain.
    """
    from corehq.apps.commtrack.models import SupplyPointCase
    return {
        row['key'][1]: row['id'] for row in SupplyPointCase.get_db().view(
            'supply_point_by_loc/view',
            startkey=[domain],
            endkey=[domain, {}],
        )
    }
Esempio n. 12
0
def get_supply_point_ids_in_domain_by_location(domain):
    """
    Returns a dict that maps from associated location id's
    to supply point id's for all supply point cases in the passed
    domain.
    """
    from corehq.apps.commtrack.models import SupplyPointCase
    return {
        row['key'][1]: row['id'] for row in SupplyPointCase.get_db().view(
            'supply_point_by_loc/view',
            startkey=[domain],
            endkey=[domain, {}],
        )
    }
Esempio n. 13
0
    def get_data(self):
        # todo: this will probably have to paginate eventually
        if self.all_relevant_forms:
            sp_ids = get_relevant_supply_point_ids(
                self.domain,
                self.active_location,
            )

            supply_points = (
                SupplyPointCase.wrap(doc)
                for doc in iter_docs(SupplyPointCase.get_db(), sp_ids))
            form_xmlnses = [
                form['xmlns'] for form in self.all_relevant_forms.values()
            ]

            for supply_point in supply_points:
                # todo: get locations in bulk
                loc = supply_point.location
                transactions = StockTransaction.objects.filter(
                    case_id=supply_point._id,
                ).exclude(report__date__lte=self.start_date).exclude(
                    report__date__gte=self.end_date).order_by('-report__date')
                matched = False
                for trans in transactions:
                    if XFormInstance.get(
                            trans.report.form_id).xmlns in form_xmlnses:
                        yield {
                            'loc_id': loc._id,
                            'loc_path': loc.path,
                            'name': loc.name,
                            'type': loc.location_type,
                            'reporting_status': 'reporting',
                            'geo': loc._geopoint,
                        }
                        matched = True
                        break
                if not matched:
                    yield {
                        'loc_id': loc._id,
                        'loc_path': loc.path,
                        'name': loc.name,
                        'type': loc.location_type,
                        'reporting_status': 'nonreporting',
                        'geo': loc._geopoint,
                    }
Esempio n. 14
0
    def get_data(self):
        # todo: this will probably have to paginate eventually
        if self.all_relevant_forms:
            sp_ids = get_relevant_supply_point_ids(
                self.domain,
                self.active_location,
            )

            supply_points = (SupplyPointCase.wrap(doc) for doc in iter_docs(SupplyPointCase.get_db(), sp_ids))
            form_xmlnses = [form['xmlns'] for form in self.all_relevant_forms.values()]

            for supply_point in supply_points:
                # todo: get locations in bulk
                loc = supply_point.location
                transactions = StockTransaction.objects.filter(
                    case_id=supply_point._id,
                ).exclude(
                    report__date__lte=self.start_date
                ).exclude(
                    report__date__gte=self.end_date
                ).order_by('-report__date')
                matched = False
                for trans in transactions:
                    if XFormInstance.get(trans.report.form_id).xmlns in form_xmlnses:
                        yield {
                            'loc_id': loc._id,
                            'loc_path': loc.path,
                            'name': loc.name,
                            'type': loc.location_type,
                            'reporting_status': 'reporting',
                            'geo': loc._geopoint,
                        }
                        matched = True
                        break
                if not matched:
                    yield {
                        'loc_id': loc._id,
                        'loc_path': loc.path,
                        'name': loc.name,
                        'type': loc.location_type,
                        'reporting_status': 'nonreporting',
                        'geo': loc._geopoint,
                    }
Esempio n. 15
0
    def get_data(self):
        # todo: this will probably have to paginate eventually
        if self.all_relevant_forms:
            sp_ids = get_relevant_supply_point_ids(
                self.domain,
                self.active_location,
            )

            form_xmlnses = [
                form['xmlns'] for form in self.all_relevant_forms.values()
            ]
            form_xmlnses.append(COMMTRACK_REPORT_XMLNS)
            spoint_loc_map = {
                doc['_id']: doc['location_id']
                for doc in iter_docs(SupplyPointCase.get_db(), sp_ids)
            }
            locations = _location_map(list(spoint_loc_map.values()))

            for spoint_id, loc_id in spoint_loc_map.items():
                if loc_id not in locations:
                    continue  # it's archived, skip
                loc = locations[loc_id]

                results = StockReport.objects.filter(
                    stocktransaction__case_id=spoint_id).filter(
                        date__gte=self.converted_start_datetime,
                        date__lte=self.converted_end_datetime).values_list(
                            'form_id', 'date').distinct(
                            )  # not truly distinct due to ordering

                matched = False
                for form_id, date in results:
                    try:
                        if XFormInstance.get(form_id).xmlns in form_xmlnses:
                            yield {
                                'parent_name':
                                loc.parent.name if loc.parent else '',
                                'loc_id': loc.location_id,
                                'loc_path': loc.path,
                                'name': loc.name,
                                'type': loc.location_type.name,
                                'reporting_status': 'reporting',
                                'geo': geopoint(loc),
                                'last_reporting_date': date,
                            }
                            matched = True
                            break
                    except ResourceNotFound:
                        logging.error(
                            'Stock report for location {} in {} references non-existent form {}'
                            .format(loc.location_id, loc.domain, form_id))

                if not matched:
                    result = StockReport.objects.filter(
                        stocktransaction__case_id=spoint_id).values_list(
                            'date').order_by('-date')[:1]
                    yield {
                        'parent_name': loc.parent.name if loc.parent else '',
                        'loc_id': loc.location_id,
                        'loc_path': loc.path,
                        'name': loc.name,
                        'type': loc.location_type.name,
                        'reporting_status': 'nonreporting',
                        'geo': geopoint(loc),
                        'last_reporting_date': result[0][0] if result else ''
                    }
Esempio n. 16
0
 def get_supply_points(supply_point_ids):
     supply_points = []
     for doc in iter_docs(SupplyPointCase.get_db(), supply_point_ids):
         supply_points.append(SupplyPointCase.wrap(doc))
     return supply_points
Esempio n. 17
0
def stock_data_task(domain, endpoint, apis, config, test_facilities=None):
    # checkpoint logic
    start_date = datetime.today()
    default_api = apis[0][0]

    try:
        checkpoint = StockDataCheckpoint.objects.get(domain=domain)
        api = checkpoint.api
        # legacy
        if api == 'product_stock':
            api = default_api
        date = checkpoint.date
        limit = checkpoint.limit
        offset = checkpoint.offset
        location = checkpoint.location
        if not checkpoint.start_date:
            checkpoint.start_date = start_date
            checkpoint.save()
        else:
            start_date = checkpoint.start_date
    except StockDataCheckpoint.DoesNotExist:
        checkpoint = StockDataCheckpoint()
        checkpoint.domain = domain
        checkpoint.start_date = start_date
        api = default_api
        date = None
        limit = 1000
        offset = 0
        location = None

    if not config.all_stock_data:
        facilities = test_facilities
    else:
        supply_points_ids = SQLLocation.objects.filter(
            domain=domain,
            location_type__in=get_reporting_types(domain)
        ).order_by('created_at').values_list('supply_point_id', flat=True)
        facilities = [doc['external_id'] for doc in iter_docs(SupplyPointCase.get_db(), supply_points_ids)]

    apis_from_checkpoint = itertools.dropwhile(lambda x: x[0] != api, apis)
    facilities_copy = list(facilities)
    if location:
        supply_point = SupplyPointCase.get_by_location_id(domain, location.location_id)
        external_id = supply_point.external_id if supply_point else None
        if external_id:
            facilities = itertools.dropwhile(lambda x: int(x) != int(external_id), facilities)

    for idx, (api_name, api_function) in enumerate(apis_from_checkpoint):
        api_function(
            domain=domain,
            checkpoint=checkpoint,
            date=date,
            limit=limit,
            offset=offset,
            endpoint=endpoint,
            facilities=facilities
        )
        limit = 1000
        offset = 0
        # todo: see if we can avoid modifying the list of facilities in place
        if idx == 0:
            facilities = facilities_copy

    save_stock_data_checkpoint(checkpoint, default_api, 1000, 0, start_date, None, False)
    checkpoint.start_date = None
    checkpoint.save()
Esempio n. 18
0
    def get_data(self):
        # todo: this will probably have to paginate eventually
        if self.all_relevant_forms:
            sp_ids = get_relevant_supply_point_ids(
                self.domain,
                self.active_location,
            )

            form_xmlnses = [form['xmlns'] for form in self.all_relevant_forms.values()]
            spoint_loc_map = {
                doc['_id']: doc['location_id']
                for doc in iter_docs(SupplyPointCase.get_db(), sp_ids)
            }
            locations = _location_map(spoint_loc_map.values())

            for spoint_id, loc_id in spoint_loc_map.items():
                if loc_id not in locations:
                    continue  # it's archived, skip
                loc = locations[loc_id]

                results = StockReport.objects.filter(
                    stocktransaction__case_id=spoint_id
                ).filter(
                    date__gte=self.converted_start_datetime,
                    date__lte=self.converted_end_datetime
                ).values_list(
                    'form_id',
                    'date'
                ).distinct()  # not truly distinct due to ordering

                matched = False
                for form_id, date in results:
                    try:
                        if XFormInstance.get(form_id).xmlns in form_xmlnses:
                            yield {
                                'loc': loc,
                                'loc_id': loc.location_id,
                                'loc_path': loc.path,
                                'name': loc.name,
                                'type': loc.location_type.name,
                                'reporting_status': 'reporting',
                                'geo': geopoint(loc),
                                'last_reporting_date': date,
                            }
                            matched = True
                            break
                    except ResourceNotFound:
                        logging.error('Stock report for location {} in {} references non-existent form {}'.format(
                            loc.location_id, loc.domain, form_id
                        ))

                if not matched:
                    result = StockReport.objects.filter(
                        stocktransaction__case_id=spoint_id
                    ).values_list(
                        'date'
                    ).order_by('-date')[:1]
                    yield {
                        'loc': loc,
                        'loc_id': loc.location_id,
                        'loc_path': loc.path,
                        'name': loc.name,
                        'type': loc.location_type.name,
                        'reporting_status': 'nonreporting',
                        'geo': geopoint(loc),
                        'last_reporting_date': result[0][0] if result else ''
                    }
Esempio n. 19
0
 def get_supply_points(supply_point_ids):
     supply_points = []
     for doc in iter_docs(SupplyPointCase.get_db(), supply_point_ids):
         supply_points.append(SupplyPointCase.wrap(doc))
     return supply_points