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