whyclxw
2025-02-13 d62fa80f3d12a56ae60eeef45f07cea6477c2507
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
package com.whyc.webService;
 
import com.whyc.mapper.CommonMapper;
import com.whyc.pojo.PageParam2;
import com.whyc.service.BattInfService;
import com.whyc.service.PageParam2Service;
import com.whyc.service.UserService;
import com.whyc.util.DateUtil;
import com.whyc.util.SerialNumberUtil;
import com.whyc.webService.dto.request.Api;
import com.whyc.webService.dto.request.RequestParam;
import com.whyc.webService.dto.response.response1.Corporation;
import com.whyc.webService.dto.response.response1.ResponseResult;
import com.whyc.webService.dto.response.response2.ResponseResult2;
import com.whyc.webService.dto.response.response2.UserInfo;
import com.whyc.webService.util.XmlParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.servlet.ServletContext;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
 
@Component
@WebService(targetNamespace = "http://service.webService.whyc.com/",
        endpointInterface = "com.whyc.webService.IMS_Service")
public class IMS_ServiceImpl implements IMS_Service{
 
    @Autowired
    private UserService userService;
 
    @Autowired
    private PageParam2Service pageParam2Service;
 
    @Resource
    private CommonMapper commonMapper;
 
    @Autowired
    private BattInfService battInfService;
 
