forked from zmops/zeus-iot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
174 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
zeus-webapp/src/main/java/com/zmops/iot/web/analyse/controller/HomeController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
zeus-webapp/src/main/java/com/zmops/iot/web/analyse/service/HomeService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |