You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

resource_specification.go 3.9 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package resource
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/grampus"
  5. "errors"
  6. "strings"
  7. )
  8. func AddResourceSpecification(req models.ResourceSpecificationReq) error {
  9. if req.Status == 0 {
  10. req.Status = models.SpecNotVerified
  11. }
  12. if _, err := models.InsertResourceSpecification(req.ToDTO()); err != nil {
  13. return err
  14. }
  15. return nil
  16. }
  17. func UpdateResourceSpecification(specId int64, req models.ResourceSpecificationReq) error {
  18. if _, err := models.UpdateResourceSpecificationById(specId, models.ResourceSpecification{
  19. UnitPrice: req.UnitPrice,
  20. }); err != nil {
  21. return err
  22. }
  23. return nil
  24. }
  25. func SyncGrampusSpecs(doerId int64) error {
  26. r, err := grampus.GetResourceSpecs("")
  27. if err != nil {
  28. return err
  29. }
  30. specUpdateList := make([]models.ResourceSpecification, 0)
  31. specInsertList := make([]models.ResourceSpecification, 0)
  32. existIds := make([]int64, 0)
  33. for _, spec := range r.Infos {
  34. for _, c := range spec.Centers {
  35. computeResource := models.ParseComputeResourceFormGrampus(spec.SpecInfo.AccDeviceKind)
  36. if computeResource == "" {
  37. continue
  38. }
  39. accCardType := strings.ToUpper(spec.SpecInfo.AccDeviceModel)
  40. // get resource queue.if queue not exist,skip it
  41. r, err := models.GetResourceQueue(&models.ResourceQueue{
  42. Cluster: models.C2NetCluster,
  43. AiCenterCode: c.ID,
  44. ComputeResource: computeResource,
  45. AccCardType: accCardType,
  46. })
  47. if err != nil || r == nil {
  48. continue
  49. }
  50. //Determine if this specification already exists.if exist,update params
  51. //if not exist,insert a new record and status is SpecNotVerified
  52. oldSpec, err := models.GetResourceSpecification(&models.ResourceSpecification{
  53. QueueId: r.ID,
  54. SourceSpecId: spec.ID,
  55. })
  56. if err != nil {
  57. return err
  58. }
  59. if oldSpec == nil {
  60. specInsertList = append(specInsertList, models.ResourceSpecification{
  61. QueueId: r.ID,
  62. SourceSpecId: spec.ID,
  63. AccCardsNum: spec.SpecInfo.AccDeviceNum,
  64. CpuCores: spec.SpecInfo.CpuCoreNum,
  65. MemGiB: models.ParseMemSizeFromGrampus(spec.SpecInfo.MemorySize),
  66. GPUMemGiB: models.ParseMemSizeFromGrampus(spec.SpecInfo.AccDeviceMemory),
  67. Status: models.SpecNotVerified,
  68. IsAutomaticSync: true,
  69. CreatedBy: doerId,
  70. UpdatedBy: doerId,
  71. })
  72. } else {
  73. existIds = append(existIds, oldSpec.ID)
  74. specUpdateList = append(specUpdateList, models.ResourceSpecification{
  75. ID: oldSpec.ID,
  76. AccCardsNum: spec.SpecInfo.AccDeviceNum,
  77. CpuCores: spec.SpecInfo.CpuCoreNum,
  78. MemGiB: models.ParseMemSizeFromGrampus(spec.SpecInfo.MemorySize),
  79. GPUMemGiB: models.ParseMemSizeFromGrampus(spec.SpecInfo.AccDeviceMemory),
  80. UpdatedBy: doerId,
  81. })
  82. }
  83. }
  84. }
  85. return models.SyncGrampusSpecs(specUpdateList, specInsertList, existIds)
  86. }
  87. //GetResourceSpecificationList returns specification and queue
  88. func GetResourceSpecificationList(opts models.SearchResourceSpecificationOptions) (*models.ResourceSpecAndQueueListRes, error) {
  89. n, r, err := models.SearchResourceSpecification(opts)
  90. if err != nil {
  91. return nil, err
  92. }
  93. return models.NewResourceSpecAndQueueListRes(n, r), nil
  94. }
  95. func ResourceSpecOnShelf(id int64, req models.ResourceSpecificationReq) error {
  96. spec, err := models.GetResourceSpecification(&models.ResourceSpecification{ID: id})
  97. if err != nil {
  98. return err
  99. }
  100. if spec == nil {
  101. return errors.New("specification not exist")
  102. }
  103. if q, err := models.GetResourceQueue(&models.ResourceQueue{ID: spec.QueueId}); err != nil || q == nil {
  104. return errors.New("resource queue not available")
  105. }
  106. _, err = models.ResourceSpecOnShelf(id, models.ResourceSpecification{
  107. UnitPrice: req.UnitPrice,
  108. })
  109. if err != nil {
  110. return err
  111. }
  112. return nil
  113. }
  114. func ResourceSpecOffShelf(id int64) error {
  115. _, err := models.ResourceSpecOffShelf(id)
  116. if err != nil {
  117. return err
  118. }
  119. return nil
  120. }