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.

modelarts.go 6.5 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. package repo
  2. import (
  3. "code.gitea.io/gitea/modules/modelarts"
  4. "errors"
  5. "github.com/unknwon/com"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/auth"
  11. "code.gitea.io/gitea/modules/base"
  12. "code.gitea.io/gitea/modules/context"
  13. "code.gitea.io/gitea/modules/log"
  14. "code.gitea.io/gitea/modules/setting"
  15. )
  16. const (
  17. tplModelArtsIndex base.TplName = "repo/modelarts/index"
  18. tplModelArtsNew base.TplName = "repo/modelarts/new"
  19. tplModelArtsShow base.TplName = "repo/modelarts/show"
  20. )
  21. // MustEnableDataset check if repository enable internal cb
  22. func MustEnableModelArts(ctx *context.Context) {
  23. if !ctx.Repo.CanRead(models.UnitTypeCloudBrain) {
  24. ctx.NotFound("MustEnableCloudbrain", nil)
  25. return
  26. }
  27. }
  28. func ModelArtsIndex(ctx *context.Context) {
  29. MustEnableModelArts(ctx)
  30. repo := ctx.Repo.Repository
  31. page := ctx.QueryInt("page")
  32. if page <= 0 {
  33. page = 1
  34. }
  35. ciTasks, count, err := models.Cloudbrains(&models.CloudbrainsOptions{
  36. ListOptions: models.ListOptions{
  37. Page: page,
  38. PageSize: setting.UI.IssuePagingNum,
  39. },
  40. RepoID: repo.ID,
  41. Type: models.TypeCloudBrainTwo,
  42. })
  43. if err != nil {
  44. ctx.ServerError("Cloudbrain", err)
  45. return
  46. }
  47. for i, task := range ciTasks {
  48. if task.Status == string(models.JobRunning) {
  49. ciTasks[i].CanDebug = true
  50. } else {
  51. ciTasks[i].CanDebug = false
  52. }
  53. }
  54. pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, 5)
  55. pager.SetDefaultParams(ctx)
  56. ctx.Data["Page"] = pager
  57. ctx.Data["PageIsCloudBrain"] = true
  58. ctx.Data["Tasks"] = ciTasks
  59. ctx.HTML(200, tplModelArtsIndex)
  60. }
  61. func ModelArtsNew(ctx *context.Context) {
  62. ctx.Data["PageIsCloudBrain"] = true
  63. t := time.Now()
  64. var jobName = cutString(ctx.User.Name, 5) + t.Format("2006010215") + strconv.Itoa(int(t.Unix()))[5:]
  65. ctx.Data["job_name"] = jobName
  66. attachs, err := models.GetModelArtsUserAttachments(ctx.User.ID)
  67. if err != nil {
  68. ctx.ServerError("GetAllUserAttachments failed:", err)
  69. return
  70. }
  71. ctx.Data["attachments"] = attachs
  72. ctx.Data["dataset_path"] = modelarts.DataSetMountPath
  73. ctx.Data["env"] = modelarts.NotebookEnv
  74. ctx.Data["notebook_type"] = modelarts.NotebookType
  75. ctx.Data["flavor"] = modelarts.FlavorInfo
  76. ctx.HTML(200, tplModelArtsNew)
  77. }
  78. func ModelArtsCreate(ctx *context.Context, form auth.CreateModelArtsForm) {
  79. ctx.Data["PageIsCloudBrain"] = true
  80. jobName := form.JobName
  81. uuid := form.Attachment
  82. description := form.Description
  83. //repo := ctx.Repo.Repository
  84. err := modelarts.GenerateTask(ctx, jobName, uuid, description)
  85. if err != nil {
  86. ctx.RenderWithErr(err.Error(), tplModelArtsNew, &form)
  87. return
  88. }
  89. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts")
  90. }
  91. func ModelArtsShow(ctx *context.Context) {
  92. ctx.Data["PageIsCloudBrain"] = true
  93. var jobID = ctx.Params(":jobid")
  94. task, err := models.GetCloudbrainByJobID(jobID)
  95. if err != nil {
  96. ctx.Data["error"] = err.Error()
  97. ctx.RenderWithErr(err.Error(), tplModelArtsIndex, nil)
  98. return
  99. }
  100. result, err := modelarts.GetJob(jobID)
  101. if err != nil {
  102. ctx.Data["error"] = err.Error()
  103. ctx.RenderWithErr(err.Error(), tplModelArtsIndex, nil)
  104. return
  105. }
  106. if result != nil {
  107. task.Status = result.Status
  108. err = models.UpdateJob(task)
  109. if err != nil {
  110. ctx.Data["error"] = err.Error()
  111. ctx.RenderWithErr(err.Error(), tplModelArtsIndex, nil)
  112. return
  113. }
  114. createTime, _ := com.StrTo(result.CreationTimestamp).Int64()
  115. result.CreateTime = time.Unix(int64(createTime/1000), 0).Format("2006-01-02 15:04:05")
  116. endTime, _ := com.StrTo(result.LatestUpdateTimestamp).Int64()
  117. result.LatestUpdateTime = time.Unix(int64(endTime/1000), 0).Format("2006-01-02 15:04:05")
  118. result.QueuingInfo.BeginTime = time.Unix(int64(result.QueuingInfo.BeginTimestamp/1000), 0).Format("2006-01-02 15:04:05")
  119. result.QueuingInfo.EndTime = time.Unix(int64(result.QueuingInfo.EndTimestamp/1000), 0).Format("2006-01-02 15:04:05")
  120. }
  121. ctx.Data["task"] = task
  122. ctx.Data["jobID"] = jobID
  123. ctx.Data["result"] = result
  124. ctx.HTML(200, tplModelArtsShow)
  125. }
  126. func ModelArtsDebug(ctx *context.Context) {
  127. var jobID = ctx.Params(":jobid")
  128. _, err := models.GetCloudbrainByJobID(jobID)
  129. if err != nil {
  130. ctx.ServerError("GetCloudbrainByJobID failed", err)
  131. return
  132. }
  133. result, err := modelarts.GetJob(jobID)
  134. if err != nil {
  135. ctx.RenderWithErr(err.Error(), tplModelArtsIndex, nil)
  136. return
  137. }
  138. res, err := modelarts.GetJobToken(jobID)
  139. if err != nil {
  140. ctx.RenderWithErr(err.Error(), tplModelArtsIndex, nil)
  141. return
  142. }
  143. urls := strings.Split(result.Spec.Annotations.Url, "/")
  144. urlPrefix := result.Spec.Annotations.TargetDomain
  145. for i, url := range urls {
  146. if i > 2 {
  147. urlPrefix += "/" + url
  148. }
  149. }
  150. //urlPrefix := result.Spec.Annotations.TargetDomain + "/modelarts/internal/hub/notebook/user/" + task.JobID
  151. log.Info(urlPrefix)
  152. debugUrl := urlPrefix + "?token=" + res.Token
  153. ctx.Redirect(debugUrl)
  154. }
  155. func ModelArtsStop(ctx *context.Context) {
  156. var jobID = ctx.Params(":jobid")
  157. log.Info(jobID)
  158. task, err := models.GetCloudbrainByJobID(jobID)
  159. if err != nil {
  160. ctx.ServerError("GetCloudbrainByJobID failed", err)
  161. return
  162. }
  163. if task.Status != string(models.JobRunning) {
  164. log.Error("the job(%s) is not running", task.JobName)
  165. ctx.ServerError("the job is not running", errors.New("the job is not running"))
  166. return
  167. }
  168. param := models.NotebookAction{
  169. Action: models.ActionStop,
  170. }
  171. res, err := modelarts.StopJob(jobID, param)
  172. if err != nil {
  173. log.Error("StopJob(%s) failed:%v", task.JobName, err.Error())
  174. ctx.ServerError("StopJob failed", err)
  175. return
  176. }
  177. task.Status = res.CurrentStatus
  178. err = models.UpdateJob(task)
  179. if err != nil {
  180. ctx.ServerError("UpdateJob failed", err)
  181. return
  182. }
  183. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts")
  184. }
  185. func ModelArtsDel(ctx *context.Context) {
  186. var jobID = ctx.Params(":jobid")
  187. task, err := models.GetCloudbrainByJobID(jobID)
  188. if err != nil {
  189. ctx.ServerError("GetCloudbrainByJobID failed", err)
  190. return
  191. }
  192. if task.Status != string(models.JobStopped) {
  193. log.Error("the job(%s) has not been stopped", task.JobName)
  194. ctx.ServerError("the job has not been stopped", errors.New("the job has not been stopped"))
  195. return
  196. }
  197. _, err = modelarts.DelJob(jobID)
  198. if err != nil {
  199. log.Error("DelJob(%s) failed:%v", task.JobName, err.Error())
  200. ctx.ServerError("DelJob failed", err)
  201. return
  202. }
  203. err = models.DeleteJob(task)
  204. if err != nil {
  205. ctx.ServerError("DeleteJob failed", err)
  206. return
  207. }
  208. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/modelarts")
  209. }