def test_sector_data(self):
     self.assertDictEqual(
         get_stadiometer_sector_data('icds-cas',
                                     config={
                                         'month': (2017, 4, 1),
                                         'state_id': 'st1',
                                         'district_id': 'd1',
                                         'block_id': 'b2',
                                         'aggregation_level': 4
                                     },
                                     location_id='b1',
                                     loc_level='supervisor'),
         {
             "info":
             awcs_reported_stadiometer_text(),
             "tooltips_data": {
                 "s3": {
                     "in_month": 0,
                     "all": 2
                 },
                 "s4": {
                     "in_month": 1,
                     "all": 3
                 }
             },
             "chart_data":
             [{
                 "color": MapColors.BLUE,
                 "values": [["s3", 0.0], ["s4", 0.3333333333333333]],
                 "strokeWidth": 2,
                 "classed": "dashed",
                 "key": ""
             }]
         })
Example #2
0
def get_stadiometer_sector_data(domain,
                                config,
                                loc_level,
                                location_id,
                                show_test=False):
    group_by = ['%s_name' % loc_level]

    config['month'] = datetime(*config['month'])
    data = AggAwcMonthly.objects.filter(**config).values(*group_by).annotate(
        in_month=Sum('stadiometer'),
        all=Sum('num_awc_infra_last_update')).order_by('%s_name' % loc_level)

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

    chart_data = {
        'blue': [],
    }

    tooltips_data = defaultdict(lambda: {'in_month': 0, 'all': 0})

    for row in data:
        valid = row['all']
        name = row['%s_name' % loc_level]

        in_month = row['in_month']
        row_values = {'in_month': in_month or 0, 'all': valid or 0}

        for prop, value in row_values.items():
            tooltips_data[name][prop] += value

        value = (in_month or 0) / float(valid or 1)

        chart_data['blue'].append([name, value])

    chart_data['blue'] = sorted(chart_data['blue'])

    return {
        "tooltips_data":
        dict(tooltips_data),
        "info":
        awcs_reported_stadiometer_text(),
        "chart_data": [
            {
                "values": chart_data['blue'],
                "key": "",
                "strokeWidth": 2,
                "classed": "dashed",
                "color": MapColors.BLUE
            },
        ]
    }
