2
13693261870
2022-09-16 653761a31dfeb50dd3d007e892d69c90bf0cdafc
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package com.landtool.lanbase.modules.res.controller;
 
import java.io.IOException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Collectors;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.landtool.lanbase.common.utils.HttpOperateUtils;
import com.landtool.lanbase.config.SysTemPropertyConfig;
import com.landtool.lanbase.modules.org.entity.OrgUnit;
import com.landtool.lanbase.modules.org.entity.OrgUser;
import com.landtool.lanbase.modules.org.redis.OrgUserDefUnitRedis;
import com.landtool.lanbase.modules.org.redis.OrgUserRedis;
import com.landtool.lanbase.modules.org.redis.SysSystemInfoRedis;
import com.landtool.lanbase.modules.org.service.OrgUnitService;
import com.landtool.lanbase.modules.org.service.OrgUserService;
import com.landtool.lanbase.modules.res.entity.Res_ActionRecord;
import com.landtool.lanbase.modules.res.entity.Res_MainInfo;
import com.landtool.lanbase.modules.res.entity.ZiYuanTongJi.GroupByMuLu;
import com.landtool.lanbase.modules.res.entity.ZiYuanTongJi.GroupByZiYuanLeiXing;
import com.landtool.lanbase.modules.res.service.JitMonitorService;
import com.landtool.lanbase.modules.res.service.ResActionRecordService;
import com.landtool.lanbase.modules.res.service.ResApplyRecommendService;
import com.landtool.lanbase.modules.res.service.ResCatalogService;
import com.landtool.lanbase.modules.res.service.ResMainInfoService;
import com.landtool.lanbase.modules.res.service.ResProblemFeedbackService;
import com.landtool.lanbase.modules.res.service.ZiYuanMuLuService;
import com.landtool.lanbase.modules.sys.controller.AbstractController;
import com.landtool.lanbase.modules.sys.entity.SysSysteminfo;
import com.landtool.lanbase.modules.sys.service.SysFieldvalueService;
 
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GlobalMemory;
 
@Controller
@RequestMapping("monitor/jitmonitor")
public class JitmonitorController extends AbstractController {
 
    @Autowired
    private SysTemPropertyConfig sysConfig;
    
    @Autowired
    private OrgUserRedis orgUserRedis;
    
    @Autowired
    SysSystemInfoRedis  sysSystemInfoRedis;
    
    @Autowired
    private OrgUserDefUnitRedis orgUserDefUnitRedis;
 
    @Autowired
    private JitMonitorService jitMonitorService;
    
    @Autowired
    private ResMainInfoService resMainInfoService;
    
    @Autowired
    private ResProblemFeedbackService resProblemFeedbackService;
    
    @Autowired
    private ResCatalogService resCatalogService;
    
    @Autowired
    private SysFieldvalueService FieldUtils;
    
    @Autowired
    private OrgUnitService orgUnitService;
    
    @Autowired
    private OrgUserService orgUserService;
    
    @Autowired
    private ResApplyRecommendService resApplyRecommendService;
    
    @Autowired
    private ResActionRecordService resActionRecordService;
 
    @Autowired
    public ZiYuanMuLuService ziYuanMuLuService;
    
    public static String strMuLu = "";
 
