Beispiel #1
0
def get_demographics_data(domain,
                          now_date,
                          config,
                          show_test=False,
                          beta=False):
    now_date = datetime(*now_date)
    current_month = datetime(*config['month'])
    previous_month = datetime(*config['prev_month'])
    del config['month']
    del config['prev_month']

    def get_data_for(query_class, filters):
        queryset = query_class.objects.filter(
            **filters).values('aggregation_level').annotate(
                household=Sum('cases_household'),
                child_health=Sum('cases_child_health'),
                child_health_all=Sum('cases_child_health_all'),
                ccs_pregnant=Sum('cases_ccs_pregnant'),
                ccs_pregnant_all=Sum('cases_ccs_pregnant_all'),
                css_lactating=Sum('cases_ccs_lactating'),
                css_lactating_all=Sum('cases_ccs_lactating_all'),
                person_adolescent=Sum('cases_person_adolescent_girls_11_14'),
                person_adolescent_all=Sum(
                    'cases_person_adolescent_girls_11_14_all'),
                person_aadhaar=Sum(person_has_aadhaar_column(beta)),
                all_persons=Sum(person_is_beneficiary_column(beta)))

        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    if current_month.month == now_date.month and current_month.year == now_date.year:
        config['date'] = now_date.date()
        data = None
        # keep the record in searched - current - month
        while data is None or (not data and config['date'].day != 1):
            config['date'] -= relativedelta(days=1)
            data = get_data_for(AggAwcDailyView, config)
        prev_data = None
        while prev_data is None or (not prev_data and config['date'].day != 1):
            config['date'] -= relativedelta(days=1)
            prev_data = get_data_for(AggAwcDailyView, config)
        frequency = 'day'
    else:
        config['month'] = current_month
        data = get_data_for(AggAwcMonthly, config)
        config['month'] = previous_month
        prev_data = get_data_for(AggAwcMonthly, config)
        frequency = 'month'

    return {
        'records':
        [[{
            'label':
            _('Registered Households'),
            'help_text':
            _('Total number of households registered'),
            'percent':
            percent_increase('household', data, prev_data),
            'color':
            get_color_with_green_positive(
                percent_increase('household', data, prev_data)),
            'value':
            get_value(data, 'household'),
            'all':
            None,
            'format':
            'number',
            'frequency':
            frequency,
            'redirect':
            'demographics/registered_household'
        }, {
            'label':
            _(AADHAR_SEEDED_BENEFICIARIES),
            'help_text':
            percent_aadhaar_seeded_beneficiaries_help_text(),
            'percent':
            percent_diff('person_aadhaar', data, prev_data, 'all_persons'),
            'color':
            get_color_with_green_positive(
                percent_diff('person_aadhaar', data, prev_data,
                             'all_persons')),
            'value':
            get_value(data, 'person_aadhaar'),
            'all':
            get_value(data, 'all_persons'),
            'format':
            'percent_and_div',
            'frequency':
            frequency,
            'redirect':
            'demographics/adhaar'
        }],
         [{
             'label':
             _(CHILDREN_ENROLLED_FOR_ANGANWADI_SERVICES),
             'help_text':
             percent_children_enrolled_help_text(),
             'percent':
             percent_diff('child_health', data, prev_data, 'child_health_all'),
             'color':
             get_color_with_green_positive(
                 percent_diff('child_health', data, prev_data,
                              'child_health_all')),
             'value':
             get_value(data, 'child_health'),
             'all':
             get_value(data, 'child_health_all'),
             'format':
             'percent_and_div',
             'frequency':
             frequency,
             'redirect':
             'demographics/enrolled_children'
         }, {
             'label':
             _(PREGNANT_WOMEN_ENROLLED_FOR_ANGANWADI_SERVICES),
             'help_text':
             percent_pregnant_women_enrolled_help_text(),
             'percent':
             percent_diff('ccs_pregnant', data, prev_data, 'ccs_pregnant_all'),
             'color':
             get_color_with_green_positive(
                 percent_diff('ccs_pregnant', data, prev_data,
                              'ccs_pregnant_all')),
             'value':
             get_value(data, 'ccs_pregnant'),
             'all':
             get_value(data, 'ccs_pregnant_all'),
             'format':
             'percent_and_div',
             'frequency':
             frequency,
             'redirect':
             'demographics/enrolled_women'
         }],
         [{
             'label':
             _(LACTATING_WOMEN_ENROLLED_FOR_ANGANWADI_SERVICES),
             'help_text':
             percent_lactating_women_enrolled_help_text(),
             'percent':
             percent_diff('css_lactating', data, prev_data,
                          'css_lactating_all'),
             'color':
             get_color_with_green_positive(
                 percent_diff('css_lactating', data, prev_data,
                              'css_lactating_all')),
             'value':
             get_value(data, 'css_lactating'),
             'all':
             get_value(data, 'css_lactating_all'),
             'format':
             'percent_and_div',
             'frequency':
             frequency,
             'redirect':
             'demographics/lactating_enrolled_women'
         }, {
             'label':
             _(ADOLESCENT_GIRLS_ENROLLED_FOR_ANGANWADI_SERVICES),
             'help_text':
             percent_adolescent_girls_enrolled_help_text(),
             'percent':
             percent_diff('person_adolescent', data, prev_data,
                          'person_adolescent_all'),
             'color':
             get_color_with_green_positive(
                 percent_diff('person_adolescent', data, prev_data,
                              'person_adolescent_all')),
             'value':
             get_value(data, 'person_adolescent'),
             'all':
             get_value(data, 'person_adolescent_all'),
             'format':
             'percent_and_div',
             'frequency':
             frequency,
             'redirect':
             'demographics/adolescent_girls'
         }]]
    }
