Esempio n. 1
0
 def test_sector_data(self):
     self.assertDictEqual(
         get_awcs_covered_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": (
                 "{}<br /><br />"
                 "Number of AWCs launched: 4 <br />"
                 "Number of Supervisors launched: 2".format(awcs_launched_help_text())
             ),
             "tooltips_data": {
                 "s2": {
                     "districts": 1,
                     "states": 1,
                     "supervisors": 1,
                     "blocks": 1,
                     "awcs": 2
                 }, 
                 "s1": {
                     "districts": 1,
                     "states": 1,
                     "supervisors": 1,
                     "blocks": 1,
                     "awcs": 2
                 }
             },
             "chart_data": [
                 {
                     "color": MapColors.BLUE,
                     "classed": "dashed",
                     "strokeWidth": 2,
                     "values": [
                         [
                             "s1",
                             2
                         ],
                         [
                             "s2",
                             2
                         ]
                     ],
                     "key": ""
                 }
             ],
             "format": "number"
         }
     )
Esempio n. 2
0
 def test_sector_data(self):
     self.assertDictEqual(
         get_awcs_covered_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": (
                 "{}<br /><br />"
                 "Number of AWCs launched: 4 <br />"
                 "Number of Supervisors launched: 2".format(awcs_launched_help_text())
             ),
             "tooltips_data": {
                 "s2": {
                     "districts": 1,
                     "states": 1,
                     "supervisors": 1,
                     "blocks": 1,
                     "awcs": 2
                 }, 
                 "s1": {
                     "districts": 1,
                     "states": 1,
                     "supervisors": 1,
                     "blocks": 1,
                     "awcs": 2
                 }
             },
             "chart_data": [
                 {
                     "color": MapColors.BLUE,
                     "classed": "dashed",
                     "strokeWidth": 2,
                     "values": [
                         [
                             "s1",
                             2
                         ],
                         [
                             "s2",
                             2
                         ]
                     ],
                     "key": ""
                 }
             ],
             "format": "number"
         }
     )
Esempio n. 3
0
 def test_map_name_is_different_data(self):
     self.assertDictEqual(
         get_awcs_covered_data_map(
             'icds-cas',
             config={
                 'month': (2017, 5, 1),
                 'state_id': 'st1',
                 'district_id': 'd1',
                 'aggregation_level': 3
             },
             loc_level='block',
         ),
         {
             "rightLegend": {
                 "info": (
                     "{}<br /><br />"
                     'Number of AWCs launched: 9 <br />'
                     'Number of Blocks launched: 2'.format(awcs_launched_help_text())
                 )
             },
             "fills": {
                 "Launched": MapColors.PINK,
                 "Not launched": MapColors.GREY,
                 "defaultFill": MapColors.GREY
             },
             "data": {
                 'block_map': {
                     'states': 1,
                     'blocks': 2,
                     'awcs': 9,
                     'original_name': ['b1', 'b2'],
                     'districts': 1,
                     'supervisors': 4,
                     'fillKey': 'Launched'
                 }
             },
             "slug": "awc_covered",
             "label": ""
         }
     )
Esempio n. 4
0
 def test_map_name_is_different_data(self):
     self.assertDictEqual(
         get_awcs_covered_data_map(
             'icds-cas',
             config={
                 'month': (2017, 5, 1),
                 'state_id': 'st1',
                 'district_id': 'd1',
                 'aggregation_level': 3
             },
             loc_level='block',
         ),
         {
             "rightLegend": {
                 "info": (
                     "{}<br /><br />"
                     'Number of AWCs launched: 9 <br />'
                     'Number of Blocks launched: 2'.format(awcs_launched_help_text())
                 )
             },
             "fills": {
                 "Launched": MapColors.PINK,
                 "Not launched": MapColors.GREY,
                 "defaultFill": MapColors.GREY
             },
             "data": {
                 'block_map': {
                     'states': 1,
                     'blocks': 2,
                     'awcs': 9,
                     'original_name': ['b1', 'b2'],
                     'districts': 1,
                     'supervisors': 4,
                     'fillKey': 'Launched'
                 }
             },
             "slug": "awc_covered",
             "label": ""
         }
     )
