package com.fshark.overseas.advert;


import com.fshark.overseas.advert.modle.AppsFlyerEvent;
import com.fshark.overseas.advert.modle.EventTypeEnum;
import com.fshark.overseas.advert.modle.metrics.ActivationMetrics;
import com.fshark.overseas.advert.modle.metrics.RemainMetrics;
import com.fshark.overseas.advert.processor.*;
import com.fshark.overseas.advert.sink.ActicitionSink;
import com.fshark.overseas.advert.sink.PurchaseSink;
import com.fshark.overseas.advert.sink.RemainSink;
import com.fshark.overseas.advert.util.AppConfig;
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.datastream.SingleOutputStreamOperator;
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 AppsflyerAnalyze {
    static AppConfig loadConfigFromArgs(String[] args) throws Exception {
        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"));

        } 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>" +
                    " --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);
        // TODO: 2021/8/10  设置消费开始的时间戳1628524944000
        dataConsumer.setStartFromTimestamp(1628524944000L);
        DataStream<AppsFlyerEvent> adEventDataStream = sse.addSource(dataConsumer)
                .flatMap(new ParseEventProcessor())
                .uid("parse-event")
                .name("parse-event")
                .shuffle();

//        adEventDataStream.print();

        //广告指标(激活)
        DataStream<ActivationMetrics> activitionMetricStream = adEventDataStream
                .filter(EventTypeEnum::installEvent)
                .flatMap(new ActivitionProcess(appConfig))
                .uid("activition-event")
                .name("activition-event");


        DataStream<ActivationMetrics> createRoleMetricStream = adEventDataStream
                .filter(EventTypeEnum::createRoleEvent)
                .flatMap(new CreateRoleProcess(appConfig))
                .uid("create-role-event")
                .name("create-role-event");

        //注册\留存
        DataStream<ActivationMetrics> loginRoleMetricStream = adEventDataStream
                .filter(EventTypeEnum::roleLoginEvent)
                .flatMap(new RoleLoginProcess(appConfig))
                .uid("role-login-event")
                .name("role-login-event");

        //角色注册激活预聚合
        DataStream<ActivationMetrics> activitionResult = activitionMetricStream
                .union(loginRoleMetricStream, createRoleMetricStream)
                .keyBy(ActivationMetrics::getKey)
                .window(TumblingProcessingTimeWindows.of(Time.minutes(1)))
                .aggregate(new ActivationAggregateFunction(), new ActivationResultWindows())
                .uid("activation-agg-event")
                .name("activation-agg-event");

        //      角色注册激活写入
        activitionResult.addSink(new ActicitionSink(appConfig))
                .uid("login-sink-event")
                .name("login-sink-event");
//
//        //指标详细记录
        activitionMetricStream.union(loginRoleMetricStream, createRoleMetricStream)
                .timeWindowAll(Time.minutes(1)).process(new StoreMetricProcess(appConfig))
                .uid("metric-detail")
                .name("metric-detail");

////        留存预聚合
        DataStream<RemainMetrics> remainResult = loginRoleMetricStream.flatMap(new RemainProcess())
                .keyBy(RemainMetrics::getRemind_key)
                .window(TumblingProcessingTimeWindows.of(Time.minutes(1)))
                .sum("user_num")
                .uid("remain-event")
                .name("remain-event");
        //留存写入
        remainResult.addSink(new RemainSink(appConfig))
                .uid("remain-sink-event")
                .name("remain-sink-event");

        //        订单信息
        adEventDataStream.filter(EventTypeEnum::orderEvent)
                .addSink(new PurchaseSink(appConfig))
                .uid("order-event")
                .name("order-event");

        sse.execute("appsflyer-analyze");

    }
}