def get_awc_infrastructure_data(domain, config, show_test=False):
    def get_data_for(month, filters):
        queryset = AggAwcMonthly.objects.filter(
            month=month, **filters
        ).values(
            'aggregation_level'
        ).annotate(
            clean_water=Sum('infra_clean_water'),
            functional_toilet=Sum('infra_functional_toilet'),
            medicine_kits=Sum('infra_medicine_kits'),
            infant_scale=Sum('infra_infant_weighing_scale'),
            adult_scale=Sum('infra_adult_weighing_scale'),
            sum_last_update=Sum('num_awc_infra_last_update')
        )

        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    current_month = datetime(*config['month'])
    previous_month = datetime(*config['prev_month'])
    del config['month']
    del config['prev_month']

    this_month_data = get_data_for(current_month, config)
    prev_month_data = get_data_for(previous_month, config)

    return {
        'records': [
            [
                {
                    'label': _('AWCs Reported Clean Drinking Water'),
                    'help_text': awcs_reported_clean_drinking_water_help_text(),
                    'percent': percent_diff(
                        'clean_water',
                        this_month_data,
                        prev_month_data,
                        'sum_last_update'
                    ),
                    'color': get_color_with_green_positive(percent_diff(
                        'clean_water',
                        this_month_data,
                        prev_month_data,
                        'sum_last_update'
                    )),
                    'value': get_value(this_month_data, 'clean_water'),
                    'all': get_value(this_month_data, 'sum_last_update'),
                    'format': 'percent_and_div',
                    'frequency': 'month',
                    'redirect': 'awc_infrastructure/clean_water'
                },
                {
                    'label': _("AWCs Reported Functional Toilet"),
                    'help_text': awcs_reported_functional_toilet_help_text(),
                    'percent': percent_diff(
                        'functional_toilet',
                        this_month_data,
                        prev_month_data,
                        'sum_last_update'
                    ),
                    'color': get_color_with_green_positive(percent_diff(
                        'functional_toilet',
                        this_month_data,
                        prev_month_data,
                        'sum_last_update'
                    )),
                    'value': get_value(this_month_data, 'functional_toilet'),
                    'all': get_value(this_month_data, 'sum_last_update'),
                    'format': 'percent_and_div',
                    'frequency': 'month',
                    'redirect': 'awc_infrastructure/functional_toilet'
                }
            ],
            [
                # {
                #     'label': _('AWCs with Electricity'),
                #     'help_text': _('Percentage of AWCs with access to electricity'),
                #     'percent': 0,
                #     'value': 0,
                #     'all': 0,
                #     'format': 'percent_and_div',
                #     'frequency': 'month'
                # },
                {
                    'label': _('AWCs Reported Weighing Scale: Infants'),
                    'help_text': awcs_reported_weighing_scale_infants_help_text(),
                    'percent': percent_diff(
                        'infant_scale',
                        this_month_data,
                        prev_month_data,
                        'sum_last_update'
                    ),
                    'color': get_color_with_green_positive(percent_diff(
                        'infant_scale',
                        this_month_data,
                        prev_month_data,
                        'sum_last_update'
                    )),
                    'value': get_value(this_month_data, 'infant_scale'),
                    'all': get_value(this_month_data, 'sum_last_update'),
                    'format': 'percent_and_div',
                    'frequency': 'month',
                    'redirect': 'awc_infrastructure/infants_weight_scale'
                },
                {
                    'label': _('AWCs Reported Weighing Scale: Mother and Child'),
                    'help_text': awcs_reported_weighing_scale_mother_and_child_help_text(),
                    'percent': percent_diff(
                        'adult_scale',
                        this_month_data,
                        prev_month_data,
                        'sum_last_update'
                    ),
                    'color': get_color_with_green_positive(percent_diff(
                        'adult_scale',
                        this_month_data,
                        prev_month_data,
                        'sum_last_update'
                    )),
                    'value': get_value(this_month_data, 'adult_scale'),
                    'all': get_value(this_month_data, 'sum_last_update'),
                    'format': 'percent_and_div',
                    'frequency': 'month',
                    'redirect': 'awc_infrastructure/adult_weight_scale'
                }
            ],
            [
                {
                    'label': _('AWCs Reported Medicine Kit'),
                    'help_text': awcs_reported_medicine_kit_help_text(),
                    'percent': percent_diff(
                        'medicine_kits',
                        this_month_data,
                        prev_month_data,
                        'sum_last_update'
                    ),
                    'color': get_color_with_green_positive(percent_diff(
                        'medicine_kits',
                        this_month_data,
                        prev_month_data,
                        'sum_last_update'
                    )),
                    'value': get_value(this_month_data, 'medicine_kits'),
                    'all': get_value(this_month_data, 'sum_last_update'),
                    'format': 'percent_and_div',
                    'frequency': 'month',
                    'redirect': 'awc_infrastructure/medicine_kit'
                }
            ],
            # [
            #     {
            #         'label': _('AWCs with infantometer'),
            #         'help_text': _('Percentage of AWCs with an Infantometer'),
            #         'percent': 0,
            #         'value': 0,
            #         'all': 0,
            #         'format': 'percent_and_div',
            #         'frequency': 'month'
            #     },
            #     {
            #         'label': _('AWCs with Stadiometer'),
            #         'help_text': _('Percentage of AWCs with a Stadiometer'),
            #         'percent': 0,
            #         'value': 0,
            #         'all': 0,
            #         'format': 'percent_and_div',
            #         'frequency': 'month'
            #     }
            # ]
        ]
    }
