From 6366003ab1cd3443b42b4c3826f2b5783d7f082d Mon Sep 17 00:00:00 2001 From: chenyifan01 Date: Fri, 19 Aug 2022 10:23:45 +0800 Subject: [PATCH] #2701 find available specs --- models/cloudbrain.go | 4 ++ models/resource_specification.go | 60 +++++++++++++++++++ routers/repo/cloudbrain.go | 8 +++ .../resource/resource_specification.go | 25 ++++++++ 4 files changed, 97 insertions(+) diff --git a/models/cloudbrain.go b/models/cloudbrain.go index 61b7abd47..67d6f4244 100755 --- a/models/cloudbrain.go +++ b/models/cloudbrain.go @@ -119,6 +119,10 @@ const ( //AI center AICenterOfCloudBrainOne = "OpenIOne" AICenterOfCloudBrainTwo = "OpenITwo" + + //ComputeResource + GPU = "GPU" + NPU = "NPU" ) type Cloudbrain struct { diff --git a/models/resource_specification.go b/models/resource_specification.go index dca6647ab..60e59b253 100644 --- a/models/resource_specification.go +++ b/models/resource_specification.go @@ -141,6 +141,37 @@ func (r ResourceSpecAndQueue) ConvertToRes() *ResourceSpecAndQueueRes { } } +type FindSpecsOptions struct { + JobType JobType + ComputeResource string + Cluster string + AiCenterCode string +} + +type Specification struct { + ID int64 + SourceSpecId string + AccCardsNum int + AccCardType string + CpuCores int + MemGiB float32 + GPUMemGiB float32 + ShareMemGiB float32 + ComputeResource string + UnitPrice int + QueueId int64 + QueueCode string + Cluster string + AiCenterCode string + AiCenterName string + IsExclusive bool + ExclusiveOrg string +} + +func (Specification) TableName() string { + return "resource_specification" +} + func InsertResourceSpecification(r ResourceSpecification) (int64, error) { return x.Insert(&r) } @@ -283,3 +314,32 @@ func SyncGrampusSpecs(updateList []ResourceSpecification, insertList []ResourceS return sess.Commit() } + +func FindAvailableSpecs(opts FindSpecsOptions) ([]Specification, error) { + var cond = builder.NewCond() + if opts.JobType != "" { + cond = cond.And(builder.Eq{"resource_scene.job_type": opts.JobType}) + } + if opts.ComputeResource != "" { + cond = cond.And(builder.Eq{"resource_queue.compute_resource": opts.ComputeResource}) + } + if opts.ComputeResource != "" { + cond = cond.And(builder.Eq{"resource_queue.cluster": opts.Cluster}) + } + if opts.AiCenterCode != "" { + cond = cond.And(builder.Eq{"resource_queue.ai_center_code": opts.AiCenterCode}) + } + cond = cond.And(builder.Or(builder.Eq{"resource_scene.delete_time": 0}, builder.IsNull{"resource_scene.delete_time"})) + + r := make([]Specification, 0) + err := x.Where(cond). + Join("INNER", "resource_scene_spec", "resource_scene_spec.spec_id = resource_specification.id"). + Join("INNER", "resource_scene", "resource_scene_spec.scene_id = resource_scene.id"). + Join("INNER", "resource_queue", "resource_queue.id = resource_specification.queue_id"). + OrderBy("resource_queue.compute_resource asc,resource_queue.acc_card_type asc,resource_specification.acc_cards_num asc"). + Unscoped().Find(&r) + if err != nil { + return nil, err + } + return r, nil +} diff --git a/routers/repo/cloudbrain.go b/routers/repo/cloudbrain.go index 76bf9b076..97ba9bde4 100755 --- a/routers/repo/cloudbrain.go +++ b/routers/repo/cloudbrain.go @@ -2,6 +2,7 @@ package repo import ( "bufio" + "code.gitea.io/gitea/services/cloudbrain/resource" "encoding/json" "errors" "fmt" @@ -166,6 +167,13 @@ func cloudBrainNewDataPrepare(ctx *context.Context) error { if cloudbrain.InferenceResourceSpecs != nil { ctx.Data["inference_resource_specs"] = cloudbrain.InferenceResourceSpecs.ResourceSpec } + specs, _ := resource.FindAvailableSpecs(ctx.User.ID, models.FindSpecsOptions{ + JobType: models.JobTypeDebug, + ComputeResource: models.GPU, + Cluster: models.OpenICluster, + AiCenterCode: models.AICenterOfCloudBrainOne, + }) + ctx.Data["Specs"] = specs if cloudbrain.SpecialPools != nil { var debugGpuTypes []*models.GpuInfo diff --git a/services/cloudbrain/resource/resource_specification.go b/services/cloudbrain/resource/resource_specification.go index 680b98933..31c8b3b25 100644 --- a/services/cloudbrain/resource/resource_specification.go +++ b/services/cloudbrain/resource/resource_specification.go @@ -184,3 +184,28 @@ func AddSpecOperateLog(doerId int64, operateType string, newValue, oldValue *mod Comment: comment, }) } + +func FindAvailableSpecs(userId int64, opts models.FindSpecsOptions) ([]models.Specification, error) { + r, err := models.FindAvailableSpecs(opts) + if err != nil { + log.Error("FindAvailableSpecs error.%v", err) + return nil, err + } + specs := make([]models.Specification, 0, len(r)) + //filter exclusive spec + for i := 0; i < len(r); i++ { + spec := r[i] + if !spec.IsExclusive { + specs = append(specs, spec) + continue + } + orgs := strings.Split(spec.ExclusiveOrg, ";") + for _, org := range orgs { + isMember, _ := models.IsOrganizationMemberByOrgName(org, userId) + if isMember { + specs = append(specs, spec) + } + } + } + return specs, err +}