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.

pulltaskinfologic.go 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package core
  2. import (
  3. "context"
  4. "github.com/jinzhu/copier"
  5. "github.com/zeromicro/go-zero/core/logx"
  6. clientCore "gitlink.org.cn/JointCloud/pcm-coordinator/client"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  9. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models/cloud"
  10. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
  11. "gorm.io/gorm"
  12. )
  13. type PullTaskInfoLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. }
  18. func NewPullTaskInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PullTaskInfoLogic {
  19. return &PullTaskInfoLogic{
  20. Logger: logx.WithContext(ctx),
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. }
  24. }
  25. func (l *PullTaskInfoLogic) PullTaskInfo(req *clientCore.PullTaskInfoReq) (*clientCore.PullTaskInfoResp, error) {
  26. resp := clientCore.PullTaskInfoResp{}
  27. // check the kind of adapter
  28. var kind int32
  29. l.svcCtx.DbEngin.Raw("select type as kind from `t_adapter` where id = ?", req.AdapterId).Scan(&kind)
  30. // pull task list from database
  31. switch kind {
  32. case 2:
  33. var hpcModelList []models.TaskHpc
  34. err := findModelList(req.AdapterId, l.svcCtx.DbEngin, &hpcModelList)
  35. if err != nil {
  36. return nil, err
  37. }
  38. utils.Convert(hpcModelList, &resp.HpcInfoList)
  39. if len(resp.HpcInfoList) > 0 {
  40. for i, hpcInfo := range hpcModelList {
  41. err := copier.CopyWithOption(resp.HpcInfoList[i], hpcInfo, copier.Option{Converters: utils.Converters})
  42. if err != nil {
  43. return nil, err
  44. }
  45. var clusterType string
  46. l.svcCtx.DbEngin.Raw("SELECT label FROM `t_cluster` where id = ? ", hpcInfo.ClusterId).Scan(&clusterType)
  47. utils.Convert(hpcInfo.Environment, &resp.HpcInfoList[i].Environment)
  48. resp.HpcInfoList[i].ClusterType = clusterType
  49. }
  50. }
  51. case 0:
  52. var resourceType int32
  53. l.svcCtx.DbEngin.Raw("select resource_type as resourceType from `t_adapter` where id = ?", req.AdapterId).Scan(&resourceType)
  54. switch resourceType {
  55. case 01:
  56. var cloudModelList []cloud.TaskCloudModel
  57. err := findModelList(req.AdapterId, l.svcCtx.DbEngin, &cloudModelList)
  58. if err != nil {
  59. return nil, err
  60. }
  61. utils.Convert(cloudModelList, &resp.CloudInfoList)
  62. case 02:
  63. var vmModelList []models.TaskVm
  64. err := findModelList(req.AdapterId, l.svcCtx.DbEngin, &vmModelList)
  65. if err != nil {
  66. return nil, err
  67. }
  68. utils.Convert(vmModelList, &resp.VmInfoList)
  69. }
  70. case 1:
  71. var aiModelList []models.TaskAiModelarts
  72. err := findModelList(req.AdapterId, l.svcCtx.DbEngin, &aiModelList)
  73. if err != nil {
  74. return nil, err
  75. }
  76. utils.Convert(aiModelList, &resp.AiInfoList)
  77. /*if len(resp.AiInfoList) > 0 {
  78. for i, aiInfo := range aiModelList {
  79. if resp.AiInfoList[i].Environments != "" {
  80. // 定义一个map来存储解析后的JSON数据
  81. var result map[string]interface{}
  82. // 解析JSON字符串
  83. err := json.Unmarshal([]byte(resp.AiInfoList[i].Environments), &result)
  84. if err != nil {
  85. log.Fatalf("Error parsing JSON: %v", err)
  86. }
  87. // 如果你需要将解析后的map再次转换为JSON字符串,可以使用json.MarshalIndent
  88. formattedJSON, err := json.MarshalIndent(result, "", " ")
  89. aiInfo.Environments = string(formattedJSON)
  90. fmt.Println(aiInfo.Environments)
  91. resp.AiInfoList[i].Environments = aiInfo.Environments
  92. }
  93. if resp.AiInfoList[i].Parameters != "" {
  94. // 定义一个map来存储解析后的JSON数据
  95. var result []interface{}
  96. // 解析JSON字符串
  97. err := json.Unmarshal([]byte(resp.AiInfoList[i].Parameters), &result)
  98. if err != nil {
  99. log.Fatalf("Error parsing JSON: %v", err)
  100. }
  101. // 如果你需要将解析后的map再次转换为JSON字符串,可以使用json.MarshalIndent
  102. formattedJSON, err := json.MarshalIndent(result, "", " ")
  103. aiInfo.Parameters = string(formattedJSON)
  104. fmt.Println(aiInfo.Parameters)
  105. resp.AiInfoList[i].Parameters = aiInfo.Parameters
  106. }
  107. }
  108. }*/
  109. }
  110. return &resp, nil
  111. }
  112. func findModelList(adapterId int64, dbEngin *gorm.DB, data interface{}) error {
  113. tx := dbEngin.Where("cluster_id in (select id from t_cluster where adapter_id = ?) AND status not in "+
  114. "('Deleted', 'Succeeded', 'COMPLETED', 'Completed', 'Failed','FAIL','statC','statE')", adapterId).Find(data)
  115. if tx.Error != nil {
  116. return tx.Error
  117. }
  118. return nil
  119. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.