<commit_before>from casexml.apps.phone.data_providers import LongRunningRestoreDataProvider
from casexml.apps.phone.data_providers.case.batched import get_case_payload_batched
from casexml.apps.phone.data_providers.case.clean_owners import get_case_payload
from corehq.toggles import OWNERSHIP_CLEANLINESS_RESTORE


class CasePayloadProvider(LongRunningRestoreDataProvider):
    """
    Long running restore provider responsible for generating the case and stock payloads.
    """
    def get_response(self, restore_state):
        if OWNERSHIP_CLEANLINESS_RESTORE.enabled(restore_state.domain):
            return get_case_payload(restore_state)
        else:
            return _batched_response(restore_state)


def _batched_response(restore_state):
    case_response, num_batches = get_case_payload_batched(
        restore_state=restore_state,
    )
    # keep track of the number of batches (if any) for comparison in unit tests
    restore_state.provider_log['num_case_batches'] = num_batches
    return case_response<commit_msg>use override pattern in ownership cleanliness restore<commit_after>from django.conf import settings
from casexml.apps.phone.data_providers import LongRunningRestoreDataProvider
from casexml.apps.phone.data_providers.case.batched import get_case_payload_batched
from casexml.apps.phone.data_providers.case.clean_owners import get_case_payload
from corehq.toggles import OWNERSHIP_CLEANLINESS_RESTORE


class CasePayloadProvider(LongRunningRestoreDataProvider):
    """
    Long running restore provider responsible for generating the case and stock payloads.
    """
    def get_response(self, restore_state):
        if should_use_clean_restore(restore_state.domain):
            return get_case_payload(restore_state)
        else:
            return _batched_response(restore_state)


def _batched_response(restore_state):
    case_response, num_batches = get_case_payload_batched(
        restore_state=restore_state,
    )
    # keep track of the number of batches (if any) for comparison in unit tests
    restore_state.provider_log['num_case_batches'] = num_batches
    return case_response


def should_use_clean_restore(domain):
    if settings.UNIT_TESTING:
        override = getattr(
            settings, 'SHOULD_USE_CLEAN_RESTORE', None)
        if override is not None:
            return override
    return OWNERSHIP_CLEANLINESS_RESTORE.enabled(domain)
{'fraction_non_alphanumeric': 0.05574043261231281, 'fraction_numerical': 0.0, 'mean_word_length': 4.56712962962963, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '1117644', 'n_tokens_mistral': 701, 'n_tokens_neox': 681, 'n_words': 155}