<commit_before>from datetime import datetime

from dateutil.relativedelta import relativedelta
from django.db.models.aggregates import Sum
from django.utils.translation import ugettext as _

from custom.icds_reports.const import AADHAR_SEEDED_BENEFICIARIES, CHILDREN_ENROLLED_FOR_ANGANWADI_SERVICES, \
    PREGNANT_WOMEN_ENROLLED_FOR_ANGANWADI_SERVICES, LACTATING_WOMEN_ENROLLED_FOR_ANGANWADI_SERVICES, \
    ADOLESCENT_GIRLS_ENROLLED_FOR_ANGANWADI_SERVICES, OUT_OF_SCHOOL_ADOLESCENT_GIRLS_11_14_YEARS
from custom.icds_reports.messages import percent_aadhaar_seeded_beneficiaries_help_text, \
    percent_children_enrolled_help_text, percent_pregnant_women_enrolled_help_text, \
    percent_lactating_women_enrolled_help_text, percent_adolescent_girls_enrolled_help_text, \
    percent_adolescent_girls_enrolled_help_text_v2
from custom.icds_reports.models import AggAwcDailyView, AggAwcMonthly
from custom.icds_reports.utils import (
    percent_increase, percent_diff, get_value, apply_exclude,
    person_has_aadhaar_column, person_is_beneficiary_column,
    get_color_with_green_positive, get_color_with_red_positive
)


def get_adolescent_girls_data(domain, filters, show_test):
    queryset = AggAwcMonthly.objects.filter(
        **filters
    ).values(
        'aggregation_level'
    ).annotate(
        person_adolescent=Sum('cases_person_adolescent_girls_11_14_out_of_school'),
        person_adolescent_all=Sum('cases_person_adolescent_girls_11_14_all_v2')
    )
    if not show_test:
        queryset = apply_exclude(domain, queryset)

    return queryset


