def test_sector_data(self): self.assertDictEqual( get_medicine_kit_sector_data('icds-cas', config={ 'month': (2017, 5, 1), 'state_id': 'st1', 'district_id': 'd1', 'block_id': 'b1', 'aggregation_level': 4 }, location_id='b1', loc_level='supervisor'), { "info": awcs_reported_medicine_kit_help_text(), "tooltips_data": { "s2": { "in_month": 2, "all": 3 }, "s1": { "in_month": 3, "all": 5 } }, "chart_data": [{ "color": MapColors.BLUE, "values": [["s1", 0.6], ["s2", 0.6666666666666666]], "strokeWidth": 2, "classed": "dashed", "key": "" }] })
def get_medicine_kit_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('infra_medicine_kits'), 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_medicine_kit_help_text(), "chart_data": [ { "values": chart_data['blue'], "key": "", "strokeWidth": 2, "classed": "dashed", "color": MapColors.BLUE }, ] }
def test_map_data_right_legend_info(self): data = get_medicine_kit_data_map('icds-cas', config={ 'month': (2017, 5, 1), 'aggregation_level': 1 }, loc_level='state') expected = awcs_reported_medicine_kit_help_text() self.assertEquals(data['rightLegend']['info'], expected)
def get_medicine_kit_data_map(domain, config, loc_level, show_test=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('infra_medicine_kits'), 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}) fills.update({'defaultFill': MapColors.GREY}) return { "slug": "medicine_kit", "label": "Percentage of AWCs that reported having a Medicine Kit", "fills": fills, "rightLegend": { "average": average, "info": awcs_reported_medicine_kit_help_text(), "extended_info": [ { 'indicator': ( 'Total number of AWCs with a Medicine Kit:' ), 'value': indian_formatted_number(in_month_total) }, { 'indicator': ( '% of AWCs with a Medicine Kit:' ), 'value': '%.2f%%' % (in_month_total * 100 / float(valid_total or 1)) } ] }, "data": dict(data_for_map), }
def get_medicine_kit_data_map(domain, config, loc_level, show_test=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('infra_medicine_kits'), 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}) fills.update({'defaultFill': MapColors.GREY}) return { "slug": "medicine_kit", "label": "Percentage of AWCs that reported having a Medicine Kit", "fills": fills, "rightLegend": { "average": average, "info": awcs_reported_medicine_kit_help_text(), "extended_info": [ { 'indicator': ( 'Total number of AWCs with a Medicine Kit:' ), 'value': indian_formatted_number(in_month_total) }, { 'indicator': ( '% of AWCs with a Medicine Kit:' ), 'value': '%.2f%%' % (in_month_total * 100 / float(valid_total or 1)) } ] }, "data": dict(data_for_map), }
def test_map_data_right_legend_info(self): data = get_medicine_kit_data_map( 'icds-cas', config={ 'month': (2017, 5, 1), 'aggregation_level': 1 }, loc_level='state' ) expected = awcs_reported_medicine_kit_help_text() self.assertEquals(data['rightLegend']['info'], expected)
def test_sector_data(self): self.assertDictEqual( get_medicine_kit_sector_data( 'icds-cas', config={ 'month': (2017, 5, 1), 'state_id': 'st1', 'district_id': 'd1', 'block_id': 'b1', 'aggregation_level': 4 }, location_id='b1', loc_level='supervisor' ), { "info": awcs_reported_medicine_kit_help_text(), "tooltips_data": { "s2": { "in_month": 2, "all": 3 }, "s1": { "in_month": 3, "all": 5 } }, "chart_data": [ { "color": MapColors.BLUE, "values": [ [ "s1", 0.6 ], [ "s2", 0.6666666666666666 ] ], "strokeWidth": 2, "classed": "dashed", "key": "" } ] } )
def test_data_AWCs_reported_medicine_kit(self): self.assertDictEqual( get_awc_infrastructure_data( 'icds-cas', { 'month': (2017, 5, 1), 'prev_month': (2017, 4, 1), 'aggregation_level': 1 })['records'][2][0], { "redirect": "awc_infrastructure/medicine_kit", "all": 30, "format": "percent_and_div", "color": "red", "percent": -15.151515151515161, "value": 20, "label": "AWCs Reported Medicine Kit", "frequency": "month", "help_text": awcs_reported_medicine_kit_help_text() })
def test_data_AWCs_reported_medicine_kit(self): self.assertDictEqual( get_awc_infrastructure_data( 'icds-cas', { 'month': (2017, 5, 1), 'prev_month': (2017, 4, 1), 'aggregation_level': 1 } )['records'][2][0], { "redirect": "awc_infrastructure/medicine_kit", "all": 30, "format": "percent_and_div", "color": "red", "percent": -15.151515151515161, "value": 20, "label": "AWCs Reported Medicine Kit", "frequency": "month", "help_text": awcs_reported_medicine_kit_help_text() } )
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_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' # } # ] ] }
def get_medicine_kit_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('infra_medicine_kits'), 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 }) loc_children = get_child_locations(domain, location_id, show_test) result_set = set() for row in data: valid = row['all'] name = row['%s_name' % loc_level] result_set.add(name) in_month = row['in_month'] row_values = { 'in_month': in_month or 0, 'all': valid or 0 } for prop, value in six.iteritems(row_values): tooltips_data[name][prop] += value value = (in_month or 0) / float(valid or 1) chart_data['blue'].append([ name, value ]) for sql_location in loc_children: if sql_location.name not in result_set: chart_data['blue'].append([sql_location.name, 0]) chart_data['blue'] = sorted(chart_data['blue']) return { "tooltips_data": dict(tooltips_data), "info": awcs_reported_medicine_kit_help_text(), "chart_data": [ { "values": chart_data['blue'], "key": "", "strokeWidth": 2, "classed": "dashed", "color": MapColors.BLUE }, ] }