package com.fshark.overseas.advert.processor;


import com.fshark.overseas.advert.modle.metrics.ActivationMetrics;
import com.fshark.overseas.advert.modle.metrics.BaseMetrics;
import com.google.gson.Gson;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

public class ActivationResultWindows implements WindowFunction<ActivationMetrics, ActivationMetrics, String, TimeWindow> {
    @Override
    public void apply(String s, TimeWindow window, Iterable<ActivationMetrics> input, Collector<ActivationMetrics> out) throws Exception {
        BaseMetrics baseMetrics = BaseMetrics.setKey(s);
        ActivationMetrics activationMetrics = copyMetrics(baseMetrics);
        ActivationMetrics accumulator = input.iterator().next();
        activationMetrics.setData_activation(accumulator.getData_activation());
        activationMetrics.setData_register(accumulator.getData_register());
        activationMetrics.setData_device_register(accumulator.getData_device_register());
        activationMetrics.setData_create_role(accumulator.getData_create_role());
        activationMetrics.setData_day_act_register(accumulator.getData_day_act_register());
        activationMetrics.setData_day_register_create(accumulator.getData_day_register_create());
        activationMetrics.setData_active_day_dau(accumulator.getData_active_day_dau());
        out.collect(activationMetrics);
    }

    private ActivationMetrics copyMetrics(BaseMetrics metrics) {
        Gson gson = new Gson();
        return gson.fromJson(gson.toJson(metrics), ActivationMetrics.class);
    }
}
