package com.sagacloud.endpoints;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sagacloud.endpointService.AccessLogService;
import com.sagacloud.utils.CommonUtil;
import com.sagacloud.utils.DpUtils;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;
import java.util.Map;

@Path("access_log")
public class AccessLogEnd {
    @Path("/query")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    public String query(JSONObject post) {
        try{
            JSONArray pathArr = post.getJSONArray("path");
            JSONObject time = post.getJSONObject("time");
            JSONArray logContent = AccessLogService.getData(pathArr, time);
            // 时间(精确到小时), <接口, 次数>
            Map<String, Map<String, Integer>> map = AccessLogService.processData(logContent);
            JSONArray result = AccessLogService.getReturnResult(map);
            return DpUtils.returnJSONArray(result, "");
        }catch (Exception ex){
            ex.printStackTrace();
            return DpUtils.errorJsonMsg(ex.getMessage());
        }
    }
}