Esempio n. 5
0
 def test_map_data(self):
     self.assertDictEqual(
         get_awcs_covered_data_map('icds-cas',
                                   config={
                                       'month': (2017, 5, 1),
                                       'aggregation_level': 1
                                   },
                                   loc_level='state'),
         {
             "rightLegend": {
                 "info": ("{}<br /><br />"
                          'Number of AWCs launched: 21 <br />'
                          'Number of States launched: 3'.format(
                              awcs_launched_help_text()))
             },
             "fills": {
                 "Launched": MapColors.PINK,
                 "Not launched": MapColors.GREY,
                 "defaultFill": MapColors.GREY
             },
             "data": {
                 'st4': {
                     'districts': 0,
                     'blocks': 0,
                     'awcs': 0,
                     'original_name': ['st4'],
                     'states': 0,
                     'supervisors': 0,
                     'fillKey': 'Not launched'
                 },
                 'st5': {
                     'districts': 0,
                     'blocks': 0,
                     'awcs': 0,
                     'original_name': ['st5'],
                     'states': 0,
                     'supervisors': 0,
                     'fillKey': 'Not launched'
                 },
                 'st6': {
                     'districts': 0,
                     'blocks': 0,
                     'awcs': 0,
                     'original_name': ['st6'],
                     'states': 0,
                     'supervisors': 0,
                     'fillKey': 'Not launched'
                 },
                 'st7': {
                     'districts': 1,
                     'blocks': 1,
                     'awcs': 1,
                     'original_name': ['st7'],
                     'states': 1,
                     'supervisors': 1,
                     'fillKey': 'Launched'
                 },
                 'st1': {
                     'districts': 1,
                     'blocks': 2,
                     'awcs': 9,
                     'original_name': ['st1'],
                     'states': 1,
                     'supervisors': 4,
                     'fillKey': 'Launched'
                 },
                 'st2': {
                     'districts': 2,
                     'blocks': 2,
                     'awcs': 11,
                     'original_name': ['st2'],
                     'states': 1,
                     'supervisors': 4,
                     'fillKey': 'Launched'
                 },
                 'st3': {
                     'districts': 0,
                     'blocks': 0,
                     'awcs': 0,
                     'original_name': ['st3'],
                     'states': 0,
                     'supervisors': 0,
                     'fillKey': 'Not launched'
                 }
             },
             "slug": "awc_covered",
             "label": ""
         })
Esempio n. 6
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}
Esempio n. 7
0
def get_awcs_covered_data_map(domain, config, loc_level, show_test=False):
    level = config['aggregation_level']

    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(
                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'),
            ).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 = defaultdict(
        lambda: {
            'awcs': [],
            'supervisors': [],
            'blocks': [],
            'districts': [],
            'states': [],
            'original_name': []
        })

    for row in get_data_for(config):
        name = row['%s_name' % loc_level]
        on_map_name = row['%s_map_location_name' % loc_level] or name
        awcs = row['awcs'] or 0
        supervisors = row['supervisors'] or 0
        blocks = row['blocks'] or 0
        districts = row['districts'] or 0
        states = row['states'] or 0

        data_for_map[on_map_name]['awcs'].append(awcs)
        data_for_map[on_map_name]['supervisors'].append(supervisors)
        data_for_map[on_map_name]['blocks'].append(blocks)
        data_for_map[on_map_name]['districts'].append(districts)
        data_for_map[on_map_name]['states'].append(states)
        data_for_map[on_map_name]['original_name'].append(name)

    for data_for_location in data_for_map.values():
        data_for_location['awcs'] = (sum(data_for_location['awcs'])
                                     if level <= 5 else max(
                                         data_for_location['awcs']))
        data_for_location['supervisors'] = (sum(
            data_for_location['supervisors']) if level <= 4 else max(
                data_for_location['supervisors']))
        data_for_location['blocks'] = (sum(data_for_location['blocks'])
                                       if level <= 3 else max(
                                           data_for_location['blocks']))
        data_for_location['districts'] = (sum(data_for_location['districts'])
                                          if level <= 2 else max(
                                              data_for_location['districts']))
        data_for_location['states'] = (sum(data_for_location['states'])
                                       if level <= 1 else max(
                                           data_for_location['states']))
        data_for_location.update({
            'fillKey':
            'Launched' if data_for_location['awcs'] > 0 else 'Not launched'
        })

    if level == 1:
        prop = 'states'
    elif level == 2:
        prop = 'districts'
    elif level == 3:
        prop = 'blocks'
    elif level == 4:
        prop = 'supervisors'
    else:
        prop = 'awcs'

    total_awcs = sum([(x['awcs'] or 0) for x in data_for_map.values()])
    total = sum([(x[prop] or 0) for x in data_for_map.values()])

    fills = OrderedDict()
    fills.update({'Launched': MapColors.PINK})
    fills.update({'Not launched': MapColors.GREY})
    fills.update({'defaultFill': MapColors.GREY})

    info = _("{:s}<br /><br />"
             "Number of AWCs launched: {:s}".format(
                 awcs_launched_help_text(),
                 indian_formatted_number(total_awcs)))
    if level != 5:
        info = _("{:s}<br /><br />"
                 "Number of AWCs launched: {:s} <br />"
                 "Number of {:s} launched: {:s}".format(
                     awcs_launched_help_text(),
                     indian_formatted_number(total_awcs), prop.title(),
                     indian_formatted_number(total)))

    return {
        "slug": "awc_covered",
        "label": "",
        "fills": fills,
        "rightLegend": {
            "info": info
        },
        "data": dict(data_for_map),
    }
