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_queue.go 2.9 kB

3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package resource
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/grampus"
  5. "strings"
  6. )
  7. func AddResourceQueue(req models.ResourceQueueReq) error {
  8. if _, err := models.InsertResourceQueue(req.ToDTO()); err != nil {
  9. return err
  10. }
  11. return nil
  12. }
  13. func UpdateResourceQueue(queueId int64, req models.ResourceQueueReq) error {
  14. if _, err := models.UpdateResourceQueueById(queueId, models.ResourceQueue{
  15. CardsTotalNum: req.CardsTotalNum,
  16. Remark: req.Remark,
  17. }); err != nil {
  18. return err
  19. }
  20. return nil
  21. }
  22. func GetResourceQueueList(opts models.SearchResourceQueueOptions) (*models.ResourceQueueListRes, error) {
  23. n, r, err := models.SearchResourceQueue(opts)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return models.NewResourceQueueListRes(n, r), nil
  28. }
  29. func GetResourceQueueCodes(opts models.GetQueueCodesOptions) ([]*models.ResourceQueueCodesRes, error) {
  30. r, err := models.GetResourceQueueCodes(opts)
  31. if err != nil {
  32. return nil, err
  33. }
  34. return r, nil
  35. }
  36. func GetResourceAiCenters() ([]models.ResourceAiCenterRes, error) {
  37. r, err := models.GetResourceAiCenters()
  38. if err != nil {
  39. return nil, err
  40. }
  41. return r, nil
  42. }
  43. func SyncGrampusQueue(doerId int64) error {
  44. r, err := grampus.GetAiCenters(1, 100)
  45. if err != nil {
  46. return err
  47. }
  48. queueUpdateList := make([]models.ResourceQueue, 0)
  49. queueInsertList := make([]models.ResourceQueue, 0)
  50. existIds := make([]int64, 0)
  51. for _, center := range r.Infos {
  52. for _, device := range center.AccDevices {
  53. computeResource := models.ParseComputeResourceFormGrampus(device.Kind)
  54. accCardType := strings.ToUpper(device.Model)
  55. if computeResource == "" {
  56. continue
  57. }
  58. //Determine if this quque already exists.if exist,update params
  59. //if not exist,insert a new record
  60. oldQueue, err := models.GetResourceQueue(&models.ResourceQueue{
  61. Cluster: models.C2NetCluster,
  62. AiCenterCode: center.Id,
  63. ComputeResource: computeResource,
  64. AccCardType: accCardType,
  65. })
  66. if err != nil {
  67. return err
  68. }
  69. if oldQueue == nil {
  70. queueInsertList = append(queueInsertList, models.ResourceQueue{
  71. Cluster: models.C2NetCluster,
  72. AiCenterCode: center.Id,
  73. AiCenterName: center.Name,
  74. ComputeResource: computeResource,
  75. AccCardType: accCardType,
  76. IsAutomaticSync: true,
  77. CreatedBy: doerId,
  78. UpdatedBy: doerId,
  79. })
  80. } else {
  81. existIds = append(existIds, oldQueue.ID)
  82. queueUpdateList = append(queueUpdateList, models.ResourceQueue{
  83. ID: oldQueue.ID,
  84. ComputeResource: computeResource,
  85. AiCenterName: center.Name,
  86. AccCardType: accCardType,
  87. UpdatedBy: doerId,
  88. })
  89. }
  90. }
  91. }
  92. return models.SyncGrampusQueues(queueUpdateList, queueInsertList, existIds)
  93. }
  94. func SyncGrampusQueueAndSpecs() {
  95. SyncGrampusQueue(0)
  96. SyncGrampusSpecs(0)
  97. }