예제 #1
0
 def get_extra_data(self, obj):
     data = get_druid_data(dimensions=["rhc_id"],
                           filter_list=[['rhc_id', operator.eq, 14]],
                           order_by=["rhc_id"])
     if len(data) > 0:
         return data[0]['event']
     return {}
예제 #2
0
def daily_spray_success_by_spray_area(district_id, spray_date):
    """
    Gets spray success rates for all the spray areas in a district, for a
    specific date.  If data is found, prepares a payload and sends it to
    RapidPro - which decides what to do with the payload.

    spray_date format == 2016-10-15
    """
    dimensions = [
        "target_area_id",
        "target_area_name",
        "target_area_structures",
        "rhc_id",
        "rhc_name",
        "district_id",
        "district_name",
    ]
    filters = [
        ["district_id", operator.eq, district_id],
        ["spray_date", operator.eq, spray_date],
    ]
    druid_result = get_druid_data(dimensions, filters)
    data, _ = process_druid_data(druid_result)
    if data:
        flow_uuid = settings.RAPIDPRO_DAILY_SPRAY_SUCCESS_FLOW_ID
        for item in data:
            payload = RapidProBaseSerializer(item, date=spray_date)
            return start_flow(flow_uuid, payload)
예제 #3
0
def no_revisit(target_area_code, no_revisit_reason):
    """
    Retrieve target area spray data using target_area_code
    then package for sending to RapidPro
    """
    try:
        target_area = Location.objects.get(code=target_area_code, level="ta")
    except Location.DoesNotExist:
        pass
    else:
        dimensions = [
            "target_area_id",
            "target_area_name",
            "target_area_structures",
            "rhc_id",
            "rhc_name",
            "district_id",
            "district_name",
        ]
        filters = [["target_area_id", operator.eq, target_area.id]]
        # get today's data for this spray area
        druid_result = get_druid_data(dimensions, filters)
        data, _ = process_druid_data(druid_result)
        if data:
            payload = FoundCoverageSerializer(data[0], target_area=target_area)
        else:
            payload = FoundCoverageSerializer({}, target_area=target_area)
        payload = payload.data
        payload["no_revisit_reason"] = no_revisit_reason
        flow_uuid = settings.RAPIDPRO_NO_REVISIT_FLOW_ID
        return start_flow(flow_uuid, payload)
예제 #4
0
 def get_context_data(self, **kwargs):
     context = super(AllTargetAreas, self).get_context_data(**kwargs)
     druid_result = get_druid_data(dimensions=[
         'target_area_id', 'target_area_name', 'target_area_structures',
         'rhc_name', 'district_name'
     ])
     data, totals = process_druid_data(druid_result)
     context['data'] = data
     context.update(DEFINITIONS['ta'])
     # update data with numbers of any missing target areas
     ids_present = []
     for x in data:
         try:
             ids_present.append(int(x['target_area_id']))
         except TypeError:
             pass
     missing = Location.objects.select_related('parent').filter(
         level='ta').exclude(id__in=ids_present).values(
             'name', 'parent__name', 'parent__parent__name', 'structures')
     if missing:
         for m in missing:
             totals['structures'] += m['structures']
         context['missing_target_areas'] = missing
     context['totals'] = calculate_target_area_totals(totals)
     return context
예제 #5
0
    def get_context_data(self, **kwargs):
        context = super(DistrictMap, self).get_context_data(**kwargs)
        druid_result = get_druid_data(
            dimensions=[
                'target_area_id', 'target_area_name', 'target_area_structures',
                'rhc_name', 'district_name', 'rhc_id', 'district_id'
            ],
            filter_list=[['district_id', operator.eq, self.object.pk]])
        data, totals = process_druid_data(druid_result)

        district_druid_data = process_location_data(self.object.__dict__, data)

        district_data = AreaSerializer(self.object,
                                       druid_data=district_druid_data).data

        rhc_druid_data_list = []
        rhc_list = Location.objects.filter(level='RHC', parent=self.object)
        for rhc in rhc_list:
            rhc_druid_data = [x for x in data if x['rhc_id'] == str(rhc.id)]
            rhc_data = process_location_data(rhc.__dict__, rhc_druid_data)
            rhc_data['rhc_id'] = rhc.id
            rhc_druid_data_list.append(rhc_data)

        rhc_geojson_data = AreaSerializer(rhc_list,
                                          druid_data=rhc_druid_data_list,
                                          many=True).data
        context['district_data'] = JSONRenderer().render(district_data)
        context['hh_geojson'] = JSONRenderer().render(rhc_geojson_data)
        return context
