From aa3909a047b34e6f97ea944ef692791a57da0bc2 Mon Sep 17 00:00:00 2001 From: chenzhihang <709011834@qq.com> Date: Tue, 20 May 2025 17:03:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/autoML/AutoMlController.java | 72 ----- .../autoML/AutoMlInsController.java | 61 ---- .../com/ruoyi/platform/mapper/AutoMlDao.java | 23 -- .../ruoyi/platform/mapper/AutoMlInsDao.java | 23 -- .../platform/service/AutoMlInsService.java | 29 -- .../ruoyi/platform/service/AutoMlService.java | 26 -- .../service/impl/AutoMlInsServiceImpl.java | 261 ------------------ .../service/impl/AutoMlServiceImpl.java | 219 --------------- .../mapper/managementPlatform/AutoMlDao.xml | 149 ---------- .../managementPlatform/AutoMlInsDao.xml | 86 ------ 10 files changed, 949 deletions(-) delete mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/autoML/AutoMlController.java delete mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/autoML/AutoMlInsController.java delete mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/AutoMlDao.java delete mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/AutoMlInsDao.java delete mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/AutoMlInsService.java delete mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/AutoMlService.java delete mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/AutoMlInsServiceImpl.java delete mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/AutoMlServiceImpl.java delete mode 100644 ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/AutoMlDao.xml delete mode 100644 ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/AutoMlInsDao.xml diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/autoML/AutoMlController.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/autoML/AutoMlController.java deleted file mode 100644 index 64c419c6..00000000 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/autoML/AutoMlController.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.ruoyi.platform.controller.autoML; - -import com.ruoyi.common.core.web.controller.BaseController; -import com.ruoyi.common.core.web.domain.AjaxResult; -import com.ruoyi.common.core.web.domain.GenericsAjaxResult; -import com.ruoyi.platform.domain.AutoMl; -import com.ruoyi.platform.service.AutoMlService; -import com.ruoyi.platform.vo.AutoMlVo; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.web.bind.annotation.*; -import org.springframework.web.multipart.MultipartFile; - -import javax.annotation.Resource; -import java.io.IOException; - -@RestController -@RequestMapping("autoML") -@Api("自动机器学习") -public class AutoMlController extends BaseController { - - @Resource - private AutoMlService autoMlService; - - @GetMapping - @ApiOperation("分页查询") - public GenericsAjaxResult> queryByPage(@RequestParam("page") int page, - @RequestParam("size") int size, - @RequestParam(value = "ml_name", required = false) String mlName) { - PageRequest pageRequest = PageRequest.of(page, size); - return genericsSuccess(this.autoMlService.queryByPage(mlName, pageRequest)); - } - - @PostMapping - @ApiOperation("新增自动机器学习") - public GenericsAjaxResult addAutoMl(@RequestBody AutoMlVo autoMlVo) throws Exception { - return genericsSuccess(this.autoMlService.save(autoMlVo)); - } - - @PutMapping - @ApiOperation("编辑自动机器学习") - public GenericsAjaxResult editAutoMl(@RequestBody AutoMlVo autoMlVo) throws Exception { - return genericsSuccess(this.autoMlService.edit(autoMlVo)); - } - - @GetMapping("/getAutoMlDetail") - @ApiOperation("获取自动机器学习详细信息") - public GenericsAjaxResult getAutoMlDetail(@RequestParam("id") Long id) throws IOException { - return genericsSuccess(this.autoMlService.getAutoMlDetail(id)); - } - - @DeleteMapping("{id}") - @ApiOperation("删除自动机器学习") - public GenericsAjaxResult deleteAutoMl(@PathVariable("id") Long id) { - return genericsSuccess(this.autoMlService.delete(id)); - } - - @CrossOrigin(origins = "*", allowedHeaders = "*") - @PostMapping("/upload") - @ApiOperation(value = "上传数据文件csv", notes = "上传数据文件csv,并将信息存入数据库。") - public AjaxResult upload(@RequestParam("file") MultipartFile file, @RequestParam("uuid") String uuid) throws Exception { - return AjaxResult.success(this.autoMlService.upload(file, uuid)); - } - - @PostMapping("/run/{id}") - @ApiOperation("运行自动机器学习实验") - public GenericsAjaxResult runAutoML(@PathVariable("id") Long id) throws Exception { - return genericsSuccess(this.autoMlService.runAutoMlIns(id)); - } -} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/autoML/AutoMlInsController.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/autoML/AutoMlInsController.java deleted file mode 100644 index d57e7dd8..00000000 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/autoML/AutoMlInsController.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.ruoyi.platform.controller.autoML; - -import com.ruoyi.common.core.web.controller.BaseController; -import com.ruoyi.common.core.web.domain.GenericsAjaxResult; -import com.ruoyi.platform.domain.AutoMlIns; -import com.ruoyi.platform.service.AutoMlInsService; -import io.swagger.annotations.Api; -import io.swagger.annotations.ApiOperation; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.web.bind.annotation.*; - -import javax.annotation.Resource; -import java.io.IOException; -import java.util.List; - -@RestController -@RequestMapping("autoMLIns") -@Api("自动机器学习实验实例") -public class AutoMlInsController extends BaseController { - - @Resource - private AutoMlInsService autoMLInsService; - - @GetMapping - @ApiOperation("分页查询") - public GenericsAjaxResult> queryByPage(AutoMlIns autoMlIns, int page, int size) throws IOException { - PageRequest pageRequest = PageRequest.of(page, size); - return genericsSuccess(this.autoMLInsService.queryByPage(autoMlIns, pageRequest)); - } - - @PostMapping - @ApiOperation("新增实验实例") - public GenericsAjaxResult add(@RequestBody AutoMlIns autoMlIns) { - return genericsSuccess(this.autoMLInsService.insert(autoMlIns)); - } - - @DeleteMapping("{id}") - @ApiOperation("删除实验实例") - public GenericsAjaxResult deleteById(@PathVariable("id") Long id) { - return genericsSuccess(this.autoMLInsService.removeById(id)); - } - - @DeleteMapping("batchDelete") - @ApiOperation("批量删除实验实例") - public GenericsAjaxResult batchDelete(@RequestBody List ids) { - return genericsSuccess(this.autoMLInsService.batchDelete(ids)); - } - - @PutMapping("{id}") - @ApiOperation("终止实验实例") - public GenericsAjaxResult terminateAutoMlIns(@PathVariable("id") Long id) throws Exception { - return genericsSuccess(this.autoMLInsService.terminateAutoMlIns(id)); - } - - @GetMapping("{id}") - @ApiOperation("查看实验实例详情") - public GenericsAjaxResult getDetailById(@PathVariable("id") Long id) { - return genericsSuccess(this.autoMLInsService.getDetailById(id)); - } -} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/AutoMlDao.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/AutoMlDao.java deleted file mode 100644 index 188c14c8..00000000 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/AutoMlDao.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.ruoyi.platform.mapper; - -import com.ruoyi.platform.domain.AutoMl; -import org.apache.ibatis.annotations.Param; -import org.springframework.data.domain.Pageable; - -import java.util.List; - -public interface AutoMlDao { - long count(@Param("mlName") String mlName); - - List queryByPage(@Param("mlName") String mlName, @Param("pageable") Pageable pageable); - - AutoMl getAutoMlById(@Param("id") Long id); - - AutoMl getAutoMlByName(@Param("mlName") String mlName); - - int save(@Param("autoMl") AutoMl autoMl); - - int edit(@Param("autoMl") AutoMl autoMl); - - List queryByDatasetId(@Param("datasetId") String datasetId); -} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/AutoMlInsDao.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/AutoMlInsDao.java deleted file mode 100644 index 7fcc3ca9..00000000 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/AutoMlInsDao.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.ruoyi.platform.mapper; - -import com.ruoyi.platform.domain.AutoMlIns; -import org.apache.ibatis.annotations.Param; -import org.springframework.data.domain.Pageable; - -import java.util.List; - -public interface AutoMlInsDao { - long count(@Param("autoMlIns") AutoMlIns autoMlIns); - - List queryAllByLimit(@Param("autoMlIns") AutoMlIns autoMlIns, @Param("pageable") Pageable pageable); - - List getByAutoMlId(@Param("autoMlId") Long AutoMlId); - - int insert(@Param("autoMlIns") AutoMlIns autoMlIns); - - int update(@Param("autoMlIns") AutoMlIns autoMlIns); - - AutoMlIns queryById(@Param("id") Long id); - - List queryByAutoMlInsIsNotTerminated(); -} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/AutoMlInsService.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/AutoMlInsService.java deleted file mode 100644 index 9776fa14..00000000 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/AutoMlInsService.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.ruoyi.platform.service; - -import com.ruoyi.platform.domain.AutoMlIns; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; - -import java.io.IOException; -import java.util.List; - -public interface AutoMlInsService { - - Page queryByPage(AutoMlIns autoMlIns, PageRequest pageRequest) throws IOException; - - AutoMlIns insert(AutoMlIns autoMlIns); - - String removeById(Long id); - - String batchDelete(List ids); - - List queryByAutoMlInsIsNotTerminated(); - - AutoMlIns queryStatusFromArgo(AutoMlIns autoMlIns); - - boolean terminateAutoMlIns(Long id) throws Exception; - - AutoMlIns getDetailById(Long id); - - void updateAutoMlStatus(Long autoMlId); -} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/AutoMlService.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/AutoMlService.java deleted file mode 100644 index a2bad796..00000000 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/AutoMlService.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.ruoyi.platform.service; - -import com.ruoyi.platform.domain.AutoMl; -import com.ruoyi.platform.vo.AutoMlVo; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageRequest; -import org.springframework.web.multipart.MultipartFile; - -import java.io.IOException; -import java.util.Map; - -public interface AutoMlService { - Page queryByPage(String mlName, PageRequest pageRequest); - - AutoMl save(AutoMlVo autoMlVo) throws Exception; - - String edit(AutoMlVo autoMlVo) throws Exception; - - String delete(Long id); - - AutoMlVo getAutoMlDetail(Long id) throws IOException; - - Map upload(MultipartFile file, String uuid) throws Exception; - - String runAutoMlIns(Long id) throws Exception; -} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/AutoMlInsServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/AutoMlInsServiceImpl.java deleted file mode 100644 index 1074f7ef..00000000 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/AutoMlInsServiceImpl.java +++ /dev/null @@ -1,261 +0,0 @@ -package com.ruoyi.platform.service.impl; - -import com.ruoyi.system.api.constant.Constant; -import com.ruoyi.platform.domain.AutoMl; -import com.ruoyi.platform.domain.AutoMlIns; -import com.ruoyi.platform.mapper.AutoMlDao; -import com.ruoyi.platform.mapper.AutoMlInsDao; -import com.ruoyi.platform.service.AutoMlInsService; -import com.ruoyi.platform.utils.DateUtils; -import com.ruoyi.platform.utils.HttpUtils; -import com.ruoyi.platform.utils.JsonUtils; -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.PageRequest; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import javax.annotation.Resource; -import java.io.IOException; -import java.text.SimpleDateFormat; -import java.util.*; - -@Service -public class AutoMlInsServiceImpl implements AutoMlInsService { - @Value("${argo.url}") - private String argoUrl; - @Value("${argo.workflowStatus}") - private String argoWorkflowStatus; - @Value("${argo.workflowTermination}") - private String argoWorkflowTermination; - - @Resource - private AutoMlInsDao autoMlInsDao; - @Resource - private AutoMlDao autoMlDao; - - @Override - public Page queryByPage(AutoMlIns autoMlIns, PageRequest pageRequest) throws IOException { - long total = this.autoMlInsDao.count(autoMlIns); - List autoMlInsList = this.autoMlInsDao.queryAllByLimit(autoMlIns, pageRequest); - return new PageImpl<>(autoMlInsList, pageRequest, total); - } - - @Override - public AutoMlIns insert(AutoMlIns autoMlIns) { - this.autoMlInsDao.insert(autoMlIns); - return autoMlIns; - } - - @Override - public String removeById(Long id) { - AutoMlIns autoMlIns = autoMlInsDao.queryById(id); - if (autoMlIns == null) { - return "实验实例不存在"; - } - - if (StringUtils.isEmpty(autoMlIns.getStatus())) { - autoMlIns = queryStatusFromArgo(autoMlIns); - } - if (StringUtils.equals(autoMlIns.getStatus(), Constant.Running)) { - return "实验实例正在运行,不可删除"; - } - - autoMlIns.setState(Constant.State_invalid); - int update = autoMlInsDao.update(autoMlIns); - if (update > 0) { - updateAutoMlStatus(autoMlIns.getAutoMlId()); - return "删除成功"; - } else { - return "删除失败"; - } - } - - @Override - @Transactional - public String batchDelete(List ids) { - for (Long id : ids) { - String result = removeById(id); - if (!"删除成功".equals(result)) { - return result; - } - } - return "删除成功"; - } - - @Override - public List queryByAutoMlInsIsNotTerminated() { - return autoMlInsDao.queryByAutoMlInsIsNotTerminated(); - } - - @Override - public AutoMlIns queryStatusFromArgo(AutoMlIns ins) { - String namespace = ins.getArgoInsNs(); - String name = ins.getArgoInsName(); - - // 创建请求数据map - Map requestData = new HashMap<>(); - requestData.put("namespace", namespace); - requestData.put("name", name); - - // 创建发送数据map,将请求数据作为"data"键的值 - Map res = new HashMap<>(); - res.put("data", requestData); - - try { - // 发送POST请求到Argo工作流状态查询接口,并将请求数据转换为JSON - String req = HttpUtils.sendPost(argoUrl + argoWorkflowStatus, null, JsonUtils.mapToJson(res)); - // 检查响应是否为空或无内容 - if (req == null || StringUtils.isEmpty(req)) { - throw new RuntimeException("工作流状态响应为空"); - } - // 将响应的JSON字符串转换为Map对象 - Map runResMap = JsonUtils.jsonToMap(req); - // 从响应Map中获取"data"部分 - Map data = (Map) runResMap.get("data"); - if (data == null || data.isEmpty()) { - throw new RuntimeException("工作流数据为空"); - } - // 从"data"中获取"status"部分,并返回"phase"的值 - Map status = (Map) data.get("status"); - if (status == null || status.isEmpty()) { - throw new RuntimeException("工作流状态为空"); - } - - //解析流水线结束时间 - String finishedAtString = (String) status.get("finishedAt"); - if (finishedAtString != null && !finishedAtString.isEmpty()) { - Date finishTime = DateUtils.convertUTCtoShanghaiDate(finishedAtString); - ins.setFinishTime(finishTime); - } - - // 解析nodes字段,提取节点状态并转换为JSON字符串 - Map nodes = (Map) status.get("nodes"); - Map modifiedNodes = new LinkedHashMap<>(); - if (nodes != null) { - for (Map.Entry nodeEntry : nodes.entrySet()) { - Map nodeDetails = (Map) nodeEntry.getValue(); - String templateName = (String) nodeDetails.get("displayName"); - modifiedNodes.put(templateName, nodeDetails); - } - } - - String nodeStatusJson = JsonUtils.mapToJson(modifiedNodes); - ins.setNodeStatus(nodeStatusJson); - - //终止态为终止不改 - if (!StringUtils.equals(ins.getStatus(), Constant.Terminated)) { - ins.setStatus(StringUtils.isNotEmpty((String) status.get("phase")) ? (String) status.get("phase") : Constant.Pending); - } - if (StringUtils.equals(ins.getStatus(), Constant.Error)) { - ins.setStatus(Constant.Failed); - } - return ins; - } catch (Exception e) { - throw new RuntimeException("查询状态失败: " + e.getMessage(), e); - } - } - - @Override - public boolean terminateAutoMlIns(Long id) throws Exception { - AutoMlIns autoMlIns = autoMlInsDao.queryById(id); - if (autoMlIns == null) { - throw new IllegalStateException("实验实例未查询到,id: " + id); - } - - String currentStatus = autoMlIns.getStatus(); - String name = autoMlIns.getArgoInsName(); - String namespace = autoMlIns.getArgoInsNs(); - - // 获取当前状态,如果为空,则从Argo查询 - if (StringUtils.isEmpty(currentStatus)) { - currentStatus = queryStatusFromArgo(autoMlIns).getStatus(); - } - // 只有状态是"Running"时才能终止实例 - if (!currentStatus.equalsIgnoreCase(Constant.Running)) { - throw new Exception("终止错误,只有运行状态的实例才能终止"); // 如果不是"Running"状态,则不执行终止操作 - } - - // 创建请求数据map - Map requestData = new HashMap<>(); - requestData.put("namespace", namespace); - requestData.put("name", name); - // 创建发送数据map,将请求数据作为"data"键的值 - Map res = new HashMap<>(); - res.put("data", requestData); - - try { - // 发送POST请求到Argo工作流状态查询接口,并将请求数据转换为JSON - String req = HttpUtils.sendPost(argoUrl + argoWorkflowTermination, null, JsonUtils.mapToJson(res)); - // 检查响应是否为空或无内容 - if (StringUtils.isEmpty(req)) { - throw new RuntimeException("终止响应内容为空"); - - } - // 将响应的JSON字符串转换为Map对象 - Map runResMap = JsonUtils.jsonToMap(req); - // 从响应Map中直接获取"errCode"的值 - Integer errCode = (Integer) runResMap.get("errCode"); - if (errCode != null && errCode == 0) { - //更新autoMlIns,确保状态更新被保存到数据库 - AutoMlIns ins = queryStatusFromArgo(autoMlIns); - String nodeStatus = ins.getNodeStatus(); - Map nodeMap = JsonUtils.jsonToMap(nodeStatus); - - // 遍历 map - for (Map.Entry entry : nodeMap.entrySet()) { - // 获取每个 Map 中的值并强制转换为 Map - Map innerMap = (Map) entry.getValue(); - // 检查 phase 的值 - if (innerMap.containsKey("phase")) { - String phaseValue = (String) innerMap.get("phase"); - // 如果值不等于 Succeeded,则赋值为 Failed - if (!StringUtils.equals(Constant.Succeeded, phaseValue)) { - innerMap.put("phase", Constant.Failed); - Calendar calendar = Calendar.getInstance(); - calendar.add(Calendar.HOUR_OF_DAY, -8); - innerMap.put("finishedAt", new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(calendar.getTime())); - } - } - } - ins.setNodeStatus(JsonUtils.mapToJson(nodeMap)); - ins.setStatus(Constant.Terminated); - ins.setUpdateTime(new Date()); - ins.setFinishTime(new Date()); - this.autoMlInsDao.update(ins); - updateAutoMlStatus(autoMlIns.getAutoMlId()); - return true; - } else { - return false; - } - } catch (Exception e) { - throw new RuntimeException("终止实例错误: " + e.getMessage(), e); - } - } - - @Override - public AutoMlIns getDetailById(Long id) { - AutoMlIns autoMlIns = this.autoMlInsDao.queryById(id); - if (Constant.Running.equals(autoMlIns.getStatus()) || Constant.Pending.equals(autoMlIns.getStatus())) { - autoMlIns = queryStatusFromArgo(autoMlIns); - } - return autoMlIns; - } - - public void updateAutoMlStatus(Long autoMlId) { - List insList = autoMlInsDao.getByAutoMlId(autoMlId); - List statusList = new ArrayList<>(); - // 更新实验状态列表 - for (int i = 0; i < insList.size(); i++) { - statusList.add(insList.get(i).getStatus()); - } - String subStatus = statusList.toString().substring(1, statusList.toString().length() - 1); - AutoMl autoMl = autoMlDao.getAutoMlById(autoMlId); - if (!StringUtils.equals(autoMl.getStatusList(), subStatus)) { - autoMl.setStatusList(subStatus); - autoMlDao.edit(autoMl); - } - } -} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/AutoMlServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/AutoMlServiceImpl.java deleted file mode 100644 index b8d236fc..00000000 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/AutoMlServiceImpl.java +++ /dev/null @@ -1,219 +0,0 @@ -package com.ruoyi.platform.service.impl; - -import com.ruoyi.common.security.utils.SecurityUtils; -import com.ruoyi.system.api.constant.Constant; -import com.ruoyi.platform.domain.AutoMl; -import com.ruoyi.platform.domain.AutoMlIns; -import com.ruoyi.platform.mapper.AutoMlDao; -import com.ruoyi.platform.mapper.AutoMlInsDao; -import com.ruoyi.platform.service.AutoMlInsService; -import com.ruoyi.platform.service.AutoMlService; -import com.ruoyi.platform.utils.FileUtil; -import com.ruoyi.platform.utils.HttpUtils; -import com.ruoyi.platform.utils.JacksonUtil; -import com.ruoyi.platform.utils.JsonUtils; -import com.ruoyi.platform.vo.AutoMlParamVo; -import com.ruoyi.platform.vo.AutoMlVo; -import org.apache.commons.collections4.MapUtils; -import org.apache.commons.io.FileUtils; -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.BeanUtils; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.PageImpl; -import org.springframework.data.domain.PageRequest; -import org.springframework.stereotype.Service; -import org.springframework.web.multipart.MultipartFile; - -import javax.annotation.Resource; -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -@Service("autoMLService") -public class AutoMlServiceImpl implements AutoMlService { - @Value("${git.localPath}") - String localPath; - - @Value("${argo.url}") - private String argoUrl; - @Value("${argo.convertAutoML}") - String convertAutoML; - @Value("${argo.workflowRun}") - private String argoWorkflowRun; - - @Value("${minio.endpointIp}") - private String minioEndpoint; - - @Resource - private AutoMlDao autoMlDao; - - @Resource - private AutoMlInsDao autoMlInsDao; - - @Resource - private AutoMlInsService autoMlInsService; - - @Override - public Page queryByPage(String mlName, PageRequest pageRequest) { - long total = autoMlDao.count(mlName); - List autoMls = autoMlDao.queryByPage(mlName, pageRequest); - return new PageImpl<>(autoMls, pageRequest, total); - } - - @Override - public AutoMl save(AutoMlVo autoMlVo) throws Exception { - if (autoMlVo.getMlName().length() > 64) { - throw new RuntimeException("实验名称大于最大长度"); - } - AutoMl autoMlByName = autoMlDao.getAutoMlByName(autoMlVo.getMlName()); - if (autoMlByName != null) { - throw new RuntimeException("实验名称已存在"); - } - AutoMl autoMl = new AutoMl(); - BeanUtils.copyProperties(autoMlVo, autoMl); - String username = SecurityUtils.getLoginUser().getUsername(); - autoMl.setCreateBy(username); - autoMl.setUpdateBy(username); - String datasetJson = JacksonUtil.toJSONString(autoMlVo.getDataset()); - autoMl.setDataset(datasetJson); - autoMlDao.save(autoMl); - return autoMl; - } - - @Override - public String edit(AutoMlVo autoMlVo) throws Exception { - AutoMl oldAutoMl = autoMlDao.getAutoMlByName(autoMlVo.getMlName()); - if (oldAutoMl != null && !oldAutoMl.getId().equals(autoMlVo.getId())) { - throw new RuntimeException("实验名称已存在"); - } - AutoMl autoMl = new AutoMl(); - BeanUtils.copyProperties(autoMlVo, autoMl); - String username = SecurityUtils.getLoginUser().getUsername(); - autoMl.setUpdateBy(username); - String datasetJson = JacksonUtil.toJSONString(autoMlVo.getDataset()); - autoMl.setDataset(datasetJson); - - autoMlDao.edit(autoMl); - return "修改成功"; - } - - @Override - public String delete(Long id) { - AutoMl autoMl = autoMlDao.getAutoMlById(id); - if (autoMl == null) { - throw new RuntimeException("实验不存在"); - } - - String username = SecurityUtils.getLoginUser().getUsername(); - String createBy = autoMl.getCreateBy(); - if (!(StringUtils.equals(username, "admin") || StringUtils.equals(username, createBy))) { - throw new RuntimeException("无权限删除该实验"); - } - - autoMl.setState(Constant.State_invalid); - return autoMlDao.edit(autoMl) > 0 ? "删除成功" : "删除失败"; - } - - @Override - public AutoMlVo getAutoMlDetail(Long id) throws IOException { - AutoMl autoMl = autoMlDao.getAutoMlById(id); - AutoMlVo autoMlVo = new AutoMlVo(); - BeanUtils.copyProperties(autoMl, autoMlVo); - if (StringUtils.isNotEmpty(autoMl.getDataset())) { - autoMlVo.setDataset(JsonUtils.jsonToMap(autoMl.getDataset())); - } - return autoMlVo; - } - - @Override - public Map upload(MultipartFile file, String uuid) throws Exception { - Map result = new HashMap<>(); - - String username = SecurityUtils.getLoginUser().getUsername(); - String fileName = file.getOriginalFilename(); - String path = localPath + "temp/" + username + "/automl_data/" + uuid; - long sizeInBytes = file.getSize(); - String formattedSize = FileUtil.formatFileSize(sizeInBytes); - File targetFile = new File(path, file.getOriginalFilename()); - // 确保目录存在 - targetFile.getParentFile().mkdirs(); - // 保存文件到目标路径 - FileUtils.copyInputStreamToFile(file.getInputStream(), targetFile); - // 返回上传文件的路径 - result.put("fileName", fileName); - result.put("url", path); // objectName根据实际情况定义 - result.put("fileSize", formattedSize); - return result; - } - - @Override - public String runAutoMlIns(Long id) throws Exception { - AutoMl autoMl = autoMlDao.getAutoMlById(id); - if (autoMl == null) { - throw new Exception("自动机器学习配置不存在"); - } - - AutoMlParamVo autoMlParam = new AutoMlParamVo(); - BeanUtils.copyProperties(autoMl, autoMlParam); - autoMlParam.setDataset(JsonUtils.jsonToMap(autoMl.getDataset())); - String param = JsonUtils.objectToJson(autoMlParam); - // 调argo转换接口 - try { - String convertRes = HttpUtils.sendPost(argoUrl + convertAutoML, param); - if (convertRes == null || StringUtils.isEmpty(convertRes)) { - throw new RuntimeException("转换流水线失败"); - } - Map converMap = JsonUtils.jsonToMap(convertRes); - // 组装运行接口json - Map output = (Map) converMap.get("output"); - Map runReqMap = new HashMap<>(); - runReqMap.put("data", converMap.get("data")); - // 调argo运行接口 - String runRes = HttpUtils.sendPost(argoUrl + argoWorkflowRun, JsonUtils.mapToJson(runReqMap)); - - if (runRes == null || StringUtils.isEmpty(runRes)) { - throw new RuntimeException("运行流水线失败"); - } - Map runResMap = JsonUtils.jsonToMap(runRes); - Map data = (Map) runResMap.get("data"); - //判断data为空 - if (data == null || MapUtils.isEmpty(data)) { - throw new RuntimeException("运行流水线失败"); - } - Map metadata = (Map) data.get("metadata"); - // 插入记录到实验实例表 - AutoMlIns autoMlIns = new AutoMlIns(); - autoMlIns.setAutoMlId(autoMl.getId()); - autoMlIns.setArgoInsNs((String) metadata.get("namespace")); - autoMlIns.setArgoInsName((String) metadata.get("name")); - autoMlIns.setParam(param); - autoMlIns.setStatus(Constant.Pending); - //替换argoInsName - String outputString = JsonUtils.mapToJson(output); - autoMlIns.setNodeResult(outputString.replace("{{workflow.name}}", (String) metadata.get("name"))); - - Map param_output = (Map) output.get("param_output"); - List output1 = (ArrayList) param_output.values().toArray()[0]; - Map output2 = (Map) output1.get(0); - String outputPath = minioEndpoint + "/" + output2.get("path").replace("{{workflow.name}}", (String) metadata.get("name")) + "/"; - autoMlIns.setModelPath(outputPath + "save_model.joblib"); - if (Constant.AutoMl_Classification.equals(autoMl.getTaskType())) { - autoMlIns.setImgPath(outputPath + "Auto-sklearn_metric_over_time.png" + "," + outputPath + "Train_Confusion_Matrix.png" + "," + outputPath + "Test_Confusion_Matrix.png"); - } else { - autoMlIns.setImgPath(outputPath + "Auto-sklearn_metric_over_time.png" + "," + outputPath + "regression.png"); - } - autoMlIns.setResultPath(outputPath + "result.txt"); - String seed = autoMl.getSeed() != null ? String.valueOf(autoMl.getSeed()) : "1"; - autoMlIns.setRunHistoryPath(outputPath + "smac3-output/run_" + seed + "/runhistory.json"); - autoMlInsDao.insert(autoMlIns); - autoMlInsService.updateAutoMlStatus(id); - } catch (Exception e) { - throw new RuntimeException(e.getMessage()); - } - return "执行成功"; - } -} diff --git a/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/AutoMlDao.xml b/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/AutoMlDao.xml deleted file mode 100644 index 21b37b20..00000000 --- a/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/AutoMlDao.xml +++ /dev/null @@ -1,149 +0,0 @@ - - - - - insert into auto_ml(ml_name, ml_description, task_type, dataset, time_left_for_this_task, - per_run_time_limit, ensemble_size, ensemble_class, ensemble_nbest, max_models_on_disc, seed, - memory_limit, - include_classifier, include_feature_preprocessor, include_regressor, exclude_classifier, - exclude_regressor, exclude_feature_preprocessor, test_size, resampling_strategy, train_size, - shuffle, folds, target_columns, metric_name, metrics, greater_is_better, scoring_functions, - tmp_folder, - create_by, update_by) - values (#{autoMl.mlName}, #{autoMl.mlDescription}, #{autoMl.taskType}, #{autoMl.dataset}, - #{autoMl.timeLeftForThisTask}, #{autoMl.perRunTimeLimit}, - #{autoMl.ensembleSize}, #{autoMl.ensembleClass}, #{autoMl.ensembleNbest}, - #{autoMl.maxModelsOnDisc}, #{autoMl.seed}, - #{autoMl.memoryLimit}, #{autoMl.includeClassifier}, #{autoMl.includeFeaturePreprocessor}, - #{autoMl.includeRegressor}, #{autoMl.excludeClassifier}, - #{autoMl.excludeRegressor}, #{autoMl.excludeFeaturePreprocessor}, #{autoMl.testSize}, - #{autoMl.resamplingStrategy}, - #{autoMl.trainSize}, #{autoMl.shuffle}, - #{autoMl.folds}, - #{autoMl.targetColumns}, #{autoMl.metricName}, #{autoMl.metrics}, #{autoMl.greaterIsBetter}, - #{autoMl.scoringFunctions}, #{autoMl.tmpFolder}, - #{autoMl.createBy}, #{autoMl.updateBy}) - - - - update auto_ml - - - ml_name = #{autoMl.mlName}, - - - ml_description = #{autoMl.mlDescription}, - - - status_list = #{autoMl.statusList}, - - - task_type = #{autoMl.taskType}, - - - dataset = #{autoMl.dataset}, - - - time_left_for_this_task = #{autoMl.timeLeftForThisTask}, - - - per_run_time_limit = #{autoMl.perRunTimeLimit}, - - - ensemble_size = #{autoMl.ensembleSize}, - - - ensemble_class = #{autoMl.ensembleClass}, - - - ensemble_nbest = #{autoMl.ensembleNbest}, - - - max_models_on_disc = #{autoMl.maxModelsOnDisc}, - - - seed = #{autoMl.seed}, - - - memory_limit = #{autoMl.memoryLimit}, - - include_classifier = #{autoMl.includeClassifier}, - include_feature_preprocessor = #{autoMl.includeFeaturePreprocessor}, - include_regressor = #{autoMl.includeRegressor}, - exclude_classifier = #{autoMl.excludeClassifier}, - exclude_regressor = #{autoMl.excludeRegressor}, - exclude_feature_preprocessor = #{autoMl.excludeFeaturePreprocessor}, - scoring_functions = #{autoMl.scoringFunctions}, - metrics = #{autoMl.metrics}, - metric_name = #{autoMl.metricName}, - - test_size = #{autoMl.testSize}, - - - resampling_strategy = #{autoMl.resamplingStrategy}, - - - train_size = #{autoMl.trainSize}, - - - shuffle = #{autoMl.shuffle}, - - - folds = #{autoMl.folds}, - - - tmp_folder = #{autoMl.tmpFolder}, - - - greater_is_better = #{autoMl.greaterIsBetter}, - - - target_columns = #{autoMl.targetColumns}, - - - state = #{autoMl.state}, - - - where id = #{autoMl.id} - - - - - - - - - - - - - - - state = 1 - - and ml_name like concat('%', #{mlName}, '%') - - - - \ No newline at end of file diff --git a/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/AutoMlInsDao.xml b/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/AutoMlInsDao.xml deleted file mode 100644 index 26f9c20d..00000000 --- a/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/AutoMlInsDao.xml +++ /dev/null @@ -1,86 +0,0 @@ - - - - - - insert into auto_ml_ins(auto_ml_id, result_path, model_path, img_path, run_history_path, node_status, - node_result, param, source, argo_ins_name, argo_ins_ns, status) - values (#{autoMlIns.autoMlId}, #{autoMlIns.resultPath}, #{autoMlIns.modelPath}, #{autoMlIns.imgPath}, - #{autoMlIns.runHistoryPath}, #{autoMlIns.nodeStatus}, - #{autoMlIns.nodeResult}, #{autoMlIns.param}, #{autoMlIns.source}, #{autoMlIns.argoInsName}, - #{autoMlIns.argoInsNs}, #{autoMlIns.status}) - - - - update auto_ml_ins - - - model_path = #{autoMlIns.modelPath}, - - - img_path = #{autoMlIns.imgPath}, - - - status = #{autoMlIns.status}, - - - node_status = #{autoMlIns.nodeStatus}, - - - node_result = #{autoMlIns.nodeResult}, - - - state = #{autoMlIns.state}, - - - update_time = #{autoMlIns.updateTime}, - - - finish_time = #{autoMlIns.finishTime}, - - - where id = #{autoMlIns.id} - - - - - - - - - - - - \ No newline at end of file