Skip to content

Commit

Permalink
feat(controller): get resource pools api (star-whale#978)
Browse files Browse the repository at this point in the history
  • Loading branch information
jialeicui authored and dreamlandliu committed Aug 25, 2022
1 parent a17f923 commit 81c2123
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -68,6 +70,20 @@ ResponseEntity<ResponseMessage<PageInfo<AgentVO>>> 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<ResponseMessage<List<ResourcePoolVO>>> listResourcePools();

@Operation(summary = "Upgrade system version or cancel upgrade")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "ok")})
@PostMapping(value = "/system/version/{action}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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{
Expand All @@ -45,6 +48,11 @@ public ResponseEntity<ResponseMessage<PageInfo<AgentVO>>> listAgent(String ip, I
return ResponseEntity.ok(Code.success.asResponse(pageInfo));
}

@Override
public ResponseEntity<ResponseMessage<List<ResourcePoolVO>>> listResourcePools() {
return ResponseEntity.ok(Code.success.asResponse(systemService.listResourcePools()));
}

@Override
public ResponseEntity<ResponseMessage<String>> systemVersionAction(String action) {
return ResponseEntity.ok(Code.success.asResponse("Unknown action"));
Expand Down
Original file line number Diff line number Diff line change
@@ -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("", "", "", "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -56,4 +65,9 @@ public String controllerVersion(){
return controllerVersion;
}

public List<ResourcePoolVO>listResourcePools() {
var entities = resourcePoolMapper.listResourcePools();
return entities.stream().map(resourcePoolConverter::toResourcePoolVO).collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,42 @@

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();
}

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();
}
}

0 comments on commit 81c2123

Please sign in to comment.