    @Autowired
    private ServletContext servletContext;
 
 
    /**
     *
     * @param param xml格式的字符串
     * @return xml格式的字符串
     */
    @Override
    public String getKPIValue(String param) {
        try {
 
            //解析传入参数
            RequestParam requestParam = XmlParser.parseXmlRequest(param);
            //地域代码,太供提供,作用不明
            String corporationCode = requestParam.getCorporationCode();
            //时间值,作用不明
            String time = requestParam.getTime();
            //指标
            List<Api> apis = requestParam.getApis();
            List<String> apiNameList = apis.stream().map(Api::getName).collect(Collectors.toList());
            if (apiNameList.contains("BusinessSystemLoginRoll")) {
                //单独调用,采用第二个返回值结构
                ResponseResult2 result2 = new ResponseResult2();
                List<UserInfo> userInfoList = new LinkedList<>();
                result2.setUserInfoList(userInfoList);
                HashMap<String, String> loginMap = (HashMap<String, String>) servletContext.getAttribute("login");
                if(loginMap != null) {
                    String dateStr = DateUtil.YYYY_MM_DD.format(new Date());
                    loginMap.values().removeIf(aString -> !aString.equals(dateStr));
                }
                if (loginMap != null && loginMap.size() != 0) {
 
                    loginMap.keySet().forEach(key -> {
                        UserInfo userInfo = new UserInfo();
                        userInfo.setName(key);
                        userInfo.setLdapId(key);
                        //省公司名称或国家电网公司总部 TODO 这个要与tg的人确认
                        userInfo.setCorporation("-");
                        //地市公司名称
                        userInfo.setSubCompany("-");
                        //县级单位名称
                        userInfo.setBureau("-");
                        //部门名称
                        userInfo.setDepartment("-");
                        userInfo.setIsLdapId(0);
                        userInfoList.add(userInfo);
                    });
                }
                return XmlParser.toXml2(result2);
 
            }
            else if(apiNameList.contains("BusinessSystemOnlineRoll")){
                //单独调用,采用第二个返回值结构
                ResponseResult2 result2 = new ResponseResult2();
                //统计更新时间在10秒钟内的
                HashMap<String, Long> onlineMap = (HashMap<String, Long>) servletContext.getAttribute("online");
                if(onlineMap != null) {
                    onlineMap.values().removeIf(aLong -> System.currentTimeMillis() - aLong > 10000);
                }
                if (onlineMap != null && onlineMap.size() != 0) {
                    List<UserInfo> userInfoList = new LinkedList<>();
                    onlineMap.keySet().forEach(key -> {
                        UserInfo userInfo = new UserInfo();
                        userInfo.setName(key);
                        userInfo.setLdapId(key);
                        //省公司名称或国家电网公司总部 TODO 这个要与tg的人确认
                        userInfo.setCorporation("-");
                        //地市公司名称
                        userInfo.setSubCompany("-");
                        //县级单位名称
                        userInfo.setBureau("-");
                        //部门名称
                        userInfo.setDepartment("-");
                        userInfo.setIsLdapId(0);
                        userInfoList.add(userInfo);
                    });
                    result2.setUserInfoList(userInfoList);
                }
                return XmlParser.toXml2(result2);
            }
            else {
                //构建返回参数
                ResponseResult result =  new ResponseResult();
                result.setStatus("success");
                result.setMessage("success");
                Corporation corporations = new Corporation();
                corporations.setId(corporationCode);
                result.setCorporation(corporations);
                List<com.whyc.webService.dto.response.response1.Api> apisResponse = new LinkedList<>();
 
                int size = apis.size();
                for (int i = 0; i < size; i++) {
                    switch (apis.get(i).getName()) {
                        //1注册用户数
                        case "BusinessUserRegNum":
                            //查询用户表的记录数
                            getItem1(apisResponse);
                            break;
                        //2在线用户数
                        case "BusinessSystemOnlineNum":
                            getItem2(apisResponse);
                            break;
                        //3日登录用户数
                        case "BusinessDayLoginNum":
                            getItem3(apisResponse);
                            break;
                        //4累计访问人数
                        case "BusinessVisitCount":
                            getItem4(apisResponse);
                            break;
                        //5.页面会话连接数(对外提供的页面连接会话个数-暂无)
                        case "BusinessSystemSessionNum":
                            getItem5(apisResponse);
                            break;
                        //6.系统服务响应时长(页面调用的接口调用时长,界面配合)
                        case "BusinessSystemResponseTime":
                            getItem6(apisResponse);
                            break;
                        //7.系统健康运行时长
                        case "BusinessSystemRunningTime":
                            getItem7(apisResponse);
                            break;
                        //8.业务应用系统占用表空间大小(/app 和/var/lib/mysql)
                        case "BusinessDataTableSpace":
                            getItem8(apisResponse);
                            break;
                        //9数据库平均响应时长
                        //这里的逻辑是5分钟内响应的接口的历史所有次数统计的平均值(目前未查到系统表有相关历史sql的每个时间统计)
                        case "BusinessSystemDBTime":
                            getItem9(apisResponse);
                            break;
                    }
                }
                corporations.setApis(apisResponse);
 
 
                return XmlParser.toXml(result);
            }
 
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
 
    private void getItem6(List<com.whyc.webService.dto.response.response1.Api> apisResponse) {
        long startTime = System.currentTimeMillis();
        battInfService.getStationIds();
        long endTime = System.currentTimeMillis();
        int timeGap = (int) (endTime - startTime);
        com.whyc.webService.dto.response.response1.Api apiResponse = new com.whyc.webService.dto.response.response1.Api();
        apiResponse.setName("BusinessSystemResponseTime");
        apiResponse.setValue(timeGap);
        apisResponse.add(apiResponse);
    }
 
    private static void getItem8(List<com.whyc.webService.dto.response.response1.Api> apisResponse) {
        Integer dataTableSpace = SerialNumberUtil.getDataTableSpace();
        com.whyc.webService.dto.response.response1.Api apiResponse = new com.whyc.webService.dto.response.response1.Api();
        apiResponse.setName("BusinessDataTableSpace");
        apiResponse.setValue(dataTableSpace);
        apisResponse.add(apiResponse);
    }
 
    private void getItem9(List<com.whyc.webService.dto.response.response1.Api> apisResponse) {
        Long systemDBTimeIn5Minutes = commonMapper.getSystemDBTimeIn5Minutes()/1000000000;
        com.whyc.webService.dto.response.response1.Api apiResponse = new com.whyc.webService.dto.response.response1.Api();
        apiResponse.setName("BusinessSystemDBTime");
        apiResponse.setValue(systemDBTimeIn5Minutes.intValue());
        apisResponse.add(apiResponse);
    }
 
    private void getItem7(List<com.whyc.webService.dto.response.response1.Api> apisResponse) {
        Long systemStartTime = (Long) servletContext.getAttribute("system_start_time");
        com.whyc.webService.dto.response.response1.Api apiResponse = new com.whyc.webService.dto.response.response1.Api();
        apiResponse.setName("BusinessSystemRunningTime");
        apiResponse.setValue((int) ((System.currentTimeMillis() - systemStartTime)/1000));
        apisResponse.add(apiResponse);
    }
 
    private static void getItem5(List<com.whyc.webService.dto.response.response1.Api> apisResponse) {
        com.whyc.webService.dto.response.response1.Api apiResponse = new com.whyc.webService.dto.response.response1.Api();
        apiResponse.setName("BusinessUserRegNum");
        apiResponse.setValue(0);
        apisResponse.add(apiResponse);
    }
 
    private void getItem4(List<com.whyc.webService.dto.response.response1.Api> apisResponse) {
        PageParam2 Param4visitCount = pageParam2Service.getById(5);
        com.whyc.webService.dto.response.response1.Api apiResponse = new com.whyc.webService.dto.response.response1.Api();
        apiResponse.setName("BusinessUserRegNum");
        apiResponse.setValue(Integer.valueOf(Param4visitCount.getValue()));
        apisResponse.add(apiResponse);
    }
 
    private void getItem3(List<com.whyc.webService.dto.response.response1.Api> apisResponse) {
        HashMap<String, String> loginMap = (HashMap<String, String>) servletContext.getAttribute("login");
        int loginQuantity = 0;
        if(loginMap != null){
            String dateStr = DateUtil.YYYY_MM_DD.format(new Date());
            loginMap.values().removeIf(aString -> !aString.equals(dateStr));
            loginQuantity = loginMap.size();
        }
        com.whyc.webService.dto.response.response1.Api apiResponse = new com.whyc.webService.dto.response.response1.Api();
        apiResponse.setName("BusinessDayLoginNum");
        apiResponse.setValue(loginQuantity);
        apisResponse.add(apiResponse);
    }
 
    private void getItem2(List<com.whyc.webService.dto.response.response1.Api> apisResponse) {
        HashMap<String, Long> onlineMap = (HashMap<String, Long>) servletContext.getAttribute("online");
        //统计更新时间在10秒钟内的
        int onlineQuantity = 0;
        if(onlineMap != null){
            onlineMap.values().removeIf(aLong -> System.currentTimeMillis() - aLong > 10000);
            onlineQuantity = onlineMap.size();
        }
        com.whyc.webService.dto.response.response1.Api apiResponse = new com.whyc.webService.dto.response.response1.Api();
        apiResponse.setName("BusinessSystemOnlineNum");
        apiResponse.setValue(onlineQuantity);
        apisResponse.add(apiResponse);
    }
 
    private void getItem1(List<com.whyc.webService.dto.response.response1.Api> apisResponse) {
        int userQuantity = userService.getAll().size();
        com.whyc.webService.dto.response.response1.Api apiResponse = new com.whyc.webService.dto.response.response1.Api();
        apiResponse.setName("BusinessUserRegNum");
        apiResponse.setValue(userQuantity);
        apisResponse.add(apiResponse);
    }
 
    /**
     *
     * @param param
     * @return
     */
    @Override
    public Integer getStatus(String param) {
        return 1;
    }
}