package com.fshark.overseas.advert.app;

import com.fshark.overseas.advert.modle.AppsFlyerEvent;
import com.fshark.overseas.advert.modle.EventTypeEnum;
import com.fshark.overseas.advert.processor.ParseEventProcessor;
import com.fshark.overseas.advert.sink.EventInfoSink;
import com.fshark.overseas.advert.sink.EventInfoSinkProcess;
import com.fshark.overseas.advert.util.AppConfig;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.restartstrategy.RestartStrategies;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaConsumer;

import java.util.Properties;
import java.util.concurrent.TimeUnit;

public class AppsflyerInfoDmi {

    static AppConfig loadConfigFromArgs(String[] args) {
        ParameterTool parameters = ParameterTool.fromArgs(args);
        AppConfig appConfig = new AppConfig();
        try {
            appConfig.setSourceKafkaBootstrapServers(parameters.get("source-kafka-bootstrap-servers"));
            appConfig.setSourceKafkaGroupId(parameters.get("source-kafka-group-id"));
            appConfig.setSourceKafkaTopic(parameters.get("source-kafka-topic"));

            appConfig.setDbHost(parameters.get("jdbc-host"));
            appConfig.setDbUser(parameters.get("jdbc-user"));
            appConfig.setDbPassword(parameters.get("jdbc-password"));

            appConfig.setSinkType(parameters.get("sink-type"));

        } catch (Exception e) {
            System.err.println("Place set args: " +
                    " --source-kafka-bootstrap-servers <source-kafka-bootstrap-servers>" +
                    " --source-kafka-group-id <source-kafka-group-id>" +
                    " --source-kafka-topic <source-kafka-topic>" +
                    " --sink-kafka-bootstrap-servers <sink-kafka-bootstrap-servers>" +
                    " --sink-kafka-group-id <sink-kafka-group-id>" +
                    " --sink-kafka-topic <sink-kafka-topic>" +
                    " --jdbc-host <jdbc-host> " +
                    " --jdbc-user <jdbc-user> " +
                    " --jdbc-password <jdbc-password> ");
            throw e;
        }
        return appConfig;
    }

    public static void main(String[] args) throws Exception {
        AppConfig appConfig = loadConfigFromArgs(args);
        final StreamExecutionEnvironment sse = StreamExecutionEnvironment.getExecutionEnvironment();
        sse.setParallelism(1);
        sse.enableCheckpointing(1000 * 60 * 10);
        sse.setRestartStrategy(RestartStrategies.fixedDelayRestart(Integer.MAX_VALUE, org.apache.flink.api.common.time.Time.of(10, TimeUnit.SECONDS)));

        //source设置
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", appConfig.getSourceKafkaBootstrapServers());
        properties.setProperty("group.id", appConfig.getSourceKafkaGroupId());
//        properties.setProperty("auto.offset.reset", "earliest");
        FlinkKafkaConsumer<String> dataConsumer = new FlinkKafkaConsumer<>(appConfig.getSourceKafkaTopic(), new SimpleStringSchema(), properties);

        DataStream<AppsFlyerEvent> adEventDataStream = sse.addSource(dataConsumer)
                .flatMap(new ParseEventProcessor())
                .filter(new FilterFunction<AppsFlyerEvent>() {
                    @Override
                    public boolean filter(AppsFlyerEvent value) throws Exception {
                        if (value.getMedia_source().contains("rank") || value.getCampaign_id().contains("rank")) {
                            return false;
                        }
                        return true;
                    }
                })
                .uid("parse-event")
                .name("parse-event")
                .shuffle();

        adEventDataStream.filter(EventTypeEnum::notHeartBeatEvent).windowAll(TumblingProcessingTimeWindows.of(Time.seconds(5)))
                .process(new EventInfoSinkProcess(appConfig))
                .uid("event")
                .name("event");

        sse.execute("appsflyer-analyze");
    }
}