예제 #6
0
    def get_context_data(self, **kwargs):
        context = super(RHCMap, self).get_context_data(**kwargs)
        druid_result = get_druid_data(
            filter_list=[['rhc_id', operator.eq, self.object.pk]])
        data, totals = process_druid_data(druid_result)
        rhc_druid_data = process_location_data(self.object.__dict__, data)

        rhc_data = AreaSerializer(self.object, druid_data=rhc_druid_data).data

        ta_data = TargetAreaSerializer(
            self.object.get_children().filter(level='ta'),
            druid_data=data,
            many=True).data

        context['rhc_data'] = JSONRenderer().render(rhc_data)
        context['hh_geojson'] = JSONRenderer().render(ta_data)
        return context
예제 #7
0
 def get_context_data(self, **kwargs):
     context = super(DistrictView, self).get_context_data(**kwargs)
     context.update(DEFINITIONS['RHC'])
     druid_result = get_druid_data(dimensions=[
         'target_area_id', 'target_area_name', 'target_area_structures',
         'rhc_name', 'district_name', 'rhc_id'
     ])
     data, totals = process_druid_data(druid_result)
     object_list = Location.objects.filter(level='RHC',
                                           parent=self.object).values(
                                               'id', 'name', 'structures',
                                               'level')
     for rhc in object_list:
         rhc_data = [x for x in data if x['rhc_id'] == str(rhc['id'])]
         result = process_location_data(rhc, rhc_data)
         rhc.update(result)
     context['object_list'] = object_list
     return context
예제 #8
0
 def get_context_data(self, **kwargs):
     context = super(Home, self).get_context_data(**kwargs)
     context.update(DEFINITIONS['district'])
     druid_result = get_druid_data(dimensions=[
         'target_area_id', 'target_area_name', 'target_area_structures',
         'rhc_name', 'district_name', 'district_id'
     ])
     data, totals = process_druid_data(druid_result)
     object_list = Location.objects.filter(level='district',
                                           parent=None).values(
                                               'id', 'name', 'structures',
                                               'level')
     for district in object_list:
         district_data = [
             x for x in data if x['district_id'] == str(district['id'])
         ]
         result = process_location_data(district, district_data)
         district.update(result)
     context['object_list'] = object_list
     return context
예제 #9
0
 def get_context_data(self, **kwargs):
     context = super(RHCView, self).get_context_data(**kwargs)
     druid_result = get_druid_data(
         filter_list=[['rhc_id', operator.eq, self.object.pk]])
     data, totals = process_druid_data(druid_result)
     context['data'] = data
     context.update(DEFINITIONS['ta'])
     # update data with numbers of any missing target areas
     ids_present = []
     for x in data:
         try:
             ids_present.append(int(x['target_area_id']))
         except TypeError:
             pass
     missing = self.object.get_children().exclude(
         id__in=ids_present).values('name', 'structures')
     if missing:
         for m in missing:
             totals['structures'] += m['structures']
         context['missing_target_areas'] = missing
     context['totals'] = calculate_target_area_totals(totals)
     return context
예제 #10
0
    def get_context_data(self, **kwargs):
        context = super(TargetAreaMap, self).get_context_data(**kwargs)
        spray_date = parse_spray_date(self.request)
        filter_list = [['target_area_id', operator.eq, self.object.pk]]
        if spray_date:
            filter_list.append([
                'spray_date', operator.eq,
                self.request.GET.get('spray_date')
            ])
        druid_result = get_druid_data(filter_list=filter_list,
                                      dimensions=[
                                          'target_area_id', 'target_area_name',
                                          'target_area_structures',
                                          'district_name', 'district_id',
                                          'rhc_name', 'rhc_id'
                                      ])
        data, totals = process_druid_data(druid_result)
        if data:
            ta_data = TargetAreaSerializer(self.object,
                                           druid_data=data[0]).data
        else:
            ta_data = TargetAreaSerializer(self.object).data

        sprayed_duplicates = get_duplicates(ta_pk=self.object.id, sprayed=True)
        not_sprayed_duplicates = get_duplicates(ta_pk=self.object.id,
                                                sprayed=False)
        context['sprayed_duplicates'] = len(sprayed_duplicates)
        context['sprayed_duplicates_data'] = sprayed_duplicates
        context['not_sprayed_duplicates'] = len(not_sprayed_duplicates)
        context['not_sprayed_duplicates_data'] = not_sprayed_duplicates
        context['spray_dates'] = ta_data['properties']['spray_dates']
        context['spray_date'] = spray_date
        context['target_data'] = JSONRenderer().render(ta_data)
        context['districts'] = Location.objects.filter(parent=None)\
            .values_list('id', 'code', 'name').order_by('name')

        context.update({'map_menu': True})
        context.update(get_location_dict(self.object.pk))
        return context