     /**
     * 资源中心
     * @param model
     * @return
     */
    @RequestMapping("index")
    @ResponseBody
    public String newZiYuanZhongXinIndex(Model model) {
       Map<String,Object> map=new HashMap<String, Object>();
        LinkedHashMap<String, String> ResourceTypeList = FieldUtils.getFieldListByKey("ResourceType");// 获取资源类型列表
        // 资源类型饼状统计图
        List<GroupByZiYuanLeiXing> resourceclassList = resMainInfoService.selectResMainInfoGroupByLeiXing();
        
        Integer resourcecount=resMainInfoService.getAllTotal();
        String AllSeries = "";
        // 多于4个类型的话数据显示前7个资源数量最多的类型,后面的都以其它统计
        if (resourceclassList.size() > 4) {
            int counter = 0;
            for (int i = 4; i < resourceclassList.size(); i++) {
                counter += resourceclassList.get(i).getCount();
            }
            for (int i = 0; i < 4; i++) {
                GroupByZiYuanLeiXing item = resourceclassList.get(i);
                String leixing = ResourceTypeList.get(item.getResourceclass());
                AllSeries += (AllSeries == "" ? "" : ",") + "{ value: " + item.getCount() + ", name: '" + leixing + "'}";
            }
            AllSeries += (AllSeries == "" ? "" : ",") + "{ value: " + counter + ", name: '" + "其它" + "'}";
        }
        // 少于等于4个类型就全部显示
        else {
            for (int i = 0; i < resourceclassList.size(); i++) {
                GroupByZiYuanLeiXing item = resourceclassList.get(i);
                String leixing = ResourceTypeList.get(item.getResourceclass());
                AllSeries += (AllSeries == "" ? "" : ",") + "{ value: " + item.getCount() + ", name: '" + leixing + "'}";
            }
        }
        map.put("resourceclassSeries", "[" + AllSeries + "]");
 
   
 
        // 资源目录统计图
        List<GroupByMuLu> catlogidList = resMainInfoService.selectResMainInfoGroupByMuLu();
        String MuLuLegends = "";
        String MuLuSeries = "";
        for (int i = 0; i < catlogidList.size(); i++) {
            GroupByMuLu item = catlogidList.get(i);
            //            if (item.getCatlogCode().equals("0"))
            //                continue;
            strMuLu = "";
            //            String legend = GetBianMu(item.getCatlogCode() == null ? 0 : Integer.parseInt(item.getCatlogCode()));
            MuLuLegends += (MuLuLegends == "" ? "" : ",") + "'" + item.getTitle() + "'";
            MuLuSeries += (MuLuSeries == "" ? "" : ",") + item.getCount();
        }
        map.put("MuLuLegends", "[" + MuLuLegends + "]");
        map.put("MuLuSeries", "[" + MuLuSeries + "]");
 
        // 资源审核状态统计图
        map.put("shenhezhuangtaiSeries", "[" + ziYuanMuLuService.getZYTJ_ShenHeZhuangTai() + "]");
 
        //共享协议统计图
        map.put("sharprotocolSeries", "[" + ziYuanMuLuService.getGongXiangXieYiChart() + "]");
        map.put("resourcecount",resourcecount);
 
 
 
 
        return JSONObject.toJSONStringWithDateFormat(map, "yyyy-MM-dd HH:mm:ss", SerializerFeature.PrettyFormat);
    }
    /**
     * 资源推荐
     */
    private int getMainInfoRecommendCount() {
        Long userid = getUserId();
        Long unitId = orgUserService.getDefaultUnit(userid).getUnitid();
        int count = 0;
        //获取关键字
        List<String> keyWords = resActionRecordService.selectKeyWordsByUserid(Integer.parseInt(userid.toString()));
        if (keyWords != null && keyWords.size() > 0) {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("userid", userid);
            paramMap.put("isDiTuFuwu", null);
            List<Res_MainInfo> resourceList = resActionRecordService.selectMainInfoRecommends(paramMap);
            count = resourceList != null && resourceList.size() > 0 ? count = resourceList.size() : 0;
        }
        return count;
    }
 
    @RequestMapping("/queryResActionRecordByZyId")
    @ResponseBody
    public String selectResActionRecordByResourceId(Integer resourceid,Integer page,Integer limit){
        Map<String,Object> param=new HashMap<>();
        param.put("page", page);
        param.put("limit", limit);
        param.put("resourceid", resourceid);
        List<Res_ActionRecord> list=jitMonitorService.selectResActionRecordByResourceId(param);
        for(Res_ActionRecord map: list){
            map.setUsername(orgUserService.getChinesename(map.getUserid()));
        }
       
        JSONObject json=new JSONObject();
        json.put("total", list.size());
        json.put("data", list);
        return JSONObject.toJSONStringWithDateFormat(json, "yyyy-MM-dd HH:mm:ss", SerializerFeature.PrettyFormat);
    }
    @RequestMapping("warning")
    @ResponseBody
    public String warnlogshow() {
        List<Map> list=resProblemFeedbackService.getlatestalarm();     
        JSONObject json=new JSONObject();
        json.put("total", list.size());
        json.put("data", list);
        return JSONObject.toJSONStringWithDateFormat(json, "yyyy-MM-dd HH:mm:ss", SerializerFeature.PrettyFormat);
    }
    @RequestMapping("serverstatus")
    @ResponseBody
    public String serverstatus() throws InterruptedException {
        JSONObject json=new JSONObject();
        json.put("cpuinfo", printlnCpuInfo());
        json.put("meminfo", MemInfo());
        return JSONObject.toJSONStringWithDateFormat(json, "yyyy-MM-dd HH:mm:ss", SerializerFeature.PrettyFormat);
        
    }
    