Esempio n. 8
0
def get_awcs_covered_sector_data(domain,
                                 config,
                                 loc_level,
                                 location_id,
                                 show_test=False):
    group_by = ['%s_name' % loc_level]

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

    level = config['aggregation_level']
    data = AggAwcMonthly.objects.filter(**config).values(*group_by).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'),
    ).order_by('%s_name' % loc_level)

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

    chart_data = {
        'blue': [],
    }

    tooltips_data = defaultdict(lambda: {
        'districts': 0,
        'blocks': 0,
        'states': 0,
        'supervisors': 0,
        'awcs': 0
    })

    for row in data:
        name = row['%s_name' % loc_level]
        awcs = row['awcs'] or 0
        supervisors = row['supervisors'] or 0
        blocks = row['blocks'] or 0
        districts = row['districts'] or 0
        states = row['states'] or 0

        row_values = {
            'awcs': awcs,
            'supervisors': supervisors,
            'blocks': blocks,
            'districts': districts,
            'states': states,
        }
        for prop, value in row_values.items():
            tooltips_data[name][prop] += (value or 0)

    for name, value_dict in tooltips_data.items():
        chart_data['blue'].append([name, value_dict['awcs']])

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

    if level == 1:
        prop = 'states'
    elif level == 2:
        prop = 'districts'
    elif level == 3:
        prop = 'blocks'
    elif level == 4:
        prop = 'supervisors'
    else:
        prop = 'awcs'

    total_awcs = sum([(x['awcs'] or 0) for x in tooltips_data.values()])
    total = sum([(x[prop] or 0) for x in tooltips_data.values()])

    info = _("{:s}<br /><br />"
             "Number of AWCs launched: {:d}".format(awcs_launched_help_text(),
                                                    total_awcs))
    if level != 5:
        info = _("{:s}<br /><br />"
                 "Number of AWCs launched: {:d} <br />"
                 "Number of {:s} launched: {:d}".format(
                     awcs_launched_help_text(), total_awcs, prop.title(),
                     total))

    return {
        "tooltips_data":
        dict(tooltips_data),
        "format":
        "number",
        "info":
        info,
        "chart_data": [{
            "values": chart_data['blue'],
            "key": "",
            "strokeWidth": 2,
            "classed": "dashed",
            "color": MapColors.BLUE
        }]
    }
