def get_lady_supervisor_data(domain, config, show_test=False):

    def get_data(date, filters):
        queryset = AggLsMonthly.objects.filter(
            month=date, **filters
        ).values(
            "aggregation_level",
            "awc_visits",
            "vhnd_observed",
            "beneficiary_vists",
            "num_launched_awcs",
        )
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

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

    data = get_data(current_month, config)

    return {
        'records': [
            [
                {
                    'label': _('Number of AWCs visited'),
                    'help_text': lady_supervisor_number_of_awcs_visited_help_text(),
                    'percent': None,
                    'value': get_value(data, 'awc_visits'),
                    'all': None,
                    'format': 'number',
                    'frequency': 'month',
                },
                {
                    'label': _('Number of Beneficiaries Visited'),
                    'help_text': lady_supervisor_number_of_beneficiaries_visited_help_text(),
                    'percent': None,
                    'value': get_value(data, 'beneficiary_vists'),
                    'all': None,
                    'format': 'number',
                    'frequency': 'month',
                }
            ],
            [
                {
                    'label': _('Number of VHSNDs observed'),
                    'help_text': lady_supervisor_number_of_vhnds_observed_help_text(),
                    'percent': None,
                    'value': get_value(data, 'vhnd_observed'),
                    'all': None,
                    'format': 'number',
                    'frequency': 'month',
                }
            ]
        ]
    }
示例#2
0
def get_lady_supervisor_data(domain, config, show_test=False):

    def get_data(date, filters):
        queryset = AggLsMonthly.objects.filter(
            month=date, **filters
        ).values(
            "aggregation_level",
            "awc_visits",
            "vhnd_observed",
            "beneficiary_vists",
            "num_launched_awcs",
        )
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

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

    data = get_data(current_month, config)

    return {
        'records': [
            [
                {
                    'label': _('Number of AWCs visited'),
                    'help_text': lady_supervisor_number_of_awcs_visited_help_text(),
                    'percent': None,
                    'value': get_value(data, 'awc_visits'),
                    'all': None,
                    'format': 'number',
                    'frequency': 'month',
                },
                {
                    'label': _('Number of Beneficiaries Visited'),
                    'help_text': lady_supervisor_number_of_beneficiaries_visited_help_text(),
                    'percent': None,
                    'value': get_value(data, 'beneficiary_vists'),
                    'all': None,
                    'format': 'number',
                    'frequency': 'month',
                }
            ],
            [
                {
                    'label': _('Number of VHNDs observed'),
                    'help_text': lady_supervisor_number_of_vhnds_observed_help_text(),
                    'percent': None,
                    'value': get_value(data, 'vhnd_observed'),
                    'all': None,
                    'format': 'number',
                    'frequency': 'month',
                }
            ]
        ]
    }
def get_awc_reports_pse(config, month, domain, show_test=False):
    selected_month = datetime(*month)
    last_30_days = (selected_month - relativedelta(days=30))
    last_months = (selected_month - relativedelta(months=1))
    last_three_months = (selected_month - relativedelta(months=3))
    last_day_of_selected_month = (
        selected_month + relativedelta(months=1)) - relativedelta(days=1)

    map_image_data = DailyAttendanceView.objects.filter(
        pse_date__range=(selected_month, last_day_of_selected_month),
        **config).values('awc_name', 'form_location_lat', 'form_location_long',
                         'image_name', 'doc_id',
                         'pse_date').order_by('-pse_date')

    kpi_data_tm = AggAwcMonthly.objects.filter(
        month=selected_month,
        **config).values('awc_name').annotate(days_open=Sum('awc_days_open'))
    kpi_data_lm = AggAwcMonthly.objects.filter(
        month=last_months,
        **config).values('awc_name').annotate(days_open=Sum('awc_days_open'))

    open_count_data = DailyAttendanceView.objects.filter(
        pse_date__range=(last_three_months, last_day_of_selected_month),
        **config).values('awc_name', 'pse_date').annotate(
            open_count=Sum('awc_open_count'), ).order_by('pse_date')

    daily_attendance = DailyAttendanceView.objects.filter(
        pse_date__range=(selected_month, last_day_of_selected_month),
        **config).values('awc_name', 'pse_date').annotate(
            avg_percent=Avg('attended_children_percent'),
            attended=Sum('attended_children'),
            eligible=Sum('eligible_children'))

    if not show_test:
        map_image_data = apply_exclude(domain, map_image_data)
        kpi_data_tm = apply_exclude(domain, kpi_data_tm)
        kpi_data_lm = apply_exclude(domain, kpi_data_lm)
        open_count_data = apply_exclude(domain, open_count_data)
        daily_attendance = apply_exclude(domain, daily_attendance)

    attended_children_chart = {}
    dates = [
        dt for dt in rrule(
            DAILY, dtstart=selected_month, until=last_day_of_selected_month)
    ]
    for date in dates:
        attended_children_chart[int(date.strftime("%s")) * 1000] = {
            'avg_percent': 0,
            'attended': 0,
            'eligible': 0
        }

    open_count_chart = {}
    for chart_row in open_count_data:
        first_day_of_week = chart_row['pse_date'] - timedelta(
            days=chart_row['pse_date'].isoweekday() - 1)
        pse_week = int(first_day_of_week.strftime("%s")) * 1000

        if pse_week in open_count_chart:
            open_count_chart[pse_week] += (chart_row['open_count'] or 0)
        else:
            open_count_chart[pse_week] = (chart_row['open_count'] or 0)

    for daily_attendance_row in daily_attendance:
        pse_day = int(daily_attendance_row['pse_date'].strftime("%s")) * 1000
        attended_children_chart[pse_day] = {
            'avg_percent': daily_attendance_row['avg_percent'] or 0,
            'attended': daily_attendance_row['attended'] or 0,
            'eligible': daily_attendance_row['eligible'] or 0
        }

    map_data = {}

    date_to_image_data = {}

    for map_row in map_image_data:
        lat = map_row['form_location_lat']
        long = map_row['form_location_long']
        awc_name = map_row['awc_name']
        image_name = map_row['image_name']
        doc_id = map_row['doc_id']
        pse_date = map_row['pse_date']
        if lat and long:
            key = doc_id.replace('-', '')
            map_data.update({
                key: {
                    'lat': float(lat),
                    'lng': float(long),
                    'focus': 'true',
                    'message': awc_name,
                }
            })
        if image_name:
            date_str = pse_date.strftime("%d/%m/%Y")
            date_to_image_data[date_str] = map_row

    images = []
    tmp_image = []

    for idx, date in enumerate(
            rrule(DAILY,
                  dtstart=selected_month,
                  until=last_day_of_selected_month)):
        date_str = date.strftime("%d/%m/%Y")
        image_data = date_to_image_data.get(date_str)

        if image_data:
            image_name = image_data['image_name']
            doc_id = image_data['doc_id']

            tmp_image.append({
                'id':
                idx,
                'image':
                absolute_reverse('api_form_attachment',
                                 args=(domain, doc_id, image_name)),
                'date':
                date_str
            })
        else:
            tmp_image.append({'id': idx, 'image': None, 'date': date_str})

        if (idx + 1) % 4 == 0:
            images.append(tmp_image)
            tmp_image = []

    if tmp_image:
        images.append(tmp_image)

    return {
        'kpi': [[{
            'label':
            _('AWC Days Open'),
            'help_text':
            _(("""
                        Total number of days the AWC is open in the given month.
                        The AWC is expected to be open 6 days a week (Not on Sundays and public holidays)
                        """)),
            'percent':
            percent_increase(
                'days_open',
                kpi_data_tm,
                kpi_data_lm,
            ),
            'value':
            get_value(kpi_data_tm, 'days_open'),
            'all':
            '',
            'format':
            'number',
            'frequency':
            'month',
            'color':
            'green' if percent_increase(
                'days_open',
                kpi_data_tm,
                kpi_data_lm,
            ) > 0 else 'red',
        }]],
        'charts':
        [[{
            'key':
            'AWC Days Open per week',
            'values':
            sorted([
                dict(x=x_val, y=y_val)
                for x_val, y_val in open_count_chart.iteritems()
            ],
                   key=lambda d: d['x']),
            "strokeWidth":
            2,
            "classed":
            "dashed",
            "color":
            BLUE
        }],
         [
             {
                 'key':
                 'PSE - Daily Attendance',
                 'values':
                 sorted([
                     dict(x=x_val,
                          y=y_val['avg_percent'],
                          attended=y_val['attended'],
                          eligible=y_val['eligible'])
                     for x_val, y_val in attended_children_chart.iteritems()
                 ],
                        key=lambda d: d['x']),
                 "strokeWidth":
                 2,
                 "classed":
                 "dashed",
                 "color":
                 BLUE
             },
         ]],
        'map': {
            'markers': map_data,
        },
        'images':
        images
    }
