<commit_before>from xml.etree import ElementTree
from casexml.apps.case.xml import V2
from corehq.apps.domain.models import Domain
from corehq.apps.callcenter.indicator_sets import CallCenter
from corehq.apps.users.models import CommCareUser


def indicators(user, version=V2, last_sync=None):
    if isinstance(user, CommCareUser):
        pass
    elif hasattr(user, "_hq_user") and user._hq_user is not None:
        user = user._hq_user
    else:
        return []

    fixtures = []
    indicator_sets = []
    for dom in user.get_domains():
        domain = Domain.get_by_name(dom)
        if hasattr(domain, 'call_center_config') and domain.call_center_config.enabled:
            indicator_sets.append(CallCenter(domain, user))

    for set in indicator_sets:
        fixtures.append(gen_fixture(user, set))

    return fixtures


def gen_fixture(user, indicator_set):
    """
    Generate the fixture from the indicator data.

    :param user: The user.
    :param indicator_set: A subclass of SqlIndicatorSet
    :param include_empty: True to include indicators that have no value for the current time period.
    """
    """
    Example output:
    indicator_set {
        name = 'demo'
        group = None
        data = {'indicator_a': 1}
    }
    <fixture id="indicators:demo" user_id="...">
        <indicators>
            <indicator_a>1</indicator_a><commit_msg>failsafe exception catching<commit_after>import logging
from xml.etree import ElementTree
from casexml.apps.case.xml import V2
from corehq.apps.domain.models import Domain
from corehq.apps.callcenter.indicator_sets import CallCenter
from corehq.apps.users.models import CommCareUser


logger = logging.getLogger(__name__)


def indicators(user, version=V2, last_sync=None):
    if isinstance(user, CommCareUser):
        pass
    elif hasattr(user, "_hq_user") and user._hq_user is not None:
        user = user._hq_user
    else:
        return []

    fixtures = []
    indicator_sets = []
    for dom in user.get_domains():
        domain = Domain.get_by_name(dom)
        if hasattr(domain, 'call_center_config') and domain.call_center_config.enabled:
            indicator_sets.append(CallCenter(domain, user))

    for set in indicator_sets:
        try:
            fixtures.append(gen_fixture(user, set))
        except Exception as e:  # blanket exception catching intended
            logger.exception(e)

    return fixtures


def gen_fixture(user, indicator_set):
    """
    Generate the fixture from the indicator data.

    :param user: The user.
    :param indicator_set: A subclass of SqlIndicatorSet
    :param include_empty: True to include indicators that have no value for the current time period.
    """
    """
    Example output:
    indicator_set {
        name = 'demo'
        group = None
        data = {'indicator_a': 1}
    }
    <fixture id="indicators:demo" user_id="...">
        <indicators>
            <indicator_a>1</indicator_a>
{'fraction_non_alphanumeric': 0.09020006781959987, 'fraction_numerical': 0.0027127839945744322, 'mean_word_length': 2.975741239892183, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '663503', 'n_tokens_mistral': 907, 'n_tokens_neox': 880, 'n_words': 241}