def get_cas_reach_data(domain, now_date, config, show_test=False):
    now_date = datetime(*now_date)

    def get_data_for_awc_monthly(month, filters):
        level = filters['aggregation_level']
        queryset = AggAwcMonthly.objects.filter(
            month=month, **filters).values('aggregation_level').annotate(
                states=Sum('num_launched_states')
                if level <= 1 else Max('num_launched_states'),
                districts=Sum('num_launched_districts')
                if level <= 2 else Max('num_launched_districts'),
                blocks=Sum('num_launched_blocks')
                if level <= 3 else Max('num_launched_blocks'),
                sectors=Sum('num_launched_supervisors')
                if level <= 4 else Max('num_launched_supervisors'),
                awc_num_open=Sum('awc_num_open')
                if level <= 5 else Max('awc_num_open'),
                awcs=Sum('num_launched_awcs')
                if level <= 5 else Max('num_launched_awcs'),
                all_awcs=Sum('num_awcs') if level <= 5 else Max('num_awcs'))
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    def get_data_for_daily_usage(date, filters):
        queryset = AggAwcDailyView.objects.filter(
            date=date, **filters).values('aggregation_level').annotate(
                daily_attendance=Sum('daily_attendance_open'))
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    def get_data_for_ls_launched(month, filters):
        queryset = SystemUsageReportView.objects.filter(
            month=month, **filters).values('aggregation_level').annotate(
                ls_launched=Sum('num_supervisor_launched'))
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    current_month = datetime(*config['month'])
    previous_month = datetime(*config['prev_month'])
    del config['month']
    del config['prev_month']

    awc_this_month_data = get_data_for_awc_monthly(current_month, config)
    awcs = get_value(awc_this_month_data, 'awcs')

    current_month_selected = (current_month.year == now_date.year
                              and current_month.month == now_date.month)

    if current_month_selected:
        date = now_date.date()
        daily_yesterday = None
        # keep the record in searched - current - month
        while daily_yesterday is None or (not daily_yesterday
                                          and date.day != 1):
            date -= relativedelta(days=1)
            daily_yesterday = get_data_for_daily_usage(date, config)
        daily_two_days_ago = None
        while daily_two_days_ago is None or (not daily_two_days_ago
                                             and date.day != 1):
            date -= relativedelta(days=1)
            daily_two_days_ago = get_data_for_daily_usage(date, config)
        daily_attendance_percent = percent_increase('daily_attendance',
                                                    daily_yesterday,
                                                    daily_two_days_ago)
        number_of_awc_open_yesterday = {
            'label':
            _('Number of AWCs Open yesterday'),
            'help_text':
            _(("Total Number of Angwanwadi Centers that were open yesterday "
               "by the AWW or the AWW helper")),
            'color':
            get_color_with_green_positive(daily_attendance_percent),
            'percent':
            daily_attendance_percent,
            'value':
            get_value(daily_yesterday, 'daily_attendance'),
            'all':
            awcs,
            'format':
            'number_and_percent',
            'frequency':
            'day',
            'redirect':
            'icds_cas_reach/awc_daily_status',
        }
    else:
        awc_prev_month_data = get_data_for_awc_monthly(previous_month, config)
        monthly_attendance_percent = percent_increase('awc_num_open',
                                                      awc_this_month_data,
                                                      awc_prev_month_data)
        number_of_awc_open_yesterday = {
            'help_text':
            _("Total Number of AWCs open for at least one day in month"),
            'label':
            _('Number of AWCs open for at least one day in month'),
            'color':
            get_color_with_green_positive(monthly_attendance_percent),
            'percent':
            monthly_attendance_percent,
            'value':
            get_value(awc_this_month_data, 'awc_num_open'),
            'all':
            awcs,
            'format':
            'number_and_percent',
            'frequency':
            'month',
        }

    awcs_launched = {
        'label': _('AWCs Launched'),
        'help_text': awcs_launched_help_text(),
        'color': None,
        'percent': None,
        'value': awcs,
        'all': None,
        'format': 'number',
        'frequency': 'month',
        'redirect': 'icds_cas_reach/awcs_covered'
    }

    sectors_covered = {
        'label': _('Sectors covered'),
        'help_text': _('Total Sectors that have launched ICDS CAS'),
        'percent': None,
        'value': get_value(awc_this_month_data, 'sectors'),
        'all': None,
        'format': 'number',
        'frequency': 'month',
    }

    blocks_covered = {
        'label': _('Blocks covered'),
        'help_text': _('Total Blocks that have launched ICDS CAS'),
        'percent': None,
        'value': get_value(awc_this_month_data, 'blocks'),
        'all': None,
        'format': 'number',
        'frequency': 'month',
    }

    districts_covered = {
        'label': _('Districts covered'),
        'help_text': _('Total Districts that have launched ICDS CAS'),
        'percent': None,
        'value': get_value(awc_this_month_data, 'districts'),
        'all': None,
        'format': 'number',
        'frequency': 'month',
    }

    states_covered = {
        'label': _('States/UTs covered'),
        'help_text': _('Total States that have launched ICDS CAS'),
        'percent': None,
        'value': get_value(awc_this_month_data, 'states'),
        'all': None,
        'format': 'number',
        'frequency': 'month',
    }

    ls_launched_data = get_data_for_ls_launched(current_month, config)
    number_of_lss_launched = {
        'label': _('LSs Launched'),
        'help_text': ls_launched_help_text(),
        'color': None,
        'percent': None,
        'value': get_value(ls_launched_data, 'ls_launched'),
        'all': None,
        'format': 'number',
        'frequency': 'month',
        'redirect': 'icds_cas_reach/ls_launched'
    }

    cas_reach_records = [[awcs_launched, number_of_awc_open_yesterday],
                         [number_of_lss_launched, sectors_covered],
                         [blocks_covered, districts_covered], [states_covered]]

    return {'records': cas_reach_records}