Esempio n. 9
0
 def test_data_daily(self):
     self.assertDictEqual(
         get_cas_reach_data(
             'icds-cas',
             (2017, 5, 29),
             {
                 'month': (2017, 5, 1),
                 'prev_month': (2017, 4, 1),
                 'aggregation_level': 1
             },
             show_test=False,
         ),
         {
             "records": [
                 [
                     {
                         'redirect': 'icds_cas_reach/awcs_covered',
                         'all': None,
                         'color': None,
                         'format': 'number',
                         'percent': None,
                         'value': 21,
                         'label': 'AWCs Launched',
                         'frequency': 'month',
                         'help_text': awcs_launched_help_text()
                     },
                     {
                         'redirect': 'icds_cas_reach/awc_daily_status',
                         'all': 21,
                         'format': 'div',
                         'color': 'green',
                         'percent': 'Data in the previous reporting period was 0',
                         'value': 0,
                         'label': 'Number of AWCs Open yesterday',
                         'frequency': 'day',
                         'help_text': 'Total Number of Angwanwadi Centers that '
                                      'were open yesterday by the AWW or the AWW helper'
                     }
                 ],
                 [
                     {
                         'all': None,
                         'format': 'number',
                         'percent': None,
                         'value': 9,
                         'label': 'Sectors covered',
                         'frequency': 'month',
                         'help_text': 'Total Sectors that have launched ICDS CAS'
                     },
                     {
                         'all': None,
                         'format': 'number',
                         'percent': None,
                         'value': 5,
                         'label': 'Blocks covered',
                         'frequency': 'month',
                         'help_text': 'Total Blocks that have launched ICDS CAS'
                     }
                 ],
                 [
                     {
                         'all': None,
                         'format': 'number',
                         'percent': None,
                         'value': 4,
                         'label': 'Districts covered',
                         'frequency': 'month',
                         'help_text': 'Total Districts that have launched ICDS CAS'
                     },
                     {
                         'all': None,
                         'format': 'number',
                         'percent': None,
                         'value': 3,
                         'label': 'States/UTs covered',
                         'frequency': 'month',
                         'help_text': 'Total States that have launched ICDS CAS'
                     }
                 ]
             ]
         }
     )
Esempio n. 10
0
 def test_data(self):
     self.assertDictEqual(
         get_cas_reach_data(
             'icds-cas',
             (2017, 6, 1),
             {
                 'month': (2017, 5, 1),
                 'prev_month': (2017, 4, 1),
                 'aggregation_level': 1
             },
             show_test=False,
         ),
         {
             "records": [
                 [
                     {
                         'redirect': 'icds_cas_reach/awcs_covered',
                         'all': None,
                         'color': None,
                         'format': 'number',
                         'percent': None,
                         'value': 21,
                         'label': 'AWCs Launched',
                         'frequency': 'month',
                         'help_text': awcs_launched_help_text()
                     },
                     {
                         'all': 21,
                         'format': 'div',
                         'color': 'green',
                         'percent': 127.27272727272728,
                         'value': 50,
                         'label': 'Number of AWCs open for at least one day in month',
                         'frequency': 'month',
                         'help_text': 'Total Number of AWCs open for at least one day in month'
                     }
                 ],
                 [
                     {
                         'all': None,
                         'format': 'number',
                         'percent': None,
                         'value': 9,
                         'label': 'Sectors covered',
                         'frequency': 'month',
                         'help_text': 'Total Sectors that have launched ICDS CAS'
                     },
                     {
                         'all': None,
                         'format': 'number',
                         'percent': None,
                         'value': 5,
                         'label': 'Blocks covered',
                         'frequency': 'month',
                         'help_text': 'Total Blocks that have launched ICDS CAS'
                     }
                 ],
                 [
                     {
                         'all': None,
                         'format': 'number',
                         'percent': None,
                         'value': 4,
                         'label': 'Districts covered',
                         'frequency': 'month',
                         'help_text': 'Total Districts that have launched ICDS CAS'
                     },
                     {
                         'all': None,
                         'format': 'number',
                         'percent': None,
                         'value': 3,
                         'label': 'States/UTs covered',
                         'frequency': 'month',
                         'help_text': 'Total States that have launched ICDS CAS'
                     }
                 ]
             ]
         }
     )
Esempio n. 11
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',
                }
            ]
        ]
    }
