1
13693261870
2022-09-16 fee60c3e25fac0982f3b8cb8feea7225c4ed22f8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package com.terra.proxy.controller;
 
 
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.terra.proxy.bean.BlackRule;
import com.terra.proxy.bean.VistorBean;
import com.terra.proxy.service.Impl.LogServiceImpl;
import com.terra.proxy.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
 
@Controller
@RequestMapping("/log")
@CrossOrigin
public class LoggerController {
 
 
    private Logger log = LoggerFactory.getLogger(LoggerController.class);
    /**
     * 查询每日访问日志统计图
     *
     * @param 时间,为空取今日
     */
 
    @Autowired
    LogServiceImpl logservice;
 
    @RequestMapping(value = "/getDailyEchart", method = RequestMethod.GET)
    @ResponseBody
    public String getDailyEchart(HttpServletRequest request, String starttime, String endtime) {
        Map<String, Object> param = new HashMap<>();
        Map<String, Object> result = new HashMap<>();
        param.put("starttime", starttime);
        param.put("endtime", endtime);
        List<Map> list = logservice.getTotalCountPerHour(param);
        List<Map> resultlist = new ArrayList<>();
        List<Map> infolist = logservice.getVisitInfoPerHourByip(param);
        JSONObject json = new JSONObject();
 
        for (Map map : infolist) {
            Map<String, Object> tempmap = new HashMap<>();
            tempmap.put(map.get("requestip").toString(), map.get("count").toString());
            if (result.containsKey(map.get("caltime").toString())) {
                List<Map> tlist = (List<Map>) result.get(map.get("caltime").toString());
                tlist.add(tempmap);
            } else {
                List<Map> templist = new ArrayList<Map>();
                templist.add(tempmap);
                result.put(map.get("caltime").toString(), templist);
            }
        }
 
        for (Map map : list) {
            Map<String, Object> tmap = new HashMap<>();
            tmap.put("name", map.get("caltime").toString());
            Object[] obj = new Object[3];
            obj[0] = map.get("caltime").toString();
            obj[1] = map.get("sum").toString();
 
            tmap.put("value", obj);
            if (result.containsKey(map.get("caltime").toString())) {
                tmap.put("info", result.get(map.get("caltime").toString()));
            }
            resultlist.add(tmap);
        }
        return JSONUtil.toJsonStr(resultlist);
    }
 
 
    @RequestMapping(value = "/querylog", method = RequestMethod.GET)
    @ResponseBody
    public String querylog(HttpServletRequest request, HttpServletResponse httpresponse, String ip, Integer id, String result,
                           Date starttime, Date endtime, String servername, Integer resourceid, String fromsys, Integer page, Integer limit) {
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> resultmap = new HashMap<>();
        params.put("ip", ip);
        params.put("id", id);
        params.put("result", result);
        params.put("servername", servername);
        params.put("resourceid", resourceid);
        params.put("fromsys", fromsys);
        params.put("starttime", starttime);
        params.put("endtime", endtime);
        params.put("offSet", (page - 1) * limit);
        params.put("limit", limit);
        List<Map> list = logservice.querylog(params);
        int total = logservice.queryTotalLog(params);
        resultmap.put("data", list);
        resultmap.put("total", total);
        return JSONUtil.toJsonStr(resultmap);
    }
 
    @RequestMapping(value = "/error", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView error(String result) {
        ModelAndView mv = new ModelAndView();
        JSONObject json = JSONObject.parseObject(result);
 
        mv.addObject("message", json.get("msg"));
        mv.addObject("status", json.get("code"));
 
        mv.setViewName("error");
        return mv;
    }
 
    @RequestMapping(value = "/queryBlackLists")
    @ResponseBody
    public Result queryBlackLists(Integer limit, Integer page, String ip, String status) {
        int total = 0;
 
        HashMap<String, Object> param = new HashMap<>();
        param.put("limit", limit);
        param.put("page", (page - 1) * limit);
        param.put("ip", ip == "" ? null : ip);
        param.put("status", status == "" ? null : status);
        List<VistorBean> data = logservice.queryBlackLists(param);
        total = logservice.getTotalBlackList(param);
        return Result.ok().put("data", data).put("total", total).put("totalpage", Math.ceil((double) total / (double) limit));
    }
 
    @RequestMapping(value = "/delBlack")
    @ResponseBody
    public Result delBlackById(VistorBean vistorBean) {
        logservice.delBlackById(vistorBean);
        return Result.ok();
    }
 
    @RequestMapping(value = "/updateBlackRule")
    @ResponseBody
    public Result updateBlackRule(@RequestBody BlackRule blackRule) {
        logservice.updateRules(blackRule);
        return Result.ok();
    }
 
    @RequestMapping(value = "/selectBalckRule")
    @ResponseBody
    public Result selectBalckRule() {
        BlackRule blackRule = logservice.selectBlackRule();
        return Result.ok().put("data", blackRule);
    }
 
    @RequestMapping(value = "/selectWhiteList")
    @ResponseBody
    public Result selectWhiteList(Integer limit, Integer page, String ip, String status) {
        int total = 0;
        page = page == null ? 0 : page;
        HashMap<String, Object> param = new HashMap<>();
        param.put("limit", limit);
        param.put("page", (page - 1) * limit);
        param.put("ip", ip == "" ? null : ip);
        param.put("status", status == "" ? null : status);
        List<VistorBean> data = logservice.selectWhiteList(param);
        total = logservice.getTotalWhiteList(param);
        return Result.ok().put("data", data).put("total", total).put("totalpage", Math.ceil((double) total / (double) limit));
    }
 
    @RequestMapping(value = "/updateWhite")
    @ResponseBody
    public Result updateWhite(VistorBean vistorBean) {
        //先移除黑名单 status 1,2
        logservice.delBlackById(vistorBean);
        //再加入白名单 status 3,4
        logservice.updateWhite(vistorBean);
 
        return Result.ok();
    }
}