Example #3
0
def get_stadiometer_data_map(domain,
                             config,
                             loc_level,
                             show_test=False,
                             beta=False):
    def get_data_for(filters):
        filters['month'] = datetime(*filters['month'])
        queryset = AggAwcMonthly.objects.filter(**filters).values(
            '%s_name' % loc_level,
            '%s_map_location_name' % loc_level).annotate(
                in_month=Sum('stadiometer'),
                all=Sum('num_awc_infra_last_update'),
            ).order_by('%s_name' % loc_level,
                       '%s_map_location_name' % loc_level)

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

    data_for_map, valid_total, in_month_total, average, total = generate_data_for_map(
        get_data_for(config), loc_level, 'in_month', 'all', 25, 75)

    fills = OrderedDict()
    fills.update({'0%-25%': MapColors.RED})
    fills.update({'25%-75%': MapColors.ORANGE})
    fills.update({'75%-100%': MapColors.PINK})
    if beta:
        fills.update({'Not Launched': MapColors.GREY})
    fills.update({'defaultFill': MapColors.GREY})

    return {
        "slug": "stadiometer",
        "label": "Percentage of AWCs that reported having a Stadiometer",
        "fills": fills,
        "rightLegend": {
            "average":
            average,
            "info":
            awcs_reported_stadiometer_text(),
            "extended_info": [{
                'indicator': ('Total number of AWCs with a Stadiometer:'),
                'value':
                indian_formatted_number(in_month_total)
            }, {
                'indicator': ('% of AWCs with a Stadiometer:'),
                'value':
                '%.2f%%' % (in_month_total * 100 / float(valid_total or 1))
            }]
        },
        "data": dict(data_for_map),
    }
 def test_map_name_is_different_data(self):
     self.assertDictEqual(
         get_stadiometer_data_map(
             'icds-cas',
             config={
                 'month': (2017, 4, 1),
                 'state_id': 'st1',
                 'district_id': 'd1',
                 'aggregation_level': 3
             },
             loc_level='block',
         ), {
             "rightLegend": {
                 "info":
                 awcs_reported_stadiometer_text(),
                 "average":
                 11.11111111111111,
                 'extended_info': [{
                     'indicator':
                     'Total number of AWCs with a Stadiometer:',
                     'value': "1"
                 }, {
                     'indicator': '% of AWCs with a Stadiometer:',
                     'value': '11.11%'
                 }]
             },
             "label":
             "Percentage of AWCs that reported having a Stadiometer",
             "data": {
                 "block_map": {
                     "in_month": 1,
                     "original_name": ["b1", "b2"],
                     "all": 9,
                     "fillKey": "0%-25%"
                 }
             },
             "slug": "stadiometer",
             "fills": {
                 "0%-25%": MapColors.RED,
                 "25%-75%": MapColors.ORANGE,
                 "75%-100%": MapColors.PINK,
                 "defaultFill": MapColors.GREY
             }
         })
 def test_map_data(self):
     self.assertDictEqual(
         get_stadiometer_data_map('icds-cas',
                                  config={
                                      'month': (2017, 4, 1),
                                      'aggregation_level': 1
                                  },
                                  loc_level='state'),
         {
             "rightLegend": {
                 "info":
                 awcs_reported_stadiometer_text(),
                 "average":
                 7.142857142857143,
                 'extended_info': [{
                     'indicator':
                     'Total number of AWCs with a Stadiometer:',
                     'value': "1"
                 }, {
                     'indicator': '% of AWCs with a Stadiometer:',
                     'value': '7.14%'
                 }]
             },
             "label":
             "Percentage of AWCs that reported having a Stadiometer",
             "data": {
                 'st4': {
                     'in_month': 0,
                     'original_name': ['st4'],
                     'all': 0,
                     'fillKey': '0%-25%'
                 },
                 'st5': {
                     'in_month': 0,
                     'original_name': ['st5'],
                     'all': 0,
                     'fillKey': '0%-25%'
                 },
                 'st6': {
                     'in_month': 0,
                     'original_name': ['st6'],
                     'all': 0,
                     'fillKey': '0%-25%'
                 },
                 'st7': {
                     'in_month': 0,
                     'original_name': ['st7'],
                     'all': 0,
                     'fillKey': '0%-25%'
                 },
                 'st1': {
                     'in_month': 1,
                     'original_name': ['st1'],
                     'all': 9,
                     'fillKey': '0%-25%'
                 },
                 'st2': {
                     'in_month': 0,
                     'original_name': ['st2'],
                     'all': 5,
                     'fillKey': '0%-25%'
                 },
                 'st3': {
                     'in_month': 0,
                     'original_name': ['st3'],
                     'all': 0,
                     'fillKey': '0%-25%'
                 }
             },
             "slug": "stadiometer",
             "fills": {
                 "0%-25%": MapColors.RED,
                 "25%-75%": MapColors.ORANGE,
                 "75%-100%": MapColors.PINK,
                 "defaultFill": MapColors.GREY
             }
         })
Example #6
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'),
                infantometer=Sum('infantometer'),
                stadiometer=Sum('stadiometer'))

        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 Reported Infantometer'),
                    'help_text':
                    awcs_reported_infantometer_text(),
                    'percent':
                    percent_diff('infantometer', this_month_data,
                                 prev_month_data, 'sum_last_update'),
                    'color':
                    get_color_with_green_positive(
                        percent_diff('infantometer', this_month_data,
                                     prev_month_data, 'sum_last_update')),
                    'value':
                    get_value(this_month_data, 'infantometer'),
                    'all':
                    get_value(this_month_data, 'sum_last_update'),
                    'format':
                    'percent_and_div',
                    'frequency':
                    'month',
                    'redirect':
                    'awc_infrastructure/infantometer'
                },
            ],
            [{
                'label':
                _('AWCs Reported Stadiometer'),
                'help_text':
                awcs_reported_stadiometer_text(),
                'percent':
                percent_diff('stadiometer', this_month_data, prev_month_data,
                             'sum_last_update'),
                'color':
                get_color_with_green_positive(
                    percent_diff('stadiometer', this_month_data,
                                 prev_month_data, 'sum_last_update')),
                'value':
                get_value(this_month_data, 'stadiometer'),
                'all':
                get_value(this_month_data, 'sum_last_update'),
                'format':
                'percent_and_div',
                'frequency':
                'month',
                'redirect':
                'awc_infrastructure/stadiometer'
            }]
            # [
            #     {
            #         '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'
            #     }
            # ]
        ]
    }