package com.sagacloud.endpointService;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sagacloud.utils.CommonUtil;
import com.sagacloud.utils.tables.AccessLogTable;
import com.sagacloud.utils.tables.HBaseTable;

import java.util.HashMap;
import java.util.Map;

public class AccessLogService {

    public static JSONArray getData(JSONArray pathArr, JSONObject time) throws Exception {
        JSONArray criterias = new JSONArray();
        if(pathArr == null || pathArr.size() == 0){
            JSONObject criteria = new JSONObject();
            criteria.put(AccessLogTable.KEY_TIME, time);
            criterias.add(criteria);
        }else{
            for(int i = 0; i < pathArr.size(); ++i){
                JSONObject criteria = new JSONObject();
                criteria.put(AccessLogTable.KEY_PATH, pathArr.getString(i));
                criteria.put(AccessLogTable.KEY_TIME, time);
                criterias.add(criteria);
            }
        }
        JSONObject result = CommonUtil.combine_query(HBaseTable.access_log, criterias);
        return result.getJSONArray("Content");
    }

    public static Map<String, Map<String, Integer>> processData(JSONArray logContent) {
        // time, Map<path, times>
        Map<String, Map<String, Integer>> data = new HashMap<>();
        for(int i = 0; i < logContent.size(); ++i){
            JSONObject single = logContent.getJSONObject(i);
            putToDataMap(data, single);
        }
        return data;
    }

    private static void putToDataMap(Map<String, Map<String, Integer>> data, JSONObject single) {
        try {
            String time = single.getString(AccessLogTable.KEY_TIME).substring(0, 10);
            String path = single.getString(AccessLogTable.KEY_PATH);
            int count = single.getInteger(AccessLogTable.TIMES);
            Map<String, Integer> subMap = null;
            if (data.containsKey(time)){
                subMap = data.get(time);
            }else{
                subMap = new HashMap<>();
                data.put(time, subMap);
            }
            if(subMap.containsKey(path)){
                subMap.put(path, subMap.get(path) + count);
            }else
                subMap.put(path, count);
        }catch (Exception ex){

        }
    }

    public static JSONArray getReturnResult(Map<String, Map<String, Integer>> map) {
        JSONArray array = new JSONArray();
        for(String time : map.keySet()){
            Map<String, Integer> valuesMap = map.get(time);
            long totalCount = 0;
            for(String path : valuesMap.keySet()){
                JSONObject single = new JSONObject();
                single.put("path", path);
                single.put("time", time);
                single.put("times", valuesMap.get(path));
                array.add(single);
                totalCount += valuesMap.get(path);
            }
            JSONObject total = new JSONObject();
            total.put("path", "total");
            total.put("time", time);
            total.put("times", totalCount);
            array.add(total);
        }
        return array;
    }
}
