|
|
@@ -1,5 +1,7 @@
|
|
|
package cn.iaun.it.controller;
|
|
|
|
|
|
+import cn.iaun.it.model.ItMonitorData;
|
|
|
+import cn.iaun.it.model.ItMonitorItem;
|
|
|
import cn.iaun.it.model.vo.monitor.ItMonitorApiDataResourceInfoVo;
|
|
|
import cn.iaun.it.model.vo.monitor.controller.ItMonitorApiDataSetReqVo;
|
|
|
import cn.iaun.it.service.ItMonitorService;
|
|
|
@@ -12,8 +14,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
|
|
|
@RestController
|
|
|
@RequestMapping("/api/monitor")
|
|
|
@@ -27,10 +28,10 @@ public class ItMonitorApiController {
|
|
|
@PostMapping("/data/set")
|
|
|
public Result<?> setMonitorData(HttpServletRequest request, @Validated @RequestBody List<ItMonitorApiDataSetReqVo> itMonitorApiDataSetReqVos) {
|
|
|
String ip = request.getRemoteAddr();
|
|
|
- System.out.println(ip);
|
|
|
// map: resourceId -> INFO
|
|
|
HashMap<Long, ItMonitorApiDataResourceInfoVo> resourceInfos = new HashMap<>();
|
|
|
-
|
|
|
+ HashMap<Long, HashMap<String, ItMonitorItem>> allItems = new HashMap<>();
|
|
|
+ List<ItMonitorData> resultList = new ArrayList<>();
|
|
|
for (ItMonitorApiDataSetReqVo itMonitorApiDataSetReqVo : itMonitorApiDataSetReqVos) {
|
|
|
// 获取资源信息
|
|
|
ItMonitorApiDataResourceInfoVo resourceInfo;
|
|
|
@@ -39,6 +40,7 @@ public class ItMonitorApiController {
|
|
|
if (resourceInfo == null || resourceInfo.getItemGroupId() == null) {
|
|
|
return new Result<>(ResultCode.FAILED_PARAM_INVALID);
|
|
|
}
|
|
|
+ // 缓存到map
|
|
|
resourceInfos.put(itMonitorApiDataSetReqVo.getResourceId(), resourceInfo);
|
|
|
} else {
|
|
|
resourceInfo = resourceInfos.get(itMonitorApiDataSetReqVo.getResourceId());
|
|
|
@@ -49,9 +51,52 @@ public class ItMonitorApiController {
|
|
|
) {
|
|
|
return new Result<>(ResultCode.AGENT_KEY_ERROR);
|
|
|
}
|
|
|
+ HashMap<String, ItMonitorItem> itemMap;
|
|
|
+ String fullGroupName = "group" + resourceInfo.getItemGroupId() + "." + itMonitorApiDataSetReqVo.getItemKeyName();
|
|
|
|
|
|
-
|
|
|
+ if (!allItems.containsKey(resourceInfo.getItemGroupId())) {
|
|
|
+ itemMap = new HashMap<>();
|
|
|
+ List<ItMonitorItem> items = itMonitorService.getAllItemByGroupId(resourceInfo.getItemGroupId());
|
|
|
+ if (items.isEmpty()) {
|
|
|
+ return new Result<>(ResultCode.FAILED_PARAM_INVALID);
|
|
|
+ }
|
|
|
+ for (ItMonitorItem itMonitorItem : items) {
|
|
|
+ itemMap.put(itMonitorItem.getItemKeyName(), itMonitorItem);
|
|
|
+ }
|
|
|
+ allItems.put(resourceInfo.getItemGroupId(), itemMap);
|
|
|
+ } else {
|
|
|
+ itemMap = allItems.get(resourceInfo.getItemGroupId());
|
|
|
+ }
|
|
|
+ if (!itemMap.containsKey(fullGroupName)) {
|
|
|
+ return new Result<>(ResultCode.FAILED_PARAM_INVALID);
|
|
|
+ }
|
|
|
+ ItMonitorData res = new ItMonitorData();
|
|
|
+ Date now = new Date();
|
|
|
+ res.setResourceId(itMonitorApiDataSetReqVo.getResourceId());
|
|
|
+ res.setItemId(itemMap.get(fullGroupName).getItemId());
|
|
|
+ res.setRecordTime(now);
|
|
|
+ res.setData(itMonitorApiDataSetReqVo.getData());
|
|
|
+ resultList.add(res);
|
|
|
+ }
|
|
|
+ // 检查是否有重复
|
|
|
+ List<String> strList = new ArrayList<>();
|
|
|
+ for (ItMonitorData itMonitorData : resultList) {
|
|
|
+ String txt = itMonitorData.getItemId().toString() + itMonitorData.getItemId().toString();
|
|
|
+ if (strList.contains(txt)) {
|
|
|
+ return new Result<>(ResultCode.FAILED_PARAM_INVALID);
|
|
|
+ }
|
|
|
+ strList.add(txt);
|
|
|
+ }
|
|
|
+ strList.clear();
|
|
|
+ HashSet<Integer> set = new HashSet<>();
|
|
|
+ for (ItMonitorData itMonitorData : resultList) {
|
|
|
+ int result = itMonitorService.addData(itMonitorData);
|
|
|
+ System.out.println(result);
|
|
|
+ set.add(result);
|
|
|
+ }
|
|
|
+ if (set.size() == 1 && set.contains(1)) {
|
|
|
+ return new Result<>(ResultCode.SUCCESS);
|
|
|
}
|
|
|
- return null;
|
|
|
+ return new Result<>(ResultCode.FAILED);
|
|
|
}
|
|
|
}
|