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 18 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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 CloudbrainGetLog(ctx *context.Context) {
  324. ID := ctx.Params(":id")
  325. startLine := ctx.QueryInt("base_line")
  326. lines := ctx.QueryInt("lines")
  327. job, err := models.GetCloudbrainByID(ID)
  328. if err != nil {
  329. log.Error("GetCloudbrainByJobName failed: %v", err, ctx.Data["MsgID"])
  330. ctx.ServerError(err.Error(), err)
  331. return
  332. }
  333. result := GetLogFromModelDir(job.JobName, startLine, startLine+lines)
  334. if result == nil {
  335. log.Error("GetJobLog failed: %v", err, ctx.Data["MsgID"])
  336. ctx.ServerError(err.Error(), err)
  337. return
  338. }
  339. re := map[string]interface{}{
  340. "JobID": ID,
  341. "LogFileName": result["FileName"],
  342. "StartLine": startLine,
  343. "EndLine": startLine + lines,
  344. "Content": result["Content"],
  345. "Lines": lines,
  346. "CanLogDownload": result["Content"] != "",
  347. }
  348. //result := CloudbrainGetLogByJobId(job.JobID, job.JobName)
  349. ctx.JSON(http.StatusOK, re)
  350. }
  351. func GetLogFromModelDir(jobName string, startLine int, endLine int) map[string]interface{} {
  352. prefix := "/" + setting.CBCodePathPrefix + jobName + "/model"
  353. files, err := storage.GetOneLevelAllObjectUnderDirMinio(setting.Attachment.Minio.Bucket, prefix, "")
  354. if err != nil {
  355. log.Error("query cloudbrain model failed: %v", err)
  356. return nil
  357. }
  358. re := ""
  359. fileName := ""
  360. for _, file := range files {
  361. if strings.HasSuffix(file.FileName, "log.txt") {
  362. fileName = file.FileName
  363. path := storage.GetMinioPath(jobName+"/model/", file.FileName)
  364. log.Info("path=" + path)
  365. reader, err := os.Open(path)
  366. defer reader.Close()
  367. if err == nil {
  368. r := bufio.NewReader(reader)
  369. for i := 0; i < endLine; i++ {
  370. line, error := r.ReadString('\n')
  371. log.Info("line=" + line)
  372. if error == io.EOF {
  373. log.Info("read file completed.")
  374. break
  375. }
  376. if error != nil {
  377. log.Info("read file error." + error.Error())
  378. break
  379. }
  380. if error == nil {
  381. if i >= startLine {
  382. re = re + line
  383. }
  384. }
  385. }
  386. } else {
  387. log.Info("error:" + err.Error())
  388. }
  389. break
  390. }
  391. }
  392. return map[string]interface{}{
  393. "JobName": jobName,
  394. "Content": re,
  395. "FileName": fileName,
  396. }
  397. }
  398. func CloudBrainModelConvertList(ctx *context.APIContext) {
  399. var (
  400. err error
  401. )
  402. ID := ctx.Params(":id")
  403. parentDir := ctx.Query("parentDir")
  404. dirArray := strings.Split(parentDir, "/")
  405. job, err := models.QueryModelConvertById(ID)
  406. if err != nil {
  407. log.Error("GetCloudbrainByJobID(%s) failed:%v", job.Name, err.Error())
  408. return
  409. }
  410. if job.IsGpuTrainTask() {
  411. //get dirs
  412. dirs, err := routerRepo.GetModelDirs(job.ID, parentDir)
  413. if err != nil {
  414. log.Error("GetModelDirs failed:%v", err.Error(), ctx.Data["msgID"])
  415. ctx.ServerError("GetModelDirs failed:", err)
  416. return
  417. }
  418. var fileInfos []storage.FileInfo
  419. err = json.Unmarshal([]byte(dirs), &fileInfos)
  420. if err != nil {
  421. log.Error("json.Unmarshal failed:%v", err.Error(), ctx.Data["msgID"])
  422. ctx.ServerError("json.Unmarshal failed:", err)
  423. return
  424. }
  425. for i, fileInfo := range fileInfos {
  426. temp, _ := time.Parse("2006-01-02 15:04:05", fileInfo.ModTime)
  427. fileInfos[i].ModTime = temp.Local().Format("2006-01-02 15:04:05")
  428. }
  429. sort.Slice(fileInfos, func(i, j int) bool {
  430. return fileInfos[i].ModTime > fileInfos[j].ModTime
  431. })
  432. ctx.JSON(http.StatusOK, map[string]interface{}{
  433. "JobID": ID,
  434. "VersionName": "",
  435. "StatusOK": 0,
  436. "Path": dirArray,
  437. "Dirs": fileInfos,
  438. "task": job,
  439. "PageIsCloudBrain": true,
  440. })
  441. } else {
  442. var jobID = ctx.Params(":id")
  443. var versionName = "V0001"
  444. parentDir := ctx.Query("parentDir")
  445. dirArray := strings.Split(parentDir, "/")
  446. models, err := storage.GetObsListObject(job.ID, "output/", parentDir, versionName)
  447. if err != nil {
  448. log.Info("get TrainJobListModel failed:", err)
  449. ctx.ServerError("GetObsListObject:", err)
  450. return
  451. }
  452. ctx.JSON(http.StatusOK, map[string]interface{}{
  453. "JobID": jobID,
  454. "VersionName": versionName,
  455. "StatusOK": 0,
  456. "Path": dirArray,
  457. "Dirs": models,
  458. "task": job,
  459. "PageIsCloudBrain": true,
  460. })
  461. }
  462. }
  463. func CloudBrainModelList(ctx *context.APIContext) {
  464. var (
  465. err error
  466. )
  467. var jobID = ctx.Params(":jobid")
  468. var versionName = ctx.Query("version_name")
  469. parentDir := ctx.Query("parentDir")
  470. dirArray := strings.Split(parentDir, "/")
  471. task, err := models.GetCloudbrainByJobIDAndVersionName(jobID, versionName)
  472. if err != nil {
  473. log.Error("GetCloudbrainByJobID(%s) failed:%v", task.JobName, err.Error())
  474. return
  475. }
  476. //get dirs
  477. dirs, err := routerRepo.GetModelDirs(task.JobName, parentDir)
  478. if err != nil {
  479. log.Error("GetModelDirs failed:%v", err.Error(), ctx.Data["msgID"])
  480. ctx.ServerError("GetModelDirs failed:", err)
  481. return
  482. }
  483. var fileInfos []storage.FileInfo
  484. err = json.Unmarshal([]byte(dirs), &fileInfos)
  485. if err != nil {
  486. log.Error("json.Unmarshal failed:%v", err.Error(), ctx.Data["msgID"])
  487. ctx.ServerError("json.Unmarshal failed:", err)
  488. return
  489. }
  490. for i, fileInfo := range fileInfos {
  491. temp, _ := time.Parse("2006-01-02 15:04:05", fileInfo.ModTime)
  492. fileInfos[i].ModTime = temp.Local().Format("2006-01-02 15:04:05")
  493. }
  494. sort.Slice(fileInfos, func(i, j int) bool {
  495. return fileInfos[i].ModTime > fileInfos[j].ModTime
  496. })
  497. ctx.JSON(http.StatusOK, map[string]interface{}{
  498. "JobID": jobID,
  499. "VersionName": versionName,
  500. "StatusOK": 0,
  501. "Path": dirArray,
  502. "Dirs": fileInfos,
  503. "task": task,
  504. "PageIsCloudBrain": true,
  505. })
  506. }
  507. type JobInfo struct {
  508. JobName string `json:"job_name"`
  509. AiCenterId int `json:"ai_center_id"`
  510. }
  511. func GetNewestJobs(ctx *context.APIContext) {
  512. idsC2Net, err := models.GetNewestJobsByAiCenter()
  513. if err != nil {
  514. log.Error("GetNewestJobsByAiCenter(%s) failed:%v", err.Error())
  515. return
  516. }
  517. idsCloudbrain, err := models.GetNewestJobsByType()
  518. if err != nil {
  519. log.Error("GetNewestJobsByType(%s) failed:%v", err.Error())
  520. return
  521. }
  522. ids := make([]int64, len(idsC2Net), cap(idsC2Net)*2)
  523. copy(ids, idsC2Net)
  524. for _, id := range idsCloudbrain {
  525. ids = append(ids, id)
  526. }
  527. jobs, err := models.GetCloudbrainByIDs(ids)
  528. if err != nil {
  529. log.Error("GetCloudbrainByIDs(%s) failed:%v", err.Error())
  530. return
  531. }
  532. jobInfos := make([]JobInfo, 0)
  533. for _, job := range jobs {
  534. var id int
  535. var content string
  536. switch job.Type {
  537. case models.TypeCloudBrainOne:
  538. id, content = getAICenterID("cloudbrain_one")
  539. if content == "" {
  540. log.Error("job(%s) has no match config info", job.DisplayJobName)
  541. continue
  542. }
  543. case models.TypeCloudBrainTwo:
  544. id, content = getAICenterID("cloudbrain_two")
  545. if content == "" {
  546. log.Error("job(%s) has no match config info", job.DisplayJobName)
  547. continue
  548. }
  549. case models.TypeC2Net:
  550. centerInfo := strings.Split(job.AiCenter, "+")
  551. if len(centerInfo) != 2 {
  552. log.Error("job(%s):ai_center(%s) is wrong", job.DisplayJobName, job.AiCenter)
  553. continue
  554. }
  555. id, content = getAICenterID(centerInfo[0])
  556. if content == "" {
  557. log.Error("job(%s) has no match config info", job.DisplayJobName)
  558. continue
  559. }
  560. default:
  561. log.Error("no match info")
  562. continue
  563. }
  564. jobInfos = append(jobInfos, JobInfo{
  565. JobName: job.DisplayJobName,
  566. AiCenterId: id,
  567. })
  568. }
  569. ctx.JSON(http.StatusOK, jobInfos)
  570. }
  571. func GetAICenterInfo(ctx *context.APIContext) {
  572. if setting.C2NetInfos == nil {
  573. log.Error("C2NET_SEQUENCE is incorrect")
  574. return
  575. }
  576. ctx.JSON(http.StatusOK, setting.C2NetInfos.C2NetSqInfo)
  577. }
  578. func getAICenterID(name string) (int, string) {
  579. for _, info := range setting.C2NetInfos.C2NetSqInfo {
  580. if name == info.Name {
  581. return info.ID, info.Content
  582. }
  583. }
  584. return 0, ""
  585. }