Beispiel #4
0
def get_maternal_child_data(domain,
                            config,
                            show_test=False,
                            icds_feature_flag=False):
    def get_data_for_child_health_monthly(date, filters):

        age_filters = {
            'age_tranche': 72
        } if icds_feature_flag else {
            'age_tranche__in': [0, 6, 72]
        }

        moderately_underweight = exclude_records_by_age_for_column(
            {'age_tranche': 72}, 'nutrition_status_moderately_underweight')
        severely_underweight = exclude_records_by_age_for_column(
            {'age_tranche': 72}, 'nutrition_status_severely_underweight')
        wasting_moderate = exclude_records_by_age_for_column(
            age_filters, wasting_moderate_column(icds_feature_flag))
        wasting_severe = exclude_records_by_age_for_column(
            age_filters, wasting_severe_column(icds_feature_flag))
        stunting_moderate = exclude_records_by_age_for_column(
            age_filters, stunting_moderate_column(icds_feature_flag))
        stunting_severe = exclude_records_by_age_for_column(
            age_filters, stunting_severe_column(icds_feature_flag))
        nutrition_status_weighed = exclude_records_by_age_for_column(
            {'age_tranche': 72}, 'nutrition_status_weighed')
        height_measured_in_month = exclude_records_by_age_for_column(
            age_filters, hfa_recorded_in_month_column(icds_feature_flag))
        weighed_and_height_measured_in_month = exclude_records_by_age_for_column(
            age_filters, wfh_recorded_in_month_column(icds_feature_flag))

        queryset = AggChildHealthMonthly.objects.filter(
            month=date, **filters).values('aggregation_level').annotate(
                underweight=(Sum(moderately_underweight) +
                             Sum(severely_underweight)),
                valid=Sum(nutrition_status_weighed),
                wasting=Sum(wasting_moderate) + Sum(wasting_severe),
                stunting=Sum(stunting_moderate) + Sum(stunting_severe),
                height_measured_in_month=Sum(height_measured_in_month),
                weighed_and_height_measured_in_month=Sum(
                    weighed_and_height_measured_in_month),
                low_birth_weight=Sum('low_birth_weight_in_month'),
                bf_birth=Sum('bf_at_birth'),
                born=Sum('born_in_month'),
                weighed_and_born_in_month=Sum('weighed_and_born_in_month'),
                ebf=Sum('ebf_in_month'),
                ebf_eli=Sum('ebf_eligible'),
                cf_initiation=Sum('cf_initiation_in_month'),
                cf_initiation_eli=Sum('cf_initiation_eligible'))
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    def get_data_for_deliveries(date, filters):
        queryset = AggCcsRecordMonthly.objects.filter(
            month=date, **filters).values('aggregation_level').annotate(
                institutional_delivery=Sum('institutional_delivery_in_month'),
                delivered=Sum('delivered_in_month'))
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    current_month = datetime(*config['month'])
    previous_month = datetime(*config['prev_month'])
    del config['month']
    del config['prev_month']

    this_month_data = get_data_for_child_health_monthly(current_month, config)
    prev_month_data = get_data_for_child_health_monthly(previous_month, config)

    deliveries_this_month = get_data_for_deliveries(current_month, config)
    deliveries_prev_month = get_data_for_deliveries(previous_month, config)

    gender_label, age_label, chosen_filters = chosen_filters_to_labels(
        config, default_interval=default_age_interval(icds_feature_flag))

    return {
        'records':
        [[{
            'label':
            _('Underweight (Weight-for-Age)'),
            'help_text':
            underweight_children_help_text(),
            'percent':
            percent_diff('underweight', this_month_data, prev_month_data,
                         'valid'),
            'color':
            'red' if percent_diff('underweight', this_month_data,
                                  prev_month_data, 'valid') > 0 else 'green',
            'value':
            get_value(this_month_data, 'underweight'),
            'all':
            get_value(this_month_data, 'valid'),
            'format':
            'percent_and_div',
            'frequency':
            'month',
            'redirect':
            'maternal_and_child/underweight_children'
        }, {
            'label':
            _('Wasting (Weight-for-Height)'),
            'help_text':
            _(wasting_help_text(age_label)),
            'percent':
            percent_diff('wasting', this_month_data, prev_month_data,
                         'weighed_and_height_measured_in_month'),
            'color':
            'red' if percent_diff('wasting', this_month_data, prev_month_data,
                                  'weighed_and_height_measured_in_month') > 0
            else 'green',
            'value':
            get_value(this_month_data, 'wasting'),
            'all':
            get_value(this_month_data, 'weighed_and_height_measured_in_month'),
            'format':
            'percent_and_div',
            'frequency':
            'month',
            'redirect':
            'maternal_and_child/wasting'
        }],
         [{
             'label':
             _('Stunting (Height-for-Age)'),
             'help_text':
             _(stunting_help_text(age_label)),
             'percent':
             percent_diff('stunting', this_month_data, prev_month_data,
                          'height_measured_in_month'),
             'color':
             'red'
             if percent_diff('stunting', this_month_data, prev_month_data,
                             'height_measured_in_month') > 0 else 'green',
             'value':
             get_value(this_month_data, 'stunting'),
             'all':
             get_value(this_month_data, 'height_measured_in_month'),
             'format':
             'percent_and_div',
             'frequency':
             'month',
             'redirect':
             'maternal_and_child/stunting'
         }, {
             'label':
             _('Newborns with Low Birth Weight'),
             'help_text':
             _((new_born_with_low_weight_help_text(html=False))),
             'percent':
             percent_diff('low_birth_weight', this_month_data, prev_month_data,
                          'weighed_and_born_in_month'),
             'color':
             get_color_with_red_positive(
                 percent_diff('low_birth_weight', this_month_data,
                              prev_month_data, 'weighed_and_born_in_month')),
             'value':
             get_value(this_month_data, 'low_birth_weight'),
             'all':
             get_value(this_month_data, 'weighed_and_born_in_month'),
             'format':
             'percent_and_div',
             'frequency':
             'month',
             'redirect':
             'maternal_and_child/low_birth'
         }],
         [{
             'label':
             _('Early Initiation of Breastfeeding'),
             'help_text':
             early_initiation_breastfeeding_help_text(),
             'percent':
             percent_diff('bf_birth', this_month_data, prev_month_data,
                          'born'),
             'color':
             get_color_with_green_positive(
                 percent_diff('bf_birth', this_month_data, prev_month_data,
                              'born')),
             'value':
             get_value(this_month_data, 'bf_birth'),
             'all':
             get_value(this_month_data, 'born'),
             'format':
             'percent_and_div',
             'frequency':
             'month',
             'redirect':
             'maternal_and_child/early_initiation'
         }, {
             'label':
             _('Exclusive Breastfeeding'),
             'help_text':
             exclusive_breastfeeding_help_text(),
             'percent':
             percent_diff('ebf', this_month_data, prev_month_data, 'ebf_eli'),
             'color':
             get_color_with_green_positive(
                 percent_diff('ebf', this_month_data, prev_month_data,
                              'ebf_eli')),
             'value':
             get_value(this_month_data, 'ebf'),
             'all':
             get_value(this_month_data, 'ebf_eli'),
             'format':
             'percent_and_div',
             'frequency':
             'month',
             'redirect':
             'maternal_and_child/exclusive_breastfeeding'
         }],
         [{
             'label':
             _('Children initiated appropriate Complementary Feeding'),
             'help_text':
             children_initiated_appropriate_complementary_feeding_help_text(),
             'percent':
             percent_diff('cf_initiation', this_month_data, prev_month_data,
                          'cf_initiation_eli'),
             'color':
             get_color_with_green_positive(
                 percent_diff('cf_initiation', this_month_data,
                              prev_month_data, 'cf_initiation_eli')),
             'value':
             get_value(this_month_data, 'cf_initiation'),
             'all':
             get_value(this_month_data, 'cf_initiation_eli'),
             'format':
             'percent_and_div',
             'frequency':
             'month',
             'redirect':
             'maternal_and_child/children_initiated'
         }, {
             'label':
             _('Institutional Deliveries'),
             'help_text':
             institutional_deliveries_help_text(),
             'percent':
             percent_diff('institutional_delivery', deliveries_this_month,
                          deliveries_prev_month, 'delivered'),
             'color':
             get_color_with_green_positive(
                 percent_diff('institutional_delivery', deliveries_this_month,
                              deliveries_prev_month, 'delivered')),
             'value':
             get_value(deliveries_this_month, 'institutional_delivery'),
             'all':
             get_value(deliveries_this_month, 'delivered'),
             'format':
             'percent_and_div',
             'frequency':
             'month',
             'redirect':
             'maternal_and_child/institutional_deliveries'
         }]]
    }
