<?xml version="1.0" encoding="UTF-8"?>
<pipeline>
  <info>
    <name>checkpoint_citizen_error</name>
    <name_sync_with_filename>Y</name_sync_with_filename>
    <description/>
    <extended_description/>
    <pipeline_version/>
    <pipeline_type>Normal</pipeline_type>
    <parameters>
    </parameters>
    <capture_transform_performance>N</capture_transform_performance>
    <transform_performance_capturing_delay>1000</transform_performance_capturing_delay>
    <transform_performance_capturing_size_limit>100</transform_performance_capturing_size_limit>
    <created_user>-</created_user>
    <created_date>2025/06/03 15:39:31.759</created_date>
    <modified_user>-</modified_user>
    <modified_date>2025/06/03 15:39:31.759</modified_date>
  </info>
  <notepads>
  </notepads>
  <order>
    <hop>
      <from>Select values</from>
      <to>Table output</to>
      <enabled>N</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Select values</to>
      <enabled>N</enabled>
    </hop>
    <hop>
      <from>Table input</from>
      <to>Table output</to>
      <enabled>Y</enabled>
    </hop>
  </order>
  <transform>
    <name>Select values</name>
    <type>SelectValues</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <fields>
      <select_unspecified>N</select_unspecified>
      <meta>
        <name>idm_id</name>
        <rename>idm_id</rename>
        <type>String</type>
        <length>-2</length>
        <precision>-2</precision>
        <conversion_mask/>
        <date_format_lenient>false</date_format_lenient>
        <date_format_locale/>
        <date_format_timezone/>
        <lenient_string_to_number>false</lenient_string_to_number>
        <encoding/>
        <decimal_symbol/>
        <grouping_symbol/>
        <currency_symbol/>
        <storage_type/>
      </meta>
    </fields>
    <attributes/>
    <GUI>
      <xloc>1024</xloc>
      <yloc>560</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table input</name>
    <type>TableInput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <connection>ervu-dashboard</connection>
    <execute_each_row>N</execute_each_row>
    <limit>0</limit>
    <sql>WITH ordered AS (
    SELECT 
        idm_id,
        created_at,
        ROW_NUMBER() OVER (ORDER BY created_at) AS rn,
        COUNT(*) OVER () AS total_rows
    FROM ervu_dashboard.recruitment
),
splitted AS (
    SELECT *,
        CEIL(total_rows / 5.0) AS part_size,
        FLOOR((rn - 1) / CEIL(total_rows / 5.0)) + 1 AS flow_num
    FROM ordered
),
flow_ranges AS (
    SELECT 
        flow_num,
        MIN(created_at) AS min_date,
        MAX(created_at) AS max_date
    FROM splitted
    GROUP BY flow_num
),
recr_inf AS (
    SELECT DISTINCT COALESCE(recruitment_id, target_recruitment_id) AS idm_id
    FROM ervu_dashboard.citizen
),
flow_data AS (
    SELECT 
        fr.flow_num,
        ri.idm_id,
        (SELECT r.created_at FROM ervu_dashboard.recruitment r 
         WHERE r.idm_id = ri.idm_id 
         AND r.created_at BETWEEN fr.min_date AND fr.max_date
         LIMIT 1) AS created_at
    FROM recr_inf ri
    CROSS JOIN flow_ranges fr
    WHERE EXISTS (
        SELECT 1 FROM ervu_dashboard.recruitment r 
        WHERE r.idm_id = ri.idm_id
    )
),
result_ma as (
    SELECT 
        MAX(created_at) FILTER(WHERE flow_num = 1) AS cr_flow1,
        (SELECT idm_id FROM flow_data WHERE flow_num = 1 ORDER BY created_at DESC NULLS LAST LIMIT 1) AS idm_flow1,
        
        MAX(created_at) FILTER(WHERE flow_num = 2) AS cr_flow2,
        (SELECT idm_id FROM flow_data WHERE flow_num = 2 ORDER BY created_at DESC NULLS LAST LIMIT 1) AS idm_flow2,
        
        MAX(created_at) FILTER(WHERE flow_num = 3) AS cr_flow3,
        (SELECT idm_id FROM flow_data WHERE flow_num = 3 ORDER BY created_at DESC NULLS LAST LIMIT 1) AS idm_flow3,
        
        MAX(created_at) FILTER(WHERE flow_num = 4) AS cr_flow4,
        (SELECT idm_id FROM flow_data WHERE flow_num = 4 ORDER BY created_at DESC NULLS LAST LIMIT 1) AS idm_flow4,
        
        MAX(created_at) FILTER(WHERE flow_num = 5) AS cr_flow5,
        (SELECT idm_id FROM flow_data WHERE flow_num = 5 ORDER BY created_at DESC NULLS LAST LIMIT 1) AS idm_flow5
    FROM flow_data
),
result_max as ( --костыль
    SELECT 
        CASE 
            WHEN cr_flow1 is null THEN '0001-01-01 00:00:00'::timestamp
            ELSE cr_flow1
        END AS cr_flow1,
        idm_flow1,        
        CASE 
            WHEN cr_flow2 is null THEN '0001-01-01 00:00:00'::timestamp
            ELSE cr_flow2
        END AS cr_flow2,
        idm_flow2,        
        CASE 
            WHEN cr_flow3 is null THEN '0001-01-01 00:00:00'::timestamp
            ELSE cr_flow3
        END AS cr_flow3,
        idm_flow3,        
        CASE 
            WHEN cr_flow4 is null THEN '0001-01-01 00:00:00'::timestamp
            ELSE cr_flow4
        END AS cr_flow4,
        idm_flow4,       
        CASE 
            WHEN cr_flow5 is null THEN '0001-01-01 00:00:00'::timestamp
            ELSE cr_flow5
        END AS cr_flow5,
        idm_flow5
    FROM result_ma
),
for_checkpoints as (
    SELECT
        'job_citizen_flow1' as job_name,
        idm_flow1 as last_recruitment_id,
        cr_flow1 as recruitment_created_date,
        'ERROR' as status,
        current_timestamp as record_created
    from result_max

    union all

    SELECT
        'job_citizen_flow2' as job_name,
        idm_flow2 as last_recruitment_id,
        cr_flow2 as recruitment_created_date,
        'ERROR' as status,
        current_timestamp as record_created
    from result_max

    union all

    SELECT
        'job_citizen_flow3' as job_name,
        idm_flow3 as last_recruitment_id,
        cr_flow3 as recruitment_created_date,
        'ERROR' as status,
        current_timestamp as record_created
    from result_max

    union all

    SELECT
        'job_citizen_flow4' as job_name,
        idm_flow4 as last_recruitment_id,
        cr_flow4 as recruitment_created_date,
        'ERROR' as status,
        current_timestamp as record_created
    from result_max

    union all

    SELECT
        'job_citizen_flow5' as job_name,
        idm_flow5 as last_recruitment_id,
        cr_flow5 as recruitment_created_date,
        'ERROR' as status,
        current_timestamp as record_created
    from result_max
)
select * from for_checkpoints</sql>
    <variables_active>N</variables_active>
    <attributes/>
    <GUI>
      <xloc>752</xloc>
      <yloc>400</yloc>
    </GUI>
  </transform>
  <transform>
    <name>Table output</name>
    <type>TableOutput</type>
    <description/>
    <distribute>Y</distribute>
    <custom_distribution/>
    <copies>1</copies>
    <partitioning>
      <method>none</method>
      <schema_name/>
    </partitioning>
    <commit>1000</commit>
    <connection>ervu-dashboard</connection>
    <fields>
</fields>
    <ignore_errors>N</ignore_errors>
    <only_when_have_rows>N</only_when_have_rows>
    <partitioning_daily>N</partitioning_daily>
    <partitioning_enabled>N</partitioning_enabled>
    <partitioning_monthly>Y</partitioning_monthly>
    <return_keys>N</return_keys>
    <schema>public</schema>
    <specify_fields>N</specify_fields>
    <table>etl_checkpoints</table>
    <tablename_in_field>N</tablename_in_field>
    <tablename_in_table>Y</tablename_in_table>
    <truncate>N</truncate>
    <use_batch>Y</use_batch>
    <attributes/>
    <GUI>
      <xloc>1264</xloc>
      <yloc>400</yloc>
    </GUI>
  </transform>
  <transform_error_handling>
  </transform_error_handling>
  <attributes/>
</pipeline>
