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