示例#4
0
def get_maternal_child_data(domain, config, show_test=False):
    def get_data_for_child_health_monthly(date, filters):

        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_tranche__in': [0, 6, 72]}, 'wasting_moderate')
        wasting_severe = exclude_records_by_age_for_column(
            {'age_tranche__in': [0, 6, 72]}, 'wasting_severe')
        stunting_moderate = exclude_records_by_age_for_column(
            {'age_tranche__in': [0, 6, 72]}, 'stunting_moderate')
        stunting_severe = exclude_records_by_age_for_column(
            {'age_tranche__in': [0, 6, 72]}, 'stunting_severe')
        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_tranche__in': [0, 6, 72]}, 'height_measured_in_month')
        weighed_and_height_measured_in_month = exclude_records_by_age_for_column(
            {'age_tranche__in': [0, 6, 72]},
            'weighed_and_height_measured_in_month')

        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)

    return {
        'records':
        [[{
            'label':
            _('Underweight (Weight-for-Age)'),
            'help_text':
            _(("Percentage of children between 0-5 years enrolled for Anganwadi Services with "
               "weight-for-age less than -2 standard deviations of the WHO Child Growth Standards "
               "median. Children who are moderately or severely underweight have a higher risk of "
               "mortality.")),
            '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':
            'underweight_children'
        }, {
            'label':
            _('Wasting (Weight-for-Height)'),
            'help_text':
            _(("Percentage of children (6-60 months) with weight-for-height below -3 standard "
               "deviations of the WHO Child Growth Standards median. Severe Acute Malnutrition "
               "(SAM) or wasting in children is a symptom of acute undernutrition usually as a "
               "consequence of insufficient food intake or a high incidence of infectious "
               "diseases.")),
            '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':
            'wasting'
        }],
         [{
             'label':
             _('Stunting (Height-for-Age)'),
             'help_text':
             _(("Percentage of children (6-60 months) with height-for-age below -2Z standard deviations "
                "of the WHO Child Growth Standards median. Stunting is a sign of chronic undernutrition "
                "and has long lasting harmful consequences on the growth of a child"
                )),
             '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':
             'stunting'
         }, {
             'label':
             _('Newborns with Low Birth Weight'),
             'help_text':
             _(("Percentage of newborns born with birth weight less than 2500 grams. Newborns with"
                " Low Birth Weight are closely associated with foetal and neonatal mortality and "
                "morbidity, inhibited growth and cognitive development, and chronic diseases later "
                "in life")),
             'percent':
             percent_diff('low_birth_weight', this_month_data, prev_month_data,
                          'weighed_and_born_in_month'),
             'color':
             'red' if
             percent_diff('low_birth_weight', this_month_data, prev_month_data,
                          'weighed_and_born_in_month') > 0 else 'green',
             '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':
             'low_birth'
         }],
         [{
             'label':
             _('Early Initiation of Breastfeeding'),
             'help_text':
             _(("Percentage of children breastfed within an hour of birth. Early initiation of "
                "breastfeeding ensure the newborn recieves the 'first milk' rich in nutrients "
                "and encourages exclusive breastfeeding practice")),
             'percent':
             percent_diff('bf_birth', this_month_data, prev_month_data,
                          'born'),
             'color':
             'green' if percent_diff('bf_birth', this_month_data,
                                     prev_month_data, 'born') > 0 else 'red',
             'value':
             get_value(this_month_data, 'bf_birth'),
             'all':
             get_value(this_month_data, 'born'),
             'format':
             'percent_and_div',
             'frequency':
             'month',
             'redirect':
             'early_initiation'
         }, {
             'label':
             _('Exclusive Breastfeeding'),
             'help_text':
             _(("Percentage of children between 0 - 6 months exclusively breastfed. An infant is "
                "exclusively breastfed if they recieve only breastmilk with no additional food, "
                "liquids (even water) ensuring optimal nutrition and growth between 0 - 6 months"
                )),
             'percent':
             percent_diff('ebf', this_month_data, prev_month_data, 'ebf_eli'),
             'color':
             'green' if percent_diff('ebf', this_month_data, prev_month_data,
                                     'ebf_eli') > 0 else 'red',
             'value':
             get_value(this_month_data, 'ebf'),
             'all':
             get_value(this_month_data, 'ebf_eli'),
             'format':
             'percent_and_div',
             'frequency':
             'month',
             'redirect':
             'exclusive_breastfeeding'
         }],
         [{
             'label':
             _('Children initiated appropriate Complementary Feeding'),
             'help_text':
             _(("Percentage of children between 6 - 8 months given timely introduction to solid or "
                "semi-solid food. Timely intiation of complementary feeding in addition to "
                "breastmilk at 6 months of age is a key feeding practice to reduce malnutrition"
                )),
             'percent':
             percent_diff('cf_initiation', this_month_data, prev_month_data,
                          'cf_initiation_eli'),
             'color':
             'green'
             if percent_diff('cf_initiation', this_month_data, prev_month_data,
                             'cf_initiation_eli') > 0 else 'red',
             'value':
             get_value(this_month_data, 'cf_initiation'),
             'all':
             get_value(this_month_data, 'cf_initiation_eli'),
             'format':
             'percent_and_div',
             'frequency':
             'month',
             'redirect':
             'children_initiated'
         }, {
             'label':
             _('Institutional Deliveries'),
             'help_text':
             _(("Percentage of pregnant women who delivered in a public or private medical facility "
                "in the last month. Delivery in medical instituitions is associated with a "
                "decrease in maternal mortality rate")),
             'percent':
             percent_diff('institutional_delivery', deliveries_this_month,
                          deliveries_prev_month, 'delivered'),
             'color':
             'green' if
             percent_diff('institutional_delivery', deliveries_this_month,
                          deliveries_prev_month, 'delivered') > 0 else 'red',
             'value':
             get_value(deliveries_this_month, 'institutional_delivery'),
             'all':
             get_value(deliveries_this_month, 'delivered'),
             'format':
             'percent_and_div',
             'frequency':
             'month',
             'redirect':
             'institutional_deliveries'
         }]]
    }
示例#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'),
                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}
def get_cas_reach_data(domain, now_date, config, show_test=False):
    now_date = datetime(*now_date)
    yesterday_date = (now_date - relativedelta(days=1)).date()
    two_days_ago = (now_date - relativedelta(days=2)).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'),
            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_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)

    daily_yesterday = get_data_for_daily_usage(yesterday_date, config)
    daily_two_days_ago = get_data_for_daily_usage(two_days_ago, config)
    if not daily_yesterday:
        daily_yesterday = daily_two_days_ago
        daily_two_days_ago = get_data_for_daily_usage((now_date - relativedelta(days=3)).date(), config)

    daily_attendance_percent = percent_increase('daily_attendance', daily_yesterday, daily_two_days_ago)

    return {
        'records': [
            [
                {
                    'label': _('AWCs Launched'),
                    'help_text': _('Total AWCs that have launched ICDS-CAS. '
                                   'AWCs are considered launched after submitting at least '
                                   'one Household Registration form. '),
                    'percent': percent_increase('awcs', awc_this_month_data, awc_prev_month_data),
                    'color': 'green' if percent_increase(
                        'awcs',
                        awc_this_month_data,
                        awc_prev_month_data) > 0 else 'red',
                    'value': get_value(awc_this_month_data, 'awcs'),
                    'all': get_value(awc_this_month_data, 'all_awcs'),
                    'format': 'div',
                    'frequency': 'month',
                    'redirect': 'awcs_covered'
                },
                {
                    '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': 'green' if daily_attendance_percent > 0 else 'red',
                    'percent': daily_attendance_percent,
                    'value': get_value(daily_yesterday, 'daily_attendance'),
                    'all': get_value(daily_yesterday, 'awcs'),
                    'format': 'div',
                    'frequency': 'day',
                    'redirect': 'awc_daily_status'
                }
            ],
            [
                {
                    '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',
                }
            ]
        ]
    }
