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.

deployinstancelistlogic.go 2.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package inference
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/zeromicro/go-zero/core/logx"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/common"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/service/updater"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
  9. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
  10. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  11. "time"
  12. )
  13. type DeployInstanceListLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. }
  18. func NewDeployInstanceListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeployInstanceListLogic {
  19. return &DeployInstanceListLogic{
  20. Logger: logx.WithContext(ctx),
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. }
  24. }
  25. func (l *DeployInstanceListLogic) DeployInstanceList(req *types.DeployInstanceListReq) (resp *types.DeployInstanceListResp, err error) {
  26. limit := req.PageSize
  27. offset := req.PageSize * (req.PageNum - 1)
  28. resp = &types.DeployInstanceListResp{}
  29. var tasklist []*models.AiDeployInstanceTask
  30. tx := l.svcCtx.DbEngin.Raw("select * from ai_deploy_instance_task").Scan(&tasklist)
  31. if tx.Error != nil {
  32. logx.Errorf(tx.Error.Error())
  33. return nil, tx.Error
  34. }
  35. //count total
  36. var total int64
  37. err = tx.Count(&total).Error
  38. tx.Limit(limit).Offset(offset)
  39. if err != nil {
  40. return resp, err
  41. }
  42. err = tx.Order("create_time desc").Find(&tasklist).Error
  43. if err != nil {
  44. return nil, errors.New(err.Error())
  45. }
  46. deployTasks := l.GenerateDeployTasks(tasklist)
  47. slices := make([][]*models.AiInferDeployInstance, len(deployTasks))
  48. for i := 0; i < len(deployTasks); i++ {
  49. slices[i] = deployTasks[i].Instances
  50. }
  51. list := common.ConcatMultipleSlices(slices)
  52. if len(list) == 0 {
  53. return
  54. }
  55. go updater.UpdateDeployInstanceStatusBatch(l.svcCtx, list)
  56. ins := list[0]
  57. for i := range list {
  58. uTime, _ := time.Parse(time.RFC3339, ins.UpdateTime)
  59. latest, _ := time.Parse(time.RFC3339, list[i].UpdateTime)
  60. if latest.After(uTime) {
  61. ins = list[i]
  62. }
  63. }
  64. go updater.UpdateDeployInstanceStatus(l.svcCtx, ins, true)
  65. go updater.UpdateDeployTaskStatus(l.svcCtx)
  66. resp.List = &deployTasks
  67. resp.PageSize = req.PageSize
  68. resp.PageNum = req.PageNum
  69. resp.Total = total
  70. return
  71. }
  72. func (l *DeployInstanceListLogic) GenerateDeployTasks(tasklist []*models.AiDeployInstanceTask) []*DeployTask {
  73. var tasks []*DeployTask
  74. for _, t := range tasklist {
  75. list, err := l.svcCtx.Scheduler.AiStorages.GetInstanceListByDeployTaskId(t.Id)
  76. if err != nil {
  77. logx.Errorf("db GetInstanceListByDeployTaskId error")
  78. continue
  79. }
  80. deployTask := &DeployTask{
  81. Id: t.Id,
  82. Name: t.Name,
  83. Instances: list,
  84. }
  85. tasks = append(tasks, deployTask)
  86. }
  87. return tasks
  88. }
  89. type DeployTask struct {
  90. Id int64 `json:"id,string"`
  91. Name string `json:"name,string"`
  92. Instances []*models.AiInferDeployInstance `json:"instances,string"`
  93. }

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.