<commit_before>from rdrf.celery import app
from intframework.utils import get_event_code
from intframework.updater import HL7Handler
import hl7
from celery.utils.log import get_task_logger

logger = get_task_logger(__name__)

logger.info("registering tasks ...")


@app.task(name="rdrf.services.tasks.run_custom_action")
def run_custom_action(custom_action_id, user_id, patient_id, input_data):
    logger.debug("running custom action %s async" % custom_action_id)
    logger.debug("user_id = %s" % user_id)
    logger.debug("patient_id = %s" % patient_id)
    logger.debug("input_data = %s" % input_data)
    from rdrf.models.definition.models import CustomAction
    from registry.groups.models import CustomUser
    from registry.patients.models import Patient

    if patient_id != 0:
        patient_model = Patient.objects.get(id=patient_id)
    else:
        patient_model = None

    logger.debug("patient model = %s" % patient_model)

    user = CustomUser.objects.get(id=user_id)
    logger.debug("user = %s" % user)
    custom_action = CustomAction.objects.get(id=custom_action_id)

    logger.debug("running custom action execute")
    return custom_action.execute(user, patient_model, input_data)


@app.task(name="rdrf.services.tasks.handle_hl7_message")
def handle_hl7_message(umrn, message: hl7.Message):
    logger.info(f"processing task for umrn {umrn}")
    try:
        event_code = get_event_code(message)
    except Exception as ex:
        logger.error(f"error getting event code for message: {ex}")
        event_code = "unknown"
    logger.info(f"HL7 handler: {umrn} {event_code} received")
    hl7_handler = HL7Handler(umrn=umrn, hl7message=message, username="updater")
    response_data = hl7_handler.handle()
    if response_data:
        logger.info(f"HL7 handler: {umrn} {event_code} processed")
    else:
        logger.error(f"HL7 handler: {umrn} {event_code} failed")
    return response_data


@app.task(name="rdrf.services.tasks.recalculate_cde")
def recalculate_cde(patient_id, registry_code, context_id, form_name, section_code, section_index, cde_code):
    """
    Problem: If a user changes an input of a calculated field which sits on
    another form, the calculation will not be re-evaluated unless
    they go that form and save.
    This task recalculates a calculated cde automatically.
    The task is spawn in the postsave of the form with the changed input
    """
    from rdrf.models.definition.models import Registry
    from rdrf.models.definition.models import CommonDataElement
    from rdrf.models.definition.models import RDRFContext
    from rdrf.forms.fields import calculated_functions as cf
    from registry.patients.models import Patient

    what = f"{registry_code}/{form_name}/{section_index}/{section_code}/{cde_code}"
    msg = f"recalc of {what} in context {context_id}"
    logger.info(msg)

    registry: Registry = Registry.objects.get(code=registry_code)

    cde_model = CommonDataElement.objects.get(code=cde_code)
    if cde_model.datatype != "calculated":
        logger.error(f"cannot recalculate cde {cde_code} as it is not a calculated field")
        return

    calculation_func = getattr(cf, cde_code)
    if not callable(calculation_func):
        logger.error(f"could not find calculation for {cde_code}")
        return<commit_msg>rdrf #2202 only perform recalc on new style calcs<commit_after>from rdrf.celery import app
from intframework.utils import get_event_code
from intframework.updater import HL7Handler
import hl7
from celery.utils.log import get_task_logger

logger = get_task_logger(__name__)

logger.info("registering tasks ...")


@app.task(name="rdrf.services.tasks.run_custom_action")
def run_custom_action(custom_action_id, user_id, patient_id, input_data):
    logger.debug("running custom action %s async" % custom_action_id)
    logger.debug("user_id = %s" % user_id)
    logger.debug("patient_id = %s" % patient_id)
    logger.debug("input_data = %s" % input_data)
    from rdrf.models.definition.models import CustomAction
    from registry.groups.models import CustomUser
    from registry.patients.models import Patient

    if patient_id != 0:
        patient_model = Patient.objects.get(id=patient_id)
    else:
        patient_model = None

    logger.debug("patient model = %s" % patient_model)

    user = CustomUser.objects.get(id=user_id)
    logger.debug("user = %s" % user)
    custom_action = CustomAction.objects.get(id=custom_action_id)

    logger.debug("running custom action execute")
    return custom_action.execute(user, patient_model, input_data)


@app.task(name="rdrf.services.tasks.handle_hl7_message")
def handle_hl7_message(umrn, message: hl7.Message):
    logger.info(f"processing task for umrn {umrn}")
    try:
        event_code = get_event_code(message)
    except Exception as ex:
        logger.error(f"error getting event code for message: {ex}")
        event_code = "unknown"
    logger.info(f"HL7 handler: {umrn} {event_code} received")
    hl7_handler = HL7Handler(umrn=umrn, hl7message=message, username="updater")
    response_data = hl7_handler.handle()
    if response_data:
        logger.info(f"HL7 handler: {umrn} {event_code} processed")
    else:
        logger.error(f"HL7 handler: {umrn} {event_code} failed")
    return response_data


@app.task(name="rdrf.services.tasks.recalculate_cde")
def recalculate_cde(patient_id, registry_code, context_id, form_name, section_code, section_index, cde_code):
    """
    Problem: If a user changes an input of a calculated field which sits on
    another form, the calculation will not be re-evaluated unless
    they go that form and save.
    This task recalculates a calculated cde automatically.
    The task is spawn in the postsave of the form with the changed input
    """
    from rdrf.models.definition.models import Registry
    from rdrf.models.definition.models import CommonDataElement
    from rdrf.models.definition.models import RDRFContext
    from rdrf.forms.fields import calculated_functions as cf
    from registry.patients.models import Patient

    what = f"{registry_code}/{form_name}/{section_index}/{section_code}/{cde_code}"
    msg = f"recalc of {what} in context {context_id}"
    logger.info(msg)

    registry: Registry = Registry.objects.get(code=registry_code)

    if not registry.has_feature("use_new_style_calcs"):
        logger.info("recalc: will not recalculate cde as registry not using new style calcs feature")
        return

    cde_model = CommonDataElement.objects.get(code=cde_code)
    if cde_model.datatype != "calculated":
        logger.error(f"cannot recalculate cde {cde_code} as it is not a calculated field")
        return

    calculation_func = getattr(cf, cde_code)
    if not callable(calculation_func):
        logger.error(f"could not find calculation for {cde_code}")
        return
{'fraction_non_alphanumeric': 0.0945353594389246, 'fraction_numerical': 0.004383401519579193, 'mean_word_length': 4.119670905011219, 'pattern_counts': {'":': 2, '<': 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': '895973', 'n_tokens_mistral': 2177, 'n_tokens_neox': 2122, 'n_words': 540}