Esempio n. 12
0
 def test_map_data(self):
     self.assertDictEqual(
         get_awcs_covered_data_map(
             'icds-cas',
             config={
                 'month': (2017, 5, 1),
                 'aggregation_level': 1
             },
             loc_level='state'
         ),
         {
             "rightLegend": {
                 "info": (
                     "{}<br /><br />"
                     'Number of AWCs launched: 21 <br />'
                     'Number of States launched: 3'.format(awcs_launched_help_text())
                 )
             },
             "fills": {
                 "Launched": MapColors.PINK,
                 "Not launched": MapColors.GREY,
                 "defaultFill": MapColors.GREY
             },
             "data": {
                 'st4': {
                     'districts': 0,
                     'blocks': 0,
                     'awcs': 0,
                     'original_name': ['st4'],
                     'states': 0,
                     'supervisors': 0,
                     'fillKey': 'Not launched'},
                 'st5': {
                     'districts': 0,
                     'blocks': 0,
                     'awcs': 0,
                     'original_name': ['st5'],
                     'states': 0,
                     'supervisors': 0,
                     'fillKey': 'Not launched'
                 },
                 'st6': {
                     'districts': 0,
                     'blocks': 0,
                     'awcs': 0,
                     'original_name': ['st6'],
                     'states': 0,
                     'supervisors': 0,
                     'fillKey': 'Not launched'
                 },
                 'st7': {
                     'districts': 1,
                     'blocks': 1,
                     'awcs': 1,
                     'original_name': ['st7'],
                     'states': 1,
                     'supervisors': 1,
                     'fillKey': 'Launched'
                 },
                 'st1': {
                     'districts': 1,
                     'blocks': 2,
                     'awcs': 9,
                     'original_name': ['st1'],
                     'states': 1,
                     'supervisors': 4,
                     'fillKey': 'Launched'
                 },
                 'st2': {
                     'districts': 2,
                     'blocks': 2,
                     'awcs': 11,
                     'original_name': ['st2'],
                     'states': 1,
                     'supervisors': 4,
                     'fillKey': 'Launched'
                 },
                 'st3': {
                     'districts': 0,
                     'blocks': 0,
                     'awcs': 0,
                     'original_name': ['st3'],
                     'states': 0,
                     'supervisors': 0,
                     'fillKey': 'Not launched'
                 }
             },
             "slug": "awc_covered",
             "label": ""
         }
     )
Esempio n. 13
0
def get_awcs_covered_data_map(domain, config, loc_level, show_test=False):
    level = config['aggregation_level']

    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(
            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'),
        ).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 = defaultdict(lambda: {
        'awcs': [],
        'supervisors': [],
        'blocks': [],
        'districts': [],
        'states': [],
        'original_name': []
    })

    for row in get_data_for(config):
        name = row['%s_name' % loc_level]
        on_map_name = row['%s_map_location_name' % loc_level] or name
        awcs = row['awcs'] or 0
        supervisors = row['supervisors'] or 0
        blocks = row['blocks'] or 0
        districts = row['districts'] or 0
        states = row['states'] or 0

        data_for_map[on_map_name]['awcs'].append(awcs)
        data_for_map[on_map_name]['supervisors'].append(supervisors)
        data_for_map[on_map_name]['blocks'].append(blocks)
        data_for_map[on_map_name]['districts'].append(districts)
        data_for_map[on_map_name]['states'].append(states)
        data_for_map[on_map_name]['original_name'].append(name)

    for data_for_location in six.itervalues(data_for_map):
        data_for_location['awcs'] = (
            sum(data_for_location['awcs']) if level <= 5 else max(data_for_location['awcs'])
        )
        data_for_location['supervisors'] = (
            sum(data_for_location['supervisors']) if level <= 4 else max(data_for_location['supervisors'])
        )
        data_for_location['blocks'] = (
            sum(data_for_location['blocks']) if level <= 3 else max(data_for_location['blocks'])
        )
        data_for_location['districts'] = (
            sum(data_for_location['districts']) if level <= 2 else max(data_for_location['districts'])
        )
        data_for_location['states'] = (
            sum(data_for_location['states']) if level <= 1 else max(data_for_location['states'])
        )
        data_for_location.update({'fillKey': 'Launched' if data_for_location['awcs'] > 0 else 'Not launched'})

    if level == 1:
        prop = 'states'
    elif level == 2:
        prop = 'districts'
    elif level == 3:
        prop = 'blocks'
    elif level == 4:
        prop = 'supervisors'
    else:
        prop = 'awcs'

    total_awcs = sum([(x['awcs'] or 0) for x in six.itervalues(data_for_map)])
    total = sum([(x[prop] or 0) for x in six.itervalues(data_for_map)])

    fills = OrderedDict()
    fills.update({'Launched': MapColors.PINK})
    fills.update({'Not launched': MapColors.GREY})
    fills.update({'defaultFill': MapColors.GREY})

    info = _(
        "{:s}<br /><br />"
        "Number of AWCs launched: {:s}".format(
            awcs_launched_help_text(),
            indian_formatted_number(total_awcs)
        )
    )
    if level != 5:
        info = _(
            "{:s}<br /><br />"
            "Number of AWCs launched: {:s} <br />"
            "Number of {:s} launched: {:s}".format(
                awcs_launched_help_text(),
                indian_formatted_number(total_awcs),
                prop.title(),
                indian_formatted_number(total)
            )
        )

    return {
        "slug": "awc_covered",
        "label": "",
        "fills": fills,
        "rightLegend": {
            "info": info
        },
        "data": dict(data_for_map),
    }
