|
|
@@ -909,3 +909,269 @@ func GetGrampusSpecs() (map[string]*Specification, error) { |
|
|
|
} |
|
|
|
return grampusSpecs, nil |
|
|
|
} |
|
|
|
|
|
|
|
type GetResourceListOpts struct { |
|
|
|
ListOptions |
|
|
|
Resource []string |
|
|
|
AccCardType string |
|
|
|
AccCardNum int |
|
|
|
ExcludeAccCardNums []int |
|
|
|
AICenterCode string |
|
|
|
MinPrice int |
|
|
|
MaxPrice int |
|
|
|
} |
|
|
|
|
|
|
|
type ResourceDetailInfo struct { |
|
|
|
Spec ResourceSpecificationRes |
|
|
|
IsQueueExclusive bool |
|
|
|
AICenterList []ResourceAiCenterRes |
|
|
|
} |
|
|
|
|
|
|
|
type ResourceInfo4CardRequest struct { |
|
|
|
ComputeResource string |
|
|
|
AccCardType string |
|
|
|
AccCardsNum int |
|
|
|
CpuCores int |
|
|
|
MemGiB float32 |
|
|
|
GPUMemGiB float32 |
|
|
|
ShareMemGiB float32 |
|
|
|
UnitPrice int |
|
|
|
IsExclusive bool |
|
|
|
IsSpecExclusive string |
|
|
|
AICenterList []*ResourceAiCenterRes |
|
|
|
} |
|
|
|
|
|
|
|
func (r *ResourceInfo4CardRequest) Tr(language string) { |
|
|
|
if r.AICenterList == nil { |
|
|
|
return |
|
|
|
} |
|
|
|
for i := 0; i < len(r.AICenterList); i++ { |
|
|
|
r.AICenterList[i].Tr(language) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
type ResourceWithAICenter4CardRequest struct { |
|
|
|
Cluster string |
|
|
|
AICenterCode string |
|
|
|
AICenterName string |
|
|
|
ComputeResource string |
|
|
|
AccCardType string |
|
|
|
AccCardsNum int |
|
|
|
CpuCores int |
|
|
|
MemGiB float32 |
|
|
|
GPUMemGiB float32 |
|
|
|
ShareMemGiB float32 |
|
|
|
UnitPrice int |
|
|
|
IsExclusive bool |
|
|
|
IsSpecExclusive string |
|
|
|
} |
|
|
|
|
|
|
|
func GetResourceListPaging(opts GetResourceListOpts) ([]*ResourceInfo4CardRequest, int64, error) { |
|
|
|
cond := builder.NewCond() |
|
|
|
resourceList := make([]string, 0) |
|
|
|
for i := 0; i < len(opts.Resource); i++ { |
|
|
|
if opts.Resource[i] != "" { |
|
|
|
resourceList = append(resourceList, opts.Resource[i]) |
|
|
|
} |
|
|
|
} |
|
|
|
if len(resourceList) > 0 { |
|
|
|
cond = cond.And(builder.In("resource_queue.compute_resource", resourceList)) |
|
|
|
} |
|
|
|
if opts.AccCardType != "" { |
|
|
|
cond = cond.And(builder.Eq{"resource_queue.acc_card_type": opts.AccCardType}) |
|
|
|
} |
|
|
|
if opts.AccCardNum >= 0 { |
|
|
|
if opts.AccCardNum > 999 { |
|
|
|
cond = cond.And(builder.NotIn("resource_specification.acc_cards_num", opts.ExcludeAccCardNums)) |
|
|
|
} else { |
|
|
|
cond = cond.And(builder.Eq{"resource_specification.acc_cards_num": opts.AccCardNum}) |
|
|
|
} |
|
|
|
} |
|
|
|
if opts.AICenterCode != "" { |
|
|
|
cond = cond.And(builder.Eq{"resource_queue.ai_center_code": opts.AICenterCode}) |
|
|
|
} |
|
|
|
if opts.MaxPrice >= 0 && opts.MinPrice >= 0 && opts.MaxPrice < opts.MinPrice { |
|
|
|
opts.MaxPrice = -1 |
|
|
|
opts.MinPrice = -1 |
|
|
|
} |
|
|
|
if opts.MaxPrice >= 0 { |
|
|
|
cond = cond.And(builder.Lte{"resource_specification.unit_price": opts.MaxPrice}) |
|
|
|
} |
|
|
|
if opts.MinPrice >= 0 { |
|
|
|
cond = cond.And(builder.Gte{"resource_specification.unit_price": opts.MinPrice}) |
|
|
|
} |
|
|
|
cond = cond.And(builder.Or(builder.Eq{"resource_queue.deleted_time": 0}, builder.IsNull{"resource_queue.deleted_time"})) |
|
|
|
cond = cond.And(builder.Eq{"resource_specification.status": 2}) |
|
|
|
//先按多字段去重分页查询资源规格 |
|
|
|
//再基于结果查询智算中心信息 |
|
|
|
resourceInfos := make([]*ResourceInfo4CardRequest, 0) |
|
|
|
err := x.Table("resource_specification"). |
|
|
|
Join("LEFT", "resource_exclusive_pool", "resource_specification.queue_id = resource_exclusive_pool.queue_id"). |
|
|
|
Join("INNER", "resource_queue", "resource_specification.queue_id = resource_queue.id"). |
|
|
|
Join("INNER", "resource_scene_spec", "resource_specification.id = resource_scene_spec.spec_id"). |
|
|
|
Join("INNER", "resource_scene", "resource_scene.id = resource_scene_spec.scene_id"). |
|
|
|
Select("Distinct resource_queue.compute_resource, resource_queue.acc_card_type," + |
|
|
|
"resource_specification.acc_cards_num, resource_specification.cpu_cores, resource_specification.mem_gi_b, " + |
|
|
|
"resource_specification.gpu_mem_gi_b, resource_specification.share_mem_gi_b, resource_specification.unit_price," + |
|
|
|
"COALESCE(resource_exclusive_pool.queue_id IS NOT NULL, false) AS is_exclusive,resource_scene.is_spec_exclusive"). |
|
|
|
Where(cond). |
|
|
|
OrderBy(" resource_queue.compute_resource DESC,resource_queue.acc_card_type ,is_exclusive," + |
|
|
|
"resource_specification.acc_cards_num DESC,resource_specification.gpu_mem_gi_b DESC," + |
|
|
|
"resource_specification.cpu_cores DESC,resource_specification.mem_gi_b DESC"). |
|
|
|
Find(&resourceInfos) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
return nil, 0, err |
|
|
|
} |
|
|
|
tmpResourceInfos := make([]*ResourceInfo4CardRequest, 0) |
|
|
|
for i := 0; i < len(resourceInfos); i++ { |
|
|
|
//此处是为了过滤那些专属池中的规格又被配置到共享场景中的情况 |
|
|
|
if !resourceInfos[i].IsExclusive || (resourceInfos[i].IsExclusive && resourceInfos[i].IsSpecExclusive == "") { |
|
|
|
tmpResourceInfos = append(tmpResourceInfos, resourceInfos[i]) |
|
|
|
} |
|
|
|
} |
|
|
|
resourceInfos = tmpResourceInfos |
|
|
|
if len(resourceInfos) == 0 { |
|
|
|
return []*ResourceInfo4CardRequest{}, 0, nil |
|
|
|
} |
|
|
|
|
|
|
|
total := int64(len(resourceInfos)) |
|
|
|
startIndex := int64((opts.Page - 1) * opts.PageSize) |
|
|
|
endIndex := int64(opts.Page * opts.PageSize) |
|
|
|
if startIndex >= total { |
|
|
|
return []*ResourceInfo4CardRequest{}, 0, nil |
|
|
|
} |
|
|
|
if endIndex > total { |
|
|
|
endIndex = total |
|
|
|
} |
|
|
|
resourceInfos = resourceInfos[startIndex:endIndex] |
|
|
|
|
|
|
|
newCond := builder.NewCond() |
|
|
|
for _, spec := range resourceInfos { |
|
|
|
if spec.IsExclusive { |
|
|
|
newCond = newCond.Or(builder.And(builder.Eq{"resource_queue.compute_resource": spec.ComputeResource}, |
|
|
|
builder.Eq{"resource_queue.acc_card_type": spec.AccCardType}, |
|
|
|
builder.Eq{"resource_specification.acc_cards_num": spec.AccCardsNum}, |
|
|
|
builder.Eq{"resource_specification.cpu_cores": spec.CpuCores}, |
|
|
|
builder.Eq{"resource_specification.mem_gi_b": spec.MemGiB}, |
|
|
|
builder.Eq{"resource_specification.gpu_mem_gi_b": spec.GPUMemGiB}, |
|
|
|
builder.Eq{"resource_specification.share_mem_gi_b": spec.ShareMemGiB}, |
|
|
|
builder.Eq{"resource_specification.unit_price": spec.UnitPrice}, |
|
|
|
builder.NotNull{"resource_exclusive_pool.queue_id"})) |
|
|
|
} else if spec.IsSpecExclusive == SpecExclusive { |
|
|
|
newCond = newCond.Or(builder.And(builder.Eq{"resource_queue.compute_resource": spec.ComputeResource}, |
|
|
|
builder.Eq{"resource_queue.acc_card_type": spec.AccCardType}, |
|
|
|
builder.Eq{"resource_specification.acc_cards_num": spec.AccCardsNum}, |
|
|
|
builder.Eq{"resource_specification.cpu_cores": spec.CpuCores}, |
|
|
|
builder.Eq{"resource_specification.mem_gi_b": spec.MemGiB}, |
|
|
|
builder.Eq{"resource_specification.gpu_mem_gi_b": spec.GPUMemGiB}, |
|
|
|
builder.Eq{"resource_specification.share_mem_gi_b": spec.ShareMemGiB}, |
|
|
|
builder.Eq{"resource_specification.unit_price": spec.UnitPrice}, |
|
|
|
builder.Eq{"resource_scene.is_spec_exclusive": SpecExclusive}, |
|
|
|
builder.IsNull{"resource_exclusive_pool.queue_id"})) |
|
|
|
} else { |
|
|
|
newCond = newCond.Or(builder.And(builder.Eq{"resource_queue.compute_resource": spec.ComputeResource}, |
|
|
|
builder.Eq{"resource_queue.acc_card_type": spec.AccCardType}, |
|
|
|
builder.Eq{"resource_specification.acc_cards_num": spec.AccCardsNum}, |
|
|
|
builder.Eq{"resource_specification.cpu_cores": spec.CpuCores}, |
|
|
|
builder.Eq{"resource_specification.mem_gi_b": spec.MemGiB}, |
|
|
|
builder.Eq{"resource_specification.gpu_mem_gi_b": spec.GPUMemGiB}, |
|
|
|
builder.Eq{"resource_specification.share_mem_gi_b": spec.ShareMemGiB}, |
|
|
|
builder.Eq{"resource_specification.unit_price": spec.UnitPrice}, |
|
|
|
builder.Or(builder.Eq{"resource_scene.is_spec_exclusive": SpecPublic}, builder.IsNull{"resource_scene.is_spec_exclusive"}), |
|
|
|
builder.IsNull{"resource_exclusive_pool.queue_id"})) |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
newCond = newCond.And(builder.Or(builder.Eq{"resource_queue.deleted_time": 0}, builder.IsNull{"resource_queue.deleted_time"})) |
|
|
|
newCond = newCond.And(builder.Eq{"resource_specification.status": 2}) |
|
|
|
|
|
|
|
withCenterInfos := make([]ResourceWithAICenter4CardRequest, 0) |
|
|
|
err = x.Table("resource_specification"). |
|
|
|
Join("LEFT", "resource_exclusive_pool", "resource_specification.queue_id = resource_exclusive_pool.queue_id"). |
|
|
|
Join("INNER", "resource_queue", "resource_specification.queue_id = resource_queue.id"). |
|
|
|
Join("INNER", "resource_scene_spec", "resource_specification.id = resource_scene_spec.spec_id"). |
|
|
|
Join("INNER", "resource_scene", "resource_scene.id = resource_scene_spec.scene_id"). |
|
|
|
Select("resource_queue.cluster,resource_queue.ai_center_code,resource_queue.ai_center_name,resource_queue.compute_resource, resource_queue.acc_card_type," + |
|
|
|
"resource_specification.acc_cards_num, resource_specification.cpu_cores, resource_specification.mem_gi_b, " + |
|
|
|
"resource_specification.gpu_mem_gi_b, resource_specification.share_mem_gi_b, resource_specification.unit_price," + |
|
|
|
"COALESCE(resource_exclusive_pool.queue_id IS NOT NULL, false) AS is_exclusive,resource_scene.is_spec_exclusive"). |
|
|
|
Where(newCond). |
|
|
|
Find(&withCenterInfos) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
return nil, 0, err |
|
|
|
} |
|
|
|
tmpMap := make(map[string][]*ResourceAiCenterRes, 0) |
|
|
|
for i := 0; i < len(withCenterInfos); i++ { |
|
|
|
t := withCenterInfos[i] |
|
|
|
key := fmt.Sprintf("%s_%s_%d_%d_%f_%f_%f_%d_%t_%s", t.ComputeResource, t.AccCardType, t.AccCardsNum, |
|
|
|
t.CpuCores, t.MemGiB, t.GPUMemGiB, t.ShareMemGiB, t.UnitPrice, t.IsExclusive, t.IsSpecExclusive) |
|
|
|
if _, exists := tmpMap[key]; exists { |
|
|
|
centerExists := false |
|
|
|
for _, center := range tmpMap[key] { |
|
|
|
if center.AiCenterCode == t.AICenterCode { |
|
|
|
centerExists = true |
|
|
|
} |
|
|
|
} |
|
|
|
if centerExists { |
|
|
|
continue |
|
|
|
} |
|
|
|
tmpMap[key] = append(tmpMap[key], &ResourceAiCenterRes{ |
|
|
|
AiCenterCode: t.AICenterCode, |
|
|
|
AiCenterName: t.AICenterName, |
|
|
|
}) |
|
|
|
} else { |
|
|
|
tmpMap[key] = []*ResourceAiCenterRes{{ |
|
|
|
AiCenterCode: t.AICenterCode, |
|
|
|
AiCenterName: t.AICenterName, |
|
|
|
}} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
for i := 0; i < len(resourceInfos); i++ { |
|
|
|
t := resourceInfos[i] |
|
|
|
key := fmt.Sprintf("%s_%s_%d_%d_%f_%f_%f_%d_%t_%s", t.ComputeResource, t.AccCardType, t.AccCardsNum, |
|
|
|
t.CpuCores, t.MemGiB, t.GPUMemGiB, t.ShareMemGiB, t.UnitPrice, t.IsExclusive, t.IsSpecExclusive) |
|
|
|
resourceInfos[i].AICenterList = tmpMap[key] |
|
|
|
} |
|
|
|
return resourceInfos, total, nil |
|
|
|
} |
|
|
|
|
|
|
|
type AccCardInfo struct { |
|
|
|
ComputeSource string |
|
|
|
CardList []string |
|
|
|
} |
|
|
|
|
|
|
|
func GetAccCardList() ([]AccCardInfo, error) { |
|
|
|
res := make([]AccCardInfo, 0) |
|
|
|
r := make([]*Specification, 0) |
|
|
|
err := x.Where("resource_specification.status = ? and (resource_queue.deleted_time = 0 or resource_queue.deleted_time is null)", SpecOnShelf). |
|
|
|
Join("INNER", "resource_queue", "resource_queue.id = resource_specification.queue_id"). |
|
|
|
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"). |
|
|
|
OrderBy("resource_queue.compute_resource asc,resource_queue.acc_card_type asc"). |
|
|
|
Unscoped().Distinct("resource_queue.compute_resource,resource_queue.acc_card_type").Find(&r) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
} |
|
|
|
tmpMap := make(map[string][]string, 0) |
|
|
|
keys := make([]string, 0) |
|
|
|
for i := 0; i < len(r); i++ { |
|
|
|
spec := r[i] |
|
|
|
if _, exists := tmpMap[spec.ComputeResource]; exists { |
|
|
|
tmpMap[spec.ComputeResource] = append(tmpMap[spec.ComputeResource], spec.AccCardType) |
|
|
|
} else { |
|
|
|
keys = append(keys, spec.ComputeResource) |
|
|
|
tmpMap[spec.ComputeResource] = []string{spec.AccCardType} |
|
|
|
} |
|
|
|
} |
|
|
|
for i := 0; i < len(keys); i++ { |
|
|
|
res = append(res, AccCardInfo{ |
|
|
|
ComputeSource: keys[i], |
|
|
|
CardList: tmpMap[keys[i]], |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
return res, nil |
|
|
|
} |