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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. var versionName = ctx.Query("version_name")
  134. errStr := cloudbrain.DelCloudBrainJob(jobID)
  135. if errStr != "" {
  136. ctx.JSON(http.StatusOK, map[string]interface{}{
  137. "Message": ctx.Tr(errStr),
  138. "VersionName": versionName,
  139. "code": 1,
  140. })
  141. } else {
  142. ctx.JSON(http.StatusOK, map[string]interface{}{
  143. "Message": "",
  144. "VersionName": versionName,
  145. "code": 0,
  146. })
  147. }
  148. }
  149. func InferencJobResultList(ctx *context.APIContext) {
  150. jobID := ctx.Params(":jobid")
  151. parentDir := ctx.Query("parentDir")
  152. dirArray := strings.Split(parentDir, "/")
  153. task, err := models.GetCloudbrainByJobID(jobID)
  154. if err != nil {
  155. log.Error("get cloud brain err:", err)
  156. ctx.ServerError("get cloud brain information failed:", err)
  157. }
  158. //get dirs
  159. dirs, err := routerRepo.GetResultDirs(task.JobName, parentDir)
  160. if err != nil {
  161. log.Error("GetModelDirs failed:%v", err.Error(), ctx.Data["msgID"])
  162. ctx.ServerError("GetModelDirs failed:", err)
  163. return
  164. }
  165. var fileInfos []storage.FileInfo
  166. err = json.Unmarshal([]byte(dirs), &fileInfos)
  167. if err != nil {
  168. log.Error("json.Unmarshal failed:%v", err.Error(), ctx.Data["msgID"])
  169. ctx.ServerError("json.Unmarshal failed:", err)
  170. return
  171. }
  172. for i, fileInfo := range fileInfos {
  173. temp, _ := time.Parse("2006-01-02 15:04:05", fileInfo.ModTime)
  174. fileInfos[i].ModTime = temp.Local().Format("2006-01-02 15:04:05")
  175. }
  176. sort.Slice(fileInfos, func(i, j int) bool {
  177. return fileInfos[i].ModTime > fileInfos[j].ModTime
  178. })
  179. ctx.JSON(http.StatusOK, map[string]interface{}{
  180. "JobID": jobID,
  181. "StatusOK": 0,
  182. "Path": dirArray,
  183. "Dirs": fileInfos,
  184. "task": task,
  185. "PageIsCloudBrain": true,
  186. })
  187. }
  188. func CloudbrainGetLog(ctx *context.Context) {
  189. ID := ctx.Params(":id")
  190. job, err := models.GetCloudbrainByID(ID)
  191. if err != nil {
  192. log.Error("GetCloudbrainByJobName failed: %v", err, ctx.Data["MsgID"])
  193. ctx.ServerError(err.Error(), err)
  194. return
  195. }
  196. var hits []models.Hits
  197. result, err := cloudbrain.GetJobLog(job.JobID)
  198. if err != nil {
  199. log.Error("GetJobLog failed: %v", err, ctx.Data["MsgID"])
  200. ctx.ServerError(err.Error(), err)
  201. return
  202. }
  203. hits = result.Hits.Hits
  204. //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)
  205. if len(result.Hits.Hits) >= cloudbrain.LogPageSize {
  206. for {
  207. resultNext, err := cloudbrain.GetJobAllLog(result.ScrollID)
  208. if err != nil {
  209. log.Error("GetJobAllLog failed: %v", err, ctx.Data["MsgID"])
  210. } else {
  211. for _, hit := range resultNext.Hits.Hits {
  212. hits = append(hits, hit)
  213. }
  214. }
  215. if len(resultNext.Hits.Hits) < cloudbrain.LogPageSize {
  216. log.Info("get all log already")
  217. break
  218. }
  219. }
  220. }
  221. cloudbrain.DeleteJobLogToken(result.ScrollID)
  222. sort.Slice(hits, func(i, j int) bool {
  223. return hits[i].Sort[0] < hits[j].Sort[0]
  224. })
  225. var content string
  226. for _, log := range hits {
  227. content += log.Source.Message + "\n"
  228. }
  229. ctx.JSON(http.StatusOK, map[string]interface{}{
  230. "JobName": job.JobName,
  231. "Content": content,
  232. })
  233. return
  234. }
  235. func CloudBrainModelList(ctx *context.APIContext) {
  236. var (
  237. err error
  238. )
  239. var jobID = ctx.Params(":jobid")
  240. var versionName = ctx.Query("version_name")
  241. parentDir := ctx.Query("parentDir")
  242. dirArray := strings.Split(parentDir, "/")
  243. task, err := models.GetCloudbrainByJobIDAndVersionName(jobID, versionName)
  244. if err != nil {
  245. log.Error("GetCloudbrainByJobID(%s) failed:%v", task.JobName, err.Error())
  246. return
  247. }
  248. //get dirs
  249. dirs, err := routerRepo.GetModelDirs(task.JobName, parentDir)
  250. if err != nil {
  251. log.Error("GetModelDirs failed:%v", err.Error(), ctx.Data["msgID"])
  252. ctx.ServerError("GetModelDirs failed:", err)
  253. return
  254. }
  255. var fileInfos []storage.FileInfo
  256. err = json.Unmarshal([]byte(dirs), &fileInfos)
  257. if err != nil {
  258. log.Error("json.Unmarshal failed:%v", err.Error(), ctx.Data["msgID"])
  259. ctx.ServerError("json.Unmarshal failed:", err)
  260. return
  261. }
  262. for i, fileInfo := range fileInfos {
  263. temp, _ := time.Parse("2006-01-02 15:04:05", fileInfo.ModTime)
  264. fileInfos[i].ModTime = temp.Local().Format("2006-01-02 15:04:05")
  265. }
  266. sort.Slice(fileInfos, func(i, j int) bool {
  267. return fileInfos[i].ModTime > fileInfos[j].ModTime
  268. })
  269. ctx.JSON(http.StatusOK, map[string]interface{}{
  270. "JobID": jobID,
  271. "VersionName": versionName,
  272. "StatusOK": 0,
  273. "Path": dirArray,
  274. "Dirs": fileInfos,
  275. "task": task,
  276. "PageIsCloudBrain": true,
  277. })
  278. }