def get_adhaar_sector_data(domain, config, loc_level, location_id, show_test=False, beta=False): group_by = ['%s_name' % loc_level] config['month'] = datetime(*config['month']) data = AggAwcMonthly.objects.filter(**config).values(*group_by).annotate( in_month=Sum(person_has_aadhaar_column(beta)), all=Sum(person_is_beneficiary_column(beta)), ).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": percent_aadhaar_seeded_beneficiaries_help_text(), "chart_data": [ { "values": chart_data['blue'], "key": "", "strokeWidth": 2, "classed": "dashed", "color": MapColors.BLUE }, ] }
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(person_has_aadhaar_column(beta)), all=Sum(person_is_beneficiary_column(beta)), ).order_by('%s_name' % loc_level, '%s_map_location_name' % loc_level) if not show_test: queryset = apply_exclude(domain, queryset) return queryset
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(person_has_aadhaar_column(beta)), all=Sum(person_is_beneficiary_column(beta)), ).order_by('%s_name' % loc_level, '%s_map_location_name' % loc_level) if not show_test: queryset = apply_exclude(domain, queryset) return queryset
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
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
def person_is_beneficiary_column(self): return person_is_beneficiary_column(self.beta)
def get_adhaar_data_chart(domain, config, loc_level, show_test=False, beta=False): month = datetime(*config['month']) three_before = datetime(*config['month']) - relativedelta(months=3) config['month__range'] = (three_before, month) del config['month'] chart_data = AggAwcMonthly.objects.filter( **config ).values( 'month', '%s_name' % loc_level ).annotate( in_month=Sum(person_has_aadhaar_column(beta)), all=Sum(person_is_beneficiary_column(beta)), ).order_by('month') if not show_test: chart_data = apply_exclude(domain, chart_data) data = { 'blue': OrderedDict(), } dates = [dt for dt in rrule(MONTHLY, dtstart=three_before, until=month)] for date in dates: miliseconds = int(date.strftime("%s")) * 1000 data['blue'][miliseconds] = {'y': 0, 'all': 0} best_worst = defaultdict(lambda: { 'in_month': 0, 'all': 0 }) for row in chart_data: date = row['month'] in_month = row['in_month'] location = row['%s_name' % loc_level] valid = row['all'] best_worst[location]['in_month'] = in_month best_worst[location]['all'] = (valid or 0) date_in_miliseconds = int(date.strftime("%s")) * 1000 data['blue'][date_in_miliseconds]['y'] += in_month data['blue'][date_in_miliseconds]['all'] += valid all_locations = [ { 'loc_name': key, 'percent': (value['in_month'] * 100) / float(value['all'] or 1), } for key, value in six.iteritems(best_worst) ] all_locations_sorted_by_name = sorted(all_locations, key=lambda x: x['loc_name']) all_locations_sorted_by_percent_and_name = sorted( all_locations_sorted_by_name, key=lambda x: x['percent'], reverse=True) return { "chart_data": [ { "values": [ { 'x': key, 'y': value['y'] / float(value['all'] or 1), 'all': value['all'] } for key, value in six.iteritems(data['blue']) ], "key": "Percentage of beneficiaries with Aadhaar numbers", "strokeWidth": 2, "classed": "dashed", "color": ChartColors.BLUE } ], "all_locations": all_locations_sorted_by_percent_and_name, "top_five": all_locations_sorted_by_percent_and_name[:5], "bottom_five": all_locations_sorted_by_percent_and_name[-5:], "location_type": loc_level.title() if loc_level != LocationTypes.SUPERVISOR else 'Sector' }
def get_adhaar_sector_data(domain, config, loc_level, location_id, show_test=False, beta=False): group_by = ['%s_name' % loc_level] config['month'] = datetime(*config['month']) data = AggAwcMonthly.objects.filter(**config).values(*group_by).annotate( in_month=Sum(person_has_aadhaar_column(beta)), all=Sum(person_is_beneficiary_column(beta)), ).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": _(("Of the total number of ICDS beneficiaries, the percentage whose Adhaar " "identification has been captured. ")), "chart_data": [ { "values": chart_data['blue'], "key": "", "strokeWidth": 2, "classed": "dashed", "color": MapColors.BLUE }, ] }
def get_adhaar_sector_data(domain, config, loc_level, location_id, show_test=False, beta=False): group_by = ['%s_name' % loc_level] config['month'] = datetime(*config['month']) data = AggAwcMonthly.objects.filter( **config ).values( *group_by ).annotate( in_month=Sum(person_has_aadhaar_column(beta)), all=Sum(person_is_beneficiary_column(beta)), ).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": percent_aadhaar_seeded_beneficiaries_help_text(), "chart_data": [ { "values": chart_data['blue'], "key": "", "strokeWidth": 2, "classed": "dashed", "color": MapColors.BLUE }, ] }
def get_adhaar_data_chart(domain, config, loc_level, show_test=False, beta=False): month = datetime(*config['month']) three_before = datetime(*config['month']) - relativedelta(months=3) config['month__range'] = (three_before, month) del config['month'] chart_data = AggAwcMonthly.objects.filter( **config ).values( 'month', '%s_name' % loc_level ).annotate( in_month=Sum(person_has_aadhaar_column(beta)), all=Sum(person_is_beneficiary_column(beta)), ).order_by('month') if not show_test: chart_data = apply_exclude(domain, chart_data) data = { 'blue': OrderedDict(), } dates = [dt for dt in rrule(MONTHLY, dtstart=three_before, until=month)] for date in dates: miliseconds = int(date.strftime("%s")) * 1000 data['blue'][miliseconds] = {'y': 0, 'all': 0} best_worst = defaultdict(lambda: { 'in_month': 0, 'all': 0 }) for row in chart_data: date = row['month'] in_month = row['in_month'] location = row['%s_name' % loc_level] valid = row['all'] best_worst[location]['in_month'] = in_month best_worst[location]['all'] = (valid or 0) date_in_miliseconds = int(date.strftime("%s")) * 1000 data['blue'][date_in_miliseconds]['y'] += in_month data['blue'][date_in_miliseconds]['all'] += valid top_locations = sorted( [ dict( loc_name=key, percent=(value['in_month'] * 100) / float(value['all'] or 1) ) for key, value in six.iteritems(best_worst) ], key=lambda x: x['percent'], reverse=True ) return { "chart_data": [ { "values": [ { 'x': key, 'y': value['y'] / float(value['all'] or 1), 'all': value['all'] } for key, value in six.iteritems(data['blue']) ], "key": "Percentage of beneficiaries with Aadhaar numbers", "strokeWidth": 2, "classed": "dashed", "color": ChartColors.BLUE } ], "all_locations": top_locations, "top_five": top_locations[:5], "bottom_five": top_locations[-5:], "location_type": loc_level.title() if loc_level != LocationTypes.SUPERVISOR else 'Sector' }