예제 #11
0
def daily_found_coverage_by_spray_area(district_id, spray_date):
    """
    Gets spray coverage data for spray areas in a district and compares this
    with the same data for other areas, as well as data from the same spray
    area for different dates.
    Then prepares a payload to send to RapidPro for further proccessing.

    spray_date format == 2016-10-15
    """
    try:
        this_district = Location.objects.get(id=district_id)
    except Location.DoesNotExist:
        pass
    else:
        flow_uuid = settings.RAPIDPRO_DAILY_FOUND_COVERAGE_FLOW_ID
        target_areas = this_district.get_descendants().filter(level="ta")
        for target_area in target_areas:
            dimensions = [
                "target_area_id",
                "target_area_name",
                "target_area_structures",
                "rhc_id",
                "rhc_name",
                "district_id",
                "district_name",
            ]

            filters = [
                ["target_area_id", operator.eq, target_area.id],
                ["spray_date", operator.eq, spray_date],
            ]
            # get today's data for this spray area
            druid_result1 = get_druid_data(dimensions, filters)
            today_data, _ = process_druid_data(druid_result1)
            # get today's data for all other spray areas
            druid_result2 = get_druid_data(
                dimensions=dimensions,
                filter_list=[
                    ["target_area_id", operator.ne, target_area.id],
                    ["spray_date", operator.eq, spray_date],
                ],
            )
            other_data, _ = process_druid_data(druid_result2)
            # get all data for this area for other dates
            druid_result3 = get_druid_data(
                dimensions=dimensions,
                filter_list=[
                    ["target_area_id", operator.eq, target_area.id],
                    ["spray_date", operator.ne, spray_date],
                ],
            )
            all_data, _ = process_druid_data(druid_result3)
            # prepare payload
            payload_source_data = {}
            if all_data:
                payload_source_data = all_data[0]

            if today_data:
                payload_source_data["has_submissions_today"] = 1
            else:
                payload_source_data["has_submissions_today"] = 0

            if other_data:
                payload_source_data["today_is_working_day"] = 1
            else:
                payload_source_data["today_is_working_day"] = 0

            payload = FoundCoverageSerializer(payload_source_data,
                                              date=spray_date,
                                              target_area=target_area)

            return start_flow(flow_uuid, payload.data)
예제 #12
0
def health_facility_catchment(spray_day_obj_id, force=False):
    """
    Checks each submission for evidence that it represents data from a new
    RHC.  If this is the case, then we send a summary of the 'previous RHC'
    """

    try:
        spray_day_obj = SprayDay.objects.get(pk=spray_day_obj_id)
    except SprayDay.DoesNotExist:
        pass
    else:
        current_rhc = spray_day_obj.location.parent

        if current_rhc:
            should_continue = False

            if force:
                should_continue = True
            else:
                threshold = settings.HEALTH_FACILITY_CATCHMENT_THRESHOLD
                upper_threshold = threshold * 2
                # count number of records for this RHC
                current_rhc_records = SprayDay.objects.filter(
                    location__parent=current_rhc).count()
                if upper_threshold >= current_rhc_records >= threshold:
                    should_continue = True

            if should_continue:
                # get previous RHC by looking for RHC with records from a
                # previous date
                previous_record = (SprayDay.objects.exclude(
                    location__parent__id=current_rhc.id).filter(
                        location__parent__parent__id=current_rhc.parent.id
                    ).filter(spray_date__lt=spray_day_obj.spray_date).order_by(
                        "-spray_date").first())
                previous_rhc = None
                if previous_record:
                    previous_rhc = previous_record.location.parent
                if previous_rhc:
                    # get summary data and send to flow
                    dimensions = [
                        "target_area_id",
                        "target_area_name",
                        "target_area_structures",
                        "rhc_id",
                        "rhc_name",
                        "district_id",
                        "district_name",
                    ]
                    druid_result = get_druid_data(
                        dimensions, [["rhc_id", operator.eq, previous_rhc.id]])
                    data, _ = process_druid_data(druid_result)
                    payload = process_location_data(previous_rhc.__dict__,
                                                    data)
                    payload["rhc_id"] = previous_rhc.id
                    payload["rhc_name"] = previous_rhc.name
                    payload["sprayed_coverage"] = int(
                        payload["sprayed_coverage"])
                    payload["sprayed_percentage"] = int(
                        payload["sprayed_percentage"])
                    payload["visited_percentage"] = int(
                        payload["visited_percentage"])
                    if previous_rhc.parent:
                        payload["district_id"] = previous_rhc.parent.id
                        payload["district_name"] = previous_rhc.parent.name

                    return start_flow(settings.RAPIDPRO_HF_CATCHMENT_FLOW_ID,
                                      payload)
    return None