|
|
@@ -1,23 +1,16 @@
|
|
|
package cn.reghao.bnt.web.devops.machine.service;
|
|
|
|
|
|
-import cn.reghao.bnt.common.msg.constant.NodeStatus;
|
|
|
-import cn.reghao.bnt.web.devops.machine.db.repository.MachineHostRepository;
|
|
|
import cn.reghao.bnt.web.devops.machine.db.repository.MachineInfoRepository;
|
|
|
import cn.reghao.bnt.web.devops.machine.model.po.MachineHost;
|
|
|
import cn.reghao.bnt.web.devops.machine.model.po.MachineInfo;
|
|
|
-import cn.reghao.bnt.web.devops.machine.model.po.MachineStat;
|
|
|
import cn.reghao.bnt.web.devops.machine.model.vo.MachineHostVO;
|
|
|
import cn.reghao.jutil.jdk.converter.DateTimeConverter;
|
|
|
-import cn.reghao.jutil.jdk.db.PageBound;
|
|
|
import cn.reghao.jutil.jdk.db.PageList;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import javax.annotation.PostConstruct;
|
|
|
-import java.util.Collections;
|
|
|
-import java.util.Comparator;
|
|
|
import java.util.List;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.concurrent.ConcurrentHashMap;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -26,89 +19,38 @@ import java.util.stream.Collectors;
|
|
|
*/
|
|
|
@Service
|
|
|
public class MachineService {
|
|
|
- // machineId -> MachineStat
|
|
|
- private final static Map<String, MachineStat> agents = new ConcurrentHashMap<>();
|
|
|
private final MachineInfoRepository machineInfoRepository;
|
|
|
- private final MachineHostRepository machineHostRepository;
|
|
|
|
|
|
- public MachineService(MachineHostRepository machineHostRepository, MachineInfoRepository machineInfoRepository) {
|
|
|
- this.machineHostRepository = machineHostRepository;
|
|
|
+ public MachineService(MachineInfoRepository machineInfoRepository) {
|
|
|
this.machineInfoRepository = machineInfoRepository;
|
|
|
}
|
|
|
|
|
|
- //@PostConstruct
|
|
|
- private void loadFromDataSource() {
|
|
|
- machineHostRepository.findAll().forEach(machineHost -> {
|
|
|
- String machineId = machineHost.getMachineId();
|
|
|
- MachineStat machineStat = new MachineStat(machineHost, NodeStatus.Offline.name());
|
|
|
- agents.put(machineId, machineStat);
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
- public void flushToDataSource() {
|
|
|
- List<MachineInfo> machineInfos = agents.values().stream()
|
|
|
- .map(machineStat -> machineStat.getMachineHost().getMachineInfo())
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
- if (!machineInfos.isEmpty()) {
|
|
|
- machineInfoRepository.saveAll(machineInfos);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public void agentStart(MachineHost machineHost) {
|
|
|
- String machineId = machineHost.getMachineId();
|
|
|
- MachineStat machineStat = agents.get(machineId);
|
|
|
- if (machineStat != null) {
|
|
|
- machineStat.setStatus(NodeStatus.Online.name());
|
|
|
- } else {
|
|
|
- machineStat = new MachineStat(machineHost, NodeStatus.Online.name());
|
|
|
- agents.put(machineId, machineStat);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
public void agentShutdown(String machineId) {
|
|
|
- MachineStat machineStat = agents.get(machineId);
|
|
|
- machineStat.setStatus(NodeStatus.Offline.name());
|
|
|
- }
|
|
|
-
|
|
|
- public void removeAgent(String machineId) {
|
|
|
- agents.remove(machineId);
|
|
|
+ MachineInfo machineInfo = machineInfoRepository.findByMachineId(machineId);
|
|
|
+ if (machineInfo != null) {
|
|
|
+ machineInfo.setStat(1);
|
|
|
+ machineInfoRepository.save(machineInfo);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public PageList<MachineHostVO> getByPage(String env, int pageNumber, int pageSize) {
|
|
|
- List<MachineStat> list = agents.values().stream()
|
|
|
- .filter(machineStat -> machineStat.getMachineHost().getEnv().equals(env))
|
|
|
- // 降序
|
|
|
- .sorted(Comparator.comparing(MachineStat::getBootTime).reversed())
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
- int total = list.size();
|
|
|
- if (total == 0) {
|
|
|
- return PageList.empty();
|
|
|
- }
|
|
|
-
|
|
|
- PageBound pageBound = PageBound.getPageBound(pageNumber, pageSize, total);
|
|
|
- List<MachineHostVO> list1 = list.subList(pageBound.getStart(), pageBound.getEnd()).stream()
|
|
|
- .map(machineStat -> {
|
|
|
- MachineHost machineHost = machineStat.getMachineHost();
|
|
|
- String status = machineStat.getStatus();
|
|
|
- if (status.equals(NodeStatus.Offline.name())) {
|
|
|
- return new MachineHostVO(machineHost);
|
|
|
- } else {
|
|
|
- String bootTime = DateTimeConverter.format(machineHost.getMachineInfo().getBootTime()*1000);
|
|
|
- return new MachineHostVO(machineHost, bootTime);
|
|
|
- }
|
|
|
- })
|
|
|
- .collect(Collectors.toList());
|
|
|
- return PageList.pageList(pageNumber, pageSize, total, list1);
|
|
|
+ PageRequest pageRequest = PageRequest.of(pageNumber-1, pageSize);
|
|
|
+ Page<MachineInfo> machineInfos = machineInfoRepository.findAll(pageRequest);
|
|
|
+ List<MachineHostVO> list1 = machineInfos.stream().map(machineInfo -> {
|
|
|
+ MachineHost machineHost = new MachineHost(machineInfo);
|
|
|
+ long bootTime = machineInfo.getBootTime();
|
|
|
+ String bootTimeStr = DateTimeConverter.format(bootTime*1000);
|
|
|
+ return new MachineHostVO(machineHost, bootTimeStr);
|
|
|
+ }).collect(Collectors.toList());
|
|
|
+ return PageList.pageList(pageNumber, pageSize, (int) machineInfos.getTotalElements(), list1);
|
|
|
}
|
|
|
|
|
|
public boolean isAgentOnline(String machineId) {
|
|
|
- MachineStat machineStat = agents.get(machineId);
|
|
|
- return machineStat.getStatus().equals(NodeStatus.Online.name());
|
|
|
- }
|
|
|
+ MachineInfo machineInfo = machineInfoRepository.findByMachineId(machineId);
|
|
|
+ if (machineInfo == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- public List<String> getOnlineAgents() {
|
|
|
- return Collections.emptyList();
|
|
|
+ return machineInfo.getStat() == 1;
|
|
|
}
|
|
|
}
|