Beispiel #5
0
def get_cas_reach_data(domain, now_date, config, show_test=False):
    now_date = datetime(*now_date)

    def get_data_for_awc_monthly(month, filters):
        level = filters['aggregation_level']
        queryset = AggAwcMonthly.objects.filter(
            month=month, **filters
        ).values(
            'aggregation_level'
        ).annotate(
            states=Sum('num_launched_states') if level <= 1 else Max('num_launched_states'),
            districts=Sum('num_launched_districts') if level <= 2 else Max('num_launched_districts'),
            blocks=Sum('num_launched_blocks') if level <= 3 else Max('num_launched_blocks'),
            supervisors=Sum('num_launched_supervisors') if level <= 4 else Max('num_launched_supervisors'),
            awc_num_open=Sum('awc_num_open') if level <= 5 else Max('awc_num_open'),
            awcs=Sum('num_launched_awcs') if level <= 5 else Max('num_launched_awcs'),
            all_awcs=Sum('num_awcs') if level <= 5 else Max('num_awcs')
        )
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    def get_data_for_daily_usage(date, filters):
        queryset = AggAwcDailyView.objects.filter(
            date=date, **filters
        ).values(
            'aggregation_level'
        ).annotate(
            awcs=Sum('num_launched_awcs'),
            daily_attendance=Sum('daily_attendance_open')
        )
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    current_month = datetime(*config['month'])
    previous_month = datetime(*config['prev_month'])
    del config['month']
    del config['prev_month']

    awc_this_month_data = get_data_for_awc_monthly(current_month, config)
    awc_prev_month_data = get_data_for_awc_monthly(previous_month, config)

    current_month_selected = (current_month.year == now_date.year and current_month.month == now_date.month)

    if current_month_selected:
        date = now_date.date()
        daily_yesterday = None
        # keep the record in searched - current - month
        while daily_yesterday is None or (not daily_yesterday and date.day != 1):
            date -= relativedelta(days=1)
            daily_yesterday = get_data_for_daily_usage(date, config)
        daily_two_days_ago = None
        while daily_two_days_ago is None or (not daily_two_days_ago and date.day != 1):
            date -= relativedelta(days=1)
            daily_two_days_ago = get_data_for_daily_usage(date, config)
        daily_attendance_percent = percent_increase('daily_attendance', daily_yesterday, daily_two_days_ago)
        number_of_awc_open_yesterday = {
            'label': _('Number of AWCs Open yesterday'),
            'help_text': _(("Total Number of Angwanwadi Centers that were open yesterday "
                            "by the AWW or the AWW helper")),
            'color': get_color_with_green_positive(daily_attendance_percent),
            'percent': daily_attendance_percent,
            'value': get_value(daily_yesterday, 'daily_attendance'),
            'all': get_value(daily_yesterday, 'awcs'),
            'format': 'div',
            'frequency': 'day',
            'redirect': 'icds_cas_reach/awc_daily_status',
        }
    else:
        monthly_attendance_percent = percent_increase('awc_num_open', awc_this_month_data, awc_prev_month_data)
        number_of_awc_open_yesterday = {
            'help_text': _("Total Number of AWCs open for at least one day in month"),
            'label': _('Number of AWCs open for at least one day in month'),
            'color': get_color_with_green_positive(monthly_attendance_percent),
            'percent': monthly_attendance_percent,
            'value': get_value(awc_this_month_data, 'awc_num_open'),
            'all': get_value(awc_this_month_data, 'awcs'),
            'format': 'div',
            'frequency': 'month',
        }

    return {
        'records': [
            [
                {
                    'label': _('AWCs Launched'),
                    'help_text': awcs_launched_help_text(),
                    'color': None,
                    'percent': None,
                    'value': get_value(daily_yesterday if current_month_selected else awc_this_month_data, 'awcs'),
                    'all': None,
                    'format': 'number',
                    'frequency': 'month',
                    'redirect': 'icds_cas_reach/awcs_covered'
                },
                number_of_awc_open_yesterday
            ],
            [
                {
                    'label': _('Sectors covered'),
                    'help_text': _('Total Sectors that have launched ICDS CAS'),
                    'percent': None,
                    'value': get_value(awc_this_month_data, 'supervisors'),
                    'all': None,
                    'format': 'number',
                    'frequency': 'month',
                },
                {
                    'label': _('Blocks covered'),
                    'help_text': _('Total Blocks that have launched ICDS CAS'),
                    'percent': None,
                    'value': get_value(awc_this_month_data, 'blocks'),
                    'all': None,
                    'format': 'number',
                    'frequency': 'month',
                },
            ],
            [

                {
                    'label': _('Districts covered'),
                    'help_text': _('Total Districts that have launched ICDS CAS'),
                    'percent': None,
                    'value': get_value(awc_this_month_data, 'districts'),
                    'all': None,
                    'format': 'number',
                    'frequency': 'month',
                },
                {
                    'label': _('States/UTs covered'),
                    'help_text': _('Total States that have launched ICDS CAS'),
                    'percent': None,
                    'value': get_value(awc_this_month_data, 'states'),
                    'all': None,
                    'format': 'number',
                    'frequency': 'month',
                }
            ]
        ]
    }
