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 5.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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright 2016 The Gogs Authors. All rights reserved.
  2. // Copyright 2018 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package repo
  6. import (
  7. "net/http"
  8. "strconv"
  9. "code.gitea.io/gitea/models"
  10. "code.gitea.io/gitea/modules/context"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/modelarts"
  13. )
  14. func GetModelArtsNotebook(ctx *context.APIContext) {
  15. var (
  16. err error
  17. )
  18. jobID := ctx.Params(":jobid")
  19. repoID := ctx.Repo.Repository.ID
  20. job, err := models.GetRepoCloudBrainByJobID(repoID, jobID)
  21. if err != nil {
  22. ctx.NotFound(err)
  23. return
  24. }
  25. result, err := modelarts.GetJob(jobID)
  26. if err != nil {
  27. ctx.NotFound(err)
  28. return
  29. }
  30. job.Status = result.Status
  31. err = models.UpdateJob(job)
  32. if err != nil {
  33. log.Error("UpdateJob failed:", err)
  34. }
  35. ctx.JSON(http.StatusOK, map[string]interface{}{
  36. "JobID": jobID,
  37. "JobStatus": result.Status,
  38. })
  39. }
  40. func GetModelArtsTrainJob(ctx *context.APIContext) {
  41. var (
  42. err error
  43. )
  44. jobID := ctx.Params(":jobid")
  45. repoID := ctx.Repo.Repository.ID
  46. job, err := models.GetRepoCloudBrainByJobID(repoID, jobID)
  47. if err != nil {
  48. ctx.NotFound(err)
  49. return
  50. }
  51. result, err := modelarts.GetTrainJob(jobID, strconv.FormatInt(job.VersionID, 10))
  52. if err != nil {
  53. ctx.NotFound(err)
  54. return
  55. }
  56. job.Status = modelarts.TransTrainJobStatus(result.IntStatus)
  57. job.Duration = result.Duration
  58. job.TrainJobDuration = result.TrainJobDuration
  59. err = models.UpdateJob(job)
  60. if err != nil {
  61. log.Error("UpdateJob failed:", err)
  62. }
  63. ctx.JSON(http.StatusOK, map[string]interface{}{
  64. "JobID": jobID,
  65. "JobStatus": job.Status,
  66. "JobDuration": job.Duration,
  67. })
  68. }
  69. func GetModelArtsTrainJobVersion(ctx *context.APIContext) {
  70. var (
  71. err error
  72. )
  73. jobID := ctx.Params(":jobid")
  74. versionName := ctx.Query("version_name")
  75. repoID := ctx.Repo.Repository.ID
  76. job, err := models.GetRepoCloudBrainByJobIDAndVersionName(repoID, jobID, versionName)
  77. if err != nil {
  78. ctx.NotFound(err)
  79. return
  80. }
  81. result, err := modelarts.GetTrainJob(jobID, strconv.FormatInt(job.VersionID, 10))
  82. if err != nil {
  83. ctx.NotFound(err)
  84. return
  85. }
  86. job.Status = modelarts.TransTrainJobStatus(result.IntStatus)
  87. job.Duration = result.Duration
  88. job.TrainJobDuration = result.TrainJobDuration
  89. if result.Duration != 0 {
  90. job.TrainJobDuration = addZero(result.Duration/3600000) + ":" + addZero(result.Duration%3600000/60000) + ":" + addZero(result.Duration%60000/1000)
  91. } else {
  92. job.TrainJobDuration = "00:00:00"
  93. }
  94. err = models.UpdateJob(job)
  95. if err != nil {
  96. log.Error("UpdateJob failed:", err)
  97. }
  98. ctx.JSON(http.StatusOK, map[string]interface{}{
  99. "JobID": jobID,
  100. "JobStatus": job.Status,
  101. "JobDuration": job.TrainJobDuration,
  102. })
  103. }
  104. func addZero(t int64) (m string) {
  105. if t < 10 {
  106. m = "0" + strconv.FormatInt(t, 10)
  107. return m
  108. } else {
  109. return strconv.FormatInt(t, 10)
  110. }
  111. }
  112. func TrainJobGetLog(ctx *context.APIContext) {
  113. var (
  114. err error
  115. )
  116. var jobID = ctx.Params(":jobid")
  117. var versionName = ctx.Query("version_name")
  118. // var logFileName = ctx.Query("file_name")
  119. var baseLine = ctx.Query("base_line")
  120. var order = ctx.Query("order")
  121. var lines = ctx.Query("lines")
  122. lines_int, err := strconv.Atoi(lines)
  123. if err != nil {
  124. log.Error("change lines(%d) string to int failed", lines_int)
  125. }
  126. if order != modelarts.OrderDesc && order != modelarts.OrderAsc {
  127. log.Error("order(%s) check failed", order)
  128. ctx.JSON(http.StatusBadRequest, map[string]interface{}{
  129. "err_msg": "order check failed",
  130. })
  131. return
  132. }
  133. // task, err := models.GetCloudbrainByJobIDAndVersionName(jobID, versionName)
  134. // if err != nil {
  135. // log.Error("GetCloudbrainByJobIDAndVersionName(%s) failed:%v", jobID, err.Error())
  136. // ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  137. // "err_msg": "GetCloudbrainByJobIDAndVersionName failed",
  138. // })
  139. // return
  140. // }
  141. resultLogFile, result, err := trainJobGetLogContent(jobID, versionName, baseLine, order, lines_int)
  142. if err != nil {
  143. log.Error("trainJobGetLog(%s) failed:%v", jobID, err.Error())
  144. // ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobShow, nil)
  145. return
  146. }
  147. ctx.Data["log_file_name"] = resultLogFile.LogFileList[0]
  148. // result, err := modelarts.GetTrainJobLog(jobID, strconv.FormatInt(task.VersionID, 10), baseLine, logFileName, order, modelarts.Lines)
  149. // if err != nil {
  150. // log.Error("GetTrainJobLog(%s) failed:%v", jobID, err.Error())
  151. // ctx.JSON(http.StatusInternalServerError, map[string]interface{}{
  152. // "err_msg": "GetTrainJobLog failed",
  153. // })
  154. // return
  155. // }
  156. ctx.JSON(http.StatusOK, map[string]interface{}{
  157. "JobID": jobID,
  158. "StartLine": result.StartLine,
  159. "EndLine": result.EndLine,
  160. "Content": result.Content,
  161. "Lines": result.Lines,
  162. })
  163. }
  164. func trainJobGetLogContent(jobID string, versionName string, baseLine string, order string, lines int) (*models.GetTrainJobLogFileNamesResult, *models.GetTrainJobLogResult, error) {
  165. task, err := models.GetCloudbrainByJobIDAndVersionName(jobID, versionName)
  166. if err != nil {
  167. log.Error("GetCloudbrainByJobID(%s) failed:%v", jobID, err.Error())
  168. return nil, nil, err
  169. }
  170. resultLogFile, err := modelarts.GetTrainJobLogFileNames(jobID, strconv.FormatInt(task.VersionID, 10))
  171. if err != nil {
  172. log.Error("GetTrainJobLogFileNames(%s) failed:%v", jobID, err.Error())
  173. return nil, nil, err
  174. }
  175. result, err := modelarts.GetTrainJobLog(jobID, strconv.FormatInt(task.VersionID, 10), baseLine, resultLogFile.LogFileList[0], order, lines)
  176. if err != nil {
  177. log.Error("GetTrainJobLog(%s) failed:%v", jobID, err.Error())
  178. return nil, nil, err
  179. }
  180. return resultLogFile, result, err
  181. }