示例#7
0
def get_demographics_data(domain, now_date, config, show_test=False):
    now_date = datetime(*now_date)
    yesterday_date = (now_date - relativedelta(days=1)).date()
    two_days_ago = (now_date - relativedelta(days=2)).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') +
                                   Sum('cases_person_adolescent_girls_15_18')),
                person_adolescent_all=(
                    Sum('cases_person_adolescent_girls_11_14_all') +
                    Sum('cases_person_adolescent_girls_15_18_all')),
                person_aadhaar=Sum('cases_person_has_aadhaar'),
                all_persons=Sum('cases_person_beneficiary'))

        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'] = yesterday_date
        data = get_data_for(AggAwcDailyView, config)
        config['date'] = two_days_ago
        prev_data = get_data_for(AggAwcDailyView, config)
        if not data:
            data = prev_data
            config['date'] = (now_date - relativedelta(days=3)).date()
            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':
            'green'
            if percent_increase('household', data, prev_data) > 0 else 'red',
            'value':
            get_value(data, 'household'),
            'all':
            None,
            'format':
            'number',
            'frequency':
            frequency,
            'redirect':
            'registered_household'
        }, {
            'label':
            _('Percent Aadhaar-seeded Beneficiaries'),
            'help_text':
            _(('Percentage of ICDS beneficiaries whose Aadhaar identification has been captured'
               )),
            'percent':
            percent_diff('person_aadhaar', data, prev_data, 'all_persons'),
            'color':
            'green' if percent_increase('person_aadhaar', data, prev_data) > 0
            else 'red',
            'value':
            get_value(data, 'person_aadhaar'),
            'all':
            get_value(data, 'all_persons'),
            'format':
            'percent_and_div',
            'frequency':
            frequency,
            'redirect':
            'adhaar'
        }],
         [{
             'label':
             _('Percent children (0-6 years) enrolled for ICDS services'),
             'help_text':
             _('Percentage of children registered between '
               '0-6 years old who are enrolled for ICDS services'),
             'percent':
             percent_diff('child_health', data, prev_data, 'child_health_all'),
             'color':
             'green' if percent_diff('child_health_all', data, prev_data,
                                     'child_health_all') > 0 else 'red',
             'value':
             get_value(data, 'child_health'),
             'all':
             get_value(data, 'child_health_all'),
             'format':
             'percent_and_div',
             'frequency':
             frequency,
             'redirect':
             'enrolled_children'
         }, {
             'label':
             _('Percent pregnant women enrolled for ICDS services'),
             'help_text':
             _('Percentage of pregnant women registered who are enrolled for ICDS services'
               ),
             'percent':
             percent_diff('ccs_pregnant', data, prev_data, 'ccs_pregnant_all'),
             'color':
             'green' if percent_diff('ccs_pregnant', data, prev_data,
                                     'ccs_pregnant_all') > 0 else 'red',
             'value':
             get_value(data, 'ccs_pregnant'),
             'all':
             get_value(data, 'ccs_pregnant_all'),
             'format':
             'percent_and_div',
             'frequency':
             frequency,
             'redirect':
             'enrolled_women'
         }],
         [{
             'label':
             _('Percent lactating women enrolled for ICDS services'),
             'help_text':
             _('Percentage of lactating women registered who are enrolled for ICDS services'
               ),
             'percent':
             percent_diff('css_lactating', data, prev_data,
                          'css_lactating_all'),
             'color':
             'green' if percent_diff('css_lactating', data, prev_data,
                                     'css_lactating_all') > 0 else 'red',
             'value':
             get_value(data, 'css_lactating'),
             'all':
             get_value(data, 'css_lactating_all'),
             'format':
             'percent_and_div',
             'frequency':
             frequency,
             'redirect':
             'lactating_enrolled_women'
         }, {
             'label':
             _('Percent adolescent girls (11-18 years) enrolled for ICDS services'
               ),
             'help_text':
             _(("Percentage of adolescent girls registered between 11-18 years"
                " old who are enrolled for ICDS services")),
             'percent':
             percent_diff('person_adolescent', data, prev_data,
                          'person_adolescent_all'),
             'color':
             'green' if percent_diff('person_adolescent', data, prev_data,
                                     'person_adolescent_all') > 0 else 'red',
             'value':
             get_value(data, 'person_adolescent'),
             'all':
             get_value(data, 'person_adolescent_all'),
             'format':
             'percent_and_div',
             'frequency':
             frequency,
             'redirect':
             '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_system_usage_data(yesterday, config):
    yesterday_date = datetime(*yesterday)
    two_days_ago = (yesterday_date - relativedelta(days=1)).date()

    def get_data_for(date, filters):
        return AggAwcDailyView.objects.filter(
            date=date, **filters).values('aggregation_level').annotate(
                awcs=Sum('awc_count'),
                daily_attendance=Sum('daily_attendance_open'),
                num_forms=Sum('usage_num_forms'),
                num_home_visits=Sum('usage_num_home_visit'),
                num_gmp=Sum('usage_num_gmp'),
                num_thr=Sum('usage_num_thr'))

    yesterday_data = get_data_for(yesterday_date, config)
    two_days_ago_data = get_data_for(two_days_ago, config)

    return {
        'records':
        [[{
            'label':
            _('Average number of household registration forms submitted yesterday'
              ),
            'help_text':
            _('Average number of household registration forms submitted by AWWs yesterday.'
              ),
            'percent':
            percent_increase('num_forms', yesterday_data, two_days_ago_data),
            'value':
            get_value(yesterday_data, 'num_forms'),
            'all':
            get_value(yesterday_data, 'awcs'),
            'format':
            'number'
        }],
         [{
             'label':
             _('Average number of Home Visit forms submitted yesterday'),
             'help_text':
             _(("Average number of home visit forms submitted yesterday. Home visit forms are "
                "Birth Preparedness, Delivery, Post Natal Care, Exclusive breastfeeding and "
                "Complementary feeding")),
             'percent':
             percent_increase('num_home_visits', yesterday_data,
                              two_days_ago_data),
             'value':
             get_value(yesterday_data, 'num_home_visits'),
             'all':
             get_value(yesterday_data, 'awcs'),
             'format':
             'number'
         }, {
             'label':
             _('Average number of Growth Monitoring forms submitted yesterday'
               ),
             'help_text':
             _('Average number of growth monitoring forms (GMP) submitted yesterday'
               ),
             'percent':
             percent_increase('num_gmp', yesterday_data, two_days_ago_data),
             'value':
             get_value(yesterday_data, 'num_gmp'),
             'all':
             get_value(yesterday_data, 'awcs'),
             'format':
             'number'
         }],
         [{
             'label':
             _('Average number of Take Home Ration forms submitted yesterday'),
             'help_text':
             _('Average number of Take Home Rations (THR) forms submitted yesterday'
               ),
             'percent':
             percent_increase('num_thr', yesterday_data, two_days_ago_data),
             'value':
             get_value(yesterday_data, 'num_thr'),
             'all':
             get_value(yesterday_data, 'awcs'),
             'format':
             'number'
         }]]
    }
示例#10
0
def get_awc_reports_system_usage(domain,
                                 config,
                                 month,
                                 prev_month,
                                 two_before,
                                 loc_level,
                                 show_test=False):
    def get_data_for(filters, date):
        queryset = AggAwcMonthly.objects.filter(
            month=datetime(*date), **filters).values(loc_level).annotate(
                awc_open=Sum('awc_days_open'),
                weighed=Sum('wer_weighed'),
                all=Sum('wer_eligible'),
            )
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    chart_data = DailyAttendanceView.objects.filter(
        pse_date__range=(datetime(*two_before), datetime(*month)),
        **config).values('pse_date', 'aggregation_level').annotate(
            awc_count=Sum('awc_open_count'),
            attended_children=Avg('attended_children_percent')).order_by(
                'pse_date')

    if not show_test:
        chart_data = apply_exclude(domain, chart_data)

    awc_count_chart = []
    attended_children_chart = []
    for row in chart_data:
        date = row['pse_date']
        date_in_milliseconds = int(date.strftime("%s")) * 1000
        awc_count_chart.append([date_in_milliseconds, row['awc_count']])
        attended_children_chart.append(
            [date_in_milliseconds, row['attended_children'] or 0])

    this_month_data = get_data_for(config, month)
    prev_month_data = get_data_for(config, prev_month)

    return {
        'kpi': [[{
            'label':
            _('AWC Days Open'),
            'help_text':
            _(("The total number of days the AWC is open in the given month. The AWC is expected to "
               "be open 6 days a week (Not on Sundays and public holidays)")),
            'percent':
            percent_increase(
                'awc_open',
                this_month_data,
                prev_month_data,
            ),
            'value':
            get_value(this_month_data, 'awc_open'),
            'all':
            '',
            'format':
            'number',
            'frequency':
            'month'
        }, {
            'label':
            _(("Percentage of eligible children (ICDS beneficiaries between 0-6 years) "
               "who have been weighed in the current month")),
            'help_text':
            _('Percentage of AWCs with a functional toilet'),
            'percent':
            percent_diff('weighed', this_month_data, prev_month_data, 'all'),
            'value':
            get_value(this_month_data, 'weighed'),
            'all':
            get_value(this_month_data, 'all'),
            'format':
            'percent_and_div',
            'frequency':
            'month'
        }]],
        'charts': [[{
            'key': 'AWC Days Open Per Week',
            'values': awc_count_chart,
            "classed": "dashed",
        }],
                   [{
                       'key': 'PSE- Average Weekly Attendance',
                       'values': attended_children_chart,
                       "classed": "dashed",
                   }]],
    }
示例#11
0
def get_awc_report_demographics(domain, config, month, show_test=False):
    selected_month = datetime(*month)

    chart = AggChildHealthMonthly.objects.filter(
        month=selected_month,
        **config).values('age_tranche', 'aggregation_level').annotate(
            valid=Sum('valid_in_month')).order_by('age_tranche')

    if not show_test:
        chart = apply_exclude(domain, chart)

    chart_data = OrderedDict()
    chart_data.update({'0-1 month': 0})
    chart_data.update({'1-6 months': 0})
    chart_data.update({'6-12 months': 0})
    chart_data.update({'1-3 years': 0})
    chart_data.update({'3-6 years': 0})

    for chart_row in chart:
        if chart_row['age_tranche']:
            age = int(chart_row['age_tranche'])
            valid = chart_row['valid']
            chart_data[match_age(age)] += valid

    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') +
                                   Sum('cases_person_adolescent_girls_15_18')),
                person_adolescent_all=(
                    Sum('cases_person_adolescent_girls_11_14_all') +
                    Sum('cases_person_adolescent_girls_15_18_all')),
                person_aadhaar=Sum('cases_person_has_aadhaar'),
                all_persons=Sum('cases_person_beneficiary'))

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

    yesterday = datetime.now() - relativedelta(days=1)
    two_days_ago = yesterday - relativedelta(days=1)
    now = datetime.utcnow()
    previous_month = selected_month - relativedelta(months=1)
    if selected_month.month == now.month and selected_month.year == now.year:
        config['date'] = yesterday
        data = get_data_for(AggAwcDailyView, config)
        config['date'] = two_days_ago
        prev_data = get_data_for(AggAwcDailyView, config)
        frequency = 'day'
    else:
        config['month'] = selected_month
        data = get_data_for(AggAwcMonthly, config)
        config['month'] = previous_month
        prev_data = get_data_for(AggAwcMonthly, config)
        frequency = 'month'

    return {
        'chart': [{
            'key':
            'Children (0-6 years)',
            'values': [[key, value] for key, value in chart_data.iteritems()],
            "classed":
            "dashed",
        }],
        'kpi':
        [[{
            'label':
            _('Registered Households'),
            'help_text':
            _("Total number of households registered"),
            'percent':
            percent_increase(
                'household',
                data,
                prev_data,
            ),
            'color':
            'green'
            if percent_increase('household', data, prev_data) > 0 else 'red',
            'value':
            get_value(data, 'household'),
            'all':
            '',
            'format':
            'number',
            'frequency':
            frequency
        },
          {
              'label':
              _('Percent Adhaar-seeded Beneficiaries'),
              'help_text':
              _('Percentage of ICDS beneficiaries whose Adhaar identification has been captured'
                ),
              'percent':
              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
          }],
         [{
             'label':
             _('Children (0-6 years)'),
             'help_text':
             _('Total number of children registered between the age of 0 - 6 years'
               ),
             'percent':
             percent_increase('child_health_all', data, prev_data),
             'color':
             'green'
             if percent_increase('child_health_all', data, prev_data) > 0 else
             'red',
             'value':
             get_value(data, 'child_health_all'),
             'all':
             None,
             'format':
             'number',
             'frequency':
             frequency,
         }, {
             'label':
             _('Children (0-6 years) enrolled for ICDS services'),
             'help_text':
             _(("Total number of children registered between the age of 0 - 6 years "
                "and enrolled for ICDS services")),
             'percent':
             percent_increase('child_health', data, prev_data),
             'color':
             'green' if percent_increase('child_health', data, prev_data) > 0
             else 'red',
             'value':
             get_value(data, 'child_health'),
             'all':
             None,
             'format':
             'number',
             'frequency':
             frequency
         }],
         [
             {
                 'label':
                 _('Pregnant Women'),
                 'help_text':
                 _("Total number of pregnant women registered"),
                 'percent':
                 percent_increase(
                     'ccs_pregnant_all',
                     data,
                     prev_data,
                 ),
                 'color':
                 'green'
                 if percent_increase('ccs_pregnant_all', data, prev_data) > 0
                 else 'red',
                 'value':
                 get_value(data, 'ccs_pregnant_all'),
                 'all':
                 '',
                 'format':
                 'number',
                 'frequency':
                 frequency
             },
             {
                 'label':
                 _('Pregnant Women enrolled for ICDS services'),
                 'help_text':
                 _('Total number of pregnant women registered and enrolled for ICDS services'
                   ),
                 'percent':
                 percent_increase('ccs_pregnant', data, prev_data),
                 'color':
                 'green'
                 if percent_increase('ccs_pregnant', data, prev_data) > 0 else
                 'red',
                 'value':
                 get_value(data, 'ccs_pregnant'),
                 'all':
                 None,
                 'format':
                 'number',
                 'frequency':
                 frequency
             },
         ],
         [
             {
                 'label':
                 _('Lactating Mothers'),
                 'help_text':
                 _('Total number of lactating women registered'),
                 'percent':
                 percent_increase('css_lactating_all', data, prev_data),
                 'color':
                 'green'
                 if percent_increase('css_lactating_all', data, prev_data) > 0
                 else 'red',
                 'value':
                 get_value(data, 'css_lactating_all'),
                 'all':
                 '',
                 'format':
                 'number',
                 'frequency':
                 frequency
             },
             {
                 'label':
                 _('Lactating Women enrolled for ICDS services'),
                 'help_text':
                 _('Total number of lactating women registered and enrolled for ICDS services'
                   ),
                 'percent':
                 percent_increase('css_lactating', data, prev_data),
                 'color':
                 'green'
                 if percent_increase('css_lactating', data, prev_data) > 0 else
                 'red',
                 'value':
                 get_value(data, 'css_lactating'),
                 'all':
                 None,
                 'format':
                 'number',
                 'frequency':
                 frequency
             },
         ],
         [{
             'label':
             _('Adolescent Girls (11-18 years)'),
             'help_text':
             _('Total number of adolescent girls (11 - 18 years) who are registered'
               ),
             'percent':
             percent_increase(
                 'person_adolescent_all',
                 data,
                 prev_data,
             ),
             'color':
             'green'
             if percent_increase('person_adolescent_all', data, prev_data) > 0
             else 'red',
             'value':
             get_value(data, 'person_adolescent_all'),
             'all':
             '',
             'format':
             'number',
             'frequency':
             frequency
         }, {
             'label':
             _('Adolescent Girls (11-18 years) enrolled for ICDS services'),
             'help_text':
             _(("Total number of adolescent girls (11 - 18 years) "
                "who are registered and enrolled for ICDS services")),
             'percent':
             percent_increase('person_adolescent', data, prev_data),
             'color':
             'green'
             if percent_increase('person_adolescent', data, prev_data) > 0 else
             'red',
             'value':
             get_value(data, 'person_adolescent'),
             'all':
             None,
             'format':
             'number',
             'frequency':
             frequency
         }]]
    }
