From 32883beac69bbbc90d16471d6f8958153e4deed1 Mon Sep 17 00:00:00 2001 From: chenzhihang <709011834@qq.com> Date: Tue, 18 Mar 2025 11:10:44 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=AF=E5=88=86=E5=8A=9F=E8=83=BD=E5=BC=80?= =?UTF-8?q?=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/system/api/domain/SysUser.java | 10 ++ .../controller/jupyter/JupyterController.java | 9 +- .../platform/domain/ComputingResource.java | 123 ++++-------------- .../com/ruoyi/platform/domain/Resource.java | 25 ++++ .../ruoyi/platform/domain/ResourceOccupy.java | 39 ++++++ .../platform/mapper/ResourceOccupyDao.java | 15 +++ .../service/ResourceOccupyService.java | 13 ++ .../impl/ResourceOccupyServiceImpl.java | 71 ++++++++++ .../ComputingResourceDaoMapper.xml | 24 ++-- .../managementPlatform/ResourceOccupy.xml | 31 +++++ 10 files changed, 242 insertions(+), 118 deletions(-) create mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/Resource.java create mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/ResourceOccupy.java create mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/ResourceOccupyDao.java create mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ResourceOccupyService.java create mode 100644 ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ResourceOccupyServiceImpl.java create mode 100644 ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/ResourceOccupy.xml diff --git a/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/domain/SysUser.java b/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/domain/SysUser.java index fc1aa78c..67457175 100644 --- a/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/domain/SysUser.java +++ b/ruoyi-api/ruoyi-api-system/src/main/java/com/ruoyi/system/api/domain/SysUser.java @@ -131,6 +131,8 @@ public class SysUser extends BaseEntity { private String gitLinkPassword; + private Float credit; + public SysUser() { } @@ -315,6 +317,14 @@ public class SysUser extends BaseEntity { return gitLinkPassword; } + public void setCredit(Float credit) { + this.credit = credit; + } + + public Float getCredit() { + return credit; + } + @Override public String toString() { return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE) diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/jupyter/JupyterController.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/jupyter/JupyterController.java index 6234820b..2772cd5b 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/jupyter/JupyterController.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/controller/jupyter/JupyterController.java @@ -6,9 +6,7 @@ import com.ruoyi.common.core.web.domain.GenericsAjaxResult; import com.ruoyi.platform.domain.DevEnvironment; import com.ruoyi.platform.service.JupyterService; import com.ruoyi.platform.service.NewDatasetService; -import com.ruoyi.platform.vo.NewDatasetVo; import com.ruoyi.platform.vo.PodStatusVo; -import com.ruoyi.platform.vo.VersionVo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.v3.oas.annotations.responses.ApiResponse; @@ -19,8 +17,6 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; @RestController @RequestMapping("/jupyter") @@ -30,6 +26,7 @@ public class JupyterController extends BaseController { private JupyterService jupyterService; @Resource private NewDatasetService newDatasetService; + @GetMapping(value = "/getURL") @ApiOperation("得到访问地址") public GenericsAjaxResult getURL() throws IOException { @@ -47,7 +44,7 @@ public class JupyterController extends BaseController { @ApiOperation("根据开发环境id启动jupyter pod") @ApiResponse public GenericsAjaxResult runJupyter(@PathVariable("id") Integer id) throws Exception { - return genericsSuccess(this.jupyterService.runJupyterService(id)); + return genericsSuccess(this.jupyterService.runJupyterService(id)); } @@ -68,7 +65,7 @@ public class JupyterController extends BaseController { @ApiOperation("查询jupyter pod状态") @ApiResponse public GenericsAjaxResult getStatus(DevEnvironment devEnvironment) throws Exception { - return genericsSuccess(this.jupyterService.getJupyterStatus(devEnvironment)); + return genericsSuccess(this.jupyterService.getJupyterStatus(devEnvironment)); } diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/ComputingResource.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/ComputingResource.java index d0551fa8..9a52bd6b 100644 --- a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/ComputingResource.java +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/ComputingResource.java @@ -3,9 +3,10 @@ package com.ruoyi.platform.domain; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.annotation.JsonNaming; import io.swagger.annotations.ApiModelProperty; +import lombok.Data; -import java.util.Date; import java.io.Serializable; +import java.util.Date; /** * (ComputingResource)实体类 @@ -14,13 +15,17 @@ import java.io.Serializable; * @since 2023-11-29 14:23:01 */ @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) +@Data public class ComputingResource implements Serializable { private static final long serialVersionUID = -42500298368776666L; -/** + /** * 主键 */ -@ApiModelProperty(value = "编号", notes = "唯一标识符") -private Integer id; + @ApiModelProperty(value = "编号", notes = "唯一标识符") + private Integer id; + + @ApiModelProperty("资源id") + private Integer resourceId; @ApiModelProperty("计算资源的描述") private String computingResource; @@ -31,6 +36,24 @@ private Integer id; @ApiModelProperty("资源的详细描述") private String description; + @ApiModelProperty("cpu核数") + private Integer cpuCores; + + @ApiModelProperty("内存大小GB") + private Integer memoryGb; + + @ApiModelProperty("显存大小GB") + private Integer gpuMemoryGb; + + @ApiModelProperty("GPU个数") + private Integer gpuNums; + + @ApiModelProperty("积分/小时") + private Float creditPerHour; + + @ApiModelProperty("标签") + private String labels; + @ApiModelProperty(value = "创建者的用户名", example = "admin") private String createBy; @@ -46,100 +69,8 @@ private Integer id; @ApiModelProperty(value = "状态标识", notes = "0表示失效,1表示生效") private Integer state; - @ApiModelProperty(value = "占用情况(1-占用,0-未占用)") - private Integer usedState; - @ApiModelProperty(value = "节点") private String node; - public Integer getId() { - return id; - } - - public void setId(Integer id) { - this.id = id; - } - - public String getComputingResource() { - return computingResource; - } - - public void setComputingResource(String computingResource) { - this.computingResource = computingResource; - } - - - - public String getStandard() { - return standard; - } - - public void setStandard(String standard) { - this.standard = standard; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public String getCreateBy() { - return createBy; - } - - public void setCreateBy(String createBy) { - this.createBy = createBy; - } - - public Date getCreateTime() { - return createTime; - } - - public void setCreateTime(Date createTime) { - this.createTime = createTime; - } - - public String getUpdateBy() { - return updateBy; - } - - public void setUpdateBy(String updateBy) { - this.updateBy = updateBy; - } - - public Date getUpdateTime() { - return updateTime; - } - - public void setUpdateTime(Date updateTime) { - this.updateTime = updateTime; - } - - public Integer getState() { - return state; - } - - public void setState(Integer state) { - this.state = state; - } - - public Integer getUsedState() { - return usedState; - } - - public void setUsedState(Integer usedState) { - this.usedState = usedState; - } - - public String getNode() { - return node; - } - - public void setNode(String node) { - this.node = node; - } } diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/Resource.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/Resource.java new file mode 100644 index 00000000..bdb92893 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/Resource.java @@ -0,0 +1,25 @@ +package com.ruoyi.platform.domain; + +import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.databind.annotation.JsonNaming; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) +@Data +public class Resource { + @ApiModelProperty(value = "编号", notes = "唯一标识符") + private Integer id; + + @ApiModelProperty("类型") + private String type; + + @ApiModelProperty("gpu") + private String gpu; + + @ApiModelProperty("总数") + private Integer total; + + @ApiModelProperty("已被占用的数量") + private Integer used; +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/ResourceOccupy.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/ResourceOccupy.java new file mode 100644 index 00000000..80bf9b88 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/domain/ResourceOccupy.java @@ -0,0 +1,39 @@ +package com.ruoyi.platform.domain; + +import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.databind.annotation.JsonNaming; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.util.Date; + +@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) +@Data +public class ResourceOccupy { + @ApiModelProperty(value = "编号", notes = "唯一标识符") + private Integer id; + + @ApiModelProperty("用户") + private Long userId; + + @ApiModelProperty("计算资源") + private Integer computingResourceId; + + @ApiModelProperty("积分/小时") + private Float creditPerHour; + + @ApiModelProperty("上一次扣分时间") + private Date deduceLastTime; + + @ApiModelProperty("状态") + private Integer state; + + @ApiModelProperty("开始时间") + private Date startTime; + + @ApiModelProperty("任务类型") + private String taskType; + + @ApiModelProperty("类型id") + private Integer taskId; +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/ResourceOccupyDao.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/ResourceOccupyDao.java new file mode 100644 index 00000000..e434f494 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/mapper/ResourceOccupyDao.java @@ -0,0 +1,15 @@ +package com.ruoyi.platform.mapper; + +import com.ruoyi.platform.domain.ResourceOccupy; +import org.apache.ibatis.annotations.Param; + +public interface ResourceOccupyDao { + + Boolean haveResource(@Param("id") Integer id, @Param("need") Integer need); + + int save(@Param("resourceOccupy") ResourceOccupy resourceOccupy); + + int edit(@Param("resourceOccupy") ResourceOccupy resourceOccupy); + + ResourceOccupy getResourceOccupyById(@Param("id") Integer id); +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ResourceOccupyService.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ResourceOccupyService.java new file mode 100644 index 00000000..1f50706d --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/ResourceOccupyService.java @@ -0,0 +1,13 @@ +package com.ruoyi.platform.service; + +public interface ResourceOccupyService { + + Boolean haveResource(Integer computingResourceId) throws Exception; + + void startDeduce(Integer computingResourceId); + + void endDeduce(Integer id); + + void deducing(); + +} diff --git a/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ResourceOccupyServiceImpl.java b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ResourceOccupyServiceImpl.java new file mode 100644 index 00000000..3cbbfe0b --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/java/com/ruoyi/platform/service/impl/ResourceOccupyServiceImpl.java @@ -0,0 +1,71 @@ +package com.ruoyi.platform.service.impl; + +import com.ruoyi.common.security.utils.SecurityUtils; +import com.ruoyi.platform.constant.Constant; +import com.ruoyi.platform.domain.ComputingResource; +import com.ruoyi.platform.domain.ResourceOccupy; +import com.ruoyi.platform.mapper.ComputingResourceDao; +import com.ruoyi.platform.mapper.ResourceOccupyDao; +import com.ruoyi.platform.service.ResourceOccupyService; +import com.ruoyi.system.api.model.LoginUser; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; +import java.util.Date; + +@Service("resourceOccupyService") +public class ResourceOccupyServiceImpl implements ResourceOccupyService { + @Resource + private ResourceOccupyDao resourceOccupyDao; + + @Resource + private ComputingResourceDao computingResourceDao; + + @Override + public Boolean haveResource(Integer computingResourceId) throws Exception { + ComputingResource computingResource = computingResourceDao.queryById(computingResourceId); + + LoginUser loginUser = SecurityUtils.getLoginUser(); + if (loginUser.getSysUser().getCredit() < computingResource.getCreditPerHour()) { + throw new Exception("积分不足"); + } + if (Constant.Computing_Resource_GPU.equals(computingResource.getComputingResource())) { + if (resourceOccupyDao.haveResource(computingResource.getResourceId(), computingResource.getGpuNums())) { + return true; + } else { + throw new Exception("资源不足,GPU资源已被占用"); + } + } else { + if (resourceOccupyDao.haveResource(computingResource.getResourceId(), computingResource.getCpuCores())) { + return true; + } else { + throw new Exception("资源不足,CPU资源已被占用完"); + } + } + } + + @Override + public void startDeduce(Integer computingResourceId) { + ResourceOccupy resourceOccupy = new ResourceOccupy(); + ComputingResource computingResource = computingResourceDao.queryById(computingResourceId); + resourceOccupy.setComputingResourceId(computingResourceId); + LoginUser loginUser = SecurityUtils.getLoginUser(); + resourceOccupy.setUserId(loginUser.getUserid()); + resourceOccupy.setCreditPerHour(computingResource.getCreditPerHour()); + resourceOccupyDao.save(resourceOccupy); + } + + @Override + public void endDeduce(Integer id) { + ResourceOccupy resourceOccupy = resourceOccupyDao.getResourceOccupyById(id); + resourceOccupy.setState(Constant.State_invalid); + deducing(); + resourceOccupy.setDeduceLastTime(new Date()); + resourceOccupyDao.edit(resourceOccupy); + } + + @Override + public void deducing() { + + } +} diff --git a/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/ComputingResourceDaoMapper.xml b/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/ComputingResourceDaoMapper.xml index 162a12f7..25981868 100644 --- a/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/ComputingResourceDaoMapper.xml +++ b/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/ComputingResourceDaoMapper.xml @@ -2,30 +2,22 @@ - - - - - - - - - - - - - select - id,computing_resource,standard,description,create_by,create_time,update_by,update_time,state + id,resource_id,computing_resource,standard,description, + cpu_cores,memory_gb,gpu_memory_gb,gpu_nums,credit_per_hour,labels, + create_by,create_time,update_by,update_time,state from computing_resource where id = #{id} and state = 1 - select - id,computing_resource,standard,description,create_by,create_time,update_by,update_time,state + id,resource_id,computing_resource,standard,description, + cpu_cores,memory_gb,gpu_memory_gb,gpu_nums,credit_per_hour,labels, + create_by,create_time,update_by,update_time,state from computing_resource state = 1 diff --git a/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/ResourceOccupy.xml b/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/ResourceOccupy.xml new file mode 100644 index 00000000..5a7d9cc6 --- /dev/null +++ b/ruoyi-modules/management-platform/src/main/resources/mapper/managementPlatform/ResourceOccupy.xml @@ -0,0 +1,31 @@ + + + + + insert into resource_occupy (user_id, computing_resource_id, credit_per_hour) + values (#{resourceOccupy.userId}, #{resourceOccupy.computingResourceId}, #{resourceOccupy.creditPerHour}) + + + + update resource_occupy + + + state = #{resourceOccupy.state}, + + + deduce_last_time = #{resourceOccupy.deduceLastTime}, + + + where id = #{resourceOccupy.id} + + + + + + \ No newline at end of file