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.

modeljob.go 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package schsdk
  2. import (
  3. "fmt"
  4. "gitlink.org.cn/cloudream/common/consts/errorcode"
  5. "gitlink.org.cn/cloudream/common/pkgs/mq"
  6. myhttp "gitlink.org.cn/cloudream/common/utils/http"
  7. "gitlink.org.cn/cloudream/common/utils/serder"
  8. "net/url"
  9. "strings"
  10. )
  11. // 这个结构体无任何字段,但实现了Noop,每种MessageBody都要内嵌这个结构体
  12. type MessageBodyBase struct{}
  13. // 此处的receiver是指针
  14. func (b *MessageBodyBase) Noop() {}
  15. type RunningModelResp struct {
  16. MessageBodyBase
  17. RunningModels map[string]RunningModelInfo `json:"allNode"`
  18. }
  19. type AllModelResp struct {
  20. MessageBodyBase
  21. AllModels []Models `json:"allModels"`
  22. }
  23. type Models struct {
  24. ModelID ModelID `json:"modelID"`
  25. ModelName ModelName `json:"modelName"`
  26. }
  27. type NodeInfo struct {
  28. MessageBodyBase
  29. InstanceID JobID `json:"instanceID"`
  30. //NodeID NodeID `json:"nodeID"`
  31. Address Address `json:"address"`
  32. Status string `json:"status"`
  33. }
  34. type RunningModelInfo struct {
  35. MessageBodyBase
  36. JobSetID JobSetID `json:"jobSetID"`
  37. ModelID ModelID `json:"modelID"`
  38. ModelName ModelName `json:"modelName"`
  39. CustomModelName ModelName `json:"customModelName"`
  40. Nodes []NodeInfo `json:"nodes"`
  41. }
  42. type ECSNodeRunningInfoReq struct {
  43. mq.MessageBodyBase
  44. CustomModelName ModelName `form:"customModelName" json:"customModelName" binding:"required"`
  45. ModelID ModelID `form:"modelID" json:"modelID" binding:"required"`
  46. }
  47. type ECSNodeRunningInfoResp struct {
  48. MessageBodyBase
  49. NodeUsageRateInfos []NodeUsageRateInfo `json:"nodeUsageRateInfos"`
  50. }
  51. func NewECSNodeRunningInfoResp(nodeUsageRateInfos []NodeUsageRateInfo) *ECSNodeRunningInfoResp {
  52. return &ECSNodeRunningInfoResp{
  53. NodeUsageRateInfos: nodeUsageRateInfos,
  54. }
  55. }
  56. type NodeUsageRateInfo struct {
  57. MessageBodyBase
  58. InstanceID JobID `json:"instanceID"`
  59. Address Address `json:"address"`
  60. GPURate []UsageRate `json:"GPURate"`
  61. AccCardRate []UsageRate `json:"AccCardRate"`
  62. }
  63. type UsageRate struct {
  64. Timestamp string `json:"timestamp"`
  65. Number string `json:"number"`
  66. }
  67. const (
  68. FineTuning = "finetuning"
  69. CreateECS = "create"
  70. RunECS = "run"
  71. PauseECS = "pause"
  72. DestroyECS = "destroy"
  73. OperateServer = "operate"
  74. RcloneMount = "rclone"
  75. Mounted = "mounted"
  76. )
  77. type QueryRunningModelsReq struct {
  78. UserID int64 `form:"userID" json:"userID"`
  79. }
  80. func (c *Client) QueryRunningModels(req QueryRunningModelsReq) (*RunningModelResp, error) {
  81. url, err := url.JoinPath(c.baseURL, "/job/queryRunningModels")
  82. if err != nil {
  83. return nil, err
  84. }
  85. resp, err := myhttp.GetJSON(url, myhttp.RequestParam{
  86. Body: req,
  87. })
  88. if err != nil {
  89. return nil, err
  90. }
  91. contType := resp.Header.Get("Content-Type")
  92. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  93. var codeResp response[RunningModelResp]
  94. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  95. return nil, fmt.Errorf("parsing response: %w", err)
  96. }
  97. if codeResp.Code == errorcode.OK {
  98. return &codeResp.Data, nil
  99. }
  100. return nil, codeResp.ToError()
  101. }
  102. return nil, fmt.Errorf("unknow response content type: %s", contType)
  103. }
  104. func (c *Client) QueryAllModels(req QueryRunningModelsReq) (*AllModelResp, error) {
  105. url, err := url.JoinPath(c.baseURL, "/job/getAllModels")
  106. if err != nil {
  107. return nil, err
  108. }
  109. resp, err := myhttp.GetJSON(url, myhttp.RequestParam{
  110. Body: req,
  111. })
  112. if err != nil {
  113. return nil, err
  114. }
  115. contType := resp.Header.Get("Content-Type")
  116. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  117. var codeResp response[AllModelResp]
  118. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  119. return nil, fmt.Errorf("parsing response: %w", err)
  120. }
  121. if codeResp.Code == errorcode.OK {
  122. return &codeResp.Data, nil
  123. }
  124. return nil, codeResp.ToError()
  125. }
  126. return nil, fmt.Errorf("unknow response content type: %s", contType)
  127. }
  128. func (c *Client) ECSNodeRunningInfo(req ECSNodeRunningInfoReq) (*ECSNodeRunningInfoResp, error) {
  129. url, err := url.JoinPath(c.baseURL, "/job/getECSNodeRunningInfo")
  130. if err != nil {
  131. return nil, err
  132. }
  133. resp, err := myhttp.GetJSON(url, myhttp.RequestParam{
  134. Body: req,
  135. })
  136. if err != nil {
  137. return nil, err
  138. }
  139. contType := resp.Header.Get("Content-Type")
  140. if strings.Contains(contType, myhttp.ContentTypeJSON) {
  141. var codeResp response[ECSNodeRunningInfoResp]
  142. if err := serder.JSONToObjectStream(resp.Body, &codeResp); err != nil {
  143. return nil, fmt.Errorf("parsing response: %w", err)
  144. }
  145. if codeResp.Code == errorcode.OK {
  146. return &codeResp.Data, nil
  147. }
  148. return nil, codeResp.ToError()
  149. }
  150. return nil, fmt.Errorf("unknow response content type: %s", contType)
  151. }