Esempio n. 14
0
def get_awcs_covered_sector_data(domain, config, loc_level, location_id, show_test=False):
    group_by = ['%s_name' % loc_level]

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

    level = config['aggregation_level']
    data = AggAwcMonthly.objects.filter(
        **config
    ).values(
        *group_by
    ).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'),
    ).order_by('%s_name' % loc_level)

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

    chart_data = {
        'blue': [],
    }

    tooltips_data = defaultdict(lambda: {
        'districts': 0,
        'blocks': 0,
        'states': 0,
        'supervisors': 0,
        'awcs': 0
    })

    loc_children = get_child_locations(domain, location_id, show_test)
    result_set = set()

    for row in data:
        name = row['%s_name' % loc_level]
        awcs = row['awcs'] or 0
        supervisors = row['supervisors'] or 0
        blocks = row['blocks'] or 0
        districts = row['districts'] or 0
        states = row['states'] or 0
        result_set.add(name)

        row_values = {
            'awcs': awcs,
            'supervisors': supervisors,
            'blocks': blocks,
            'districts': districts,
            'states': states,
        }
        for prop, value in six.iteritems(row_values):
            tooltips_data[name][prop] += (value or 0)

    for name, value_dict in six.iteritems(tooltips_data):
        chart_data['blue'].append([name, value_dict['awcs']])

    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'])

    if level == 1:
        prop = 'states'
    elif level == 2:
        prop = 'districts'
    elif level == 3:
        prop = 'blocks'
    elif level == 4:
        prop = 'supervisors'
    else:
        prop = 'awcs'

    total_awcs = sum([(x['awcs'] or 0) for x in six.itervalues(tooltips_data)])
    total = sum([(x[prop] or 0) for x in six.itervalues(tooltips_data)])

    info = _(
        "{:s}<br /><br />"
        "Number of AWCs launched: {:d}".format(awcs_launched_help_text(), total_awcs)
    )
    if level != 5:
        info = _(
            "{:s}<br /><br />"
            "Number of AWCs launched: {:d} <br />"
            "Number of {:s} launched: {:d}".format(awcs_launched_help_text(), total_awcs, prop.title(), total)
        )

    return {
        "tooltips_data": dict(tooltips_data),
        "format": "number",
        "info": info,
        "chart_data": [
            {
                "values": chart_data['blue'],
                "key": "",
                "strokeWidth": 2,
                "classed": "dashed",
                "color": MapColors.BLUE
            }
        ]
    }
