From 8d798afa88d5d948d4b5a7a7d3289206db5c728f Mon Sep 17 00:00:00 2001 From: fanshuai <1141904845@qq.com> Date: Wed, 6 Mar 2024 17:42:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E5=AE=9E=E4=BE=8B=E8=8A=82?= =?UTF-8?q?=E7=82=B9=E6=9F=A5=E8=AF=A2file=20,string=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E7=9A=84=E7=BB=93=E6=9E=9C=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../experiment/ExperimentInsController.java | 13 +++++ .../service/ExperimentInsService.java | 2 +- .../impl/ExperimentInsServiceImpl.java | 42 ++++++++++++++- .../service/impl/JupyterServiceImpl.java | 8 ++- .../com/ruoyi/platform/utils/FileUtil.java | 24 +++++++++ .../com/ruoyi/platform/utils/MinioUtil.java | 52 +++++++++++++++++++ 6 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/FileUtil.java diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/experiment/ExperimentInsController.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/experiment/ExperimentInsController.java index 62685eff..5f1d377d 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/experiment/ExperimentInsController.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/experiment/ExperimentInsController.java @@ -128,5 +128,18 @@ public class ExperimentInsController { + /** + * 查询实验节点结果 + * + * @return 运行日志 + */ + @GetMapping(("/nodeResult")) + @ApiOperation("查询实例节点结果") + public AjaxResult getNodeResult(@RequestParam("id") Integer id,@RequestParam("node_id") String nodeId) throws Exception { + return AjaxResult.success(this.experimentInsService.getNodeResult(id,nodeId)); + } + + + } diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ExperimentInsService.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ExperimentInsService.java index e86669bc..7d542160 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ExperimentInsService.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ExperimentInsService.java @@ -6,7 +6,6 @@ import org.springframework.data.domain.PageRequest; import java.io.IOException; import java.util.List; -import java.util.Map; /** * (ExperimentIns)表服务接口 @@ -88,4 +87,5 @@ public interface ExperimentInsService { */ String showExperimentInsLog(Integer id, String componentId); + List getNodeResult(Integer id, String nodeId) throws Exception; } diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ExperimentInsServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ExperimentInsServiceImpl.java index 600b4353..20366e3b 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ExperimentInsServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ExperimentInsServiceImpl.java @@ -10,6 +10,7 @@ import com.ruoyi.platform.service.WorkflowService; import com.ruoyi.platform.utils.DateUtils; import com.ruoyi.platform.utils.HttpUtils; import com.ruoyi.platform.utils.JsonUtils; +import com.ruoyi.platform.utils.MinioUtil; import com.ruoyi.system.api.model.LoginUser; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; @@ -20,7 +21,6 @@ import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.io.IOException; -import java.text.SimpleDateFormat; import java.util.*; /** @@ -49,6 +49,11 @@ public class ExperimentInsServiceImpl implements ExperimentInsService { private String argoWorkflowTermination; @Value("${argo.workflowLog}") private String argoWorkflowLog; + private final MinioUtil minioUtil; + + public ExperimentInsServiceImpl(MinioUtil minioUtil) { + this.minioUtil = minioUtil; + } /** * 通过ID查询单条数据 @@ -412,6 +417,41 @@ public class ExperimentInsServiceImpl implements ExperimentInsService { } } + @Override + public List getNodeResult(Integer id, String nodeId) throws Exception { + + List results = new ArrayList(); + //查询 实例 + ExperimentIns experimentIns = this.experimentInsDao.queryById(id); + if (experimentIns == null) { + throw new Exception("实验实例未查询到,id:" + id); + } + //找到 节点 节点结果 + String nodesResultString = experimentIns.getNodesResult(); + if (StringUtils.isEmpty(nodesResultString)) { + throw new Exception("实验实例未查询到节点结果,id:" + id); + } + Map nodesResult = JsonUtils.jsonToMap(nodesResultString); + List> nodeList = (List>) nodesResult.get(nodeId); + //遍历 查询minio + for (int i = 0; i < nodeList.size(); i++) { + Map map = nodeList.get(i); + String path = (String) map.get("path"); + String bucketName = path.substring(0, path.indexOf("/")); + String prefix = path.substring(path.indexOf("/")+1,path.length())+"/"; + if (StringUtils.equals( (String)map.get("type"),"file")){ + List fileInfo = minioUtil.listFilesInDirectory(bucketName, prefix); + map.put("value",fileInfo); + }else if (StringUtils.equals( (String)map.get("type"),"string")){ + String resultInfo = minioUtil.readObjectAsString(bucketName, prefix); + map.put("value",resultInfo); + } + results.add(map); + //组装 + } + return results; + } + private boolean isTerminatedState(ExperimentIns ins) throws IOException { // 定义终止态的列表,例如 "Succeeded", "Failed" 等 String status = ins.getStatus(); diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/JupyterServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/JupyterServiceImpl.java index dde7f2ab..066b7e44 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/JupyterServiceImpl.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/JupyterServiceImpl.java @@ -32,11 +32,15 @@ public class JupyterServiceImpl implements JupyterService { @Value("${k8s.storageClassName}") private String storageClassName; - @Resource - private MinioUtil minioUtil; + private final MinioUtil minioUtil; + @Resource private MlflowUtil mlflowUtil; + public JupyterServiceImpl(MinioUtil minioUtil) { + this.minioUtil = minioUtil; + } + @Override public String getJupyterServiceUrl() { LoginUser loginUser = SecurityUtils.getLoginUser(); diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/FileUtil.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/FileUtil.java new file mode 100644 index 00000000..f6ff32fb --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/FileUtil.java @@ -0,0 +1,24 @@ +package com.ruoyi.platform.utils; + +public class FileUtil { + + public static String formatFileSize(long sizeInBytes) { + if (sizeInBytes < 0) { + throw new IllegalArgumentException("File size cannot be negative."); + } + + if (sizeInBytes < 1024) { + return sizeInBytes + " B"; + } else if (sizeInBytes < 1024 * 1024) { + double sizeInKB = sizeInBytes / 1024.0; + return String.format("%.2f KB", sizeInKB); + } else if (sizeInBytes < 1024 * 1024 * 1024) { + double sizeInMB = sizeInBytes / (1024.0 * 1024); + return String.format("%.2f MB", sizeInMB); + } else { + double sizeInGB = sizeInBytes / (1024.0 * 1024 * 1024); + return String.format("%.2f GB", sizeInGB); + } + } + +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/MinioUtil.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/MinioUtil.java index 781b96da..0e9662d7 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/MinioUtil.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/utils/MinioUtil.java @@ -2,11 +2,19 @@ package com.ruoyi.platform.utils; import io.minio.*; +import io.minio.messages.Item; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import java.io.InputStream; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; @Component public class MinioUtil { @@ -57,4 +65,48 @@ public class MinioUtil { } } } + + + public List listFilesInDirectory(String bucketName, String prefix) throws Exception { + List fileInfoList = new ArrayList<>(); + Iterable> results = minioClient.listObjects( + ListObjectsArgs.builder() + .prefix(prefix) + .bucket(bucketName) + .build()); + + for (Result result : results) { + Item item = result.get(); + String fullPath = item.objectName(); + Path path = Paths.get(fullPath); + String fileName = path.getFileName().toString(); + long fileSize = item.size(); + String formattedSize = FileUtil.formatFileSize(fileSize); // 格式化文件大小 + Map map = new HashMap<>(); + map.put("name", fileName); + map.put("size", formattedSize); + fileInfoList.add(map); + } + + return fileInfoList; + } + + + public String readObjectAsString(String bucketName, String objectName) throws Exception { + try (InputStream inputStream = minioClient.getObject( + GetObjectArgs.builder() + .bucket(bucketName) + .object(objectName) + .build())) { + byte[] buffer = new byte[1024]; + int bytesRead; + StringBuilder content = new StringBuilder(); + + while ((bytesRead = inputStream.read(buffer)) != -1) { + content.append(new String(buffer, 0, bytesRead, StandardCharsets.UTF_8)); + } + + return content.toString(); + } + } } \ No newline at end of file