{% macro control_model_end(model_type) %}
    {% set model_name = model.name %}
    {{ elog("START macro -> control_model_end, model " ~ model_name, "INFO", 0)}}

    {{ elog("Get subprocess_key for model " ~ model_name, "INFO", 1)}}
    {% set a_task_history_key = get_task_history_key(model_name) %}
    {{ check_null("a_task_history_key", a_task_history_key) }}

    {% if model_type == 'SQ' %}
        {% set table_name = this.schema ~ '.' ~ this.name %}
        {{ elog("Check if data exists in SQ, model " ~ model_name, "INFO", 1)}}
        {% set sq_data_exists = check_data_exists(table_name) %}
        {% if 'm_DWH' in model_name and 'm_DWH_PRELOAD' not in model_name %}
            {% set model_sources =get_parent_models(model_name) %}
            {% if model_sources | length > 0 %}
                {% for model_source in model_sources %}
                     {% set source_name = get_table_qualified_name(model_source) %}
                    {{ elog("Run control_source_count for source " ~ source_name ~ ", model " ~ model_name, "INFO", 1)}}      
                    {{ control_source_count(a_task_history_key, source_name,'', '') }}
                {% endfor %}
            {% endif %}
        {% else %}
            {% set model_sources =model.sources %}
            {% for model_source in model_sources %}
                {% if model_source[0] != 'control_tables' %}
                    {% set source_name = source(model_source[0], model_source[1]) %}
                    {{ elog("Run control_source_count for source " ~ source_name ~ ", model " ~ model_name, "INFO", 1)}}      
                    {{ control_source_count(a_task_history_key, source_name,'', '') }}
                {% endif %}
            {% endfor %}
        {% endif %}
    
    {% elif model_type == 'TRG' %}
        {# {{ elog('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>', "INFO", 1) }} #}
        {% set parent_models = get_parent_models(model_name) %} 
        {% if parent_models | length > 0 %}
            {% for model in parent_models %}
                {% if 'm_DWH' in model or 'm_MOPDB' in model %}
                    {% set table_name = get_table_qualified_name(model) %}
                    {# {{ elog('--------------->> Table: ', "INFO", 1) }} #}
                    {# {{ elog(table_name, "INFO", 1) }} #}
                    {{ elog("Check if data exists in SQ table: " ~ table_name ~ " for model: " ~ model_name, "INFO", 1)}}
                    {% set trg_data_exists = check_data_exists(table_name) %}
                    {{ elog("Run control_source_count for source " ~ table_name ~ ", model " ~ model_name, "INFO", 1)}}
                    {{ control_source_count(a_task_history_key, table_name,'A_WORKFLOW_HISTORY_KEY', trg_data_exists) }}
                {% endif %}
            {% endfor %}
        {% else %}
            {{ elog("There are no parent SQ for model: " ~ model_name, "INFO", 1)}}
        {% endif %}
        {# {{ log(parent_models, info=True) }} #}
        
    {% endif %}

    

    {{ elog("Run macro update_A_TASK_HISTORY for model " ~ model_name, "INFO", 1)}}
    {{ update_A_TASK_HISTORY(a_task_history_key, convert_timezone(m_current_timestamp('dbt'),'YYYY-MM-DD HH24:MI:SS',''), 'Y') }}

    {% if sq_data_exists == 0 %}
        {{ exceptions.warn("Model " ~ model_name ~ " has no rows.") }}
    {% endif %}

    {% do run_query('COMMIT') %} 
    {{ elog("END macro -> control_model_end, model " ~ model_name, "INFO", 0)}}
{% endmacro %}