<commit_before>from __future__ import absolute_import

from __future__ import unicode_literals

from memoized import memoized

from custom.icds_reports.models import AggAwcMonthly, ChildHealthMonthlyView, CcsRecordMonthly, \
    AggChildHealthMonthly
from django.db.models.aggregates import Sum, Count
from django.db.models import Case, When, Q, F, IntegerField

from custom.icds_reports.sqldata import AWCInfrastructureUCR, VHNDFormUCR, CcsRecordMonthlyURC, \
    ChildHealthMonthlyURC

DATA_NOT_ENTERED = "Data Not Entered"
AWC_LOCATION_LEVEL = 5


class ISSNIPMonthlyReport(object):
    def __init__(self, config):
        self.config = config

    def filter_by(self, config, column, default=0):
        return Case(
            When(Q(**config), then=F(column)),
            default=default,
            output_field=IntegerField()
        )

    @property
    @memoized
    def agg_awc_monthly_data(self):
        data = AggAwcMonthly.objects.filter(
            awc_id=self.config['awc_id'],
            aggregation_level=AWC_LOCATION_LEVEL,
            month=self.config['month']
        ).values(
            'block_name', 'awc_name', 'awc_site_code', 'infra_type_of_building', 'infra_clean_water',
            'cases_ccs_pregnant_all', 'cases_ccs_lactating_all', 'awc_days_open', 'awc_days_pse_conducted',
            'usage_num_home_visit', 'cases_person_referred'
        )
        return data[0] if data else None

    @property
    @memoized
    def child_health_monthly_data(self):
        data = ChildHealthMonthlyView.objects.filter(
            awc_id=self.config['awc_id'],
            month=self.config['month']
        ).values('awc_id').annotate(
            infants_0_6=Sum(self.filter_by({'age_in_months__range': [0, 6]}, 'valid_in_month')),
            children_6_36=Sum(self.filter_by({'age_in_months__range': [7, 36]}, 'valid_in_month')),
            children_36_72=Sum(self.filter_by({'age_in_months__range': [37, 72]}, 'valid_in_month')),
            normal_children_breakfast_and_hcm=Count(
                self.filter_by({
                    'nutrition_status_last_recorded': 'normal',
                    'age_in_months__range': [36, 72]
                }, 'case_id', None)
            ),
            normal_children_thr=Sum(
                self.filter_by({
                    'nutrition_status_last_recorded': 'normal',
                }, 'thr_eligible')
            ),
            severely_underweight_children_breakfast_and_hcm=Count(
                self.filter_by({
                    'nutrition_status_last_recorded': 'severely_underweight',
                    'age_in_months__range': [36, 72]
                }, 'case_id', None)
            ),
            severely_underweight_children_thr=Sum(
                self.filter_by({
                    'nutrition_status_last_recorded': 'severely_underweight',
                }, 'thr_eligible')
            ),
        )
        return data[0] if data else None

    @property
    @memoized
    def css_record_monthly(self):
        data = CcsRecordMonthly.objects.filter(
            awc_id=self.config['awc_id'],
            month=self.config['month']
        ).values(
            'awc_id'
        ).annotate(
            pregnant_women_thr=Sum(
                self.filter_by({
                    'pregnant__gt': 0,
                    'thr_eligible__gt': 0
                }, 'pregnant')
            ),
            lactating_women_thr=Sum(
                self.filter_by({
                    'lactating__gt': 0,
                    'thr_eligible__gt': 0
                }, 'lactating')
            )
        )
        return data[0] if data else None

    @property
    @memoized
    def infrastructure_data(self):
        data = AWCInfrastructureUCR(self.config).data
        return list(data.values())[-1] if data else None

    @property
    @memoized
    def vhnd_data(self):
        data = VHNDFormUCR(self.config).data
        return list(data.values())[-1] if data else None

    @property
    @memoized
    def ccs_record_monthly_ucr(self):
        data = CcsRecordMonthlyURC(self.config).data
        return data[self.config['awc_id']] if data else None

    @property
    @memoized
    def child_health_monthly_ucr(self):
        data = ChildHealthMonthlyURC(self.config).data
        return data[self.config['awc_id']] if data else None

    @property
    @memoized
    def agg_child_health_monthly(self):
        data = AggChildHealthMonthly.objects.filter(
            awc_id=self.config['awc_id'],
            aggregation_level=AWC_LOCATION_LEVEL,
            month=self.config['month']
        ).values('awc_id').annotate(
            boys_normal_0_3=Sum(self.filter_by({
                'age_tranche__in': ['0', '6', '12', '24', '36'],
                'gender': 'M'
            }, 'nutrition_status_normal')),
            girls_normal_0_3=Sum(self.filter_by({
                'age_tranche__in': ['0', '6', '12', '24', '36'],
                'gender': 'F'
            }, 'nutrition_status_normal')),
            boys_normal_3_5=Sum(self.filter_by({
                'age_tranche__in': ['48', '60'],
                'gender': 'M'
            }, 'nutrition_status_normal')),
            girls_normal_3_5=Sum(self.filter_by({
                'age_tranche__in': ['48', '60'],<commit_msg>replaced memoized and property to the cached_property<commit_after>from __future__ import absolute_import

from __future__ import unicode_literals

from custom.icds_reports.models import AggAwcMonthly, ChildHealthMonthlyView, CcsRecordMonthly, \
    AggChildHealthMonthly
from django.db.models.aggregates import Sum, Count
from django.db.models import Case, When, Q, F, IntegerField
from django.utils.functional import cached_property

from custom.icds_reports.sqldata import AWCInfrastructureUCR, VHNDFormUCR, CcsRecordMonthlyURC, \
    ChildHealthMonthlyURC

DATA_NOT_ENTERED = "Data Not Entered"
AWC_LOCATION_LEVEL = 5


class ISSNIPMonthlyReport(object):
    def __init__(self, config):
        self.config = config

    def filter_by(self, config, column, default=0):
        return Case(
            When(Q(**config), then=F(column)),
            default=default,
            output_field=IntegerField()
        )

    @cached_property
    def agg_awc_monthly_data(self):
        data = AggAwcMonthly.objects.filter(
            awc_id=self.config['awc_id'],
            aggregation_level=AWC_LOCATION_LEVEL,
            month=self.config['month']
        ).values(
            'block_name', 'awc_name', 'awc_site_code', 'infra_type_of_building', 'infra_clean_water',
            'cases_ccs_pregnant_all', 'cases_ccs_lactating_all', 'awc_days_open', 'awc_days_pse_conducted',
            'usage_num_home_visit', 'cases_person_referred'
        )
        return data[0] if data else None

    @cached_property
    def child_health_monthly_data(self):
        data = ChildHealthMonthlyView.objects.filter(
            awc_id=self.config['awc_id'],
            month=self.config['month']
        ).values('awc_id').annotate(
            infants_0_6=Sum(self.filter_by({'age_in_months__range': [0, 6]}, 'valid_in_month')),
            children_6_36=Sum(self.filter_by({'age_in_months__range': [7, 36]}, 'valid_in_month')),
            children_36_72=Sum(self.filter_by({'age_in_months__range': [37, 72]}, 'valid_in_month')),
            normal_children_breakfast_and_hcm=Count(
                self.filter_by({
                    'nutrition_status_last_recorded': 'normal',
                    'age_in_months__range': [36, 72]
                }, 'case_id', None)
            ),
            normal_children_thr=Sum(
                self.filter_by({
                    'nutrition_status_last_recorded': 'normal',
                }, 'thr_eligible')
            ),
            severely_underweight_children_breakfast_and_hcm=Count(
                self.filter_by({
                    'nutrition_status_last_recorded': 'severely_underweight',
                    'age_in_months__range': [36, 72]
                }, 'case_id', None)
            ),
            severely_underweight_children_thr=Sum(
                self.filter_by({
                    'nutrition_status_last_recorded': 'severely_underweight',
                }, 'thr_eligible')
            ),
        )
        return data[0] if data else None

    @cached_property
    def css_record_monthly(self):
        data = CcsRecordMonthly.objects.filter(
            awc_id=self.config['awc_id'],
            month=self.config['month']
        ).values(
            'awc_id'
        ).annotate(
            pregnant_women_thr=Sum(
                self.filter_by({
                    'pregnant__gt': 0,
                    'thr_eligible__gt': 0
                }, 'pregnant')
            ),
            lactating_women_thr=Sum(
                self.filter_by({
                    'lactating__gt': 0,
                    'thr_eligible__gt': 0
                }, 'lactating')
            )
        )
        return data[0] if data else None

    @cached_property
    def infrastructure_data(self):
        data = AWCInfrastructureUCR(self.config).data
        return list(data.values())[-1] if data else None

    @cached_property
    def vhnd_data(self):
        data = VHNDFormUCR(self.config).data
        return list(data.values())[-1] if data else None

    @cached_property
    def ccs_record_monthly_ucr(self):
        data = CcsRecordMonthlyURC(self.config).data
        return data[self.config['awc_id']] if data else None

    @cached_property
    def child_health_monthly_ucr(self):
        data = ChildHealthMonthlyURC(self.config).data
        return data[self.config['awc_id']] if data else None

    @cached_property
    def agg_child_health_monthly(self):
        data = AggChildHealthMonthly.objects.filter(
            awc_id=self.config['awc_id'],
            aggregation_level=AWC_LOCATION_LEVEL,
            month=self.config['month']
        ).values('awc_id').annotate(
            boys_normal_0_3=Sum(self.filter_by({
                'age_tranche__in': ['0', '6', '12', '24', '36'],
                'gender': 'M'
            }, 'nutrition_status_normal')),
            girls_normal_0_3=Sum(self.filter_by({
                'age_tranche__in': ['0', '6', '12', '24', '36'],
                'gender': 'F'
            }, 'nutrition_status_normal')),
            boys_normal_3_5=Sum(self.filter_by({
                'age_tranche__in': ['48', '60'],
                'gender': 'M'
            }, 'nutrition_status_normal')),
            girls_normal_3_5=Sum(self.filter_by({
                'age_tranche__in': ['48', '60'],
{'fraction_non_alphanumeric': 0.11047115475403645, 'fraction_numerical': 0.013029931073553016, 'mean_word_length': 2.2096969696969695, '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': '6642887', 'n_tokens_mistral': 3470, 'n_tokens_neox': 3253, 'n_words': 527}