diff --git a/routers/admin/resources.go b/routers/admin/resources.go index f6689acd4..026c37e52 100644 --- a/routers/admin/resources.go +++ b/routers/admin/resources.go @@ -141,8 +141,7 @@ func GetAllResourceSpecificationList(ctx *context.Context) { status := ctx.QueryInt("status") cluster := ctx.Query("cluster") available := ctx.QueryInt("available") - list, err := resource.GetResourceSpecificationList(models.SearchResourceSpecificationOptions{ - ListOptions: models.ListOptions{Page: 1, PageSize: 1000}, + list, err := resource.GetAllDistinctResourceSpecification(models.SearchResourceSpecificationOptions{ QueueId: queue, Status: status, Cluster: cluster, @@ -153,6 +152,7 @@ func GetAllResourceSpecificationList(ctx *context.Context) { ctx.JSON(http.StatusOK, response.ServerError(err.Error())) return } + ctx.JSON(http.StatusOK, response.SuccessWithData(list)) } diff --git a/services/cloudbrain/resource/resource_specification.go b/services/cloudbrain/resource/resource_specification.go index c3c75419f..d23fd3aad 100644 --- a/services/cloudbrain/resource/resource_specification.go +++ b/services/cloudbrain/resource/resource_specification.go @@ -130,10 +130,48 @@ func GetResourceSpecificationList(opts models.SearchResourceSpecificationOptions if err != nil { return nil, err } - return models.NewResourceSpecAndQueueListRes(n, r), nil } +//GetAllDistinctResourceSpecification returns specification and queue after distinct +//totalSize is always 0 here +func GetAllDistinctResourceSpecification(opts models.SearchResourceSpecificationOptions) (*models.ResourceSpecAndQueueListRes, error) { + opts.Page = 0 + opts.PageSize = 1000 + _, r, err := models.SearchResourceSpecification(opts) + if err != nil { + return nil, err + } + nr := distinctResourceSpecAndQueue(r) + return models.NewResourceSpecAndQueueListRes(0, nr), nil +} + +func distinctResourceSpecAndQueue(r []models.ResourceSpecAndQueue) []models.ResourceSpecAndQueue { + specs := make([]models.ResourceSpecAndQueue, 0, len(r)) + sourceSpecIdMap := make(map[string]models.ResourceSpecAndQueue, 0) + for i := 0; i < len(r); i++ { + spec := r[i] + if spec.SourceSpecId == "" { + specs = append(specs, spec) + continue + } + if _, has := sourceSpecIdMap[spec.SourceSpecId]; has { + //prefer to use on-shelf spec + if sourceSpecIdMap[spec.SourceSpecId].Status != spec.Status && spec.Status == models.SpecOnShelf { + for k, v := range specs { + if v.ResourceSpecification.ID == sourceSpecIdMap[spec.SourceSpecId].ResourceSpecification.ID { + specs[k] = spec + } + } + } + continue + } + specs = append(specs, spec) + sourceSpecIdMap[spec.SourceSpecId] = spec + } + return specs +} + func GetResourceSpecificationScenes(specId int64) ([]models.ResourceSceneBriefRes, error) { r, err := models.GetSpecScenes(specId) if err != nil {