| @@ -194,6 +194,32 @@ func (Specification) TableName() string { | |||||
| return "resource_specification" | return "resource_specification" | ||||
| } | } | ||||
| func (s *Specification) ToShow() *SpecificationShow { | |||||
| return &SpecificationShow{ | |||||
| ID: s.ID, | |||||
| AccCardsNum: s.AccCardsNum, | |||||
| AccCardType: s.AccCardType, | |||||
| CpuCores: s.CpuCores, | |||||
| MemGiB: s.MemGiB, | |||||
| GPUMemGiB: s.GPUMemGiB, | |||||
| ShareMemGiB: s.ShareMemGiB, | |||||
| ComputeResource: s.ComputeResource, | |||||
| UnitPrice: s.UnitPrice, | |||||
| } | |||||
| } | |||||
| type SpecificationShow struct { | |||||
| ID int64 | |||||
| AccCardsNum int | |||||
| AccCardType string | |||||
| CpuCores int | |||||
| MemGiB float32 | |||||
| GPUMemGiB float32 | |||||
| ShareMemGiB float32 | |||||
| ComputeResource string | |||||
| UnitPrice int | |||||
| } | |||||
| func InsertResourceSpecification(r ResourceSpecification) (int64, error) { | func InsertResourceSpecification(r ResourceSpecification) (int64, error) { | ||||
| return x.Insert(&r) | return x.Insert(&r) | ||||
| } | } | ||||
| @@ -182,7 +182,7 @@ func UpdateResourceSpecification(ctx *context.Context, req models.ResourceSpecif | |||||
| if err != nil { | if err != nil { | ||||
| log.Error("UpdateResourceSpecification error. %v", err) | log.Error("UpdateResourceSpecification error. %v", err) | ||||
| ctx.JSON(http.StatusOK, response.ResponseError(err)) | |||||
| ctx.JSON(http.StatusOK, response.ResponseBizError(err)) | |||||
| return | return | ||||
| } | } | ||||
| ctx.JSON(http.StatusOK, response.Success()) | ctx.JSON(http.StatusOK, response.Success()) | ||||
| @@ -689,6 +689,10 @@ func RegisterRoutes(m *macaron.Macaron) { | |||||
| m.Post("/migrate", reqToken(), bind(auth.MigrateRepoForm{}), repo.Migrate) | m.Post("/migrate", reqToken(), bind(auth.MigrateRepoForm{}), repo.Migrate) | ||||
| m.Group("/specification", func() { | |||||
| m.Get("", repo.GetResourceSpec) | |||||
| }, reqToken()) | |||||
| m.Group("/:username/:reponame", func() { | m.Group("/:username/:reponame", func() { | ||||
| m.Combo("").Get(reqAnyRepoReader(), repo.Get). | m.Combo("").Get(reqAnyRepoReader(), repo.Get). | ||||
| Delete(reqToken(), reqOwner(), repo.Delete). | Delete(reqToken(), reqOwner(), repo.Delete). | ||||
| @@ -0,0 +1,36 @@ | |||||
| package repo | |||||
| import ( | |||||
| "code.gitea.io/gitea/models" | |||||
| "code.gitea.io/gitea/modules/context" | |||||
| "code.gitea.io/gitea/modules/log" | |||||
| "code.gitea.io/gitea/routers/response" | |||||
| "code.gitea.io/gitea/services/cloudbrain/resource" | |||||
| ) | |||||
| func GetResourceSpec(ctx *context.APIContext) { | |||||
| jobType := ctx.Query("jobType") | |||||
| computeResource := ctx.Query("compute") | |||||
| cluster := ctx.Query("cluster") | |||||
| aiCenterCode := ctx.Query("center") | |||||
| if jobType == "" || computeResource == "" || cluster == "" { | |||||
| log.Info("GetResourceSpec api.param error") | |||||
| ctx.JSON(200, response.ResponseBizError(response.PARAM_ERROR)) | |||||
| return | |||||
| } | |||||
| specs, err := resource.FindAvailableSpecs4Show(ctx.User.ID, models.FindSpecsOptions{ | |||||
| JobType: models.JobType(jobType), | |||||
| ComputeResource: computeResource, | |||||
| Cluster: cluster, | |||||
| AiCenterCode: aiCenterCode, | |||||
| }) | |||||
| if err != nil { | |||||
| log.Error("GetResourceSpec api error. %v", err) | |||||
| ctx.JSON(200, response.ResponseError(err)) | |||||
| return | |||||
| } | |||||
| specMap := make(map[string]interface{}, 0) | |||||
| specMap["Specs"] = specs | |||||
| ctx.JSON(200, response.SuccessWithData(specMap)) | |||||
| } | |||||
| @@ -24,10 +24,14 @@ func ServerError(msg string) *AiforgeResponse { | |||||
| return &AiforgeResponse{Code: RESPONSE_CODE_ERROR_DEFAULT, Msg: msg} | return &AiforgeResponse{Code: RESPONSE_CODE_ERROR_DEFAULT, Msg: msg} | ||||
| } | } | ||||
| func ResponseError(err *BizError) *AiforgeResponse { | |||||
| func ResponseBizError(err *BizError) *AiforgeResponse { | |||||
| return &AiforgeResponse{Code: err.Code, Msg: err.Err} | return &AiforgeResponse{Code: err.Code, Msg: err.Err} | ||||
| } | } | ||||
| func ResponseError(err error) *AiforgeResponse { | |||||
| return &AiforgeResponse{Code: RESPONSE_CODE_ERROR_DEFAULT, Msg: err.Error()} | |||||
| } | |||||
| func SuccessWithData(data interface{}) *AiforgeResponse { | func SuccessWithData(data interface{}) *AiforgeResponse { | ||||
| return &AiforgeResponse{Code: RESPONSE_CODE_SUCCESS, Msg: RESPONSE_MSG_SUCCESS, Data: data} | return &AiforgeResponse{Code: RESPONSE_CODE_SUCCESS, Msg: RESPONSE_MSG_SUCCESS, Data: data} | ||||
| } | } | ||||
| @@ -1,5 +1,7 @@ | |||||
| package response | package response | ||||
| var PARAM_ERROR = &BizError{Code: 9001, Err: "param error"} | |||||
| var RESOURCE_QUEUE_NOT_AVAILABLE = &BizError{Code: 1001, Err: "resource queue not available"} | var RESOURCE_QUEUE_NOT_AVAILABLE = &BizError{Code: 1001, Err: "resource queue not available"} | ||||
| var SPECIFICATION_NOT_EXIST = &BizError{Code: 1002, Err: "specification not exist"} | var SPECIFICATION_NOT_EXIST = &BizError{Code: 1002, Err: "specification not exist"} | ||||
| var SPECIFICATION_NOT_AVAILABLE = &BizError{Code: 1003, Err: "specification not available"} | var SPECIFICATION_NOT_AVAILABLE = &BizError{Code: 1003, Err: "specification not available"} | ||||
| @@ -210,6 +210,18 @@ func FindAvailableSpecs(userId int64, opts models.FindSpecsOptions) ([]*models.S | |||||
| return specs, err | return specs, err | ||||
| } | } | ||||
| func FindAvailableSpecs4Show(userId int64, opts models.FindSpecsOptions) ([]*models.SpecificationShow, error) { | |||||
| specs, err := FindAvailableSpecs(userId, opts) | |||||
| if err != nil { | |||||
| return nil, err | |||||
| } | |||||
| result := make([]*models.SpecificationShow, len(specs)) | |||||
| for i, v := range specs { | |||||
| result[i] = v.ToShow() | |||||
| } | |||||
| return result, nil | |||||
| } | |||||
| func filterExclusiveSpecs(r []*models.Specification, userId int64) []*models.Specification { | func filterExclusiveSpecs(r []*models.Specification, userId int64) []*models.Specification { | ||||
| specs := make([]*models.Specification, 0, len(r)) | specs := make([]*models.Specification, 0, len(r)) | ||||
| specMap := make(map[int64]string, 0) | specMap := make(map[int64]string, 0) | ||||