Beispiel #6
0
def get_maternal_child_data(domain, config, show_test=False, icds_feature_flag=False):

    def get_data_for_child_health_monthly(date, filters):

        age_filters = {'age_tranche': 72} if icds_feature_flag else {'age_tranche__in': [0, 6, 72]}

        moderately_underweight = exclude_records_by_age_for_column(
            {'age_tranche': 72},
            'nutrition_status_moderately_underweight'
        )
        severely_underweight = exclude_records_by_age_for_column(
            {'age_tranche': 72},
            'nutrition_status_severely_underweight'
        )
        wasting_moderate = exclude_records_by_age_for_column(
            age_filters,
            wasting_moderate_column(icds_feature_flag)
        )
        wasting_severe = exclude_records_by_age_for_column(
            age_filters,
            wasting_severe_column(icds_feature_flag)
        )
        stunting_moderate = exclude_records_by_age_for_column(
            age_filters,
            stunting_moderate_column(icds_feature_flag)
        )
        stunting_severe = exclude_records_by_age_for_column(
            age_filters,
            stunting_severe_column(icds_feature_flag)
        )
        nutrition_status_weighed = exclude_records_by_age_for_column(
            {'age_tranche': 72},
            'nutrition_status_weighed'
        )
        height_measured_in_month = exclude_records_by_age_for_column(
            age_filters,
            hfa_recorded_in_month_column(icds_feature_flag)
        )
        weighed_and_height_measured_in_month = exclude_records_by_age_for_column(
            age_filters,
            wfh_recorded_in_month_column(icds_feature_flag)
        )

        queryset = AggChildHealthMonthly.objects.filter(
            month=date, **filters
        ).values(
            'aggregation_level'
        ).annotate(
            underweight=(
                Sum(moderately_underweight) + Sum(severely_underweight)
            ),
            valid=Sum(nutrition_status_weighed),
            wasting=Sum(wasting_moderate) + Sum(wasting_severe),
            stunting=Sum(stunting_moderate) + Sum(stunting_severe),
            height_measured_in_month=Sum(height_measured_in_month),
            weighed_and_height_measured_in_month=Sum(weighed_and_height_measured_in_month),
            low_birth_weight=Sum('low_birth_weight_in_month'),
            bf_birth=Sum('bf_at_birth'),
            born=Sum('born_in_month'),
            weighed_and_born_in_month=Sum('weighed_and_born_in_month'),
            ebf=Sum('ebf_in_month'),
            ebf_eli=Sum('ebf_eligible'),
            cf_initiation=Sum('cf_initiation_in_month'),
            cf_initiation_eli=Sum('cf_initiation_eligible')
        )
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    def get_data_for_deliveries(date, filters):
        queryset = AggCcsRecordMonthly.objects.filter(
            month=date, **filters
        ).values(
            'aggregation_level'
        ).annotate(
            institutional_delivery=Sum('institutional_delivery_in_month'),
            delivered=Sum('delivered_in_month')
        )
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    current_month = datetime(*config['month'])
    previous_month = datetime(*config['prev_month'])
    del config['month']
    del config['prev_month']

    this_month_data = get_data_for_child_health_monthly(current_month, config)
    prev_month_data = get_data_for_child_health_monthly(previous_month, config)

    deliveries_this_month = get_data_for_deliveries(current_month, config)
    deliveries_prev_month = get_data_for_deliveries(previous_month, config)

    gender_label, age_label, chosen_filters = chosen_filters_to_labels(
        config,
        default_interval=default_age_interval(icds_feature_flag)
    )

    return {
        'records': [
            [
                {
                    'label': _('Underweight (Weight-for-Age)'),
                    'help_text': underweight_children_help_text(),
                    'percent': percent_diff(
                        'underweight',
                        this_month_data,
                        prev_month_data,
                        'valid'
                    ),
                    'color': 'red' if percent_diff(
                        'underweight',
                        this_month_data,
                        prev_month_data,
                        'valid'
                    ) > 0 else 'green',
                    'value': get_value(this_month_data, 'underweight'),
                    'all': get_value(this_month_data, 'valid'),
                    'format': 'percent_and_div',
                    'frequency': 'month',
                    'redirect': 'maternal_and_child/underweight_children'
                },
                {
                    'label': _('Wasting (Weight-for-Height)'),
                    'help_text': _(wasting_help_text(age_label)),
                    'percent': percent_diff(
                        'wasting',
                        this_month_data,
                        prev_month_data,
                        'weighed_and_height_measured_in_month'
                    ),
                    'color': 'red' if percent_diff(
                        'wasting',
                        this_month_data,
                        prev_month_data,
                        'weighed_and_height_measured_in_month'
                    ) > 0 else 'green',
                    'value': get_value(this_month_data, 'wasting'),
                    'all': get_value(this_month_data, 'weighed_and_height_measured_in_month'),
                    'format': 'percent_and_div',
                    'frequency': 'month',
                    'redirect': 'maternal_and_child/wasting'
                }
            ],
            [
                {
                    'label': _('Stunting (Height-for-Age)'),
                    'help_text': _(stunting_help_text(age_label)),
                    'percent': percent_diff(
                        'stunting',
                        this_month_data,
                        prev_month_data,
                        'height_measured_in_month'
                    ),
                    'color': 'red' if percent_diff(
                        'stunting',
                        this_month_data,
                        prev_month_data,
                        'height_measured_in_month'
                    ) > 0 else 'green',
                    'value': get_value(this_month_data, 'stunting'),
                    'all': get_value(this_month_data, 'height_measured_in_month'),
                    'format': 'percent_and_div',
                    'frequency': 'month',
                    'redirect': 'maternal_and_child/stunting'
                },
                {
                    'label': _('Newborns with Low Birth Weight'),
                    'help_text': _((
                        new_born_with_low_weight_help_text(html=False)
                    )),
                    'percent': percent_diff(
                        'low_birth_weight',
                        this_month_data,
                        prev_month_data,
                        'weighed_and_born_in_month'
                    ),
                    'color': get_color_with_red_positive(percent_diff(
                        'low_birth_weight',
                        this_month_data,
                        prev_month_data,
                        'weighed_and_born_in_month'
                    )),
                    'value': get_value(this_month_data, 'low_birth_weight'),
                    'all': get_value(this_month_data, 'weighed_and_born_in_month'),
                    'format': 'percent_and_div',
                    'frequency': 'month',
                    'redirect': 'maternal_and_child/low_birth'
                }
            ],
            [
                {
                    'label': _('Early Initiation of Breastfeeding'),
                    'help_text': early_initiation_breastfeeding_help_text(),
                    'percent': percent_diff(
                        'bf_birth',
                        this_month_data,
                        prev_month_data,
                        'born'
                    ),
                    'color': get_color_with_green_positive(percent_diff(
                        'bf_birth',
                        this_month_data,
                        prev_month_data,
                        'born'
                    )),
                    'value': get_value(this_month_data, 'bf_birth'),
                    'all': get_value(this_month_data, 'born'),
                    'format': 'percent_and_div',
                    'frequency': 'month',
                    'redirect': 'maternal_and_child/early_initiation'
                },
                {
                    'label': _('Exclusive Breastfeeding'),
                    'help_text': exclusive_breastfeeding_help_text(),
                    'percent': percent_diff(
                        'ebf',
                        this_month_data,
                        prev_month_data,
                        'ebf_eli'
                    ),
                    'color': get_color_with_green_positive(percent_diff(
                        'ebf',
                        this_month_data,
                        prev_month_data,
                        'ebf_eli'
                    )),
                    'value': get_value(this_month_data, 'ebf'),
                    'all': get_value(this_month_data, 'ebf_eli'),
                    'format': 'percent_and_div',
                    'frequency': 'month',
                    'redirect': 'maternal_and_child/exclusive_breastfeeding'
                }
            ],
            [
                {
                    'label': _('Children initiated appropriate Complementary Feeding'),
                    'help_text': children_initiated_appropriate_complementary_feeding_help_text(),
                    'percent': percent_diff(
                        'cf_initiation',
                        this_month_data,
                        prev_month_data,
                        'cf_initiation_eli'
                    ),
                    'color': get_color_with_green_positive(percent_diff(
                        'cf_initiation',
                        this_month_data,
                        prev_month_data,
                        'cf_initiation_eli'
                    )),
                    'value': get_value(this_month_data, 'cf_initiation'),
                    'all': get_value(this_month_data, 'cf_initiation_eli'),
                    'format': 'percent_and_div',
                    'frequency': 'month',
                    'redirect': 'maternal_and_child/children_initiated'
                },
                {
                    'label': _('Institutional Deliveries'),
                    'help_text': institutional_deliveries_help_text(),
                    'percent': percent_diff(
                        'institutional_delivery',
                        deliveries_this_month,
                        deliveries_prev_month,
                        'delivered'
                    ),
                    'color': get_color_with_green_positive(percent_diff(
                        'institutional_delivery',
                        deliveries_this_month,
                        deliveries_prev_month,
                        'delivered'
                    )),
                    'value': get_value(deliveries_this_month, 'institutional_delivery'),
                    'all': get_value(deliveries_this_month, 'delivered'),
                    'format': 'percent_and_div',
                    'frequency': 'month',
                    'redirect': 'maternal_and_child/institutional_deliveries'
                }
            ]
        ]
    }