    @RequestMapping("getGisResNumByResStatus")
    @ResponseBody
    public String getGisResNumByResStatus() throws InterruptedException {
        JSONObject json=new JSONObject();
        Map<String,Object> map=new HashMap<>();
        map.put("actionType", "调用");
      List<Map>     activeResources=jitMonitorService.getActiveResource(map);
        json.put("resStatus", jitMonitorService.getGisResNumByResStatus());
        json.put("activeResource", activeResources);
        json.put("activeResourceNum", activeResources.size());
        return JSONObject.toJSONStringWithDateFormat(json, "yyyy-MM-dd HH:mm:ss", SerializerFeature.PrettyFormat);
    }
    
    /**
     * 统计调用服务次数最多的前10个用户
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("getTop10UserAndUnit")
    @ResponseBody
    public String calTop10UserForInvokingResource(String actionType) throws InterruptedException {
        //获取用户
         Map<String,Object> map=new HashMap<>();
        map.put("actionType", actionType);
        return JSONObject.toJSONStringWithDateFormat(jitMonitorService.calTop10UserForInvokingResource(map), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue);
    }
    
    
   
    
    /**
     * 统计调用服务次数最多的前10个单位
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("getTop10Sys")
    @ResponseBody
    public String calTop10SysForInvokingResource(String actionType) throws InterruptedException { 
         Map<String,Object> map=new HashMap<>();
        map.put("actionType", actionType);
        return JSONObject.toJSONStringWithDateFormat(jitMonitorService.calTop10SysForInvokingResource(map), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue);    
    }
    
    /**
     * 日志统计 res_action_
     */
    @RequestMapping("/assignLogChart")
    public String assignLogChart() {
        
        
        return null;
 
    }
 
    
    /**
     * 查询热门资源前10
     * @params actionType 操作类型
     * @return
     * @throws InterruptedException
     */
    @RequestMapping("getHotZyByActionType")
    @ResponseBody
    public String getTop10ResourceByActionType(String actionType) throws InterruptedException { 
        Map<String,Object> map=new HashMap<>();
        map.put("actionType", actionType);
        //List<Map<String,Object>>  list=jitMonitorService.getHotZy();
        return JSONObject.toJSONStringWithDateFormat(jitMonitorService.getTop10ResourceByActionType(map), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue);    
    }
 
 
  
    
    @RequestMapping("getResourceAssignTimesCount")
    @ResponseBody
    public String getResourceAssignTimesCount(Integer resourceid) throws InterruptedException {
        Map<String, Object> map=new HashMap<>();
        map.put("actionType", "调用");
        map.put("resourceid", resourceid);
        List<Map>  list=jitMonitorService.getResourceAssignTimesCount(map);
        JSONObject json=new JSONObject();
        json.put("data", list);
        return JSONObject.toJSONStringWithDateFormat(json, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue);
        
    }
    @RequestMapping("queryResourceLog")
    @ResponseBody
    public String queryResourceLog(Integer resourceid) throws InterruptedException {
        Map<String, Object> map=new HashMap<>();
        map.put("actionType", "调用");
        map.put("resourceid", resourceid);
        List<Map>  list=jitMonitorService.queryResourceLog(map);
        JSONObject json=new JSONObject();
        json.put("data", list);
        return JSONObject.toJSONStringWithDateFormat(json, "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue);
        
    }
    
    
    private JSONObject printlnCpuInfo() throws InterruptedException {
        //map.put("----------------cpu信息----------------");
        SystemInfo systemInfo = new SystemInfo();
        CentralProcessor processor = systemInfo.getHardware().getProcessor();
        long[] prevTicks = processor.getSystemCpuLoadTicks();
        // 睡眠1s
        TimeUnit.SECONDS.sleep(1);
        long[] ticks = processor.getSystemCpuLoadTicks();
        long nice = ticks[CentralProcessor.TickType.NICE.getIndex()] - prevTicks[CentralProcessor.TickType.NICE.getIndex()];
        long irq = ticks[CentralProcessor.TickType.IRQ.getIndex()] - prevTicks[CentralProcessor.TickType.IRQ.getIndex()];
        long softirq = ticks[CentralProcessor.TickType.SOFTIRQ.getIndex()] - prevTicks[CentralProcessor.TickType.SOFTIRQ.getIndex()];
        long steal = ticks[CentralProcessor.TickType.STEAL.getIndex()] - prevTicks[CentralProcessor.TickType.STEAL.getIndex()];
        long cSys = ticks[CentralProcessor.TickType.SYSTEM.getIndex()] - prevTicks[CentralProcessor.TickType.SYSTEM.getIndex()];
        long user = ticks[CentralProcessor.TickType.USER.getIndex()] - prevTicks[CentralProcessor.TickType.USER.getIndex()];
        long iowait = ticks[CentralProcessor.TickType.IOWAIT.getIndex()] - prevTicks[CentralProcessor.TickType.IOWAIT.getIndex()];
        long idle = ticks[CentralProcessor.TickType.IDLE.getIndex()] - prevTicks[CentralProcessor.TickType.IDLE.getIndex()];
        long totalCpu = user + nice + cSys + idle + iowait + irq + softirq + steal;
        JSONObject map=new JSONObject();
      
    
        map.put("cpu核数", processor.getLogicalProcessorCount());
        map.put("cpu系统使用率", new DecimalFormat("#.##%").format(cSys * 1.0 / totalCpu));
        map.put("cpu用户使用率", new DecimalFormat("#.##%").format(user * 1.0 / totalCpu));
        map.put("cpu当前等待率", new DecimalFormat("#.##%").format(iowait * 1.0 / totalCpu));
        map.put("cpu当前使用率", new DecimalFormat("#.##%").format(1.0-(idle * 1.0 / totalCpu)));
        return map;
    }
 
    public JSONObject MemInfo(){
        JSONObject map=new JSONObject();
        SystemInfo systemInfo = new SystemInfo();
        GlobalMemory memory = systemInfo.getHardware().getMemory();
        //总内存
        long totalByte = memory.getTotal();
        //剩余
        long acaliableByte = memory.getAvailable();
        map.put("总内存", formatByte(totalByte));
        map.put("使用", formatByte(totalByte-acaliableByte));
        map.put("剩余内存 ", formatByte(acaliableByte));
        map.put("使用率", new DecimalFormat("#.##%").format((totalByte-acaliableByte)*1.0/totalByte));
        return map;
    }
    public static String formatByte(long byteNumber){
        //换算单位
        double FORMAT = 1024.0;
        double kbNumber = byteNumber/FORMAT;
        if(kbNumber<FORMAT){
            return new DecimalFormat("#.##KB").format(kbNumber);
        }
        double mbNumber = kbNumber/FORMAT;
        if(mbNumber<FORMAT){
            return new DecimalFormat("#.##MB").format(mbNumber);
        }
        double gbNumber = mbNumber/FORMAT;
        if(gbNumber<FORMAT){
            return new DecimalFormat("#.##GB").format(gbNumber);
        }
        double tbNumber = gbNumber/FORMAT;
        return new DecimalFormat("#.##TB").format(tbNumber);
    }
 
    @RequestMapping("warningIdList")
    @ResponseBody
    public String warningIdList() {
        List<Map> getlatestalarm = resProblemFeedbackService.getlatestalarm();
        List<Object> resourceids = resProblemFeedbackService.getlatestalarm().stream().map(map -> map.get("RESOURCEID")).collect(Collectors.toList());
        JSONObject json=new JSONObject();
        json.put("data", resourceids);
        return JSONObject.toJSONString(json);
    }
}