package com.lanyuanxiaoyao.service.executor.task;

import cn.hutool.core.util.ObjectUtil;
import com.lanyuanxiaoyao.service.executor.core.TaskContext;
import com.lanyuanxiaoyao.service.executor.task.entity.Prisoner;
import com.lanyuanxiaoyao.service.executor.task.functions.police.PrisonerSink;
import com.lanyuanxiaoyao.service.executor.task.functions.police.PulsarMessage2Prisoner;
import com.lanyuanxiaoyao.service.executor.task.functions.pulsar.ReadPulsarSource;
import com.lanyuanxiaoyao.service.executor.task.helper.ArgumentsHelper;
import com.lanyuanxiaoyao.service.executor.task.helper.FlinkHelper;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.RichReduceFunction;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Mini Hudi
 *
 * @author lanyuanxiaoyao
 */
public class HoodiePolice {
    private static final Logger logger = LoggerFactory.getLogger(HoodiePolice.class);

    public static void main(String[] args) throws Exception {
        TaskContext taskContext = ArgumentsHelper.getContext(args);
        logger.info("Context: {}", taskContext);

        ArgumentsHelper.checkMetadata(taskContext, "pulsar_url");
        String pulsarUrl = (String) taskContext.getMetadata().get("pulsar_url");
        ArgumentsHelper.checkMetadata(taskContext, "pulsar_topic");
        String pulsarTopic = (String) taskContext.getMetadata().get("pulsar_topic");
        ArgumentsHelper.checkMetadata(taskContext, "start_time");
        Long startTime = (Long) taskContext.getMetadata().get("start_time");
        ArgumentsHelper.checkMetadata(taskContext, "end_time");
        Long endTime = (Long) taskContext.getMetadata().get("end_time");
        ArgumentsHelper.checkMetadata(taskContext, "primary_keys");
        String[] primaryKeys = ((String) taskContext.getMetadata().get("primary_keys")).split(",");
        ArgumentsHelper.checkMetadata(taskContext, "partition_keys");
        String[] partitionKeys = ((String) taskContext.getMetadata().get("partition_keys")).split(",");

        StreamExecutionEnvironment environment = FlinkHelper.getBatchEnvironment();
        environment.setParallelism(30);
        environment
                .fromSource(new ReadPulsarSource(taskContext, pulsarUrl, pulsarTopic), WatermarkStrategy.noWatermarks(), "Read pulsar")
                .disableChaining()
                .map(new PulsarMessage2Prisoner(startTime, endTime, primaryKeys, partitionKeys))
                .filter(ObjectUtil::isNotNull)
                .keyBy(Prisoner::getPartition)
                .keyBy(Prisoner::getKey)
                .addSink(new PrisonerSink(taskContext));
        environment.execute();
    }
}
