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