Esempio n. 15
0
 def test_data_if_aggregation_script_fail(self):
     self.assertDictEqual(
         get_cas_reach_data(
             'icds-cas',
             (2017, 5, 30),
             {
                 'month': (2017, 5, 1),
                 'prev_month': (2017, 4, 1),
                 'aggregation_level': 1
             },
             show_test=False,
         ), {
             "records":
             [[{
                 'redirect': 'icds_cas_reach/awcs_covered',
                 'all': None,
                 'color': None,
                 'format': 'number',
                 'percent': None,
                 'value': 22,
                 'label': 'AWCs Launched',
                 'frequency': 'month',
                 'help_text': awcs_launched_help_text()
             }, {
                 'redirect':
                 'icds_cas_reach/awc_daily_status',
                 'all':
                 22,
                 'format':
                 'number_and_percent',
                 'color':
                 'red',
                 'percent':
                 -97.05882352941177,
                 'value':
                 1,
                 'label':
                 'Number of AWCs Open yesterday',
                 'frequency':
                 'day',
                 'help_text':
                 'Total Number of Angwanwadi Centers that '
                 'were open yesterday by the AWW or the AWW helper'
             }],
              [{
                  'all': None,
                  'color': None,
                  'format': 'number',
                  'frequency': 'month',
                  'help_text': ls_launched_help_text(),
                  'label': 'LSs Launched',
                  'percent': None,
                  'redirect': 'icds_cas_reach/ls_launched',
                  'value': 6
              }, {
                  'all': None,
                  'format': 'number',
                  'percent': None,
                  'value': 9,
                  'label': 'Sectors covered',
                  'frequency': 'month',
                  'help_text': 'Total Sectors that have launched ICDS CAS'
              }],
              [{
                  'all': None,
                  'format': 'number',
                  'percent': None,
                  'value': 5,
                  'label': 'Blocks covered',
                  'frequency': 'month',
                  'help_text': 'Total Blocks that have launched ICDS CAS'
              }, {
                  'all': None,
                  'format': 'number',
                  'percent': None,
                  'value': 4,
                  'label': 'Districts covered',
                  'frequency': 'month',
                  'help_text': 'Total Districts that have launched ICDS CAS'
              }],
              [{
                  'all': None,
                  'format': 'number',
                  'percent': None,
                  'value': 3,
                  'label': 'States/UTs covered',
                  'frequency': 'month',
                  'help_text': 'Total States that have launched ICDS CAS'
              }]]
         })
Esempio n. 16
0
 def test_data_for_ff_user(self):
     self.assertDictEqual(
         get_cas_reach_data(
             'icds-cas',
             (2020, 4, 1),
             {
                 'month': (2020, 2, 1),
                 'prev_month': (2020, 1, 1),
                 'aggregation_level': 1
             },
             show_test=False,
             show_prerelease_features=True,
         ), {
             "records":
             [[{
                 'redirect': 'icds_cas_reach/awcs_covered',
                 'all': None,
                 'color': None,
                 'format': 'number',
                 'percent': None,
                 'value': 0,
                 'label': 'AWCs Launched',
                 'frequency': 'month',
                 'help_text': awcs_launched_help_text()
             }, {
                 'all':
                 0,
                 'format':
                 'number_and_percent',
                 'color':
                 'green',
                 'percent':
                 'Data in the previous reporting period was 0',
                 'value':
                 0,
                 'label':
                 'Number of AWCs open for at least one day in month',
                 'frequency':
                 'month',
                 'help_text':
                 'Total Number of AWCs open for at least one day in month'
             }],
              [{
                  'all': None,
                  'color': None,
                  'format': 'number',
                  'frequency': 'month',
                  'help_text': ls_launched_help_text(),
                  'label': 'LSs Launched',
                  'percent': None,
                  'redirect': 'icds_cas_reach/ls_launched',
                  'value': 0
              }, {
                  'all': None,
                  'format': 'number',
                  'percent': None,
                  'value': 0,
                  'label': 'Sectors covered',
                  'frequency': 'month',
                  'help_text': 'Total Sectors that have launched ICDS CAS'
              }],
              [{
                  'all': None,
                  'format': 'number',
                  'percent': None,
                  'value': 0,
                  'label': 'Blocks covered',
                  'frequency': 'month',
                  'help_text': 'Total Blocks that have launched ICDS CAS'
              }, {
                  'all': None,
                  'format': 'number',
                  'percent': None,
                  'value': 0,
                  'label': 'Districts covered',
                  'frequency': 'month',
                  'help_text': 'Total Districts that have launched ICDS CAS'
              }],
              [{
                  'all': None,
                  'format': 'number',
                  'percent': None,
                  'value': 0,
                  'label': 'States/UTs covered',
                  'frequency': 'month',
                  'help_text': 'Total States that have launched ICDS CAS'
              }]]
         })