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 16 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
3 years ago
4 years ago
4 years ago
3 years ago
4 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 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
2 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
2 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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/modules/setting"
  13. "code.gitea.io/gitea/models"
  14. "code.gitea.io/gitea/modules/cloudbrain"
  15. "code.gitea.io/gitea/modules/context"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/modelarts"
  18. "code.gitea.io/gitea/modules/storage"
  19. routerRepo "code.gitea.io/gitea/routers/repo"
  20. )
  21. // cloudbrain get job task by jobid
  22. func GetCloudbrainTask(ctx *context.APIContext) {
  23. // swagger:operation GET /repos/{owner}/{repo}/cloudbrain/{jobid} cloudbrain jobTask
  24. // ---
  25. // summary: Get a single task
  26. // produces:
  27. // - application/json
  28. // parameters:
  29. // - name: owner
  30. // in: path
  31. // description: owner of the repo
  32. // type: string
  33. // required: true
  34. // - name: repo
  35. // in: path
  36. // description: name of the repo
  37. // type: string
  38. // required: true
  39. // - name: jobid
  40. // in: path
  41. // description: id of cloudbrain jobid
  42. // type: string
  43. // required: true
  44. // responses:
  45. // "200":
  46. // "$ref": "#/responses/Label"
  47. var (
  48. err error
  49. )
  50. ID := ctx.Params(":id")
  51. job, err := models.GetCloudbrainByID(ID)
  52. if err != nil {
  53. ctx.NotFound(err)
  54. log.Error("GetCloudbrainByID failed:", err)
  55. return
  56. }
  57. jobResult, err := cloudbrain.GetJob(job.JobID)
  58. if err != nil {
  59. ctx.NotFound(err)
  60. log.Error("GetJob failed:", err)
  61. return
  62. }
  63. result, _ := models.ConvertToJobResultPayload(jobResult.Payload)
  64. if err != nil {
  65. ctx.NotFound(err)
  66. log.Error("ConvertToJobResultPayload failed:", err)
  67. return
  68. }
  69. job.Status = result.JobStatus.State
  70. taskRoles := result.TaskRoles
  71. taskRes, _ := models.ConvertToTaskPod(taskRoles[cloudbrain.SubTaskName].(map[string]interface{}))
  72. if result.JobStatus.State != string(models.JobWaiting) && result.JobStatus.State != string(models.JobFailed) {
  73. job.ContainerIp = taskRes.TaskStatuses[0].ContainerIP
  74. job.ContainerID = taskRes.TaskStatuses[0].ContainerID
  75. job.Status = taskRes.TaskStatuses[0].State
  76. }
  77. if result.JobStatus.State != string(models.JobWaiting) {
  78. models.ParseAndSetDurationFromCloudBrainOne(result, job)
  79. err = models.UpdateJob(job)
  80. if err != nil {
  81. log.Error("UpdateJob failed:", err)
  82. }
  83. }
  84. ctx.JSON(http.StatusOK, map[string]interface{}{
  85. "ID": ID,
  86. "JobName": result.Config.JobName,
  87. "JobStatus": result.JobStatus.State,
  88. "SubState": result.JobStatus.SubState,
  89. "CreatedTime": time.Unix(result.JobStatus.CreatedTime/1000, 0).Format("2006-01-02 15:04:05"),
  90. "CompletedTime": time.Unix(result.JobStatus.CompletedTime/1000, 0).Format("2006-01-02 15:04:05"),
  91. })
  92. }
  93. func GetCloudBrainInferenceJob(ctx *context.APIContext) {
  94. jobID := ctx.Params(":jobid")
  95. job, err := models.GetCloudbrainByJobID(jobID)
  96. if err != nil {
  97. ctx.NotFound(err)
  98. return
  99. }
  100. jobResult, err := cloudbrain.GetJob(job.JobID)
  101. if err != nil {
  102. ctx.NotFound(err)
  103. log.Error("GetJob failed:", err)
  104. return
  105. }
  106. result, err := models.ConvertToJobResultPayload(jobResult.Payload)
  107. if err != nil {
  108. ctx.NotFound(err)
  109. log.Error("ConvertToJobResultPayload failed:", err)
  110. return
  111. }
  112. job.Status = result.JobStatus.State
  113. if result.JobStatus.State != string(models.JobWaiting) && result.JobStatus.State != string(models.JobFailed) {
  114. taskRoles := result.TaskRoles
  115. taskRes, _ := models.ConvertToTaskPod(taskRoles[cloudbrain.SubTaskName].(map[string]interface{}))
  116. job.ContainerIp = taskRes.TaskStatuses[0].ContainerIP
  117. job.ContainerID = taskRes.TaskStatuses[0].ContainerID
  118. job.Status = taskRes.TaskStatuses[0].State
  119. }
  120. if result.JobStatus.State != string(models.JobWaiting) {
  121. models.ParseAndSetDurationFromCloudBrainOne(result, job)
  122. err = models.UpdateJob(job)
  123. if err != nil {
  124. log.Error("UpdateJob failed:", err)
  125. }
  126. }
  127. ctx.JSON(http.StatusOK, map[string]interface{}{
  128. "JobID": jobID,
  129. "JobStatus": job.Status,
  130. "JobDuration": job.TrainJobDuration,
  131. })
  132. }
  133. func DelCloudBrainJob(ctx *context.APIContext) {
  134. jobID := ctx.Params(":jobid")
  135. errStr := cloudbrain.DelCloudBrainJob(jobID)
  136. if errStr != "" {
  137. ctx.JSON(http.StatusOK, map[string]interface{}{
  138. "Message": ctx.Tr(errStr),
  139. "VersionName": "1",
  140. "Code": 1,
  141. })
  142. } else {
  143. ctx.JSON(http.StatusOK, map[string]interface{}{
  144. "Message": "",
  145. "VersionName": "1",
  146. "Code": 0,
  147. })
  148. }
  149. }
  150. func InferencJobResultList(ctx *context.APIContext) {
  151. jobID := ctx.Params(":jobid")
  152. parentDir := ctx.Query("parentDir")
  153. dirArray := strings.Split(parentDir, "/")
  154. task, err := models.GetCloudbrainByJobID(jobID)
  155. if err != nil {
  156. log.Error("get cloud brain err:", err)
  157. ctx.ServerError("get cloud brain information failed:", err)
  158. }
  159. //get dirs
  160. dirs, err := routerRepo.GetResultDirs(task.JobName, parentDir)
  161. if err != nil {
  162. log.Error("GetModelDirs failed:%v", err.Error(), ctx.Data["msgID"])
  163. ctx.ServerError("GetModelDirs failed:", err)
  164. return
  165. }
  166. var fileInfos []storage.FileInfo
  167. err = json.Unmarshal([]byte(dirs), &fileInfos)
  168. if err != nil {
  169. log.Error("json.Unmarshal failed:%v", err.Error(), ctx.Data["msgID"])
  170. ctx.ServerError("json.Unmarshal failed:", err)
  171. return
  172. }
  173. for i, fileInfo := range fileInfos {
  174. temp, _ := time.Parse("2006-01-02 15:04:05", fileInfo.ModTime)
  175. fileInfos[i].ModTime = temp.Local().Format("2006-01-02 15:04:05")
  176. }
  177. sort.Slice(fileInfos, func(i, j int) bool {
  178. return fileInfos[i].ModTime > fileInfos[j].ModTime
  179. })
  180. ctx.JSON(http.StatusOK, map[string]interface{}{
  181. "JobID": jobID,
  182. "StatusOK": 0,
  183. "Path": dirArray,
  184. "Dirs": fileInfos,
  185. "task": task,
  186. "PageIsCloudBrain": true,
  187. })
  188. }
  189. func GetCloudbrainModelConvertTask(ctx *context.APIContext) {
  190. var (
  191. err error
  192. )
  193. ID := ctx.Params(":id")
  194. job, err := models.QueryModelConvertById(ID)
  195. if err != nil {
  196. ctx.NotFound(err)
  197. log.Error("GetCloudbrainByID failed:", err)
  198. return
  199. }
  200. if job.IsGpuTrainTask() {
  201. jobResult, err := cloudbrain.GetJob(job.CloudBrainTaskId)
  202. if err != nil {
  203. ctx.NotFound(err)
  204. log.Error("GetJob failed:", err)
  205. return
  206. }
  207. result, _ := models.ConvertToJobResultPayload(jobResult.Payload)
  208. if err != nil {
  209. ctx.NotFound(err)
  210. log.Error("ConvertToJobResultPayload failed:", err)
  211. return
  212. }
  213. job.Status = result.JobStatus.State
  214. taskRoles := result.TaskRoles
  215. taskRes, _ := models.ConvertToTaskPod(taskRoles[cloudbrain.SubTaskName].(map[string]interface{}))
  216. if result.JobStatus.State != string(models.JobWaiting) && result.JobStatus.State != string(models.JobFailed) {
  217. job.ContainerIp = taskRes.TaskStatuses[0].ContainerIP
  218. job.ContainerID = taskRes.TaskStatuses[0].ContainerID
  219. job.Status = taskRes.TaskStatuses[0].State
  220. }
  221. if result.JobStatus.State != string(models.JobWaiting) {
  222. models.ModelComputeAndSetDuration(job, result)
  223. err = models.UpdateModelConvert(job)
  224. if err != nil {
  225. log.Error("UpdateJob failed:", err)
  226. }
  227. }
  228. ctx.JSON(http.StatusOK, map[string]interface{}{
  229. "ID": ID,
  230. "JobName": result.Config.JobName,
  231. "JobStatus": result.JobStatus.State,
  232. "SubState": result.JobStatus.SubState,
  233. "CreatedTime": time.Unix(result.JobStatus.CreatedTime/1000, 0).Format("2006-01-02 15:04:05"),
  234. "CompletedTime": time.Unix(result.JobStatus.CompletedTime/1000, 0).Format("2006-01-02 15:04:05"),
  235. })
  236. } else {
  237. result, err := modelarts.GetTrainJob(job.CloudBrainTaskId, job.ModelArtsVersionId)
  238. if err != nil {
  239. log.Error("get modelart job failed:", err)
  240. ctx.NotFound(err)
  241. return
  242. }
  243. job.Status = modelarts.TransTrainJobStatus(result.IntStatus)
  244. job.RunTime = result.Duration / 1000
  245. job.TrainJobDuration = models.ConvertDurationToStr(job.RunTime)
  246. err = models.UpdateModelConvert(job)
  247. if err != nil {
  248. log.Error("UpdateJob failed:", err)
  249. }
  250. ctx.JSON(http.StatusOK, map[string]interface{}{
  251. "ID": ID,
  252. "JobStatus": job.Status,
  253. })
  254. }
  255. }
  256. func CloudbrainGetLogByJobId(jobId string, jobName string) map[string]interface{} {
  257. var hits []models.Hits
  258. result, err := cloudbrain.GetJobLog(jobId)
  259. if err != nil {
  260. log.Error("GetJobLog failed: %v", err)
  261. return nil
  262. }
  263. hits = result.Hits.Hits
  264. //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)
  265. if len(result.Hits.Hits) >= cloudbrain.LogPageSize {
  266. for {
  267. resultNext, err := cloudbrain.GetJobAllLog(result.ScrollID)
  268. if err != nil {
  269. log.Error("GetJobAllLog failed: %v", err)
  270. } else {
  271. for _, hit := range resultNext.Hits.Hits {
  272. hits = append(hits, hit)
  273. }
  274. }
  275. if len(resultNext.Hits.Hits) < cloudbrain.LogPageSize {
  276. log.Info("get all log already")
  277. break
  278. }
  279. }
  280. }
  281. cloudbrain.DeleteJobLogToken(result.ScrollID)
  282. sort.Slice(hits, func(i, j int) bool {
  283. return hits[i].Sort[0] < hits[j].Sort[0]
  284. })
  285. var content string
  286. for _, log := range hits {
  287. content += log.Source.Message + "\n"
  288. }
  289. return map[string]interface{}{
  290. "JobName": jobName,
  291. "Content": content,
  292. }
  293. }
  294. func CloudbrainForModelConvertGetLog(ctx *context.Context) {
  295. ID := ctx.Params(":id")
  296. job, err := models.QueryModelConvertById(ID)
  297. if err != nil {
  298. log.Error("GetCloudbrainByJobName failed: %v", err, ctx.Data["MsgID"])
  299. ctx.ServerError(err.Error(), err)
  300. return
  301. }
  302. result := CloudbrainGetLogByJobId(job.CloudBrainTaskId, job.Name)
  303. if result == nil {
  304. log.Error("GetJobLog failed: %v", err, ctx.Data["MsgID"])
  305. ctx.ServerError(err.Error(), err)
  306. return
  307. }
  308. ctx.JSON(http.StatusOK, result)
  309. }
  310. func CloudbrainGetLog(ctx *context.Context) {
  311. ID := ctx.Params(":id")
  312. job, err := models.GetCloudbrainByID(ID)
  313. if err != nil {
  314. log.Error("GetCloudbrainByJobName failed: %v", err, ctx.Data["MsgID"])
  315. ctx.ServerError(err.Error(), err)
  316. return
  317. }
  318. result := CloudbrainGetLogByJobId(job.JobID, job.JobName)
  319. if result == nil {
  320. log.Error("GetJobLog failed: %v", err, ctx.Data["MsgID"])
  321. ctx.ServerError(err.Error(), err)
  322. return
  323. }
  324. ctx.JSON(http.StatusOK, result)
  325. }
  326. func CloudBrainModelConvertList(ctx *context.APIContext) {
  327. var (
  328. err error
  329. )
  330. ID := ctx.Params(":id")
  331. parentDir := ctx.Query("parentDir")
  332. dirArray := strings.Split(parentDir, "/")
  333. job, err := models.QueryModelConvertById(ID)
  334. if err != nil {
  335. log.Error("GetCloudbrainByJobID(%s) failed:%v", job.Name, err.Error())
  336. return
  337. }
  338. if job.IsGpuTrainTask() {
  339. //get dirs
  340. dirs, err := routerRepo.GetModelDirs(job.ID, parentDir)
  341. if err != nil {
  342. log.Error("GetModelDirs failed:%v", err.Error(), ctx.Data["msgID"])
  343. ctx.ServerError("GetModelDirs failed:", err)
  344. return
  345. }
  346. var fileInfos []storage.FileInfo
  347. err = json.Unmarshal([]byte(dirs), &fileInfos)
  348. if err != nil {
  349. log.Error("json.Unmarshal failed:%v", err.Error(), ctx.Data["msgID"])
  350. ctx.ServerError("json.Unmarshal failed:", err)
  351. return
  352. }
  353. for i, fileInfo := range fileInfos {
  354. temp, _ := time.Parse("2006-01-02 15:04:05", fileInfo.ModTime)
  355. fileInfos[i].ModTime = temp.Local().Format("2006-01-02 15:04:05")
  356. }
  357. sort.Slice(fileInfos, func(i, j int) bool {
  358. return fileInfos[i].ModTime > fileInfos[j].ModTime
  359. })
  360. ctx.JSON(http.StatusOK, map[string]interface{}{
  361. "JobID": ID,
  362. "VersionName": "",
  363. "StatusOK": 0,
  364. "Path": dirArray,
  365. "Dirs": fileInfos,
  366. "task": job,
  367. "PageIsCloudBrain": true,
  368. })
  369. } else {
  370. var jobID = ctx.Params(":id")
  371. var versionName = "V0001"
  372. parentDir := ctx.Query("parentDir")
  373. dirArray := strings.Split(parentDir, "/")
  374. models, err := storage.GetObsListObject(job.ID, "output/", parentDir, versionName)
  375. if err != nil {
  376. log.Info("get TrainJobListModel failed:", err)
  377. ctx.ServerError("GetObsListObject:", err)
  378. return
  379. }
  380. ctx.JSON(http.StatusOK, map[string]interface{}{
  381. "JobID": jobID,
  382. "VersionName": versionName,
  383. "StatusOK": 0,
  384. "Path": dirArray,
  385. "Dirs": models,
  386. "task": job,
  387. "PageIsCloudBrain": true,
  388. })
  389. }
  390. }
  391. func CloudBrainModelList(ctx *context.APIContext) {
  392. var (
  393. err error
  394. )
  395. var jobID = ctx.Params(":jobid")
  396. var versionName = ctx.Query("version_name")
  397. parentDir := ctx.Query("parentDir")
  398. dirArray := strings.Split(parentDir, "/")
  399. task, err := models.GetCloudbrainByJobIDAndVersionName(jobID, versionName)
  400. if err != nil {
  401. log.Error("GetCloudbrainByJobID(%s) failed:%v", task.JobName, err.Error())
  402. return
  403. }
  404. //get dirs
  405. dirs, err := routerRepo.GetModelDirs(task.JobName, parentDir)
  406. if err != nil {
  407. log.Error("GetModelDirs failed:%v", err.Error(), ctx.Data["msgID"])
  408. ctx.ServerError("GetModelDirs failed:", err)
  409. return
  410. }
  411. var fileInfos []storage.FileInfo
  412. err = json.Unmarshal([]byte(dirs), &fileInfos)
  413. if err != nil {
  414. log.Error("json.Unmarshal failed:%v", err.Error(), ctx.Data["msgID"])
  415. ctx.ServerError("json.Unmarshal failed:", err)
  416. return
  417. }
  418. for i, fileInfo := range fileInfos {
  419. temp, _ := time.Parse("2006-01-02 15:04:05", fileInfo.ModTime)
  420. fileInfos[i].ModTime = temp.Local().Format("2006-01-02 15:04:05")
  421. }
  422. sort.Slice(fileInfos, func(i, j int) bool {
  423. return fileInfos[i].ModTime > fileInfos[j].ModTime
  424. })
  425. ctx.JSON(http.StatusOK, map[string]interface{}{
  426. "JobID": jobID,
  427. "VersionName": versionName,
  428. "StatusOK": 0,
  429. "Path": dirArray,
  430. "Dirs": fileInfos,
  431. "task": task,
  432. "PageIsCloudBrain": true,
  433. })
  434. }
  435. type JobInfo struct {
  436. JobName string `json:"job_name"`
  437. AiCenterId int `json:"ai_center_id"`
  438. }
  439. func GetNewestJobs(ctx *context.APIContext) {
  440. idsC2Net, err := models.GetNewestJobsByAiCenter()
  441. if err != nil {
  442. log.Error("GetNewestJobsByAiCenter(%s) failed:%v", err.Error())
  443. return
  444. }
  445. idsCloudbrain, err := models.GetNewestJobsByType()
  446. if err != nil {
  447. log.Error("GetNewestJobsByType(%s) failed:%v", err.Error())
  448. return
  449. }
  450. ids := make([]int64, len(idsC2Net), cap(idsC2Net)*2)
  451. copy(ids, idsC2Net)
  452. for _, id := range idsCloudbrain {
  453. ids = append(ids, id)
  454. }
  455. jobs, err := models.GetCloudbrainByIDs(ids)
  456. if err != nil {
  457. log.Error("GetCloudbrainByIDs(%s) failed:%v", err.Error())
  458. return
  459. }
  460. jobInfos := make([]JobInfo, 0)
  461. for _, job := range jobs {
  462. var id int
  463. var content string
  464. switch job.Type {
  465. case models.TypeCloudBrainOne:
  466. id, content = getAICenterID("cloudbrain_one")
  467. if content == "" {
  468. log.Error("job(%s) has no match config info", job.DisplayJobName)
  469. continue
  470. }
  471. case models.TypeCloudBrainTwo:
  472. id, content = getAICenterID("cloudbrain_two")
  473. if content == "" {
  474. log.Error("job(%s) has no match config info", job.DisplayJobName)
  475. continue
  476. }
  477. case models.TypeC2Net:
  478. centerInfo := strings.Split(job.AiCenter, "+")
  479. if len(centerInfo) != 2 {
  480. log.Error("job(%s):ai_center(%s) is wrong", job.DisplayJobName, job.AiCenter)
  481. continue
  482. }
  483. id, content = getAICenterID(centerInfo[0])
  484. if content == "" {
  485. log.Error("job(%s) has no match config info", job.DisplayJobName)
  486. continue
  487. }
  488. default:
  489. log.Error("no match info")
  490. continue
  491. }
  492. jobInfos = append(jobInfos, JobInfo{
  493. JobName: job.DisplayJobName,
  494. AiCenterId: id,
  495. })
  496. }
  497. ctx.JSON(http.StatusOK, jobInfos)
  498. }
  499. func GetAICenterInfo(ctx *context.APIContext) {
  500. if setting.C2NetInfos == nil {
  501. log.Error("C2NET_SEQUENCE is incorrect")
  502. return
  503. }
  504. ctx.JSON(http.StatusOK, setting.C2NetInfos.C2NetSqInfo)
  505. }
  506. func getAICenterID(name string) (int, string) {
  507. for _, info := range setting.C2NetInfos.C2NetSqInfo {
  508. if name == info.Name {
  509. return info.ID, info.Content
  510. }
  511. }
  512. return 0, ""
  513. }