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.4 kB

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

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.