<commit_before>from functools import partial
from corehq.apps.change_feed import topics
from dimagi.utils.dates import force_to_datetime
import fluff
from corehq.fluff.calculators.case import CasePropertyFilter
from custom.world_vision import WORLD_VISION_DOMAINS
from corehq.apps.users.models import CommCareUser
from custom.utils.utils import flat_field
from custom.world_vision import user_calcs
from casexml.apps.case.models import CommCareCase

from django.utils.dateformat import format


WV_DELETED_TYPES = ('CommCareCase-Deleted', )


class WorldVisionMotherFluff(fluff.IndicatorDocument):

    def case_property(property):
        return flat_field(lambda case: case.get_case_property(property))

    document_class = CommCareCase
    document_filter = CasePropertyFilter(type='ttc_mother')
    deleted_types = WV_DELETED_TYPES

    domains = WORLD_VISION_DOMAINS
    group_by = ('domain', 'user_id')
    kafka_topic = topics.CASE

    name = flat_field(lambda case: case.name)
    lvl_4 = case_property('phc')
    lvl_3 = case_property('block')
    lvl_2 = case_property('district')
    lvl_1 = case_property('state')
    reason_for_mother_closure = flat_field(lambda case: case.reason_for_mother_closure if hasattr(case, 'reason_for_mother_closure')
                                                                                          and case.reason_for_mother_closure else 'unknown')
    mother_state = case_property('mother_state')
    fp_method = case_property('fp_method')

    anc_1 = case_property('anc_1')
    anc_2 = case_property('anc_2')
    anc_3 = case_property('anc_3')
    anc_4 = case_property('anc_4')
    tt_1 = case_property('tt_1')
    tt_2 = case_property('tt_2')
    tt_booster = case_property('tt_booster')
    iron_folic = case_property('iron_folic')
    completed_100_ifa = case_property('completed_100_ifa')
    anemia_signs = case_property('anemia_signs')
    currently_referred = case_property('currently_referred')
    knows_closest_facility = case_property('knows_closest_facility')
    edd = case_property('edd')
    previous_tetanus = case_property('previous_tetanus')
    pp_1_done = case_property('pp_1_done')
    pp_2_done = case_property('pp_2_done')
    pp_3_done = case_property('pp_3_done')
    pp_4_done = case_property('pp_4_done')
    delivery_date = case_property('delivery_date')
    cause_of_death_maternal = case_property('cause_of_death_maternal')
    place_of_birth = case_property('place_of_birth')
    birth_attendant_during_delivery = case_property('birth_attendant_during_delivery')
    type_of_delivery = case_property('type_of_delivery')
    date_of_mother_death = case_property('date_of_mother_death')

    number_of_children = user_calcs.NumberChildren()
    number_of_boys = user_calcs.NumberBoys()
    number_of_girls = user_calcs.NumberGirls()
    number_of_children_born_dead = user_calcs.StillBirth()

    opened_on = flat_field(lambda case: case.opened_on.date() if case.opened_on else None)
    closed_on = flat_field(lambda case: case.closed_on.date() if case.closed_on else None)

    women_registered = user_calcs.MotherRegistered()


def referenced_case_attribute(case, field_name):
    if not case.indices[0]['referenced_id']:
        return ""
    referenced_case = CommCareCase.get(case.indices[0]['referenced_id'])
    if hasattr(referenced_case, field_name):
        return getattr(referenced_case, field_name)<commit_msg>don't fail hard if no indices<commit_after>from functools import partial
from corehq.apps.change_feed import topics
from dimagi.utils.dates import force_to_datetime
import fluff
from corehq.fluff.calculators.case import CasePropertyFilter
from custom.world_vision import WORLD_VISION_DOMAINS
from corehq.apps.users.models import CommCareUser
from custom.utils.utils import flat_field
from custom.world_vision import user_calcs
from casexml.apps.case.models import CommCareCase

from django.utils.dateformat import format


WV_DELETED_TYPES = ('CommCareCase-Deleted', )


class WorldVisionMotherFluff(fluff.IndicatorDocument):

    def case_property(property):
        return flat_field(lambda case: case.get_case_property(property))

    document_class = CommCareCase
    document_filter = CasePropertyFilter(type='ttc_mother')
    deleted_types = WV_DELETED_TYPES

    domains = WORLD_VISION_DOMAINS
    group_by = ('domain', 'user_id')
    kafka_topic = topics.CASE

    name = flat_field(lambda case: case.name)
    lvl_4 = case_property('phc')
    lvl_3 = case_property('block')
    lvl_2 = case_property('district')
    lvl_1 = case_property('state')
    reason_for_mother_closure = flat_field(lambda case: case.reason_for_mother_closure if hasattr(case, 'reason_for_mother_closure')
                                                                                          and case.reason_for_mother_closure else 'unknown')
    mother_state = case_property('mother_state')
    fp_method = case_property('fp_method')

    anc_1 = case_property('anc_1')
    anc_2 = case_property('anc_2')
    anc_3 = case_property('anc_3')
    anc_4 = case_property('anc_4')
    tt_1 = case_property('tt_1')
    tt_2 = case_property('tt_2')
    tt_booster = case_property('tt_booster')
    iron_folic = case_property('iron_folic')
    completed_100_ifa = case_property('completed_100_ifa')
    anemia_signs = case_property('anemia_signs')
    currently_referred = case_property('currently_referred')
    knows_closest_facility = case_property('knows_closest_facility')
    edd = case_property('edd')
    previous_tetanus = case_property('previous_tetanus')
    pp_1_done = case_property('pp_1_done')
    pp_2_done = case_property('pp_2_done')
    pp_3_done = case_property('pp_3_done')
    pp_4_done = case_property('pp_4_done')
    delivery_date = case_property('delivery_date')
    cause_of_death_maternal = case_property('cause_of_death_maternal')
    place_of_birth = case_property('place_of_birth')
    birth_attendant_during_delivery = case_property('birth_attendant_during_delivery')
    type_of_delivery = case_property('type_of_delivery')
    date_of_mother_death = case_property('date_of_mother_death')

    number_of_children = user_calcs.NumberChildren()
    number_of_boys = user_calcs.NumberBoys()
    number_of_girls = user_calcs.NumberGirls()
    number_of_children_born_dead = user_calcs.StillBirth()

    opened_on = flat_field(lambda case: case.opened_on.date() if case.opened_on else None)
    closed_on = flat_field(lambda case: case.closed_on.date() if case.closed_on else None)

    women_registered = user_calcs.MotherRegistered()


def referenced_case_attribute(case, field_name):
    if not (case.indices and case.indices[0]['referenced_id']):
        return ""
    referenced_case = CommCareCase.get(case.indices[0]['referenced_id'])
    if hasattr(referenced_case, field_name):
        return getattr(referenced_case, field_name)
{'fraction_non_alphanumeric': 0.08781911013858497, 'fraction_numerical': 0.009336250911743253, 'mean_word_length': 4.72765246449457, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5082694', 'n_tokens_mistral': 2295, 'n_tokens_neox': 2202, 'n_words': 368}