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.

cloudbrain.go 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package wechat
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/log"
  5. "code.gitea.io/gitea/modules/setting"
  6. "fmt"
  7. "time"
  8. )
  9. type JobOperateType string
  10. const (
  11. JobOperateTypeStart JobOperateType = "start"
  12. JobOperateTypeStop JobOperateType = "stop"
  13. )
  14. type CloudbrainStartMsg struct {
  15. }
  16. func (CloudbrainStartMsg) Data(ctx *TemplateContext) *DefaultWechatTemplate {
  17. return &DefaultWechatTemplate{
  18. First: TemplateValue{Value: setting.CloudbrainStartedTitle},
  19. Keyword1: TemplateValue{Value: ctx.Cloudbrain.DisplayJobName},
  20. Keyword2: TemplateValue{Value: getJobTypeDisplayName(ctx.Cloudbrain.JobType)},
  21. Keyword3: TemplateValue{Value: time.Unix(int64(ctx.Cloudbrain.CreatedUnix), 0).Format("2006-01-02 15:04:05")},
  22. Remark: TemplateValue{Value: setting.CloudbrainStartedRemark},
  23. }
  24. }
  25. func (CloudbrainStartMsg) ShouldSend(ctx *TemplateContext) bool {
  26. if len(setting.CloudbrainStartedNotifyList) == 0 {
  27. return false
  28. }
  29. for _, v := range setting.CloudbrainStartedNotifyList {
  30. if v == ctx.Cloudbrain.JobType {
  31. return true
  32. }
  33. }
  34. return false
  35. }
  36. func (CloudbrainStartMsg) MsgId(ctx *TemplateContext) string {
  37. return string(JobOperateTypeStart) + "_" + fmt.Sprint(ctx.Cloudbrain.ID)
  38. }
  39. func (CloudbrainStartMsg) Url(ctx *TemplateContext) string {
  40. repo, err := models.GetRepositoryByID(ctx.Cloudbrain.RepoID)
  41. if err != nil {
  42. log.Error("CloudbrainStartMsg GetRepositoryByID error,%v", err)
  43. return ""
  44. }
  45. return getCloudbrainTemplateUrl(*ctx.Cloudbrain, repo)
  46. }
  47. type CloudbrainStopMsg struct {
  48. }
  49. func (CloudbrainStopMsg) Data(ctx *TemplateContext) *DefaultWechatTemplate {
  50. return &DefaultWechatTemplate{
  51. First: TemplateValue{Value: setting.CloudbrainStoppedTitle},
  52. Keyword1: TemplateValue{Value: ctx.Cloudbrain.DisplayJobName},
  53. Keyword2: TemplateValue{Value: getJobTypeDisplayName(ctx.Cloudbrain.JobType)},
  54. Keyword3: TemplateValue{Value: time.Unix(int64(ctx.Cloudbrain.CreatedUnix), 0).Format("2006-01-02 15:04:05")},
  55. Remark: TemplateValue{Value: setting.CloudbrainStoppedRemark},
  56. }
  57. }
  58. func (CloudbrainStopMsg) ShouldSend(ctx *TemplateContext) bool {
  59. if len(setting.CloudbrainStoppedNotifyList) == 0 {
  60. return false
  61. }
  62. for _, v := range setting.CloudbrainStoppedNotifyList {
  63. if v == ctx.Cloudbrain.JobType {
  64. return true
  65. }
  66. }
  67. return false
  68. }
  69. func (CloudbrainStopMsg) MsgId(ctx *TemplateContext) string {
  70. return string(JobOperateTypeStop) + "_" + fmt.Sprint(ctx.Cloudbrain.ID)
  71. }
  72. func (CloudbrainStopMsg) Url(ctx *TemplateContext) string {
  73. repo, err := models.GetRepositoryByID(ctx.Cloudbrain.RepoID)
  74. if err != nil {
  75. log.Error("CloudbrainStopMsg GetRepositoryByID error,%v", err)
  76. return ""
  77. }
  78. return getCloudbrainTemplateUrl(*ctx.Cloudbrain, repo)
  79. }
  80. var startMsg = &CloudbrainStartMsg{}
  81. var stopMsg = &CloudbrainStopMsg{}
  82. func GetTemplateFromOperateType(operate JobOperateType) Template {
  83. switch operate {
  84. case JobOperateTypeStart:
  85. return startMsg
  86. case JobOperateTypeStop:
  87. return stopMsg
  88. }
  89. return nil
  90. }
  91. func GetJobOperateTypeFromCloudbrainStatus(cloudbrain *models.Cloudbrain) JobOperateType {
  92. if cloudbrain.IsTerminal() {
  93. return JobOperateTypeStop
  94. }
  95. if cloudbrain.IsRunning() {
  96. return JobOperateTypeStart
  97. }
  98. return ""
  99. }
  100. func getCloudbrainTemplateUrl(cloudbrain models.Cloudbrain, repo *models.Repository) string {
  101. url := setting.AppURL + repo.FullName()
  102. switch cloudbrain.JobType {
  103. case string(models.JobTypeDebug):
  104. if cloudbrain.ComputeResource == "CPU/GPU" {
  105. url += "/cloudbrain/" + fmt.Sprint(cloudbrain.ID)
  106. } else {
  107. url += "/modelarts/notebook/" + fmt.Sprint(cloudbrain.ID)
  108. }
  109. case string(models.JobTypeBenchmark):
  110. url += "/cloudbrain/benchmark/" + fmt.Sprint(cloudbrain.ID)
  111. case string(models.JobTypeTrain):
  112. if cloudbrain.Type == models.TypeCloudBrainOne {
  113. url += "/cloudbrain/train-job/" + fmt.Sprint(cloudbrain.JobID)
  114. } else if cloudbrain.Type == models.TypeCloudBrainTwo {
  115. url += "/modelarts/train-job/" + fmt.Sprint(cloudbrain.JobID)
  116. } else if cloudbrain.Type == models.TypeC2Net {
  117. url += "/grampus/train-job/" + fmt.Sprint(cloudbrain.JobID)
  118. }
  119. case string(models.JobTypeInference):
  120. url += "/modelarts/inference-job/" + fmt.Sprint(cloudbrain.JobID)
  121. }
  122. return url
  123. }
  124. func getJobTypeDisplayName(jobType string) string {
  125. switch jobType {
  126. case string(models.JobTypeDebug):
  127. return "调试任务"
  128. case string(models.JobTypeBenchmark):
  129. return "评测任务"
  130. case string(models.JobTypeTrain):
  131. return "训练任务"
  132. case string(models.JobTypeInference):
  133. return "推理任务"
  134. }
  135. return ""
  136. }