示例#12
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': 'green' if percent_increase(
                        'household',
                        data,
                        prev_data) > 0 else 'red',
                    'value': get_value(data, 'household'),
                    'all': None,
                    'format': 'number',
                    'frequency': frequency,
                    'redirect': 'demographics/registered_household'
                },
                {
                    'label': _('Percent Aadhaar-seeded Beneficiaries'),
                    'help_text': percent_aadhaar_seeded_beneficiaries_help_text(),
                    'percent': percent_diff(
                        'person_aadhaar',
                        data,
                        prev_data,
                        'all_persons'
                    ),
                    'color': 'green' if percent_diff(
                        'person_aadhaar',
                        data,
                        prev_data,
                        'all_persons') > 0 else 'red',
                    'value': get_value(data, 'person_aadhaar'),
                    'all': get_value(data, 'all_persons'),
                    'format': 'percent_and_div',
                    'frequency': frequency,
                    'redirect': 'demographics/adhaar'
                }
            ],
            [
                {
                    'label': _('Percent children (0-6 years) enrolled for Anganwadi Services'),
                    'help_text': percent_children_enrolled_help_text(),
                    'percent': percent_diff('child_health', data, prev_data, 'child_health_all'),
                    'color': 'green' if percent_diff(
                        'child_health',
                        data,
                        prev_data, 'child_health_all') > 0 else 'red',
                    'value': get_value(data, 'child_health'),
                    'all': get_value(data, 'child_health_all'),
                    'format': 'percent_and_div',
                    'frequency': frequency,
                    'redirect': 'demographics/enrolled_children'
                },
                {
                    'label': _('Percent 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': 'green' if percent_diff(
                        'ccs_pregnant',
                        data,
                        prev_data,
                        'ccs_pregnant_all'
                    ) > 0 else 'red',
                    'value': get_value(data, 'ccs_pregnant'),
                    'all': get_value(data, 'ccs_pregnant_all'),
                    'format': 'percent_and_div',
                    'frequency': frequency,
                    'redirect': 'demographics/enrolled_women'
                }
            ],
            [

                {
                    'label': _('Percent 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': 'green' if percent_diff(
                        'css_lactating',
                        data,
                        prev_data,
                        'css_lactating_all'
                    ) > 0 else 'red',
                    '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': _('Percent adolescent girls (11-14 years) enrolled for Anganwadi Services'),
                    'help_text': percent_adolescent_girls_enrolled_help_text(),
                    'percent': percent_diff(
                        'person_adolescent',
                        data,
                        prev_data,
                        'person_adolescent_all'
                    ),
                    'color': 'green' if percent_diff(
                        'person_adolescent',
                        data,
                        prev_data,
                        'person_adolescent_all'
                    ) > 0 else 'red',
                    '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':
                _('Of the AWCs that have submitted an Infrastructure Details form, the '
                  'percentage of AWCs that reported having a source of clean drinking water. '
                  ),
                'percent':
                percent_diff('clean_water', this_month_data, prev_month_data,
                             'sum_last_update'),
                'color':
                'green' if percent_diff('clean_water', this_month_data,
                                        prev_month_data, 'sum_last_update') > 0
                else 'red',
                'value':
                get_value(this_month_data, 'clean_water'),
                'all':
                get_value(this_month_data, 'sum_last_update'),
                'format':
                'percent_and_div',
                'frequency':
                'month',
                'redirect':
                'clean_water'
            }, {
                'label':
                _("AWCs Reported Functional Toilet"),
                'help_text':
                _('Of the AWCs that submitted an Infrastructure Details form, the percentage '
                  'of AWCs that reported having a functional toilet'),
                'percent':
                percent_diff('functional_toilet', this_month_data,
                             prev_month_data, 'sum_last_update'),
                'color':
                'green' if percent_diff('functional_toilet', this_month_data,
                                        prev_month_data, 'sum_last_update') > 0
                else 'red',
                'value':
                get_value(this_month_data, 'functional_toilet'),
                'all':
                get_value(this_month_data, 'sum_last_update'),
                'format':
                'percent_and_div',
                'frequency':
                'month',
                'redirect':
                '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':
                    _('Of the AWCs that have submitted an Infrastructure Details form, the '
                      'percentage of AWCs that reported having a weighing scale for infants'
                      ),
                    'percent':
                    percent_diff('infant_scale', this_month_data,
                                 prev_month_data, 'sum_last_update'),
                    'color':
                    'green'
                    if percent_diff('infant_scale', this_month_data,
                                    prev_month_data, 'sum_last_update') > 0
                    else 'red',
                    'value':
                    get_value(this_month_data, 'infant_scale'),
                    'all':
                    get_value(this_month_data, 'sum_last_update'),
                    'format':
                    'percent_and_div',
                    'frequency':
                    'month',
                    'redirect':
                    'infants_weight_scale'
                },
                {
                    'label':
                    _('AWCs Reported Weighing Scale: Mother and Child'),
                    'help_text':
                    _('Of the AWCs that have submitted an Infrastructure Details form, the percentage of '
                      'AWCs that reported having a weighing scale for mother and child'
                      ),
                    'percent':
                    percent_diff('adult_scale', this_month_data,
                                 prev_month_data, 'sum_last_update'),
                    'color':
                    'green'
                    if percent_diff('adult_scale', this_month_data,
                                    prev_month_data, 'sum_last_update') > 0
                    else 'red',
                    'value':
                    get_value(this_month_data, 'adult_scale'),
                    'all':
                    get_value(this_month_data, 'sum_last_update'),
                    'format':
                    'percent_and_div',
                    'frequency':
                    'month',
                    'redirect':
                    'adult_weight_scale'
                }
            ],
            [{
                'label':
                _('AWCs Reported Medicine Kit'),
                'help_text':
                _('Of the AWCs that have submitted an Infrastructure Details form, '
                  'the percentage of AWCs that reported having a Medicine Kit'
                  ),
                'percent':
                percent_diff('medicine_kits', this_month_data, prev_month_data,
                             'sum_last_update'),
                'color':
                'green' if percent_diff('medicine_kits', this_month_data,
                                        prev_month_data, 'sum_last_update') > 0
                else 'red',
                'value':
                get_value(this_month_data, 'medicine_kits'),
                'all':
                get_value(this_month_data, 'sum_last_update'),
                'format':
                'percent_and_div',
                'frequency':
                'month',
                'redirect':
                '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'
            #     }
            # ]
        ]
    }
示例#14
0
def get_awc_report_demographics(domain,
                                config,
                                now_date,
                                month,
                                show_test=False,
                                beta=False):
    selected_month = datetime(*month)
    now_date = datetime(*now_date)
    chart = AggChildHealthMonthly.objects.filter(
        month=selected_month,
        **config).values('age_tranche', 'aggregation_level').annotate(
            valid=Sum('valid_in_month')).order_by('age_tranche')

    if not show_test:
        chart = apply_exclude(domain, chart)

    chart_data = OrderedDict()
    chart_data.update({'0-1 month': 0})
    chart_data.update({'1-6 months': 0})
    chart_data.update({'6-12 months': 0})
    chart_data.update({'1-3 years': 0})
    chart_data.update({'3-6 years': 0})

    for chart_row in chart:
        if chart_row['age_tranche']:
            age = int(chart_row['age_tranche'])
            valid = chart_row['valid']
            chart_data[match_age(age)] += valid

    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

    yesterday = now_date - relativedelta(days=1)
    two_days_ago = yesterday - relativedelta(days=1)
    previous_month = selected_month - relativedelta(months=1)
    if selected_month.month == now_date.month and selected_month.year == now_date.year:
        config['date'] = yesterday
        data = get_data_for(AggAwcDailyView, config)
        config['date'] = two_days_ago
        prev_data = get_data_for(AggAwcDailyView, config)
        if not data:
            data = prev_data
            config['date'] = (two_days_ago - relativedelta(days=1)).date()
            prev_data = get_data_for(AggAwcDailyView, config)
        frequency = 'day'
    else:
        config['month'] = selected_month
        data = get_data_for(AggAwcMonthly, config)
        config['month'] = previous_month
        prev_data = get_data_for(AggAwcMonthly, config)
        frequency = 'month'

    return {
        'chart': [{
            'key':
            'Children (0-6 years)',
            'values':
            [[key, value] for key, value in six.iteritems(chart_data)],
            "classed":
            "dashed",
        }],
        'kpi':
        [[{
            'label':
            _('Registered Households'),
            'help_text':
            _("Total number of households registered"),
            'percent':
            percent_increase(
                'household',
                data,
                prev_data,
            ),
            'color':
            'green'
            if percent_increase('household', data, prev_data) > 0 else 'red',
            'value':
            get_value(data, 'household'),
            'all':
            '',
            'format':
            'number',
            'frequency':
            frequency
        },
          {
              'label':
              _('Percent Aadhaar-seeded Beneficiaries'),
              'help_text':
              _('Of the total number of ICDS beneficiaries, the percentage whose Adhaar identification '
                'has been captured. '),
              'percent':
              percent_diff('person_aadhaar', data, prev_data, 'all_persons'),
              'color':
              'green' if percent_diff('person_aadhaar', data, prev_data,
                                      'all_persons') > 0 else 'red',
              'value':
              get_value(data, 'person_aadhaar'),
              'all':
              get_value(data, 'all_persons'),
              'format':
              'percent_and_div',
              'frequency':
              frequency
          }],
         [{
             'label':
             _('Percent children (0-6 years) enrolled for Anganwadi Services'),
             'help_text':
             _('Of the total number of children between 0-6 years, the percentage '
               'of children who are enrolled for Anganwadi Services'),
             'percent':
             percent_diff('child_health', data, prev_data, 'child_health_all'),
             'color':
             'green' if percent_diff('child_health_all', data, prev_data,
                                     'child_health_all') > 0 else 'red',
             'value':
             get_value(data, 'child_health'),
             'all':
             get_value(data, 'child_health_all'),
             'format':
             'percent_and_div',
             'frequency':
             frequency,
         }, {
             'label':
             _('Percent pregnant women enrolled for Anganwadi Services'),
             'help_text':
             _('Of the total number of pregnant women, the percentage of pregnant '
               'women enrolled for Anganwadi Services'),
             'percent':
             percent_diff('ccs_pregnant', data, prev_data, 'ccs_pregnant_all'),
             'color':
             'green' if percent_diff('ccs_pregnant', data, prev_data,
                                     'ccs_pregnant_all') > 0 else 'red',
             'value':
             get_value(data, 'ccs_pregnant'),
             'all':
             get_value(data, 'ccs_pregnant_all'),
             'format':
             'percent_and_div',
             'frequency':
             frequency
         }],
         [{
             'label':
             _('Percent lactating women enrolled for Anganwadi Services'),
             'help_text':
             _('Of the total number of lactating women, the percentage of '
               'lactating women enrolled for Anganwadi Services'),
             'percent':
             percent_diff('css_lactating', data, prev_data,
                          'css_lactating_all'),
             'color':
             'green' if percent_diff('css_lactating', data, prev_data,
                                     'css_lactating_all') > 0 else 'red',
             'value':
             get_value(data, 'css_lactating'),
             'all':
             get_value(data, 'css_lactating_all'),
             'format':
             'percent_and_div',
             'frequency':
             frequency
         }, {
             'label':
             _('Percent adolescent girls (11-14 years) enrolled for Anganwadi Services'
               ),
             'help_text':
             _(("Of the total number of adolescent girls (aged 11-14 years), the percentage "
                "of girls enrolled for Anganwadi Services")),
             'percent':
             percent_diff('person_adolescent', data, prev_data,
                          'person_adolescent_all'),
             'color':
             'green' if percent_diff('person_adolescent', data, prev_data,
                                     'person_adolescent_all') > 0 else 'red',
             'value':
             get_value(data, 'person_adolescent'),
             'all':
             get_value(data, 'person_adolescent_all'),
             'format':
             'percent_and_div',
             'frequency':
             frequency
         }]]
    }
示例#15
0
def get_awc_reports_maternal_child(domain,
                                   config,
                                   month,
                                   prev_month,
                                   show_test=False,
                                   icds_feature_flag=False):
    def get_data_for(date):
        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,
            **config).values('month', 'aggregation_level').annotate(
                underweight=(Sum(moderately_underweight) +
                             Sum(severely_underweight)),
                valid_weighed=Sum(nutrition_status_weighed),
                immunized=(Sum('fully_immunized_on_time') +
                           Sum('fully_immunized_late')),
                eligible=Sum('fully_immunized_eligible'),
                wasting=Sum(wasting_moderate) + Sum(wasting_severe),
                height_measured_in_month=Sum(height_measured_in_month),
                weighed_and_height_measured_in_month=Sum(
                    weighed_and_height_measured_in_month),
                stunting=Sum(stunting_moderate) + Sum(stunting_severe),
                low_birth=Sum('low_birth_weight_in_month'),
                birth=Sum('bf_at_birth'),
                born=Sum('born_in_month'),
                weighed_and_born_in_month=Sum('weighed_and_born_in_month'),
                month_ebf=Sum('ebf_in_month'),
                ebf=Sum('ebf_eligible'),
                month_cf=Sum('cf_initiation_in_month'),
                cf=Sum('cf_initiation_eligible'))
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    def get_weight_efficiency(date):
        queryset = AggAwcMonthly.objects.filter(month=date, **config).values(
            'month', 'aggregation_level',
            'awc_name').annotate(wer_weight=Sum('wer_weighed'),
                                 wer_eli=Sum('wer_eligible'))
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    def get_institutional_delivery_data(date):
        queryset = AggCcsRecordMonthly.objects.filter(
            month=date, **config).values(
                'month', 'aggregation_level', 'awc_name').annotate(
                    institutional_delivery_in_month_sum=Sum(
                        'institutional_delivery_in_month'),
                    delivered_in_month_sum=Sum('delivered_in_month'))
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    this_month_data = get_data_for(datetime(*month))
    prev_month_data = get_data_for(datetime(*prev_month))

    this_month_data_we = get_weight_efficiency(datetime(*month))
    prev_month_data_we = get_weight_efficiency(datetime(*prev_month))

    this_month_institutional_delivery_data = get_institutional_delivery_data(
        datetime(*month))
    prev_month_institutional_delivery_data = get_institutional_delivery_data(
        datetime(*prev_month))

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

    return {
        'kpi':
        [[
            {
                'label':
                _('Underweight (Weight-for-Age)'),
                'help_text':
                _(("Of the total children weighed, the percentage of children between 0-5 years who were "
                   "moderately/severely underweight in the current month. Children who are moderately or "
                   "severely underweight have a higher risk of mortality. ")),
                'percent':
                percent_diff('underweight', this_month_data, prev_month_data,
                             'valid_weighed'),
                'color':
                'red' if percent_diff('underweight', this_month_data,
                                      prev_month_data, 'valid_weighed') > 0
                else 'green',
                'value':
                get_value(this_month_data, 'underweight'),
                'all':
                get_value(this_month_data, 'valid_weighed'),
                'format':
                'percent_and_div',
                'frequency':
                'month'
            },
            {
                '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'
            },
        ],
         [
             {
                 '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'
             },
             {
                 'label':
                 _('Weighing Efficiency'),
                 'help_text':
                 _("Of the children between the ages of 0-5 years who are enrolled for Anganwadi Services, "
                   "the percentage who were weighed in the given month. "),
                 'percent':
                 percent_diff('wer_weight', this_month_data_we,
                              prev_month_data_we, 'wer_eli'),
                 'color':
                 'green'
                 if percent_diff('wer_weight', this_month_data_we,
                                 prev_month_data_we, 'wer_eli') > 0 else 'red',
                 'value':
                 get_value(this_month_data_we, 'wer_weight'),
                 'all':
                 get_value(this_month_data_we, 'wer_eli'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
         ],
         [
             {
                 'label':
                 _('Newborns with Low Birth Weight'),
                 'help_text':
                 _("Of all the children born in the current month, the percentage that had a birth weight "
                   "less than 2500 grams. Newborns with Low Birth Weight are closely associated wtih foetal "
                   "and neonatal mortality and morbidity, inhibited growth and cognitive development, "
                   "and chronic diseases later in life."),
                 'percent':
                 percent_diff('low_birth', this_month_data, prev_month_data,
                              'weighed_and_born_in_month'),
                 'color':
                 'red'
                 if percent_diff('low_birth', this_month_data, prev_month_data,
                                 'weighed_and_born_in_month') > 0 else 'green',
                 'value':
                 get_value(this_month_data, 'low_birth'),
                 'all':
                 get_value(this_month_data, 'weighed_and_born_in_month'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
             {
                 'label':
                 _('Early Initiation of Breastfeeding'),
                 'help_text':
                 _("Of the children born in the last month, the percentage whose "
                   "breastfeeding was initiated within 1 hour of delivery. Early initiation "
                   "of breastfeeding ensure the newborn recieves the \"first milk\" rich "
                   "in nutrients and encourages exclusive breastfeeding practice"
                   ),
                 'percent':
                 percent_diff('birth', this_month_data, prev_month_data,
                              'born'),
                 'color':
                 'green'
                 if percent_diff('birth', this_month_data, prev_month_data,
                                 'born') > 0 else 'red',
                 'value':
                 get_value(this_month_data, 'birth'),
                 'all':
                 get_value(this_month_data, 'born'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
         ],
         [
             {
                 'label':
                 _('Exclusive breastfeeding'),
                 'help_text':
                 _("Of the total children between the ages of 0 to 6 months, the percentage that was "
                   "exclusively fed with breast milk. An infant is exclusively breastfed if they receive "
                   "only breastmilk with no additional food or liquids (even water), ensuring optimal "
                   "nutrition and growth between 0 - 6 months"),
                 'percent':
                 percent_diff('month_ebf', this_month_data, prev_month_data,
                              'ebf'),
                 'color':
                 'green'
                 if percent_diff('month_ebf', this_month_data, prev_month_data,
                                 'ebf') > 0 else 'red',
                 'value':
                 get_value(this_month_data, 'month_ebf'),
                 'all':
                 get_value(this_month_data, 'ebf'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
             {
                 'label':
                 _('Children initiated appropriate Complementary Feeding'),
                 'help_text':
                 _("Of the total children between the ages of 6 to 8 months, the percentage that was "
                   "given a timely introduction to solid, semi-solid or soft food. Timely intiation of "
                   "complementary feeding in addition to breastmilk at 6 months of age is a key feeding "
                   "practice to reduce malnutrition"),
                 'percent':
                 percent_diff('month_cf', this_month_data, prev_month_data,
                              'cf'),
                 'color':
                 'green' if percent_diff('month_cf', this_month_data,
                                         prev_month_data, 'cf') > 0 else 'red',
                 'value':
                 get_value(this_month_data, 'month_cf'),
                 'all':
                 get_value(this_month_data, 'cf'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
         ],
         [
             {
                 'label':
                 _('Immunization Coverage (at age 1 year)'),
                 'help_text':
                 _(("Of the total number of children enrolled for Anganwadi Services who are over a year old, "
                    "the percentage of children who have received the complete immunization as per the "
                    "National Immunization Schedule of India that is required by age 1."
                    "<br/><br/> "
                    "This includes the following immunizations:<br/> "
                    "If Pentavalent path: Penta1/2/3, OPV1/2/3, BCG, Measles, VitA1<br/> "
                    "If DPT/HepB path: DPT1/2/3, HepB1/2/3, OPV1/2/3, BCG, Measles, VitA1"
                    )),
                 'percent':
                 percent_diff('immunized', this_month_data, prev_month_data,
                              'eligible'),
                 'color':
                 'green'
                 if percent_diff('immunized', this_month_data, prev_month_data,
                                 'eligible') > 0 else 'red',
                 'value':
                 get_value(this_month_data, 'immunized'),
                 'all':
                 get_value(this_month_data, 'eligible'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
             {
                 'label':
                 _('Institutional Deliveries'),
                 'help_text':
                 _(("Of the total number of women who gave birth in the last month, the percentage who "
                    "delivered in a public or private medical facility. Delivery in medical instituitions "
                    "is associated with a decrease in maternal mortality rate"
                    )),
                 'percent':
                 percent_diff('institutional_delivery_in_month_sum',
                              this_month_institutional_delivery_data,
                              prev_month_institutional_delivery_data,
                              'delivered_in_month_sum'),
                 'color':
                 'green'
                 if percent_diff('institutional_delivery_in_month_sum',
                                 this_month_institutional_delivery_data,
                                 prev_month_institutional_delivery_data,
                                 'delivered_in_month_sum') > 0 else 'red',
                 'value':
                 get_value(this_month_institutional_delivery_data,
                           'institutional_delivery_in_month_sum'),
                 'all':
                 get_value(this_month_institutional_delivery_data,
                           'delivered_in_month_sum'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
         ]]
    }
示例#16
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': _((
                        "Of the total children enrolled for Anganwadi services and weighed, the percentage "
                        "of children between 0-5 years who were moderately/severely underweight in the current "
                        "month. Children who are moderately or severely underweight have a higher risk "
                        "of mortality. "
                    )),
                    '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': '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': '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': 'stunting'
                },
                {
                    'label': _('Newborns with Low Birth Weight'),
                    'help_text': _((
                        "Of all the children born in the current month and enrolled for Anganwadi services, "
                        "the percentage that had a birth weight less than 2500 grams. Newborns with Low Birth "
                        "Weight are closely associated wtih foetal and neonatal mortality and morbidity, "
                        "inhibited growth and cognitive development, and chronic diseases later in life. ")),
                    'percent': percent_diff(
                        'low_birth_weight',
                        this_month_data,
                        prev_month_data,
                        'weighed_and_born_in_month'
                    ),
                    'color': 'red' if percent_diff(
                        'low_birth_weight',
                        this_month_data,
                        prev_month_data,
                        'weighed_and_born_in_month'
                    ) > 0 else 'green',
                    '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': 'low_birth'
                }
            ],
            [
                {
                    'label': _('Early Initiation of Breastfeeding'),
                    'help_text': _((
                        "Of the children born in the last month and enrolled for Anganwadi services, "
                        "the percentage whose breastfeeding was initiated within 1 hour of delivery. "
                        "Early initiation of breastfeeding ensure the newborn recieves the \"first milk\" "
                        "rich in nutrients and encourages exclusive breastfeeding practice")
                    ),
                    'percent': percent_diff(
                        'bf_birth',
                        this_month_data,
                        prev_month_data,
                        'born'
                    ),
                    'color': 'green' if percent_diff(
                        'bf_birth',
                        this_month_data,
                        prev_month_data,
                        'born'
                    ) > 0 else 'red',
                    'value': get_value(this_month_data, 'bf_birth'),
                    'all': get_value(this_month_data, 'born'),
                    'format': 'percent_and_div',
                    'frequency': 'month',
                    'redirect': 'early_initiation'
                },
                {
                    'label': _('Exclusive Breastfeeding'),
                    'help_text': _((
                        "Of the total children enrolled for Anganwadi services between the ages of 0 to 6 months, "
                        "the percentage that was exclusively fed with breast milk. An infant is exclusively "
                        "breastfed if they receive only breastmilk with no additional food or liquids "
                        "(even water), ensuring optimal nutrition and growth between 0 - 6 months")
                    ),
                    'percent': percent_diff(
                        'ebf',
                        this_month_data,
                        prev_month_data,
                        'ebf_eli'
                    ),
                    'color': 'green' if percent_diff(
                        'ebf',
                        this_month_data,
                        prev_month_data,
                        'ebf_eli'
                    ) > 0 else 'red',
                    'value': get_value(this_month_data, 'ebf'),
                    'all': get_value(this_month_data, 'ebf_eli'),
                    'format': 'percent_and_div',
                    'frequency': 'month',
                    'redirect': 'exclusive_breastfeeding'
                }
            ],
            [
                {
                    'label': _('Children initiated appropriate Complementary Feeding'),
                    'help_text': _((
                        "Of the total children enrolled for Anganwadi services between the ages of 6 to 8 months, "
                        "the percentage that was given a timely introduction to solid, semi-solid or soft food. "
                        "Timely intiation of complementary feeding in addition to breastmilk at 6 months of age "
                        "is a key feeding practice to reduce malnutrition")
                    ),
                    'percent': percent_diff(
                        'cf_initiation',
                        this_month_data,
                        prev_month_data,
                        'cf_initiation_eli'
                    ),
                    'color': 'green' if percent_diff(
                        'cf_initiation',
                        this_month_data,
                        prev_month_data,
                        'cf_initiation_eli'
                    ) > 0 else 'red',
                    'value': get_value(this_month_data, 'cf_initiation'),
                    'all': get_value(this_month_data, 'cf_initiation_eli'),
                    'format': 'percent_and_div',
                    'frequency': 'month',
                    'redirect': 'children_initiated'
                },
                {
                    'label': _('Institutional Deliveries'),
                    'help_text': _((
                        "Of the total number of women enrolled for Anganwadi services who gave birth in the last "
                        "month, the percentage who delivered in a public or private medical facility. Delivery "
                        "in medical instituitions is associated with a decrease in maternal mortality rate")
                    ),
                    'percent': percent_diff(
                        'institutional_delivery',
                        deliveries_this_month,
                        deliveries_prev_month,
                        'delivered'
                    ),
                    'color': 'green' if percent_diff(
                        'institutional_delivery',
                        deliveries_this_month,
                        deliveries_prev_month,
                        'delivered'
                    ) > 0 else 'red',
                    'value': get_value(deliveries_this_month, 'institutional_delivery'),
                    'all': get_value(deliveries_this_month, 'delivered'),
                    'format': 'percent_and_div',
                    'frequency': 'month',
                    'redirect': 'institutional_deliveries'
                }
            ]
        ]
    }
示例#17
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',
                }
            ]
        ]
    }
示例#18
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': 'red' if percent_diff(
                        'low_birth_weight',
                        this_month_data,
                        prev_month_data,
                        'weighed_and_born_in_month'
                    ) > 0 else 'green',
                    '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': 'green' if percent_diff(
                        'bf_birth',
                        this_month_data,
                        prev_month_data,
                        'born'
                    ) > 0 else 'red',
                    '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': 'green' if percent_diff(
                        'ebf',
                        this_month_data,
                        prev_month_data,
                        'ebf_eli'
                    ) > 0 else 'red',
                    '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': 'green' if percent_diff(
                        'cf_initiation',
                        this_month_data,
                        prev_month_data,
                        'cf_initiation_eli'
                    ) > 0 else 'red',
                    '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': 'green' if percent_diff(
                        'institutional_delivery',
                        deliveries_this_month,
                        deliveries_prev_month,
                        'delivered'
                    ) > 0 else 'red',
                    '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'
                }
            ]
        ]
    }
