<commit_before>from __future__ import absolute_import
from __future__ import unicode_literals

from corehq.apps.userreports.models import StaticDataSourceConfiguration, get_datasource_config
from corehq.apps.userreports.util import get_table_name
from custom.icds_reports.const import (
    AWW_INCENTIVE_TABLE,
    AGG_CCS_RECORD_CF_TABLE
)
from custom.icds_reports.utils.aggregation_helpers import month_formatter
from custom.icds_reports.utils.aggregation_helpers.distributed.base import BaseICDSAggregationDistributedHelper


class AwwIncentiveAggregationDistributedHelper(BaseICDSAggregationDistributedHelper):
    helper_key = 'awc-incentive'
    aggregate_parent_table = AWW_INCENTIVE_TABLE
    aggregate_child_table_prefix = 'icds_db_aww_incentive_'

    def aggregate(self, cursor):
        curr_month_query, curr_month_params = self.create_table_query()
        agg_query, agg_params = self.aggregation_query()

        cursor.execute(self.drop_table_query())
        cursor.execute(curr_month_query, curr_month_params)
        cursor.execute(agg_query, agg_params)

    @property
    def ccs_record_case_ucr_tablename(self):
        doc_id = StaticDataSourceConfiguration.get_doc_id(self.domain, 'static-ccs_record_cases')
        config, _ = get_datasource_config(doc_id, self.domain)
        return get_table_name(self.domain, config.table_id)

    def aggregation_query(self):
        month = self.month.replace(day=1)
        tablename = self.generate_child_tablename(month)

        query_params = {
            "month": month_formatter(month),
            "state_id": self.state_id
        }

        return """
        CREATE TEMPORARY TABLE "tmp_incentive" AS SELECT
            %(state_id)s AS state_id,
            awcm.district_id,
            %(month)s AS month,
            awcm.awc_id,
            awcm.block_id,
            awcm.supervisor_id,
            awcm.state_name,
            awcm.district_name,
            awcm.block_name,
            awcm.supervisor_name,
            awcm.awc_name,
            awcm.aww_name,
            awcm.contact_phone_number,
            awcm.wer_weighed,
            awcm.wer_eligible,
            awcm.awc_days_open,
            sum(ccsm.valid_visits),
            sum(ccsm.expected_visits)
        FROM agg_awc_monthly as awcm
        INNER JOIN agg_ccs_record_monthly AS ccsm
        ON ccsm.month=awcm.month AND ccsm.awc_id=awcm.awc_id AND ccsm.aggregation_level=awcm.aggregation_level
        WHERE awcm.month = %(month)s AND awcm.state_id = %(state_id)s and awcm.aggregation_level=5
        GROUP BY awcm.awc_id, awcm.block_id, awcm.supervisor_id, awcm.district_id, awcm.state_name, awcm.district_name,
                   awcm.block_name, awcm.supervisor_name, awcm.awc_name, awcm.aww_name,
                   awcm.contact_phone_number, awcm.wer_weighed, awcm.wer_eligible,
                   awcm.awc_days_open;
        INSERT INTO "{tablename}" (
            state_id, district_id, month, awc_id, block_id, supervisor_id, state_name, district_name, block_name, 
            supervisor_name, awc_name, aww_name, contact_phone_number, wer_weighed,
            wer_eligible, awc_num_open, valid_visits, expected_visits
        )
        SELECT * FROM "tmp_incentive";<commit_msg>fix incentive report for citus<commit_after>from __future__ import absolute_import
from __future__ import unicode_literals

from corehq.apps.userreports.models import StaticDataSourceConfiguration, get_datasource_config
from corehq.apps.userreports.util import get_table_name
from custom.icds_reports.const import (
    AWW_INCENTIVE_TABLE,
    AGG_CCS_RECORD_CF_TABLE
)
from custom.icds_reports.utils.aggregation_helpers import month_formatter
from custom.icds_reports.utils.aggregation_helpers.distributed.base import BaseICDSAggregationDistributedHelper


class AwwIncentiveAggregationDistributedHelper(BaseICDSAggregationDistributedHelper):
    helper_key = 'awc-incentive'
    aggregate_parent_table = AWW_INCENTIVE_TABLE
    aggregate_child_table_prefix = 'icds_db_aww_incentive_'

    def aggregate(self, cursor):
        curr_month_query, curr_month_params = self.create_table_query()
        agg_query, agg_params = self.aggregation_query()

        cursor.execute(self.drop_table_query())
        cursor.execute(curr_month_query, curr_month_params)
        cursor.execute(agg_query, agg_params)

    @property
    def ccs_record_case_ucr_tablename(self):
        doc_id = StaticDataSourceConfiguration.get_doc_id(self.domain, 'static-ccs_record_cases')
        config, _ = get_datasource_config(doc_id, self.domain)
        return get_table_name(self.domain, config.table_id)

    def aggregation_query(self):
        month = self.month.replace(day=1)
        tablename = self.generate_child_tablename(month)

        query_params = {
            "month": month_formatter(month),
            "state_id": self.state_id
        }

        return """
        CREATE TEMPORARY TABLE "tmp_incentive" AS SELECT
            %(state_id)s AS state_id,
            awcm.district_id,
            %(month)s::DATE AS month,
            awcm.awc_id,
            awcm.block_id,
            awcm.supervisor_id,
            awcm.state_name,
            awcm.district_name,
            awcm.block_name,
            awcm.supervisor_name,
            awcm.awc_name,
            awcm.aww_name,
            awcm.contact_phone_number,
            awcm.wer_weighed,
            awcm.wer_eligible,
            awcm.awc_days_open,
            sum(ccsm.valid_visits) as valid_visits,
            sum(ccsm.expected_visits) as expected_visits
        FROM agg_awc_monthly as awcm
        INNER JOIN agg_ccs_record_monthly AS ccsm
        ON ccsm.month=awcm.month AND ccsm.awc_id=awcm.awc_id AND ccsm.aggregation_level=awcm.aggregation_level
        WHERE awcm.month = %(month)s AND awcm.state_id = %(state_id)s and awcm.aggregation_level=5
        GROUP BY awcm.awc_id, awcm.block_id, awcm.supervisor_id, awcm.district_id, awcm.state_name, awcm.district_name,
                   awcm.block_name, awcm.supervisor_name, awcm.awc_name, awcm.aww_name,
                   awcm.contact_phone_number, awcm.wer_weighed, awcm.wer_eligible,
                   awcm.awc_days_open;
        INSERT INTO "{tablename}" (
            state_id, district_id, month, awc_id, block_id, supervisor_id, state_name, district_name, block_name, 
            supervisor_name, awc_name, aww_name, contact_phone_number, wer_weighed,
            wer_eligible, awc_num_open, valid_visits, expected_visits
        )
        SELECT * FROM "tmp_incentive";
{'fraction_non_alphanumeric': 0.07236641221374046, 'fraction_numerical': 0.0006106870229007634, 'mean_word_length': 3.001832620647526, 'pattern_counts': {'":': 4, '<': 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': '4334980', 'n_tokens_mistral': 2206, 'n_tokens_neox': 2106, 'n_words': 341}