From 81c21237cb3e672014d198cf572c4374f775cdb2 Mon Sep 17 00:00:00 2001 From: Jialei <3217223+jialeicui@users.noreply.github.com> Date: Wed, 24 Aug 2022 15:47:33 +0800 Subject: [PATCH] feat(controller): get resource pools api (#978) --- .../ai/starwhale/mlops/api/SystemApi.java | 18 +++++++- .../starwhale/mlops/api/SystemController.java | 8 ++++ .../api/protocol/system/ResourcePoolVO.java | 42 +++++++++++++++++++ .../mlops/domain/system/SystemService.java | 14 +++++++ .../resourcepool/ResourcePoolConverter.java | 22 +++++++++- 5 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 server/controller/src/main/java/ai/starwhale/mlops/api/protocol/system/ResourcePoolVO.java diff --git a/server/controller/src/main/java/ai/starwhale/mlops/api/SystemApi.java b/server/controller/src/main/java/ai/starwhale/mlops/api/SystemApi.java index 3ad599fb1f..34c2ad4975 100644 --- a/server/controller/src/main/java/ai/starwhale/mlops/api/SystemApi.java +++ b/server/controller/src/main/java/ai/starwhale/mlops/api/SystemApi.java @@ -18,6 +18,7 @@ import ai.starwhale.mlops.api.protocol.ResponseMessage; import ai.starwhale.mlops.api.protocol.agent.AgentVO; +import ai.starwhale.mlops.api.protocol.system.ResourcePoolVO; import ai.starwhale.mlops.api.protocol.system.SystemVersionVO; import ai.starwhale.mlops.api.protocol.system.UpgradeProgressVO; import com.github.pagehelper.PageInfo; @@ -32,12 +33,13 @@ import javax.validation.Valid; import org.springframework.http.ResponseEntity; import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; +import java.util.List; + @Tag(name = "System") @Validated public interface SystemApi { @@ -68,6 +70,20 @@ ResponseEntity>> listAgent( @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize); + @Operation(summary = "Get the list of resource pool") + @ApiResponses( + value = { + @ApiResponse( + responseCode = "200", + description = "ok", + content = + @Content( + mediaType = "application/json", + schema = @Schema(implementation = List.class))) + }) + @GetMapping(value = "/system/resourcePool") + ResponseEntity>> listResourcePools(); + @Operation(summary = "Upgrade system version or cancel upgrade") @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "ok")}) @PostMapping(value = "/system/version/{action}") diff --git a/server/controller/src/main/java/ai/starwhale/mlops/api/SystemController.java b/server/controller/src/main/java/ai/starwhale/mlops/api/SystemController.java index c6f9c7c0e8..353a7b89d6 100644 --- a/server/controller/src/main/java/ai/starwhale/mlops/api/SystemController.java +++ b/server/controller/src/main/java/ai/starwhale/mlops/api/SystemController.java @@ -19,6 +19,7 @@ import ai.starwhale.mlops.api.protocol.Code; import ai.starwhale.mlops.api.protocol.ResponseMessage; import ai.starwhale.mlops.api.protocol.agent.AgentVO; +import ai.starwhale.mlops.api.protocol.system.ResourcePoolVO; import ai.starwhale.mlops.api.protocol.system.SystemVersionVO; import ai.starwhale.mlops.api.protocol.system.UpgradeProgressVO; import ai.starwhale.mlops.api.protocol.system.UpgradeProgressVO.PhaseEnum; @@ -30,6 +31,8 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import java.util.List; + @RestController @RequestMapping("${sw.controller.apiPrefix}") public class SystemController implements SystemApi{ @@ -45,6 +48,11 @@ public ResponseEntity>> listAgent(String ip, I return ResponseEntity.ok(Code.success.asResponse(pageInfo)); } + @Override + public ResponseEntity>> listResourcePools() { + return ResponseEntity.ok(Code.success.asResponse(systemService.listResourcePools())); + } + @Override public ResponseEntity> systemVersionAction(String action) { return ResponseEntity.ok(Code.success.asResponse("Unknown action")); diff --git a/server/controller/src/main/java/ai/starwhale/mlops/api/protocol/system/ResourcePoolVO.java b/server/controller/src/main/java/ai/starwhale/mlops/api/protocol/system/ResourcePoolVO.java new file mode 100644 index 0000000000..356b1ce272 --- /dev/null +++ b/server/controller/src/main/java/ai/starwhale/mlops/api/protocol/system/ResourcePoolVO.java @@ -0,0 +1,42 @@ +/* + * Copyright 2022 Starwhale, Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ai.starwhale.mlops.api.protocol.system; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Builder; +import lombok.Data; +import org.springframework.validation.annotation.Validated; + +import java.io.Serializable; + +@Data +@Builder +@Schema(description = "Resource Pool for job execution", title = "Resource Pool") +@Validated +public class ResourcePoolVO implements Serializable { + String id; + + String label; + + String name; + + String description; + + public static ResourcePoolVO empty() { + return new ResourcePoolVO("", "", "", ""); + } +} diff --git a/server/controller/src/main/java/ai/starwhale/mlops/domain/system/SystemService.java b/server/controller/src/main/java/ai/starwhale/mlops/domain/system/SystemService.java index b4ab202651..46ae792d66 100644 --- a/server/controller/src/main/java/ai/starwhale/mlops/domain/system/SystemService.java +++ b/server/controller/src/main/java/ai/starwhale/mlops/domain/system/SystemService.java @@ -17,11 +17,14 @@ package ai.starwhale.mlops.domain.system; import ai.starwhale.mlops.api.protocol.agent.AgentVO; +import ai.starwhale.mlops.api.protocol.system.ResourcePoolVO; import ai.starwhale.mlops.common.PageParams; import ai.starwhale.mlops.common.util.PageUtil; import ai.starwhale.mlops.domain.system.agent.AgentCache; import ai.starwhale.mlops.domain.system.agent.AgentConverter; +import ai.starwhale.mlops.domain.system.mapper.ResourcePoolMapper; import ai.starwhale.mlops.domain.system.po.AgentEntity; +import ai.starwhale.mlops.domain.system.resourcepool.ResourcePoolConverter; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import java.util.List; @@ -42,6 +45,12 @@ public class SystemService { @Resource private AgentConverter agentConverter; + @Resource + private ResourcePoolMapper resourcePoolMapper; + + @Resource + private ResourcePoolConverter resourcePoolConverter; + @Value("${sw.version}") private String controllerVersion; @@ -56,4 +65,9 @@ public String controllerVersion(){ return controllerVersion; } + public ListlistResourcePools() { + var entities = resourcePoolMapper.listResourcePools(); + return entities.stream().map(resourcePoolConverter::toResourcePoolVO).collect(Collectors.toList()); + } + } diff --git a/server/controller/src/main/java/ai/starwhale/mlops/domain/system/resourcepool/ResourcePoolConverter.java b/server/controller/src/main/java/ai/starwhale/mlops/domain/system/resourcepool/ResourcePoolConverter.java index 97dc7d5091..1f9a4b61c6 100644 --- a/server/controller/src/main/java/ai/starwhale/mlops/domain/system/resourcepool/ResourcePoolConverter.java +++ b/server/controller/src/main/java/ai/starwhale/mlops/domain/system/resourcepool/ResourcePoolConverter.java @@ -16,17 +16,24 @@ package ai.starwhale.mlops.domain.system.resourcepool; +import ai.starwhale.mlops.api.protocol.system.ResourcePoolVO; +import ai.starwhale.mlops.common.IDConvertor; import ai.starwhale.mlops.domain.system.po.ResourcePoolEntity; import ai.starwhale.mlops.domain.system.resourcepool.bo.ResourcePool; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; +import javax.annotation.Resource; + /** - * convert between ResourcePool and ResourcePoolEntity + * convert between ResourcePool ResourcePoolVO and ResourcePoolEntity */ @Slf4j @Component public class ResourcePoolConverter { + @Resource + private IDConvertor idConvertor; + public ResourcePoolEntity toEntity(ResourcePool pool) { return ResourcePoolEntity.builder().label(pool.getLabel()).build(); } @@ -34,4 +41,17 @@ public ResourcePoolEntity toEntity(ResourcePool pool) { public ResourcePool toResourcePool(ResourcePoolEntity entity) { return ResourcePool.builder().label(entity.getLabel()).build(); } + + public ResourcePoolVO toResourcePoolVO(ResourcePoolEntity entity) { + if (entity == null) { + return ResourcePoolVO.empty(); + } + + return ResourcePoolVO.builder() + .id(idConvertor.convert(entity.getId())) + .label(entity.getLabel()) + .name(entity.getName()) + .description(entity.getDescription()) + .build(); + } }