示例#19
0
def get_awc_reports_maternal_child(domain,
                                   config,
                                   month,
                                   prev_month,
                                   show_test=False):
    def get_data_for(date):
        queryset = AggChildHealthMonthly.objects.filter(
            month=date,
            **config).values('month', 'aggregation_level').annotate(
                underweight=(Sum('nutrition_status_moderately_underweight') +
                             Sum('nutrition_status_severely_underweight')),
                valid_in_month=Sum('valid_in_month'),
                immunized=(Sum('fully_immunized_on_time') +
                           Sum('fully_immunized_late')),
                eligible=Sum('fully_immunized_eligible'),
                wasting=(Sum('wasting_moderate') + Sum('wasting_severe')),
                height=Sum('height_eligible'),
                stunting=(Sum('stunting_moderate') + Sum('stunting_severe')),
                low_birth=Sum('low_birth_weight_in_month'),
                birth=Sum('bf_at_birth'),
                born=Sum('born_in_month'),
                month_ebf=Sum('ebf_in_month'),
                ebf=Sum('ebf_eligible'),
                month_cf=Sum('cf_initiation_in_month'),
                cf=Sum('cf_initiation_eligible'))
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    def get_weight_efficiency(date):
        queryset = AggAwcMonthly.objects.filter(month=date, **config).values(
            'month', 'aggregation_level',
            'awc_name').annotate(wer_weight=Sum('wer_weighed'),
                                 wer_eli=Sum('wer_eligible'))
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    def get_institutional_delivery_data(date):
        queryset = AggCcsRecordMonthly.objects.filter(
            month=date, **config).values(
                'month', 'aggregation_level', 'awc_name').annotate(
                    institutional_delivery_in_month_sum=Sum(
                        'institutional_delivery_in_month'),
                    delivered_in_month_sum=Sum('delivered_in_month'))
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    this_month_data = get_data_for(datetime(*month))
    prev_month_data = get_data_for(datetime(*prev_month))

    this_month_data_we = get_weight_efficiency(datetime(*month))
    prev_month_data_we = get_weight_efficiency(datetime(*prev_month))

    this_month_institutional_delivery_data = get_institutional_delivery_data(
        datetime(*month))
    prev_month_institutional_delivery_data = get_institutional_delivery_data(
        datetime(*prev_month))

    return {
        'kpi':
        [[
            {
                'label':
                _('Underweight (Weight-for-Age)'),
                'help_text':
                _(("""
                        Percentage of children between 0-5 years enrolled for ICDS services with weight-for-age
                        less than -2 standard deviations of the WHO Child Growth Standards median.
                        Children who are moderately or severely underweight have a higher risk of mortality.
                        """)),
                'percent':
                percent_diff('underweight', this_month_data, prev_month_data,
                             'valid_in_month'),
                'color':
                'red' if percent_diff('underweight', this_month_data,
                                      prev_month_data, 'valid_in_month') > 0
                else 'green',
                'value':
                get_value(this_month_data, 'underweight'),
                'all':
                get_value(this_month_data, 'valid_in_month'),
                'format':
                'percent_and_div',
                'frequency':
                'month'
            },
            {
                'label':
                _('Wasting (Weight-for-Height)'),
                'help_text':
                _(("""
                        Percentage of children between 6 - 60 months enrolled for
                        ICDS services with weight-for-height
                        below -2 standard deviations of the WHO Child Growth Standards median.

                        Severe Acute Malnutrition (SAM) or wasting in children is a symptom of acute
                        undernutrition usually as a consequence
                        of insufficient food intake or a high incidence of infectious diseases.
                        """)),
                'percent':
                percent_diff('wasting', this_month_data, prev_month_data,
                             'height'),
                'color':
                'red'
                if percent_diff('wasting', this_month_data, prev_month_data,
                                'height') > 0 else 'green',
                'value':
                get_value(this_month_data, 'wasting'),
                'all':
                get_value(this_month_data, 'height'),
                'format':
                'percent_and_div',
                'frequency':
                'month'
            },
        ],
         [
             {
                 'label':
                 _('Stunting (Height-for-Age)'),
                 'help_text':
                 _(("""
                            Percentage of children (6-60 months) with height-for-age below -2Z
                            standard deviations of the WHO Child Growth Standards median.
                            Stunting in children is a sign of chronic undernutrition and
                            has long lasting harmful consequences on the growth of a child
                        """)),
                 'percent':
                 percent_diff('stunting', this_month_data, prev_month_data,
                              'height'),
                 'color':
                 'red'
                 if percent_diff('stunting', this_month_data, prev_month_data,
                                 'height') > 0 else 'green',
                 'value':
                 get_value(this_month_data, 'stunting'),
                 'all':
                 get_value(this_month_data, 'height'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
             {
                 'label':
                 _('Weighing Efficiency'),
                 'help_text':
                 _(("""
                        Percentage of children (0-5 years) who
                        have been weighed of total children enrolled for ICDS services
                        """)),
                 'percent':
                 percent_diff('wer_weight', this_month_data_we,
                              prev_month_data_we, 'wer_eli'),
                 'color':
                 'green'
                 if percent_diff('wer_weight', this_month_data_we,
                                 prev_month_data_we, 'wer_eli') > 0 else 'red',
                 'value':
                 get_value(this_month_data_we, 'wer_weight'),
                 'all':
                 get_value(this_month_data_we, 'wer_eli'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
         ],
         [
             {
                 'label':
                 _('Newborns with Low Birth Weight'),
                 'help_text':
                 _("""
                        Percentage of newborns born with birth weight less than 2500 grams.
                        Newborns with Low Birth Weight are closely associated with foetal and
                        neonatal mortality and morbidity, inhibited growth and cognitive development,
                        and chronic diseases later in life"
                        """),
                 'percent':
                 percent_diff('low_birth', this_month_data, prev_month_data,
                              'born'),
                 'color':
                 'red'
                 if percent_diff('low_birth', this_month_data, prev_month_data,
                                 'born') > 0 else 'green',
                 'value':
                 get_value(this_month_data, 'low_birth'),
                 'all':
                 get_value(this_month_data, 'born'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
             {
                 'label':
                 _('Early Initiation of Breastfeeding'),
                 'help_text':
                 _("""
                        Percentage of children who were put to the breast within one hour of birth.

                        Early initiation of breastfeeding ensure the newborn recieves the ""first milk""
                        rich in nutrients and encourages exclusive breastfeeding practice
                        """),
                 'percent':
                 percent_diff('birth', this_month_data, prev_month_data,
                              'born'),
                 'color':
                 'green'
                 if percent_diff('birth', this_month_data, prev_month_data,
                                 'born') > 0 else 'red',
                 'value':
                 get_value(this_month_data, 'birth'),
                 'all':
                 get_value(this_month_data, 'born'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
         ],
         [
             {
                 'label':
                 _('Exclusive breastfeeding'),
                 'help_text':
                 _("""
                        Percentage of infants 0-6 months of age who are fed exclusively with breast milk.
                        An infant is exclusively breastfed if they recieve only breastmilk
                        with no additional food, liquids (even water) ensuring
                        optimal nutrition and growth between 0 - 6 months"
                        """),
                 'percent':
                 percent_diff('month_ebf', this_month_data, prev_month_data,
                              'ebf'),
                 'color':
                 'green'
                 if percent_diff('month_ebf', this_month_data, prev_month_data,
                                 'ebf') > 0 else 'red',
                 'value':
                 get_value(this_month_data, 'month_ebf'),
                 'all':
                 get_value(this_month_data, 'ebf'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
             {
                 'label':
                 _('Children initiated appropriate Complementary Feeding'),
                 'help_text':
                 _("""
                        Percentage of children between 6 - 8 months given timely introduction to solid,
                        semi-solid or soft food.
                        Timely intiation of complementary feeding in addition to breastmilk
                        at 6 months of age is a key feeding practice to reduce malnutrition"
                        """),
                 'percent':
                 percent_diff('month_cf', this_month_data, prev_month_data,
                              'cf'),
                 'color':
                 'green' if percent_diff('month_cf', this_month_data,
                                         prev_month_data, 'cf') > 0 else 'red',
                 'value':
                 get_value(this_month_data, 'month_cf'),
                 'all':
                 get_value(this_month_data, 'cf'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
         ],
         [
             {
                 'label':
                 _('Immunization Coverage (at age 1 year)'),
                 'help_text':
                 _(("""
                        Percentage of children 1 year+ who have recieved complete immunization as per
                        National Immunization Schedule of India required by age 1.
                        """)),
                 'percent':
                 percent_diff('immunized', this_month_data, prev_month_data,
                              'eligible'),
                 'color':
                 'green'
                 if percent_diff('immunized', this_month_data, prev_month_data,
                                 'eligible') > 0 else 'red',
                 'value':
                 get_value(this_month_data, 'immunized'),
                 'all':
                 get_value(this_month_data, 'eligible'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
             {
                 'label':
                 _('Institutional Deliveries'),
                 'help_text':
                 _(("""
                            Percentage of pregant women who delivered in a public or private medical
                            facility in the last month.
                            Delivery in medical instituitions is associated with a decrease maternal mortality rate
                        """)),
                 'percent':
                 percent_diff('institutional_delivery_in_month_sum',
                              this_month_institutional_delivery_data,
                              prev_month_institutional_delivery_data,
                              'delivered_in_month_sum'),
                 'color':
                 'green'
                 if percent_diff('institutional_delivery_in_month_sum',
                                 this_month_institutional_delivery_data,
                                 prev_month_institutional_delivery_data,
                                 'delivered_in_month_sum') > 0 else 'red',
                 'value':
                 get_value(this_month_institutional_delivery_data,
                           'institutional_delivery_in_month_sum'),
                 'all':
                 get_value(prev_month_institutional_delivery_data,
                           'delivered_in_month_sum'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
         ]]
    }
def get_demographics_data(domain, yesterday, config, show_test=False):
    yesterday_date = datetime(*yesterday)
    two_days_ago = (yesterday_date - relativedelta(days=1)).date()

    def get_data_for(date, filters):
        queryset = AggAwcDailyView.objects.filter(
            date=date, **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_18'),
                person_adolescent_all=Sum(
                    'cases_person_adolescent_girls_11_18_all'),
                person_aadhaar=Sum('cases_person_has_aadhaar'),
                all_persons=Sum('cases_person'))

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

    yesterday_data = get_data_for(yesterday_date, config)
    two_days_ago_data = get_data_for(two_days_ago, config)

    return {
        'records':
        [[{
            'label':
            _('Registered Households'),
            'help_text':
            _('Total number of households registered'),
            'percent':
            percent_increase('household', yesterday_data, two_days_ago_data),
            'color':
            'green' if percent_increase('household', yesterday_data,
                                        two_days_ago_data) > 0 else 'red',
            'value':
            get_value(yesterday_data, 'household'),
            'all':
            None,
            'format':
            'number',
            'frequency':
            'day',
            'redirect':
            'registered_household'
        }, {
            'label':
            _('Children (0-6 years)'),
            'help_text':
            _('Total number of children registered between the age of 0 - 6 years'
              ),
            'percent':
            percent_increase('child_health_all', yesterday_data,
                             two_days_ago_data),
            'color':
            'green' if percent_increase('child_health_all', yesterday_data,
                                        two_days_ago_data) > 0 else 'red',
            'value':
            get_value(yesterday_data, 'child_health_all'),
            'all':
            None,
            'format':
            'number',
            'frequency':
            'day',
            'redirect':
            'enrolled_children'
        }],
         [{
             'label':
             _('Children (0-6 years) enrolled for ICDS services'),
             'help_text':
             _(("Total number of children registered between the age of 0 - 6 years "
                "and enrolled for ICDS services")),
             'percent':
             percent_increase('child_health', yesterday_data,
                              two_days_ago_data),
             'color':
             'green' if percent_increase('child_health', yesterday_data,
                                         two_days_ago_data) > 0 else 'red',
             'value':
             get_value(yesterday_data, 'child_health'),
             'all':
             None,
             'format':
             'number',
             'frequency':
             'day'
         }, {
             'label':
             _('Pregnant Women'),
             'help_text':
             _('Total number of pregnant women registered'),
             'percent':
             percent_increase('ccs_pregnant_all', yesterday_data,
                              two_days_ago_data),
             'color':
             'green' if percent_increase('ccs_pregnant_all', yesterday_data,
                                         two_days_ago_data) > 0 else 'red',
             'value':
             get_value(yesterday_data, 'ccs_pregnant_all'),
             'all':
             None,
             'format':
             'number',
             'frequency':
             'day',
             'redirect':
             'enrolled_women'
         }],
         [{
             'label':
             _('Pregnant Women enrolled for ICDS services'),
             'help_text':
             _('Total number of pregnant women registered and enrolled for ICDS services'
               ),
             'percent':
             percent_increase('ccs_pregnant', yesterday_data,
                              two_days_ago_data),
             'color':
             'green' if percent_increase('ccs_pregnant', yesterday_data,
                                         two_days_ago_data) > 0 else 'red',
             'value':
             get_value(yesterday_data, 'ccs_pregnant'),
             'all':
             None,
             'format':
             'number',
             'frequency':
             'day'
         }, {
             'label':
             _('Lactating Women'),
             'help_text':
             _('Total number of lactating women registered'),
             'percent':
             percent_increase('css_lactating_all', yesterday_data,
                              two_days_ago_data),
             'color':
             'green' if percent_increase('css_lactating_all', yesterday_data,
                                         two_days_ago_data) > 0 else 'red',
             'value':
             get_value(yesterday_data, 'css_lactating_all'),
             'all':
             None,
             'format':
             'number',
             'frequency':
             'day',
             'redirect':
             'lactating_enrolled_women'
         }],
         [{
             'label':
             _('Lactating Women enrolled for ICDS services'),
             'help_text':
             _('Total number of lactating women registered and enrolled for ICDS services'
               ),
             'percent':
             percent_increase('css_lactating', yesterday_data,
                              two_days_ago_data),
             'color':
             'green' if percent_increase('css_lactating', yesterday_data,
                                         two_days_ago_data) > 0 else 'red',
             'value':
             get_value(yesterday_data, 'css_lactating'),
             'all':
             None,
             'format':
             'number',
             'frequency':
             'day'
         }, {
             'label':
             _('Adolescent Girls (11-18 years)'),
             'help_text':
             _('Total number of adolescent girls (11 - 18 years) who are registered'
               ),
             'percent':
             percent_increase('person_adolescent_all', yesterday_data,
                              two_days_ago_data),
             'color':
             'green'
             if percent_increase('person_adolescent_all', yesterday_data,
                                 two_days_ago_data) > 0 else 'red',
             'value':
             get_value(yesterday_data, 'person_adolescent_all'),
             'all':
             None,
             'format':
             'number',
             'redirect':
             'adolescent_girls'
         }],
         [{
             'label':
             _('Adolescent Girls (11-18 years) enrolled for ICDS services'),
             'help_text':
             _(("Total number of adolescent girls (11 - 18 years) "
                "who are registered and enrolled for ICDS services")),
             'percent':
             percent_increase('person_adolescent', yesterday_data,
                              two_days_ago_data),
             'color':
             'green' if percent_increase('person_adolescent', yesterday_data,
                                         two_days_ago_data) > 0 else 'red',
             'value':
             get_value(yesterday_data, 'person_adolescent'),
             'all':
             None,
             'format':
             'number',
             'frequency':
             'day'
         }, {
             'label':
             _('Percent Adhaar Seeded Individuals'),
             'help_text':
             _(('Percentage of ICDS beneficiaries whose Adhaar identification has been captured'
                )),
             'percent':
             percent_diff('person_aadhaar', yesterday_data, two_days_ago_data,
                          'all_persons'),
             'color':
             'green' if percent_increase('person_aadhaar', yesterday_data,
                                         two_days_ago_data) > 0 else 'red',
             'value':
             get_value(yesterday_data, 'person_aadhaar'),
             'all':
             get_value(yesterday_data, 'all_persons'),
             'format':
             'percent_and_div',
             'frequency':
             'day',
             'redirect':
             'adhaar'
         }]]
    }
示例#21
0
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': 'green' if percent_diff(
                        'clean_water',
                        this_month_data,
                        prev_month_data,
                        'sum_last_update'
                    ) > 0 else 'red',
                    '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': 'green' if percent_diff(
                        'functional_toilet',
                        this_month_data,
                        prev_month_data,
                        'sum_last_update'
                    ) > 0 else 'red',
                    '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': 'green' if percent_diff(
                        'infant_scale',
                        this_month_data,
                        prev_month_data,
                        'sum_last_update'
                    ) > 0 else 'red',
                    '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': 'green' if percent_diff(
                        'adult_scale',
                        this_month_data,
                        prev_month_data,
                        'sum_last_update'
                    ) > 0 else 'red',
                    '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': 'green' if percent_diff(
                        'medicine_kits',
                        this_month_data,
                        prev_month_data,
                        'sum_last_update'
                    ) > 0 else 'red',
                    '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'
            #     }
            # ]
        ]
    }
示例#22
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'
         }]]
    }
示例#23
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'
         }]]
    }
示例#24
0
def get_awc_reports_maternal_child(domain,
                                   config,
                                   month,
                                   prev_month,
                                   show_test=False,
                                   icds_feature_flag=False):
    def get_data_for(date):
        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, 'height_measured_in_month')
        weighed_and_height_measured_in_month = exclude_records_by_age_for_column(
            age_filters, 'weighed_and_height_measured_in_month')

        queryset = AggChildHealthMonthly.objects.filter(
            month=date,
            **config).values('month', 'aggregation_level').annotate(
                underweight=(Sum(moderately_underweight) +
                             Sum(severely_underweight)),
                valid_weighed=Sum(nutrition_status_weighed),
                immunized=(Sum('fully_immunized_on_time') +
                           Sum('fully_immunized_late')),
                eligible=Sum('fully_immunized_eligible'),
                wasting=Sum(wasting_moderate) + Sum(wasting_severe),
                height_measured_in_month=Sum(height_measured_in_month),
                weighed_and_height_measured_in_month=Sum(
                    weighed_and_height_measured_in_month),
                stunting=Sum(stunting_moderate) + Sum(stunting_severe),
                low_birth=Sum('low_birth_weight_in_month'),
                birth=Sum('bf_at_birth'),
                born=Sum('born_in_month'),
                weighed_and_born_in_month=Sum('weighed_and_born_in_month'),
                month_ebf=Sum('ebf_in_month'),
                ebf=Sum('ebf_eligible'),
                month_cf=Sum('cf_initiation_in_month'),
                cf=Sum('cf_initiation_eligible'))
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    def get_weight_efficiency(date):
        queryset = AggAwcMonthly.objects.filter(month=date, **config).values(
            'month', 'aggregation_level',
            'awc_name').annotate(wer_weight=Sum('wer_weighed'),
                                 wer_eli=Sum('wer_eligible'))
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    def get_institutional_delivery_data(date):
        queryset = AggCcsRecordMonthly.objects.filter(
            month=date, **config).values(
                'month', 'aggregation_level', 'awc_name').annotate(
                    institutional_delivery_in_month_sum=Sum(
                        'institutional_delivery_in_month'),
                    delivered_in_month_sum=Sum('delivered_in_month'))
        if not show_test:
            queryset = apply_exclude(domain, queryset)
        return queryset

    this_month_data = get_data_for(datetime(*month))
    prev_month_data = get_data_for(datetime(*prev_month))

    this_month_data_we = get_weight_efficiency(datetime(*month))
    prev_month_data_we = get_weight_efficiency(datetime(*prev_month))

    this_month_institutional_delivery_data = get_institutional_delivery_data(
        datetime(*month))
    prev_month_institutional_delivery_data = get_institutional_delivery_data(
        datetime(*prev_month))

    return {
        'kpi':
        [[
            {
                'label':
                _('Underweight (Weight-for-Age)'),
                'help_text':
                _(("Percentage of children between 0 - 5 years enrolled for Anganwadi Services with "
                   "weight-for-age less than -2 standard deviations of the WHO Child "
                   "Growth Standards median. Children who are moderately or severely underweight "
                   "have a higher risk of mortality. ")),
                'percent':
                percent_diff('underweight', this_month_data, prev_month_data,
                             'valid_weighed'),
                'color':
                'red' if percent_diff('underweight', this_month_data,
                                      prev_month_data, 'valid_weighed') > 0
                else 'green',
                'value':
                get_value(this_month_data, 'underweight'),
                'all':
                get_value(this_month_data, 'valid_weighed'),
                'format':
                'percent_and_div',
                'frequency':
                'month'
            },
            {
                'label':
                _('Wasting (Weight-for-Height)'),
                'help_text':
                wasting_help_text(icds_feature_flag),
                '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'
            },
        ],
         [
             {
                 'label':
                 _('Stunting (Height-for-Age)'),
                 'help_text':
                 stunting_help_text(icds_feature_flag),
                 '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'
             },
             {
                 'label':
                 _('Weighing Efficiency'),
                 'help_text':
                 _("Percentage of children (0 - 5 years) who have been weighed of total children "
                   "enrolled for Anganwadi Services"),
                 'percent':
                 percent_diff('wer_weight', this_month_data_we,
                              prev_month_data_we, 'wer_eli'),
                 'color':
                 'green'
                 if percent_diff('wer_weight', this_month_data_we,
                                 prev_month_data_we, 'wer_eli') > 0 else 'red',
                 'value':
                 get_value(this_month_data_we, 'wer_weight'),
                 'all':
                 get_value(this_month_data_we, 'wer_eli'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
         ],
         [
             {
                 'label':
                 _('Newborns with Low Birth Weight'),
                 'help_text':
                 _("Percentage of newborns born with birth weight less than 2500 grams. "
                   "Newborns with Low Birth Weight are closely associated with foetal and "
                   "neonatal mortality and morbidity, inhibited growth and cognitive development, "
                   "and chronic diseases later in life"),
                 'percent':
                 percent_diff('low_birth', this_month_data, prev_month_data,
                              'weighed_and_born_in_month'),
                 'color':
                 'red'
                 if percent_diff('low_birth', this_month_data, prev_month_data,
                                 'weighed_and_born_in_month') > 0 else 'green',
                 'value':
                 get_value(this_month_data, 'low_birth'),
                 'all':
                 get_value(this_month_data, 'weighed_and_born_in_month'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
             {
                 'label':
                 _('Early Initiation of Breastfeeding'),
                 'help_text':
                 _("Percentage of children who were put to the breast within one hour of birth. "
                   "Early initiation of breastfeeding ensure the newborn receives the 'first milk' "
                   "rich in nutrients and encourages exclusive breastfeeding practice"
                   ),
                 'percent':
                 percent_diff('birth', this_month_data, prev_month_data,
                              'born'),
                 'color':
                 'green'
                 if percent_diff('birth', this_month_data, prev_month_data,
                                 'born') > 0 else 'red',
                 'value':
                 get_value(this_month_data, 'birth'),
                 'all':
                 get_value(this_month_data, 'born'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
         ],
         [
             {
                 'label':
                 _('Exclusive breastfeeding'),
                 'help_text':
                 _("Percentage of infants 0-6 months of age who are fed exclusively with breast milk. "
                   "An infant is exclusively breastfed if they receive only breastmilk "
                   "with no additional food, liquids (even water) ensuring "
                   "optimal nutrition and growth between 0 - 6 months"),
                 'percent':
                 percent_diff('month_ebf', this_month_data, prev_month_data,
                              'ebf'),
                 'color':
                 'green'
                 if percent_diff('month_ebf', this_month_data, prev_month_data,
                                 'ebf') > 0 else 'red',
                 'value':
                 get_value(this_month_data, 'month_ebf'),
                 'all':
                 get_value(this_month_data, 'ebf'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
             {
                 'label':
                 _('Children initiated appropriate Complementary Feeding'),
                 'help_text':
                 _("Percentage of children between 6 - 8 months given timely introduction to solid, "
                   "semi-solid or soft food. "
                   "Timely initiation of complementary feeding in addition to breastmilk "
                   "at 6 months of age is a key feeding practice to reduce malnutrition"
                   ),
                 'percent':
                 percent_diff('month_cf', this_month_data, prev_month_data,
                              'cf'),
                 'color':
                 'green' if percent_diff('month_cf', this_month_data,
                                         prev_month_data, 'cf') > 0 else 'red',
                 'value':
                 get_value(this_month_data, 'month_cf'),
                 'all':
                 get_value(this_month_data, 'cf'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
         ],
         [
             {
                 'label':
                 _('Immunization Coverage (at age 1 year)'),
                 'help_text':
                 _(("Percentage of children 1 year+ who have received complete immunization as per "
                    "National Immunization Schedule of India required by age 1. "
                    "<br/><br/> "
                    "This includes the following immunizations:<br/> "
                    "If Pentavalent path: Penta1/2/3, OPV1/2/3, BCG, Measles, VitA1<br/> "
                    "If DPT/HepB path: DPT1/2/3, HepB1/2/3, OPV1/2/3, BCG, Measles, VitA1"
                    )),
                 'percent':
                 percent_diff('immunized', this_month_data, prev_month_data,
                              'eligible'),
                 'color':
                 'green'
                 if percent_diff('immunized', this_month_data, prev_month_data,
                                 'eligible') > 0 else 'red',
                 'value':
                 get_value(this_month_data, 'immunized'),
                 'all':
                 get_value(this_month_data, 'eligible'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
             {
                 'label':
                 _('Institutional Deliveries'),
                 'help_text':
                 _(("Percentage of pregnant women who delivered in a public or private medical "
                    "facility in the last month. "
                    "Delivery in medical institutions is associated with a decrease maternal mortality rate"
                    )),
                 'percent':
                 percent_diff('institutional_delivery_in_month_sum',
                              this_month_institutional_delivery_data,
                              prev_month_institutional_delivery_data,
                              'delivered_in_month_sum'),
                 'color':
                 'green'
                 if percent_diff('institutional_delivery_in_month_sum',
                                 this_month_institutional_delivery_data,
                                 prev_month_institutional_delivery_data,
                                 'delivered_in_month_sum') > 0 else 'red',
                 'value':
                 get_value(this_month_institutional_delivery_data,
                           'institutional_delivery_in_month_sum'),
                 'all':
                 get_value(this_month_institutional_delivery_data,
                           'delivered_in_month_sum'),
                 'format':
                 'percent_and_div',
                 'frequency':
                 'month'
             },
         ]]
    }