package com.sagacloud.utils.accessLog;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sagacloud.utils.CommonUtil;
import com.sagacloud.utils.DpUtils;
import com.sagacloud.utils.tables.AccessLogTable;
import com.sagacloud.utils.tables.HBaseTable;
import com.sagacloud.endpointService.pojo.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentLinkedQueue;

public class AccessLogRunnable implements Runnable {

    public static ConcurrentLinkedQueue<AccessLog> accessLogQueue = new ConcurrentLinkedQueue<>();
    // Map<接口路径, Map<项目id, 次数>>
    private Map<String, Map<String, Integer>> statistic = new HashMap<>();
    private final long delta = 6 * 60 * 1000;
    private String dpMark = DpUtils.generateUUID(CommonUtil.getActiveIpAddr() + "-", true).substring(0, 30);
    @Override
    public void run() {
        long lastSubmitTime = System.currentTimeMillis();
        while(true) {
            AccessLog log = accessLogQueue.poll();
            try {
                if (log == null) {
                    Thread.sleep(500);
                }else {
                    putToStatistic(statistic, log);
                }
                if(System.currentTimeMillis() - lastSubmitTime > delta){
                    submit();
                    lastSubmitTime = System.currentTimeMillis();
                }
            }catch (Exception ex){
                ex.printStackTrace();
            }
        }
    }

    private void submit() {
        JSONArray content = generateInsertArr();
        if(content.size() == 0)
            return;
        try {
            CommonUtil.insert(true, HBaseTable.access_log, content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private JSONArray generateInsertArr() {
        Map<String, Map<String, Integer>> tmpStatistic = statistic;
        statistic = new HashMap<>();
        String time = CommonUtil.getTime();
        JSONArray array = new JSONArray();
        for(String path : tmpStatistic.keySet()){
            Map<String, Integer> subMap = tmpStatistic.get(path);
            for(String pjId : subMap.keySet()) {
                JSONObject obj = new JSONObject();
                obj.put(AccessLogTable.KEY_PATH, path);
                obj.put(AccessLogTable.KEY_TIME, time);
                obj.put(AccessLogTable.KEY_PJ_ID, pjId);
                obj.put(AccessLogTable.KEY_DP_ID, dpMark);
                obj.put(AccessLogTable.TIMES, subMap.get(pjId));
                array.add(obj);
            }
        }
        return array;
    }

    private void putToStatistic(Map<String, Map<String, Integer>> statistic, AccessLog log) {
        Map<String, Integer> sub = null;
        if(statistic.containsKey(log.getPath())){
            sub = statistic.get(log.getPath());
        }else{
            sub = new HashMap<>();
            statistic.put(log.getPath(), sub);
        }
        if(sub.containsKey(log.getProject())){
            sub.put(log.getProject(), sub.get(log.getProject()) + 1);
        }else{
            sub.put(log.getProject(), 1);
        }
    }
}
