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 3.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package repo
  2. import (
  3. "strconv"
  4. "time"
  5. "code.gitea.io/gitea/models"
  6. "code.gitea.io/gitea/modules/auth"
  7. "code.gitea.io/gitea/modules/base"
  8. "code.gitea.io/gitea/modules/cloudbrain"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. const (
  13. tplCloudBrainIndex base.TplName = "repo/cloudbrain/index"
  14. tplCloudBrainNew base.TplName = "repo/cloudbrain/new"
  15. tplCloudBrainShow base.TplName = "repo/cloudbrain/show"
  16. )
  17. // MustEnableDataset check if repository enable internal cb
  18. func MustEnableCloudbrain(ctx *context.Context) {
  19. if !ctx.Repo.CanRead(models.UnitTypeCloudBrain) {
  20. ctx.NotFound("MustEnableCloudbrain", nil)
  21. return
  22. }
  23. }
  24. func CloudBrainIndex(ctx *context.Context) {
  25. MustEnableCloudbrain(ctx)
  26. repo := ctx.Repo.Repository
  27. page := ctx.QueryInt("page")
  28. if page <= 0 {
  29. page = 1
  30. }
  31. ciTasks, count, err := models.Cloudbrains(&models.CloudbrainsOptions{
  32. ListOptions: models.ListOptions{
  33. Page: page,
  34. PageSize: setting.UI.IssuePagingNum,
  35. },
  36. RepoID: repo.ID,
  37. // SortType: sortType,
  38. })
  39. if err != nil {
  40. ctx.ServerError("Cloudbrain", err)
  41. return
  42. }
  43. pager := context.NewPagination(int(count), setting.UI.IssuePagingNum, page, 5)
  44. pager.SetDefaultParams(ctx)
  45. ctx.Data["Page"] = pager
  46. ctx.Data["PageIsCloudBrain"] = true
  47. ctx.Data["Tasks"] = ciTasks
  48. ctx.HTML(200, tplCloudBrainIndex)
  49. }
  50. func cutString(str string, lens int) string {
  51. if len(str) < lens {
  52. return str
  53. }
  54. return str[:lens]
  55. }
  56. func CloudBrainNew(ctx *context.Context) {
  57. ctx.Data["PageIsCloudBrain"] = true
  58. t := time.Now()
  59. var jobName = cutString(ctx.User.Name, 5) + t.Format("2006010215") + strconv.Itoa(int(t.Unix()))[:5]
  60. ctx.Data["job_name"] = jobName
  61. result, err := cloudbrain.GetImages()
  62. if err != nil {
  63. ctx.Data["error"] = err.Error()
  64. }
  65. if result != nil {
  66. //models.ConvertToImagesResultPayload(result.Payload)
  67. ctx.Data["image"] = result.Payload
  68. } else {
  69. ctx.Data["image"] = "192.168.202.74:5000/user-images/deepo:v2.0"
  70. }
  71. ctx.Data["command"] = cloudbrain.Command
  72. ctx.Data["code_path"] = cloudbrain.CodeMountPath
  73. ctx.Data["dataset_path"] = cloudbrain.DataSetMountPath
  74. ctx.Data["model_path"] = cloudbrain.ModelMountPath
  75. ctx.HTML(200, tplCloudBrainNew)
  76. }
  77. func CloudBrainCreate(ctx *context.Context, form auth.CreateCloudBrainForm) {
  78. ctx.Data["PageIsCloudBrain"] = true
  79. jobName := form.JobName
  80. image := form.Image
  81. command := form.Command
  82. err := cloudbrain.GenerateTask(ctx, jobName, image, command)
  83. if err != nil {
  84. ctx.RenderWithErr(err.Error(), tplCloudBrainNew, &form)
  85. return
  86. }
  87. ctx.Redirect(setting.AppSubURL + ctx.Repo.RepoLink + "/cloudbrain")
  88. }
  89. func CloudBrainShow(ctx *context.Context) {
  90. ctx.Data["PageIsCloudBrain"] = true
  91. var jobID = ctx.Params(":jobid")
  92. task, err := models.GetCloudbrainByJobID(jobID)
  93. if err != nil {
  94. ctx.Data["error"] = err.Error()
  95. }
  96. ctx.Data["task"] = task
  97. result, err := cloudbrain.GetJob(jobID)
  98. if err != nil {
  99. ctx.Data["error"] = err.Error()
  100. }
  101. if result != nil {
  102. jobRes, _ := models.ConvertToJobResultPayload(result.Payload)
  103. ctx.Data["result"] = jobRes
  104. taskRoles := jobRes.TaskRoles
  105. taskRes, _ := models.ConvertToTaskPod(taskRoles["task1"].(map[string]interface{}))
  106. ctx.Data["taskRes"] = taskRes
  107. }
  108. ctx.Data["jobID"] = jobID
  109. ctx.HTML(200, tplCloudBrainShow)
  110. }