付延余
2022-12-16 f0f8ee8c4a945adbc742d9bab69382b28ad311fb
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
package com.wgcloud.service;
 
import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wgcloud.entity.AppInfo;
import com.wgcloud.entity.HostGroup;
import com.wgcloud.mapper.AppInfoMapper;
import com.wgcloud.mapper.AppStateMapper;
import com.wgcloud.util.DateUtil;
import com.wgcloud.util.HostUtil;
import com.wgcloud.util.UUIDUtil;
import com.wgcloud.util.staticvar.StaticKeys;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @version v3.3
 * @ClassName:AppInfoService.java
 * @author: http://www.wgstart.com
 * @date: 2021年1月16日
 * @Description: AppInfoService.java
 * @Copyright: 2019-2021 wgcloud. All rights reserved.
 */
@Service
public class AppInfoService {
 
    private static final Logger logger = LoggerFactory.getLogger(AppInfoService.class);
 
    public PageInfo selectByParams(Map<String, Object> params, int currPage, int pageSize) throws Exception {
        PageHelper.startPage(currPage, pageSize);
        List<AppInfo> list = appInfoMapper.selectByParams(params);
        PageInfo<AppInfo> pageInfo = new PageInfo<AppInfo>(list);
        return pageInfo;
    }
 
    public void save(AppInfo AppInfo, HttpServletRequest request) throws Exception {
        AppInfo.setId(UUIDUtil.getUUID());
        Date nowDate = DateUtil.getNowTime();
        AppInfo.setCreateTime(nowDate);
        //第一次保存,初始化一些数据
        AppInfo.setThreadsNum("0");
        AppInfo.setCpuPer(0d);
        AppInfo.setMemPer(0d);
        AppInfo.setGatherPid("获取中");
        AppInfo.setReadBytes("0");
        AppInfo.setWritesBytes("0");
        AppInfo.setAppTimes("获取中");
        if (!StringUtils.isEmpty(AppInfo.getAppPid())) {
            AppInfo.setAppPid(AppInfo.getAppPid().trim());
        }
        appInfoMapper.save(AppInfo);
        //保存页面动态添加的进程数据
        addExtDataForm(AppInfo, request, nowDate);
    }
 
    /**
     * 保存页面动态添加的进程数据
     *
     * @param appInfo
     * @param request
     */
    private void addExtDataForm(AppInfo appInfo, HttpServletRequest request, Date nowDate) throws Exception {
        String dataFromIndex = request.getParameter("dataFromIndex");
        int rowsLen = 0;
        if (!StringUtils.isEmpty(dataFromIndex)) {
            for (int i = 0; i <= Integer.valueOf(dataFromIndex); i++) {
 
 
                String appPid = request.getParameter("appPid_" + i);
                String appName = request.getParameter("appName_" + i);
                if (StringUtils.isEmpty(appPid) || StringUtils.isEmpty(appName)) {
                    continue;
                }
                AppInfo appInfoExt = new AppInfo();
                BeanUtil.copyProperties(appInfo, appInfoExt, true);
                appInfoExt.setId(UUIDUtil.getUUID());
                appInfoExt.setCreateTime(nowDate);
                appInfoExt.setAppPid(appPid.trim());
                appInfoExt.setAppName(appName.trim());
                appInfoMapper.save(appInfoExt);
 
                saveLog(request, StaticKeys.LOG_ADD, appInfoExt);
                rowsLen += 1;
            }
        }
    }
 
    public int deleteByHostName(Map<String, Object> map) throws Exception {
        return appInfoMapper.deleteByHostName(map);
    }
 
    @Transactional
    public void saveRecord(List<AppInfo> recordList) throws Exception {
        if (recordList.size() < 1) {
            return;
        }
        for (AppInfo as : recordList) {
            as.setId(UUIDUtil.getUUID());
        }
        appInfoMapper.insertList(recordList);
    }
 
    public int countByParams(Map<String, Object> params) throws Exception {
        return appInfoMapper.countByParams(params);
    }
 
    @Transactional
    public int deleteById(String[] id) throws Exception {
        return appInfoMapper.deleteById(id);
    }
 
    @Transactional
    public void updateRecord(List<AppInfo> recordList) throws Exception {
        if (recordList.size() < 1) {
            return;
        }
        appInfoMapper.updateList(recordList);
    }
 
    public void downByHostName(List<String> recordList) throws Exception {
        if (recordList.size() < 1) {
            return;
        }
        appInfoMapper.downByHostName(recordList);
    }
 
    public void updateById(AppInfo AppInfo)
            throws Exception {
        appInfoMapper.updateById(AppInfo);
    }
 
    public AppInfo selectById(String id) throws Exception {
        return appInfoMapper.selectById(id);
    }
 
    public List<AppInfo> selectAllByParams(Map<String, Object> params) throws Exception {
        return appInfoMapper.selectAllByParams(params);
    }
 
    /**
     * 设置资源分组 begin
     *
     * @param recordList
     */
    public List<HostGroup> setGroupInList(List<AppInfo> recordList) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("groupType", StaticKeys.HOST_GROUP_1);
        List<HostGroup> hostGroupList = hostGroupService.selectAllByParams(params);
        //查询所有主机分组列表,封装为map
        Map<String, String> hostGroupMap = new HashMap<>();
        for (HostGroup hostGroup : hostGroupList) {
            hostGroupMap.put(hostGroup.getId(), hostGroup.getGroupName());
        }
        for (AppInfo appInfo : recordList) {
            if (!StringUtils.isEmpty(appInfo.getGroupId())) {
                appInfo.setGroupId(hostGroupMap.get(appInfo.getGroupId()));
            }
        }
        return hostGroupList;
    }
 
    /**
     * 保存操作日志
     *
     * @param request 获取当前登录用户
     * @param action  操作标识
     */
    public void saveLog(HttpServletRequest request, String action, AppInfo appInfo) {
        if (null == appInfo) {
            return;
        }
        logInfoService.save(HostUtil.getAccountByRequest(request).getAccount() + action + "进程监测信息:" + appInfo.getHostname() + "," + appInfo.getAppName(),
                "获取进程方法:" + appInfo.getAppPid(), StaticKeys.LOG_XTCZ);
    }
 
    @Autowired
    private AppInfoMapper appInfoMapper;
    @Autowired
    private AppStateMapper appStateMapper;
    @Resource
    private HostGroupService hostGroupService;
    @Autowired
    private LogInfoService logInfoService;
 
 
}