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

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
5 years ago
5 years ago
3 years ago
5 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. "encoding/json"
  8. "net/http"
  9. "sort"
  10. "strings"
  11. "time"
  12. "code.gitea.io/gitea/models"
  13. "code.gitea.io/gitea/modules/cloudbrain"
  14. "code.gitea.io/gitea/modules/context"
  15. "code.gitea.io/gitea/modules/log"
  16. "code.gitea.io/gitea/modules/storage"
  17. routerRepo "code.gitea.io/gitea/routers/repo"
  18. )
  19. // cloudbrain get job task by jobid
  20. func GetCloudbrainTask(ctx *context.APIContext) {
  21. // swagger:operation GET /repos/{owner}/{repo}/cloudbrain/{jobid} cloudbrain jobTask
  22. // ---
  23. // summary: Get a single task
  24. // produces:
  25. // - application/json
  26. // parameters:
  27. // - name: owner
  28. // in: path
  29. // description: owner of the repo
  30. // type: string
  31. // required: true
  32. // - name: repo
  33. // in: path
  34. // description: name of the repo
  35. // type: string
  36. // required: true
  37. // - name: jobid
  38. // in: path
  39. // description: id of cloudbrain jobid
  40. // type: string
  41. // required: true
  42. // responses:
  43. // "200":
  44. // "$ref": "#/responses/Label"
  45. var (
  46. err error
  47. )
  48. ID := ctx.Params(":id")
  49. job, err := models.GetCloudbrainByID(ID)
  50. if err != nil {
  51. ctx.NotFound(err)
  52. log.Error("GetCloudbrainByID failed:", err)
  53. return
  54. }
  55. jobResult, err := cloudbrain.GetJob(job.JobID)
  56. if err != nil {
  57. ctx.NotFound(err)
  58. log.Error("GetJob failed:", err)
  59. return
  60. }
  61. result, _ := models.ConvertToJobResultPayload(jobResult.Payload)
  62. if err != nil {
  63. ctx.NotFound(err)
  64. log.Error("ConvertToJobResultPayload failed:", err)
  65. return
  66. }
  67. job.Status = result.JobStatus.State
  68. taskRoles := result.TaskRoles
  69. taskRes, _ := models.ConvertToTaskPod(taskRoles[cloudbrain.SubTaskName].(map[string]interface{}))
  70. if result.JobStatus.State != string(models.JobWaiting) && result.JobStatus.State != string(models.JobFailed) {
  71. job.ContainerIp = taskRes.TaskStatuses[0].ContainerIP
  72. job.ContainerID = taskRes.TaskStatuses[0].ContainerID
  73. job.Status = taskRes.TaskStatuses[0].State
  74. }
  75. if result.JobStatus.State != string(models.JobWaiting) {
  76. models.ParseAndSetDurationFromCloudBrainOne(result, job)
  77. err = models.UpdateJob(job)
  78. if err != nil {
  79. log.Error("UpdateJob failed:", err)
  80. }
  81. }
  82. ctx.JSON(http.StatusOK, map[string]interface{}{
  83. "ID": ID,
  84. "JobName": result.Config.JobName,
  85. "JobStatus": result.JobStatus.State,
  86. "SubState": result.JobStatus.SubState,
  87. "CreatedTime": time.Unix(result.JobStatus.CreatedTime/1000, 0).Format("2006-01-02 15:04:05"),
  88. "CompletedTime": time.Unix(result.JobStatus.CompletedTime/1000, 0).Format("2006-01-02 15:04:05"),
  89. })
  90. }
  91. func GetCloudBrainInferenceJob(ctx *context.APIContext) {
  92. jobID := ctx.Params(":jobid")
  93. job, err := models.GetCloudbrainByJobID(jobID)
  94. if err != nil {
  95. ctx.NotFound(err)
  96. return
  97. }
  98. jobResult, err := cloudbrain.GetJob(job.JobID)
  99. if err != nil {
  100. ctx.NotFound(err)
  101. log.Error("GetJob failed:", err)
  102. return
  103. }
  104. result, err := models.ConvertToJobResultPayload(jobResult.Payload)
  105. if err != nil {
  106. ctx.NotFound(err)
  107. log.Error("ConvertToJobResultPayload failed:", err)
  108. return
  109. }
  110. job.Status = result.JobStatus.State
  111. if result.JobStatus.State != string(models.JobWaiting) && result.JobStatus.State != string(models.JobFailed) {
  112. taskRoles := result.TaskRoles
  113. taskRes, _ := models.ConvertToTaskPod(taskRoles[cloudbrain.SubTaskName].(map[string]interface{}))
  114. job.ContainerIp = taskRes.TaskStatuses[0].ContainerIP
  115. job.ContainerID = taskRes.TaskStatuses[0].ContainerID
  116. job.Status = taskRes.TaskStatuses[0].State
  117. }
  118. if result.JobStatus.State != string(models.JobWaiting) {
  119. models.ParseAndSetDurationFromCloudBrainOne(result, job)
  120. err = models.UpdateJob(job)
  121. if err != nil {
  122. log.Error("UpdateJob failed:", err)
  123. }
  124. }
  125. ctx.JSON(http.StatusOK, map[string]interface{}{
  126. "JobID": jobID,
  127. "JobStatus": job.Status,
  128. "JobDuration": job.TrainJobDuration,
  129. })
  130. }
  131. func DelCloudBrainJob(ctx *context.APIContext) {
  132. jobID := ctx.Params(":jobid")
  133. errStr := cloudbrain.DelCloudBrainJob(jobID)
  134. if errStr != "" {
  135. ctx.JSON(http.StatusOK, map[string]interface{}{
  136. "message": ctx.Tr(errStr),
  137. "VersionName": "1",
  138. "code": 1,
  139. })
  140. } else {
  141. ctx.JSON(http.StatusOK, map[string]interface{}{
  142. "message": "",
  143. "VersionName": "1",
  144. "code": 0,
  145. })
  146. }
  147. }
  148. func InferencJobResultList(ctx *context.APIContext) {
  149. jobID := ctx.Params(":jobid")
  150. parentDir := ctx.Query("parentDir")
  151. dirArray := strings.Split(parentDir, "/")
  152. task, err := models.GetCloudbrainByJobID(jobID)
  153. if err != nil {
  154. log.Error("get cloud brain err:", err)
  155. ctx.ServerError("get cloud brain information failed:", err)
  156. }
  157. //get dirs
  158. dirs, err := routerRepo.GetResultDirs(task.JobName, parentDir)
  159. if err != nil {
  160. log.Error("GetModelDirs failed:%v", err.Error(), ctx.Data["msgID"])
  161. ctx.ServerError("GetModelDirs failed:", err)
  162. return
  163. }
  164. var fileInfos []storage.FileInfo
  165. err = json.Unmarshal([]byte(dirs), &fileInfos)
  166. if err != nil {
  167. log.Error("json.Unmarshal failed:%v", err.Error(), ctx.Data["msgID"])
  168. ctx.ServerError("json.Unmarshal failed:", err)
  169. return
  170. }
  171. for i, fileInfo := range fileInfos {
  172. temp, _ := time.Parse("2006-01-02 15:04:05", fileInfo.ModTime)
  173. fileInfos[i].ModTime = temp.Local().Format("2006-01-02 15:04:05")
  174. }
  175. sort.Slice(fileInfos, func(i, j int) bool {
  176. return fileInfos[i].ModTime > fileInfos[j].ModTime
  177. })
  178. ctx.JSON(http.StatusOK, map[string]interface{}{
  179. "JobID": jobID,
  180. "StatusOK": 0,
  181. "Path": dirArray,
  182. "Dirs": fileInfos,
  183. "task": task,
  184. "PageIsCloudBrain": true,
  185. })
  186. }
  187. func CloudbrainGetLog(ctx *context.Context) {
  188. ID := ctx.Params(":id")
  189. job, err := models.GetCloudbrainByID(ID)
  190. if err != nil {
  191. log.Error("GetCloudbrainByJobName failed: %v", err, ctx.Data["MsgID"])
  192. ctx.ServerError(err.Error(), err)
  193. return
  194. }
  195. var hits []models.Hits
  196. result, err := cloudbrain.GetJobLog(job.JobID)
  197. if err != nil {
  198. log.Error("GetJobLog failed: %v", err, ctx.Data["MsgID"])
  199. ctx.ServerError(err.Error(), err)
  200. return
  201. }
  202. hits = result.Hits.Hits
  203. //if the size equal page_size, then take the scroll_id to get all log and delete the scroll_id(the num of scroll_id is limited)
  204. if len(result.Hits.Hits) >= cloudbrain.LogPageSize {
  205. for {
  206. resultNext, err := cloudbrain.GetJobAllLog(result.ScrollID)
  207. if err != nil {
  208. log.Error("GetJobAllLog failed: %v", err, ctx.Data["MsgID"])
  209. } else {
  210. for _, hit := range resultNext.Hits.Hits {
  211. hits = append(hits, hit)
  212. }
  213. }
  214. if len(resultNext.Hits.Hits) < cloudbrain.LogPageSize {
  215. log.Info("get all log already")
  216. break
  217. }
  218. }
  219. }
  220. cloudbrain.DeleteJobLogToken(result.ScrollID)
  221. sort.Slice(hits, func(i, j int) bool {
  222. return hits[i].Sort[0] < hits[j].Sort[0]
  223. })
  224. var content string
  225. for _, log := range hits {
  226. content += log.Source.Message + "\n"
  227. }
  228. ctx.JSON(http.StatusOK, map[string]interface{}{
  229. "JobName": job.JobName,
  230. "Content": content,
  231. })
  232. return
  233. }
  234. func CloudBrainModelList(ctx *context.APIContext) {
  235. var (
  236. err error
  237. )
  238. var jobID = ctx.Params(":jobid")
  239. var versionName = ctx.Query("version_name")
  240. parentDir := ctx.Query("parentDir")
  241. dirArray := strings.Split(parentDir, "/")
  242. task, err := models.GetCloudbrainByJobIDAndVersionName(jobID, versionName)
  243. if err != nil {
  244. log.Error("GetCloudbrainByJobID(%s) failed:%v", task.JobName, err.Error())
  245. return
  246. }
  247. //get dirs
  248. dirs, err := routerRepo.GetModelDirs(task.JobName, parentDir)
  249. if err != nil {
  250. log.Error("GetModelDirs failed:%v", err.Error(), ctx.Data["msgID"])
  251. ctx.ServerError("GetModelDirs failed:", err)
  252. return
  253. }
  254. var fileInfos []storage.FileInfo
  255. err = json.Unmarshal([]byte(dirs), &fileInfos)
  256. if err != nil {
  257. log.Error("json.Unmarshal failed:%v", err.Error(), ctx.Data["msgID"])
  258. ctx.ServerError("json.Unmarshal failed:", err)
  259. return
  260. }
  261. for i, fileInfo := range fileInfos {
  262. temp, _ := time.Parse("2006-01-02 15:04:05", fileInfo.ModTime)
  263. fileInfos[i].ModTime = temp.Local().Format("2006-01-02 15:04:05")
  264. }
  265. sort.Slice(fileInfos, func(i, j int) bool {
  266. return fileInfos[i].ModTime > fileInfos[j].ModTime
  267. })
  268. ctx.JSON(http.StatusOK, map[string]interface{}{
  269. "JobID": jobID,
  270. "VersionName": versionName,
  271. "StatusOK": 0,
  272. "Path": dirArray,
  273. "Dirs": fileInfos,
  274. "task": task,
  275. "PageIsCloudBrain": true,
  276. })
  277. }