<commit_before>from dateutil.relativedelta import relativedelta

from custom.icds_reports.const import AGG_MIGRATION_TABLE
from custom.icds_reports.utils.aggregation_helpers import month_formatter
from custom.icds_reports.utils.aggregation_helpers.distributed.base import (
    StateBasedAggregationDistributedHelper,
)


class MigrationFormsAggregationDistributedHelper(StateBasedAggregationDistributedHelper):
    helper_key = 'migration-forms'
    ucr_data_source_id = 'static-migration_form'
    aggregate_parent_table = AGG_MIGRATION_TABLE

    def data_from_ucr_query(self):
        month = self.month.replace(day=1)
        current_month_start = month_formatter(self.month)
        next_month_start = month_formatter(self.month + relativedelta(month=1))

        query_params = {
            "month": month_formatter(month),
            "state_id": self.state_id,
            "current_month_start": current_month_start,
            "next_month_start": next_month_start,
        }
        return """
        SELECT DISTINCT ON (person_case_id)
            %(state_id)s AS state_id,
            supervisor_id,
            %(month)s::DATE AS month,
            person_case_id AS person_case_id,
            LAST_VALUE(is_migrated) OVER w AS is_migrated,
            CASE
                WHEN LAST_VALUE(date_left) OVER w IS NULL
                    THEN LAST_VALUE(timeend) OVER w
                ELSE LAST_VALUE(date_left) OVER w
            END AS migration_date
          FROM "{ucr_tablename}"
          WHERE state_id = %(state_id)s AND
                timeend >= %(current_month_start)s AND timeend < %(next_month_start)s AND
                person_case_id IS NOT NULL
          WINDOW w AS (
            PARTITION BY supervisor_id, person_case_id
            ORDER BY timeend RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
        )
        """.format(
            ucr_tablename=self.ucr_tablename,
            tablename=self.aggregate_parent_table,
        ), query_params

    def aggregation_query(self):<commit_msg>[fix]: minor fix in sql query<commit_after>from dateutil.relativedelta import relativedelta

from custom.icds_reports.const import AGG_MIGRATION_TABLE
from custom.icds_reports.utils.aggregation_helpers import month_formatter
from custom.icds_reports.utils.aggregation_helpers.distributed.base import (
    StateBasedAggregationDistributedHelper,
)


class MigrationFormsAggregationDistributedHelper(StateBasedAggregationDistributedHelper):
    helper_key = 'migration-forms'
    ucr_data_source_id = 'static-migration_form'
    aggregate_parent_table = AGG_MIGRATION_TABLE

    def data_from_ucr_query(self):
        month = self.month.replace(day=1)
        current_month_start = month_formatter(self.month)
        next_month_start = month_formatter(self.month + relativedelta(month=1))

        query_params = {
            "month": month_formatter(month),
            "state_id": self.state_id,
            "current_month_start": current_month_start,
            "next_month_start": next_month_start,
        }
        return """
        SELECT DISTINCT
            %(state_id)s AS state_id,
            supervisor_id,
            %(month)s::DATE AS month,
            person_case_id AS person_case_id,
            LAST_VALUE(is_migrated) OVER w AS is_migrated,
            CASE
                WHEN LAST_VALUE(date_left) OVER w IS NULL
                    THEN LAST_VALUE(timeend) OVER w
                ELSE LAST_VALUE(date_left) OVER w
            END AS migration_date
          FROM "{ucr_tablename}"
          WHERE state_id = %(state_id)s AND
                timeend >= %(current_month_start)s AND timeend < %(next_month_start)s AND
                person_case_id IS NOT NULL
          WINDOW w AS (
            PARTITION BY supervisor_id, person_case_id
            ORDER BY timeend RANGE BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
        )
        """.format(
            ucr_tablename=self.ucr_tablename,
            tablename=self.aggregate_parent_table,
        ), query_params

    def aggregation_query(self):
{'fraction_non_alphanumeric': 0.06596370769985287, 'fraction_numerical': 0.000980872976949485, 'mean_word_length': 2.6161347517730498, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 5, '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': '3875191', 'n_tokens_mistral': 1274, 'n_tokens_neox': 1210, 'n_words': 240}