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 map=new HashMap(); LinkedHashMap ResourceTypeList = FieldUtils.getFieldListByKey("ResourceType");// 获取资源类型列表 // 资源类型饼状统计图 List 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 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 keyWords = resActionRecordService.selectKeyWordsByUserid(Integer.parseInt(userid.toString())); if (keyWords != null && keyWords.size() > 0) { Map paramMap = new HashMap(); paramMap.put("userid", userid); paramMap.put("isDiTuFuwu", null); List 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 param=new HashMap<>(); param.put("page", page); param.put("limit", limit); param.put("resourceid", resourceid); List 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 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 map=new HashMap<>(); map.put("actionType", "调用"); List 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 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 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 map=new HashMap<>(); map.put("actionType", actionType); //List> 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 map=new HashMap<>(); map.put("actionType", "调用"); map.put("resourceid", resourceid); List 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 map=new HashMap<>(); map.put("actionType", "调用"); map.put("resourceid", resourceid); List 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 getlatestalarm = resProblemFeedbackService.getlatestalarm(); List resourceids = resProblemFeedbackService.getlatestalarm().stream().map(map -> map.get("RESOURCEID")).collect(Collectors.toList()); JSONObject json=new JSONObject(); json.put("data", resourceids); return JSONObject.toJSONString(json); } }