package com.fshark.overseas.advert.processor.server;


import com.fshark.overseas.advert.modle.metrics.BaseMetrics;
import com.fshark.overseas.advert.modle.metrics.BaseServerMetrics;
import com.fshark.overseas.advert.modle.metrics.ServerActivationMetrics;
import com.google.gson.Gson;
import org.apache.flink.api.java.tuple.Tuple4;
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 ServerActivationResultWindows implements WindowFunction<Tuple4<Long, Long, Long, Long>, ServerActivationMetrics, String, TimeWindow> {
    @Override
    public void apply(String s, TimeWindow window, Iterable<Tuple4<Long, Long, Long, Long>> input, Collector<ServerActivationMetrics> out) throws Exception {
        BaseServerMetrics baseMetrics = BaseServerMetrics.setKey(s);
        ServerActivationMetrics activationMetrics = copyMetrics(baseMetrics);
        Tuple4<Long, Long, Long, Long> next = input.iterator().next();
        activationMetrics.setData_register(next.f0);
        activationMetrics.setData_create_role(next.f1);
        activationMetrics.setData_day_register_create(next.f2);
        activationMetrics.setData_active_day_dau(next.f3);
        out.collect(activationMetrics);
    }

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