Skip to content

Commit

Permalink
全局概览
Browse files Browse the repository at this point in the history
  • Loading branch information
lgt1126 committed Aug 24, 2021
1 parent cfb4b82 commit 6344822
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ String hostUpdate(@ParamName("hostid") String hostid,
@JsonPath("/host/host.get")
String hostDetail(@ParamName("hostid") String hostid);

/**
* 查询主机
*
* @param host 主机ID
*/
@Post
@JsonPath("/host/host.get")
String hostGet(@ParamName("host") String host);

/**
* 更新主机宏
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,14 @@ String updateTrapperItem(@ParamName("itemid") String itemid,
@Post
@JsonPath("/item/item.get")
String getItemInfo(@ParamName("itemId") String itemId, @ParamName("hostid") String hostid);

/**
* 根据item name 获取 ITEM 信息
*
* @param key key
* @return String
*/
@Post
@JsonPath("/item/item.get")
String getItemList(@ParamName("key") String key, @ParamName("hostid") String hostid);
}
11 changes: 9 additions & 2 deletions zeus-driver/src/main/resources/api-json/host/host.get.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@
"jsonrpc": "2.0",
"method": "host.get",
"params": {
<#if groupids??>
"filter": {
"host":${host}
},
</#if>
<#if groupids??>
"hostids":${hostid},
</#if>
"output": "extend",
"selectTags":"extend",
"selectMacros":"extend",
"selectValueMaps":"extend",
"hostids":${hostid}
"selectValueMaps":"extend"
},
"auth": "${userAuth}",
"id": 1
Expand Down
19 changes: 12 additions & 7 deletions zeus-driver/src/main/resources/api-json/item/item.get.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
"selectTags":"extend",
"selectValueMap":"extend",
"selectPreprocessing":"extend",
<#if key??>
"search":{
"key_":"${key}"
},
</#if>
<#if itemId??>
"itemids": "${itemId}",
</#if>
<#if hostid??>
"hostids": "${hostid}",
</#if>
"output": [
"itemid",
"hostid",
Expand All @@ -15,13 +26,7 @@
"units",
"valuemapid",
"interfaceid"
],
<#if itemId??>
"itemids": "${itemId}"
</#if>
<#if hostid??>
"hostids": "${hostid}"
</#if>
]
},
"auth": "${userAuth}",
"id": 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public class HistoryController {

@RequestMapping("/query")
public ResponseData qeuryHistory(@Validated @RequestBody HistoryParam historyParam) {
return ResponseData.success(historyService.qeuryHistory(historyParam));
return ResponseData.success(historyService.queryHistory(historyParam));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.zmops.iot.web.analyse.controller;

import com.zmops.iot.model.response.ResponseData;
import com.zmops.iot.web.analyse.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
* @author yefei
*
* 全局概览
**/
@RestController
@RequestMapping("/home")
public class HomeController {

@Autowired
HomeService homeService;

@RequestMapping("/collectonRate")
public ResponseData collectonRate(){
return ResponseData.success(homeService.collectonRate());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
Expand All @@ -28,11 +31,11 @@ public class HistoryService {
@Autowired
ZbxHistoryGet zbxHistoryGet;

public List<LatestDto> qeuryHistory(HistoryParam historyParam) {
return qeuryHistory(historyParam.getDeviceId(), historyParam.getAttrIds(), historyParam.getTimeFrom(), historyParam.getTimeTill());
public List<LatestDto> queryHistory(HistoryParam historyParam) {
return queryHistory(historyParam.getDeviceId(), historyParam.getAttrIds(), historyParam.getTimeFrom(), historyParam.getTimeTill());
}

public List<LatestDto> qeuryHistory(Long deviceId, List<Long> attrIds, Long timeFrom, Long timeTill) {
public List<LatestDto> queryHistory(Long deviceId, List<Long> attrIds, Long timeFrom, Long timeTill) {
//查询出设备
Device one = new QDevice().deviceId.eq(deviceId).findOne();
if (null == one || ToolUtil.isEmpty(one.getZbxId())) {
Expand All @@ -57,8 +60,7 @@ public List<LatestDto> qeuryHistory(Long deviceId, List<Long> attrIds, Long time
}
//根据属性值类型 分组查询历史数据
for (Map.Entry<String, List<ProductAttribute>> map : valueTypeMap.entrySet()) {
String res = zbxHistoryGet.historyGet(one.getZbxId(), zbxIds, 1000, Integer.parseInt(map.getKey()), timeFrom, timeTill);
latestDtos.addAll(JSONObject.parseArray(res, LatestDto.class));
latestDtos.addAll(queryHitoryData(one.getZbxId(), zbxIds, Integer.parseInt(map.getKey()), timeFrom, timeTill));
}

latestDtos.forEach(latestDto -> {
Expand All @@ -69,4 +71,10 @@ public List<LatestDto> qeuryHistory(Long deviceId, List<Long> attrIds, Long time

return latestDtos;
}

public List<LatestDto> queryHitoryData(String hostId, List<String> itemIds, Integer valueType, Long timeFrom, Long timeTill) {
String res = zbxHistoryGet.historyGet(hostId, itemIds, 1000, valueType, timeFrom, timeTill);
return JSONObject.parseArray(res, LatestDto.class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.zmops.iot.web.analyse.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zmops.iot.util.LocalDateTimeUtils;
import com.zmops.iot.util.ToolUtil;
import com.zmops.iot.web.analyse.dto.LatestDto;
import com.zmops.zeus.driver.entity.ZbxItemInfo;
import com.zmops.zeus.driver.service.ZbxHost;
import com.zmops.zeus.driver.service.ZbxItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
* @author yefei
* <p>
* 全局概览服务
**/
@Service
public class HomeService {

@Autowired
private ZbxHost zbxHost;

@Autowired
private ZbxItem zbxItem;

@Autowired
HistoryService historyService;

private static String hostId = "";

private static Map<String, String> ITEM_Map = new ConcurrentHashMap<>(5);

//Zbx 指标取数速率 key
private static final String KEY = "zabbix[wcache,values";

public Map<String, List<LatestDto>> collectonRate() {
if (ToolUtil.isEmpty(hostId)) {
if (ToolUtil.isEmpty(getZbxServerId())) {
return new HashMap<>(0);
}
}
if (ToolUtil.isEmpty(ITEM_Map)) {
if (ToolUtil.isEmpty(getItemMap())) {
return new HashMap<>(0);
}
}
List<String> itemIds = new ArrayList<>(ITEM_Map.keySet());
List<LatestDto> latestDtos = historyService.queryHitoryData(hostId, itemIds, 0, LocalDateTimeUtils.getSecondsByTime(LocalDateTimeUtils.minu(LocalDateTime.now(),
7, ChronoUnit.DAYS)), LocalDateTimeUtils.getSecondsByTime(LocalDateTime.now()));

latestDtos.forEach(latestDto -> {
if (null != ITEM_Map.get(latestDto.getItemid())) {
latestDto.setName(ITEM_Map.get(latestDto.getItemid()));
}
});

return latestDtos.parallelStream().collect(Collectors.groupingBy(LatestDto::getName));
}

private String getZbxServerId() {
String response = zbxHost.hostGet("Zabbix server");
List<Map<String, String>> ids = JSON.parseObject(response, List.class);
if (null != ids && ids.size() > 0) {
hostId = ids.get(0).get("hostid");
return hostId;
}
return "";
}

private Map<String, String> getItemMap() {
String itemList = zbxItem.getItemList(KEY, hostId);
List<ZbxItemInfo> itemInfos = JSONObject.parseArray(itemList, ZbxItemInfo.class);
for (ZbxItemInfo itemInfo : itemInfos) {
ITEM_Map.put(itemInfo.getItemid(), formatName(itemInfo.getName()));
}
return ITEM_Map;
}

private static String formatName(String name) {
return name.substring(35, name.length() - 18);
}

}

0 comments on commit 6344822

Please sign in to comment.