管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-10-25 4ddfd502023662f6d25c4be416d88751e206d91a
src/main/java/com/lf/server/service/all/ScheduleService.java
@@ -1,9 +1,14 @@
package com.lf.server.service.all;
import com.lf.server.entity.all.RedisCacheKey;
import com.lf.server.entity.sys.LoginEntity;
import com.lf.server.entity.sys.OperateEntity;
import com.lf.server.entity.sys.ResEntity;
import com.lf.server.entity.sys.UserEntity;
import com.lf.server.helper.FileHelper;
import com.lf.server.helper.StringHelper;
import com.lf.server.service.sys.LoginService;
import com.lf.server.service.sys.OperateService;
import com.lf.server.service.sys.ResService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -36,15 +41,18 @@
    private ResService resService;
    @Autowired
    private LoginService loginService;
    @Autowired
    private OperateService operateService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
    private static final Log log = LogFactory.getLog(ScheduleService.class);
    /**
     * 查询服务器状态
     *
     * @return
     * @throws InterruptedException
     */
    public JSONObject selectServerStatus() throws InterruptedException {
        JSONObject json = new JSONObject();
@@ -56,9 +64,6 @@
    /**
     * 查询Cpu信息
     *
     * @return
     * @throws InterruptedException
     */
    public JSONObject selectCpuInfo() throws InterruptedException {
        SystemInfo systemInfo = new SystemInfo();
@@ -90,8 +95,6 @@
    /**
     * 查询内存信息
     *
     * @return
     */
    public JSONObject selectMemInfo() {
        JSONObject map = new JSONObject();
@@ -103,9 +106,9 @@
        // 剩余
        long surplusByte = memory.getAvailable();
        map.put("totalMem", StringHelper.formatByte(totalByte));
        map.put("use", StringHelper.formatByte(totalByte - surplusByte));
        map.put("remainMem", StringHelper.formatByte(surplusByte));
        map.put("totalMem", FileHelper.formatByte(totalByte));
        map.put("use", FileHelper.formatByte(totalByte - surplusByte));
        map.put("remainMem", FileHelper.formatByte(surplusByte));
        map.put("usage", new DecimalFormat("#.##%").format((totalByte - surplusByte) * 1.0 / totalByte));
        return map;
@@ -113,8 +116,6 @@
    /**
     * 查询在线用户
     *
     * @return
     */
    public List<JSONObject> selectOnlineUsers() {
        List<JSONObject> list = new ArrayList<JSONObject>();
@@ -122,7 +123,7 @@
        Set<String> keys = redisTemplate.keys(RedisCacheKey.signUserKey("*"));
        for (String key : keys) {
            Object obj = redisTemplate.opsForValue().get(key);
            if (obj != null && obj instanceof UserEntity) {
            if (obj instanceof UserEntity) {
                UserEntity ue = (UserEntity) obj;
                JSONObject map = new JSONObject();
@@ -139,33 +140,100 @@
    /**
     * 查询服务资源状态
     *
     * @return
     */
    public List<ResEntity> selectResStatus() {
        List<ResEntity> list = new ArrayList<ResEntity>();
        List<ResEntity> resList = resService.selectResAll();
        return testResStatus(resList);
    }
    private List<ResEntity> testResStatus(List<ResEntity> resList) {
        List<ResEntity> list = new ArrayList<ResEntity>();
        for (ResEntity re : resList) {
            Socket socket = new Socket();
            try {
                if (StringHelper.isEmpty(re.getServer())) {
                    list.add(re);
                    continue;
                }
                URI uri = new URI(re.getServer());
                SocketAddress add = new InetSocketAddress(uri.getHost(), uri.getPort() == -1 ? 80 : uri.getPort());
                // Ping通地址
                socket.connect(add, 2000);
            } catch (Exception e) {
                log.error(e.getStackTrace());
                log.error(e.getMessage());
                list.add(re);
            } finally {
                try {
                    socket.close();
                } catch (Exception e) {
                    log.error(e.getStackTrace());
                } catch (Exception ex) {
                    log.error(ex.getMessage());
                }
            }
        }
        return list;
    }
    /**
     * 统计服务资源状态
     */
    public JSONObject countResStatus() {
        List<ResEntity> resList = resService.selectResAll();
        List<ResEntity> unableList = testResStatus(resList);
        JSONObject jsonObject = new JSONObject();
        // 资源可用
        jsonObject.put("resAbleCount", resList.size() - unableList.size());
        // 资源不可用
        jsonObject.put("resUnableCount", unableList.size());
        return jsonObject;
    }
    /**
     * 资源操作状态
     */
    public JSONObject operateCount() {
        List<OperateEntity> list = operateService.operateCount();
        JSONObject jsonObject = new JSONObject();
        if (list.isEmpty()) {
            return null;
        } else {
            List<JSONObject> lister = new ArrayList<JSONObject>();
            for (OperateEntity key : list) {
                JSONObject map = new JSONObject();
                map.put("count", key.getCount());
                map.put("modular2", key.getModular2());
                lister.add(map);
            }
            jsonObject.put("operateCount", lister);
            return jsonObject;
        }
    }
    /**
     * 用户登录状态
     */
    public JSONObject userLoginCount() {
        List<LoginEntity> list = loginService.selectLoginCounts();
        if (list == null || list.isEmpty()) {
            return null;
        }
        JSONObject jsonObject = new JSONObject();
        List<JSONObject> lister = new ArrayList<JSONObject>();
        for (LoginEntity key : list) {
            JSONObject map = new JSONObject();
            map.put("count", key.getCount());
            map.put("optime", key.getOptime());
            lister.add(map);
        }
        jsonObject.put("userLoginCount", lister);
        return jsonObject;
    }
}