API数据表更新

This commit is contained in:
2025-08-25 23:23:36 +08:00
parent e366c836c6
commit cf1d83a0a2
2 changed files with 69 additions and 43 deletions

View File

@@ -4,10 +4,7 @@ package com.mini.capi.job;
import com.mini.capi.biz.domain.*;
import com.mini.capi.biz.service.*;
import com.mini.capi.model.ApiResult;
import com.mini.capi.utils.HostInfo;
import com.mini.capi.utils.vDate;
import com.mini.capi.utils.vId;
import com.mini.capi.utils.vToken;
import com.mini.capi.utils.*;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -21,7 +18,7 @@ import java.util.stream.Collectors;
@RestController
@RequestMapping("/Sys/jobs")
public class taskHostDisk {
public class taskEnable {
@Resource

View File

@@ -14,6 +14,7 @@ import com.mini.capi.utils.docker;
import com.mini.capi.utils.vDate;
import com.mini.capi.utils.vToken;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -22,6 +23,8 @@ import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;
@RestController
@@ -41,6 +44,10 @@ public class sysController {
private DockerContainerInfoService dockerInfoService;
@Resource
private Executor hostExecutor;
public static class SnapshotDTO {
public String hostName;
public String timestamp;
@@ -199,6 +206,54 @@ public class sysController {
}
private void handleSingleDockerHost(DockerHost host, List<String> errorList) {
try {
/* 1. 一次性加载现有数据 */
List<DockerContainerInfo> oldList =
dockerInfoService.lambdaQuery()
.eq(DockerContainerInfo::getDokerHostId, host.getDokerHostId())
.list();
Map<String, DockerContainerInfo> oldMap = oldList.stream()
.collect(Collectors.toMap(
c -> buildKey(c.getDokerHostId(), c.getImageName(), c.getUnames()),
c -> c));
/* 2. 远程最新数据 */
SshUser sshUser = sshUserService.getById(host.getUserId());
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
List<docker.DockerInfo> remoteList =
docker.getDockerInfo(sshInfo.getHostIp(),
Long.valueOf(sshInfo.getHostPort()),
sshUser.getCUsername(),
sshUser.getCPassword());
/* 3. 新增或更新 */
addOrEdit(host.getDokerHostId(), oldMap, sshInfo, remoteList);
/* 4. 删除已失效容器 */
if (!oldMap.isEmpty()) {
List<String> idsToDel = oldMap.values()
.stream()
.map(DockerContainerInfo::getId)
.collect(Collectors.toList());
dockerInfoService.removeByIds(idsToDel);
}
/* 5. 更新运行数 */
long runningCnt = remoteList.stream()
.filter(r -> "1".equals(r.getStatus()))
.count();
host.setRunNum(runningCnt);
host.setUpdateTime(vDate.getNow());
dockerHostService.updateById(host);
} catch (Exception e) {
errorList.add(String.format("hostId=%s, error=%s",
host.getDokerHostId(), e.getMessage()));
}
}
@GetMapping("/getApiInfo")
public ApiResult<List<SnapshotDTO>> getApiInfo(String token) {
if (vToken.isValidToken(token)) {
@@ -222,45 +277,19 @@ public class sysController {
* 获取容器列表
*/
@GetMapping("/getApiDockerInfo")
public ApiResult<?> getDockerInfo(String dockerHostId, String token) {
public ApiResult<?> getDockerInfo(String token) {
if (vToken.isValidToken(token)) {
DockerHost host = dockerHostService.getById(dockerHostId);
/* 1. 现有数据一次性加载到内存 */
List<DockerContainerInfo> oldList =
dockerInfoService.lambdaQuery()
.eq(DockerContainerInfo::getDokerHostId, dockerHostId)
.list();
Map<String, DockerContainerInfo> oldMap = oldList.stream()
.collect(Collectors.toMap(
c -> buildKey(c.getDokerHostId(), c.getImageName(), c.getUnames()),
c -> c));
/* 2. 远程最新数据 */
SshUser sshUser = sshUserService.getById(host.getUserId());
SshInfo sshInfo = sshInfoService.getById(host.getHostId());
List<docker.DockerInfo> remoteList =
docker.getDockerInfo(sshInfo.getHostIp(),
Long.valueOf(sshInfo.getHostPort()),
sshUser.getCUsername(),
sshUser.getCPassword());
/* 3. 新增 or 更新 */
addOrEdit(dockerHostId, oldMap, sshInfo, remoteList);
/* 4. 本轮未命中的即为“已失效”,批量删除 */
if (!oldMap.isEmpty()) {
List<String> idsToDel = oldMap.values()
.stream()
.map(DockerContainerInfo::getId)
.collect(Collectors.toList());
dockerInfoService.removeByIds(idsToDel);
}
/* 5. 更新运行数 */
long runningCnt = remoteList.stream()
.filter(r -> "1".equals(r.getStatus()))
.count();
host.setRunNum(runningCnt);
host.setUpdateTime(vDate.getNow());
dockerHostService.updateById(host);
List<DockerHost> dockerHosts = dockerHostService.list();
List<String> errorList = Collections.synchronizedList(new ArrayList<>());
CompletableFuture<?>[] futures = dockerHosts.stream()
.map(host -> CompletableFuture.runAsync(
() -> handleSingleDockerHost(host, errorList), hostExecutor))
.toArray(CompletableFuture[]::new);
// 等待全部执行完毕
CompletableFuture.allOf(futures).join();
return errorList.isEmpty()
? ApiResult.success()
: ApiResult.error();
}
return ApiResult.error();
}