def get_demographics_data(domain, now_date, config, show_test=False, beta=False):
    now_date = datetime(*now_date)
    current_month = datetime(*config['month'])
    previous_month = datetime(*config['prev_month'])
    del config['month']
    del config['prev_month']

    def get_data_for(query_class, filters):
        queryset = query_class.objects.filter(
            **filters
        ).values(
            'aggregation_level'
        ).annotate(
            household=Sum('cases_household'),
            child_health=Sum('cases_child_health'),
            child_health_all=Sum('cases_child_health_all'),
            ccs_pregnant=Sum('cases_ccs_pregnant'),
            ccs_pregnant_all=Sum('cases_ccs_pregnant_all'),
            css_lactating=Sum('cases_ccs_lactating'),
            css_lactating_all=Sum('cases_ccs_lactating_all'),
            person_adolescent=Sum('cases_person_adolescent_girls_11_14'),
            person_adolescent_all=Sum('cases_person_adolescent_girls_11_14_all'),
            person_aadhaar=Sum(person_has_aadhaar_column(beta)),
            all_persons=Sum(person_is_beneficiary_column(beta))
        )

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

    if beta:
        config['month'] = current_month
        data = get_data_for(AggAwcMonthly, config)
        config['month'] = previous_month
        prev_data = get_data_for(AggAwcMonthly, config)
        frequency = 'month'
    else:
        if current_month.month == now_date.month and current_month.year == now_date.year:
            config['date'] = now_date.date()
            data = None
            # keep the record in searched - current - month
            while data is None or (not data and config['date'].day != 1):
                config['date'] -= relativedelta(days=1)
                data = get_data_for(AggAwcDailyView, config)
            prev_data = None
            while prev_data is None or (not prev_data and config['date'].day != 1):
                config['date'] -= relativedelta(days=1)
                prev_data = get_data_for(AggAwcDailyView, config)
            frequency = 'day'
        else:
            config['month'] = current_month
            data = get_data_for(AggAwcMonthly, config)
            config['month'] = previous_month
            prev_data = get_data_for(AggAwcMonthly, config)
            frequency = 'month'

    if 'date' in config:
        del config['date']
    config['month'] = current_month
    ag_data = get_adolescent_girls_data(domain, config, show_test)
    config['month'] = previous_month
    ag_data_prev_data = get_adolescent_girls_data(domain, config, show_test)

    demographics_data = {
        'records': [
            [
                {
                    'label': _('Registered Households'),
                    'help_text': _('Total number of households registered'),
                    'percent': percent_increase('household', data, prev_data),
                    'color': get_color_with_green_positive(percent_increase(
                        'household',
                        data,
                        prev_data)),
                    'value': get_value(data, 'household'),
                    'all': None,
                    'format': 'number',
                    'frequency': frequency,
                    'redirect': 'demographics/registered_household'
                },
                {
                    'label': _(AADHAR_SEEDED_BENEFICIARIES),
                    'help_text': percent_aadhaar_seeded_beneficiaries_help_text(),
                    'percent': percent_diff(
                        'person_aadhaar',<commit_msg>removed feature flag<commit_after>from datetime import datetime

from dateutil.relativedelta import relativedelta
from django.db.models.aggregates import Sum
from django.utils.translation import ugettext as _

from custom.icds_reports.const import AADHAR_SEEDED_BENEFICIARIES, CHILDREN_ENROLLED_FOR_ANGANWADI_SERVICES, \
    PREGNANT_WOMEN_ENROLLED_FOR_ANGANWADI_SERVICES, LACTATING_WOMEN_ENROLLED_FOR_ANGANWADI_SERVICES, \
    ADOLESCENT_GIRLS_ENROLLED_FOR_ANGANWADI_SERVICES, OUT_OF_SCHOOL_ADOLESCENT_GIRLS_11_14_YEARS
from custom.icds_reports.messages import percent_aadhaar_seeded_beneficiaries_help_text, \
    percent_children_enrolled_help_text, percent_pregnant_women_enrolled_help_text, \
    percent_lactating_women_enrolled_help_text, percent_adolescent_girls_enrolled_help_text, \
    percent_adolescent_girls_enrolled_help_text_v2
from custom.icds_reports.models import AggAwcDailyView, AggAwcMonthly
from custom.icds_reports.utils import (
    percent_increase, percent_diff, get_value, apply_exclude,
    person_has_aadhaar_column, person_is_beneficiary_column,
    get_color_with_green_positive, get_color_with_red_positive
)


def get_adolescent_girls_data(domain, filters, show_test):
    queryset = AggAwcMonthly.objects.filter(
        **filters
    ).values(
        'aggregation_level'
    ).annotate(
        person_adolescent=Sum('cases_person_adolescent_girls_11_14_out_of_school'),
        person_adolescent_all=Sum('cases_person_adolescent_girls_11_14_all_v2')
    )
    if not show_test:
        queryset = apply_exclude(domain, queryset)

    return queryset


def get_demographics_data(domain, now_date, config, show_test=False, beta=False):
    now_date = datetime(*now_date)
    current_month = datetime(*config['month'])
    previous_month = datetime(*config['prev_month'])
    del config['month']
    del config['prev_month']

    def get_data_for(query_class, filters):
        queryset = query_class.objects.filter(
            **filters
        ).values(
            'aggregation_level'
        ).annotate(
            household=Sum('cases_household'),
            child_health=Sum('cases_child_health'),
            child_health_all=Sum('cases_child_health_all'),
            ccs_pregnant=Sum('cases_ccs_pregnant'),
            ccs_pregnant_all=Sum('cases_ccs_pregnant_all'),
            css_lactating=Sum('cases_ccs_lactating'),
            css_lactating_all=Sum('cases_ccs_lactating_all'),
            person_adolescent=Sum('cases_person_adolescent_girls_11_14'),
            person_adolescent_all=Sum('cases_person_adolescent_girls_11_14_all'),
            person_aadhaar=Sum(person_has_aadhaar_column(beta)),
            all_persons=Sum(person_is_beneficiary_column(beta))
        )

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

    config['month'] = current_month
    data = get_data_for(AggAwcMonthly, config)
    config['month'] = previous_month
    prev_data = get_data_for(AggAwcMonthly, config)
    frequency = 'month'

    if 'date' in config:
        del config['date']
    config['month'] = current_month
    ag_data = get_adolescent_girls_data(domain, config, show_test)
    config['month'] = previous_month
    ag_data_prev_data = get_adolescent_girls_data(domain, config, show_test)

    demographics_data = {
        'records': [
            [
                {
                    'label': _('Registered Households'),
                    'help_text': _('Total number of households registered'),
                    'percent': percent_increase('household', data, prev_data),
                    'color': get_color_with_green_positive(percent_increase(
                        'household',
                        data,
                        prev_data)),
                    'value': get_value(data, 'household'),
                    'all': None,
                    'format': 'number',
                    'frequency': frequency,
                    'redirect': 'demographics/registered_household'
                },
                {
                    'label': _(AADHAR_SEEDED_BENEFICIARIES),
                    'help_text': percent_aadhaar_seeded_beneficiaries_help_text(),
                    'percent': percent_diff(
                        'person_aadhaar',
{'fraction_non_alphanumeric': 0.0832371155662853, 'fraction_numerical': 0.0050383121654245825, 'mean_word_length': 2.8937474458520636, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3624354', 'n_tokens_mistral': 3100, 'n_tokens_neox': 2942, 'n_words': 434}