|
- package resource
-
- import (
- "code.gitea.io/gitea/models"
- "code.gitea.io/gitea/modules/grampus"
- "errors"
- "strings"
- )
-
- func AddResourceSpecification(req models.ResourceSpecificationReq) error {
- if req.Status == 0 {
- req.Status = models.SpecNotVerified
- }
- if _, err := models.InsertResourceSpecification(req.ToDTO()); err != nil {
- return err
- }
- return nil
- }
-
- func UpdateResourceSpecification(specId int64, req models.ResourceSpecificationReq) error {
- if _, err := models.UpdateResourceSpecificationById(specId, models.ResourceSpecification{
- UnitPrice: req.UnitPrice,
- }); err != nil {
- return err
- }
- return nil
- }
-
- func SyncGrampusSpecs(doerId int64) error {
- r, err := grampus.GetResourceSpecs("")
- if err != nil {
- return err
- }
- specUpdateList := make([]models.ResourceSpecification, 0)
- specInsertList := make([]models.ResourceSpecification, 0)
- existIds := make([]int64, 0)
- for _, spec := range r.Infos {
- for _, c := range spec.Centers {
- computeResource := models.ParseComputeResourceFormGrampus(spec.SpecInfo.AccDeviceKind)
- if computeResource == "" {
- continue
- }
- accCardType := strings.ToUpper(spec.SpecInfo.AccDeviceModel)
- // get resource queue.if queue not exist,skip it
- r, err := models.GetResourceQueue(&models.ResourceQueue{
- Cluster: models.C2NetCluster,
- AiCenterCode: c.ID,
- ComputeResource: computeResource,
- AccCardType: accCardType,
- })
- if err != nil || r == nil {
- continue
- }
-
- //Determine if this specification already exists.if exist,update params
- //if not exist,insert a new record and status is SpecNotVerified
- oldSpec, err := models.GetResourceSpecification(&models.ResourceSpecification{
- QueueId: r.ID,
- SourceSpecId: spec.ID,
- })
- if err != nil {
- return err
- }
-
- if oldSpec == nil {
- specInsertList = append(specInsertList, models.ResourceSpecification{
- QueueId: r.ID,
- SourceSpecId: spec.ID,
- AccCardsNum: spec.SpecInfo.AccDeviceNum,
- CpuCores: spec.SpecInfo.CpuCoreNum,
- MemGiB: models.ParseMemSizeFromGrampus(spec.SpecInfo.MemorySize),
- GPUMemGiB: models.ParseMemSizeFromGrampus(spec.SpecInfo.AccDeviceMemory),
- Status: models.SpecNotVerified,
- IsAutomaticSync: true,
- CreatedBy: doerId,
- UpdatedBy: doerId,
- })
- } else {
- existIds = append(existIds, oldSpec.ID)
- specUpdateList = append(specUpdateList, models.ResourceSpecification{
- ID: oldSpec.ID,
- AccCardsNum: spec.SpecInfo.AccDeviceNum,
- CpuCores: spec.SpecInfo.CpuCoreNum,
- MemGiB: models.ParseMemSizeFromGrampus(spec.SpecInfo.MemorySize),
- GPUMemGiB: models.ParseMemSizeFromGrampus(spec.SpecInfo.AccDeviceMemory),
- UpdatedBy: doerId,
- })
- }
-
- }
- }
- return models.SyncGrampusSpecs(specUpdateList, specInsertList, existIds)
- }
-
- //GetResourceSpecificationList returns specification and queue
- func GetResourceSpecificationList(opts models.SearchResourceSpecificationOptions) (*models.ResourceSpecAndQueueListRes, error) {
- n, r, err := models.SearchResourceSpecification(opts)
- if err != nil {
- return nil, err
- }
-
- return models.NewResourceSpecAndQueueListRes(n, r), nil
- }
-
- func ResourceSpecOnShelf(id int64, req models.ResourceSpecificationReq) error {
- spec, err := models.GetResourceSpecification(&models.ResourceSpecification{ID: id})
- if err != nil {
- return err
- }
- if spec == nil {
- return errors.New("specification not exist")
- }
- if q, err := models.GetResourceQueue(&models.ResourceQueue{ID: spec.QueueId}); err != nil || q == nil {
- return errors.New("resource queue not available")
- }
-
- _, err = models.ResourceSpecOnShelf(id, models.ResourceSpecification{
- UnitPrice: req.UnitPrice,
- })
- if err != nil {
- return err
- }
- return nil
-
- }
- func ResourceSpecOffShelf(id int64) error {
- _, err := models.ResourceSpecOffShelf(id)
- if err != nil {
- return err
- }
- return nil
-
- }
|