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 34 kB

3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
2 years ago
2 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
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
3 years ago
2 years ago
4 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
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
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
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
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
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  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. "io/ioutil"
  11. "net/http"
  12. "os"
  13. "path"
  14. "sort"
  15. "strconv"
  16. "strings"
  17. "time"
  18. "code.gitea.io/gitea/modules/grampus"
  19. cloudbrainService "code.gitea.io/gitea/services/cloudbrain"
  20. "code.gitea.io/gitea/modules/convert"
  21. "code.gitea.io/gitea/services/cloudbrain/cloudbrainTask"
  22. api "code.gitea.io/gitea/modules/structs"
  23. "code.gitea.io/gitea/modules/notification"
  24. "code.gitea.io/gitea/modules/setting"
  25. "code.gitea.io/gitea/models"
  26. "code.gitea.io/gitea/modules/cloudbrain"
  27. "code.gitea.io/gitea/modules/context"
  28. "code.gitea.io/gitea/modules/log"
  29. "code.gitea.io/gitea/modules/modelarts"
  30. "code.gitea.io/gitea/modules/storage"
  31. routerRepo "code.gitea.io/gitea/routers/repo"
  32. )
  33. func CloudBrainShow(ctx *context.APIContext) {
  34. task, err := models.GetCloudbrainByJobID(ctx.Params(":jobid"))
  35. if err != nil {
  36. log.Info("error:" + err.Error())
  37. ctx.JSON(http.StatusOK, models.BaseErrorMessageApi("repo.cloudbrain_query_fail"))
  38. return
  39. }
  40. cloudbrainTask.PrepareSpec4Show(task)
  41. task.ContainerIp = ""
  42. if cloudbrainTask.IsTaskNotStop(task) {
  43. cloudbrainTask.SyncTaskStatus(task)
  44. }
  45. if task.TrainJobDuration == "" {
  46. if task.Duration == 0 {
  47. var duration int64
  48. if task.Status == string(models.JobWaiting) {
  49. duration = 0
  50. } else if task.Status == string(models.JobRunning) {
  51. duration = time.Now().Unix() - int64(task.CreatedUnix)
  52. } else {
  53. duration = int64(task.UpdatedUnix) - int64(task.CreatedUnix)
  54. }
  55. task.Duration = duration
  56. }
  57. task.TrainJobDuration = models.ConvertDurationToStr(task.Duration)
  58. }
  59. //to unify image output
  60. if task.Type == models.TypeCloudBrainTwo || task.Type == models.TypeCDCenter {
  61. task.ImageID = strconv.FormatInt(task.EngineID, 10)
  62. task.Image = task.EngineName
  63. } else if task.Type == models.TypeC2Net {
  64. task.Image = task.EngineName
  65. }
  66. task.AiCenter = cloudbrainService.GetAiCenterShow(task.AiCenter, ctx.Context)
  67. ctx.JSON(http.StatusOK, models.BaseMessageWithDataApi{Code: 0, Message: "", Data: convert.ToCloudBrain(task)})
  68. }
  69. func GeneralCloudBrainJobStop(ctx *context.APIContext) {
  70. task := ctx.Cloudbrain
  71. if task.IsTerminal() {
  72. ctx.JSON(http.StatusOK, models.BaseErrorMessageApi("cloudbrain.Already_stopped"))
  73. return
  74. }
  75. var err error
  76. if ctx.Cloudbrain.Type == models.TypeCloudBrainOne {
  77. err = cloudbrain.StopJob(task.JobID)
  78. } else if ctx.Cloudbrain.Type == models.TypeCloudBrainTwo {
  79. _, err = modelarts.StopTrainJob(task.JobID, strconv.FormatInt(task.VersionID, 10))
  80. } else {
  81. _, err = grampus.StopJob(task.JobID)
  82. }
  83. if err != nil {
  84. log.Warn("cloud brain stopped failed.", err)
  85. ctx.JSON(http.StatusOK, models.BaseErrorMessageApi("cloudbrain.Stopped_failed"))
  86. return
  87. }
  88. ctx.JSON(http.StatusOK, models.BaseOKMessageApi)
  89. }
  90. func CreateFileNoteBook(ctx *context.APIContext, option api.CreateFileNotebookJobOption) {
  91. cloudbrainTask.FileNotebookCreate(ctx.Context, option)
  92. }
  93. func FileNoteBookStatus(ctx *context.APIContext, option api.CreateFileNotebookJobOption) {
  94. cloudbrainTask.FileNotebookStatus(ctx.Context, option)
  95. }
  96. func GetFileNoteBookInfo(ctx *context.APIContext) {
  97. //image description spec description waiting count
  98. specs, err := models.GetResourceSpecificationByIds([]int64{setting.FileNoteBook.SpecIdCPU, setting.FileNoteBook.SpecIdGPU, setting.FileNoteBook.SpecIdNPU, setting.FileNoteBook.SpecIdNPUCD})
  99. if err != nil {
  100. log.Error("Fail to query specifications", err)
  101. ctx.JSON(http.StatusOK, models.BaseErrorMessageApi(ctx.Tr("repo.notebook_query_fail")))
  102. return
  103. }
  104. var specCPU, specGpu, specNPU, specNPUCD *api.SpecificationShow
  105. var specGpuQueueCode string
  106. for _, spec := range specs {
  107. if spec.ID == setting.FileNoteBook.SpecIdCPU {
  108. specCPU = convert.ToSpecification(spec)
  109. } else if spec.ID == setting.FileNoteBook.SpecIdGPU {
  110. specGpu = convert.ToSpecification(spec)
  111. specGpuQueueCode = spec.QueueCode
  112. } else if spec.ID == setting.FileNoteBook.SpecIdNPU {
  113. specNPU = convert.ToSpecification(spec)
  114. } else if spec.ID == setting.FileNoteBook.SpecIdNPUCD {
  115. specNPUCD = convert.ToSpecification(spec)
  116. }
  117. }
  118. waitCountNpu := cloudbrain.GetWaitingCloudbrainCount(models.TypeCloudBrainTwo, "")
  119. queuesMap, err := cloudbrain.GetQueuesDetail()
  120. if err != nil {
  121. log.Error("Fail to query gpu queues waiting count", err)
  122. ctx.JSON(http.StatusOK, models.BaseErrorMessageApi(ctx.Tr("repo.notebook_query_fail")))
  123. return
  124. }
  125. waitCountGPU := (*queuesMap)[specGpuQueueCode]
  126. if !setting.ModelartsCD.Enabled {
  127. ctx.JSON(http.StatusOK, map[string]interface{}{
  128. "code": 0,
  129. "projectName": setting.FileNoteBook.ProjectName,
  130. "specCpu": specCPU,
  131. "specGpu": specGpu,
  132. "specNpu": specNPU,
  133. "waitCountGpu": waitCountGPU,
  134. "waitCountNpu": waitCountNpu,
  135. "imageCpuDescription": setting.FileNoteBook.ImageCPUDescription,
  136. "imageGpuDescription": setting.FileNoteBook.ImageGPUDescription,
  137. "imageNpuDescription": setting.FileNoteBook.ImageNPUDescription,
  138. })
  139. } else {
  140. ctx.JSON(http.StatusOK, map[string]interface{}{
  141. "code": 0,
  142. "projectName": setting.FileNoteBook.ProjectName,
  143. "specCpu": specCPU,
  144. "specGpu": specGpu,
  145. "specNpu": specNPUCD,
  146. "waitCountGpu": waitCountGPU,
  147. "waitCountNpu": waitCountNpu,
  148. "imageCpuDescription": setting.FileNoteBook.ImageCPUDescription,
  149. "imageGpuDescription": setting.FileNoteBook.ImageGPUDescription,
  150. "imageNpuDescription": setting.FileNoteBook.ImageNPUCDDescription,
  151. })
  152. }
  153. }
  154. func CreateCloudBrain(ctx *context.APIContext, option api.CreateTrainJobOption) {
  155. if option.Type == cloudbrainTask.TaskTypeCloudbrainOne {
  156. cloudbrainTask.CloudbrainOneTrainJobCreate(ctx.Context, option)
  157. }
  158. if option.Type == cloudbrainTask.TaskTypeModelArts {
  159. cloudbrainTask.ModelArtsTrainJobNpuCreate(ctx.Context, option)
  160. }
  161. if option.Type == cloudbrainTask.TaskTypeGrampusGPU {
  162. cloudbrainTask.GrampusTrainJobGpuCreate(ctx.Context, option)
  163. }
  164. if option.Type == cloudbrainTask.TaskTypeGrampusNPU {
  165. cloudbrainTask.GrampusTrainJobNpuCreate(ctx.Context, option)
  166. }
  167. }
  168. func CreateCloudBrainInferenceTask(ctx *context.APIContext, option api.CreateTrainJobOption) {
  169. if option.Type == 0 {
  170. cloudbrainTask.CloudBrainInferenceJobCreate(ctx.Context, option)
  171. }
  172. if option.Type == 1 {
  173. cloudbrainTask.ModelArtsInferenceJobCreate(ctx.Context, option)
  174. }
  175. }
  176. // cloudbrain get job task by jobid
  177. func GetCloudbrainTask(ctx *context.APIContext) {
  178. // swagger:operation GET /repos/{owner}/{repo}/cloudbrain/{jobid} cloudbrain jobTask
  179. // ---
  180. // summary: Get a single task
  181. // produces:
  182. // - application/json
  183. // parameters:
  184. // - name: owner
  185. // in: path
  186. // description: owner of the repo
  187. // type: string
  188. // required: true
  189. // - name: repo
  190. // in: path
  191. // description: name of the repo
  192. // type: string
  193. // required: true
  194. // - name: jobid
  195. // in: path
  196. // description: id of cloudbrain jobid
  197. // type: string
  198. // required: true
  199. // responses:
  200. // "200":
  201. // "$ref": "#/responses/Label"
  202. var (
  203. err error
  204. )
  205. ID := ctx.Params(":id")
  206. job, err := cloudbrain.GetCloudBrainByIdOrJobId(ID, "id")
  207. if err != nil {
  208. ctx.NotFound(err)
  209. return
  210. }
  211. if job.JobType == string(models.JobTypeModelSafety) {
  212. routerRepo.GetAiSafetyTaskByJob(job)
  213. job, err = models.GetCloudbrainByID(ID)
  214. ctx.JSON(http.StatusOK, map[string]interface{}{
  215. "ID": ID,
  216. "JobName": job.JobName,
  217. "JobStatus": job.Status,
  218. "SubState": "",
  219. "CreatedTime": job.CreatedUnix.Format("2006-01-02 15:04:05"),
  220. "CompletedTime": job.UpdatedUnix.Format("2006-01-02 15:04:05"),
  221. "JobDuration": job.TrainJobDuration,
  222. })
  223. } else {
  224. jobAfter, err := cloudbrainTask.SyncCloudBrainOneStatus(job)
  225. if err != nil {
  226. ctx.NotFound(err)
  227. log.Error("Sync cloud brain one status failed:", err)
  228. return
  229. }
  230. ctx.JSON(http.StatusOK, map[string]interface{}{
  231. "ID": ID,
  232. "JobName": jobAfter.JobName,
  233. "JobStatus": jobAfter.Status,
  234. "SubState": "",
  235. "CreatedTime": jobAfter.CreatedUnix.Format("2006-01-02 15:04:05"),
  236. "CompletedTime": jobAfter.UpdatedUnix.Format("2006-01-02 15:04:05"),
  237. "JobDuration": jobAfter.TrainJobDuration,
  238. })
  239. }
  240. }
  241. func GetCloudBrainInferenceJob(ctx *context.APIContext) {
  242. jobID := ctx.Params(":jobid")
  243. job, err := models.GetCloudbrainByJobID(jobID)
  244. if err != nil {
  245. ctx.NotFound(err)
  246. return
  247. }
  248. jobResult, err := cloudbrain.GetJob(job.JobID)
  249. if err != nil {
  250. ctx.NotFound(err)
  251. log.Error("GetJob failed:", err)
  252. return
  253. }
  254. result, err := models.ConvertToJobResultPayload(jobResult.Payload)
  255. if err != nil {
  256. ctx.NotFound(err)
  257. log.Error("ConvertToJobResultPayload failed:", err)
  258. return
  259. }
  260. oldStatus := job.Status
  261. job.Status = result.JobStatus.State
  262. if result.JobStatus.State != string(models.JobWaiting) && result.JobStatus.State != string(models.JobFailed) {
  263. taskRoles := result.TaskRoles
  264. taskRes, _ := models.ConvertToTaskPod(taskRoles[cloudbrain.SubTaskName].(map[string]interface{}))
  265. job.ContainerIp = taskRes.TaskStatuses[0].ContainerIP
  266. job.ContainerID = taskRes.TaskStatuses[0].ContainerID
  267. job.Status = taskRes.TaskStatuses[0].State
  268. }
  269. if result.JobStatus.State != string(models.JobWaiting) {
  270. models.ParseAndSetDurationFromCloudBrainOne(result, job)
  271. if oldStatus != job.Status {
  272. notification.NotifyChangeCloudbrainStatus(job, oldStatus)
  273. }
  274. err = models.UpdateJob(job)
  275. if err != nil {
  276. log.Error("UpdateJob failed:", err)
  277. }
  278. }
  279. ctx.JSON(http.StatusOK, map[string]interface{}{
  280. "JobID": jobID,
  281. "JobStatus": job.Status,
  282. "JobDuration": job.TrainJobDuration,
  283. "StartTime": job.StartTime,
  284. })
  285. }
  286. func DelCloudBrainJob(ctx *context.APIContext) {
  287. jobID := ctx.Params(":jobid")
  288. errStr := cloudbrain.DelCloudBrainJob(jobID)
  289. if errStr != "" {
  290. ctx.JSON(http.StatusOK, map[string]interface{}{
  291. "Message": ctx.Tr(errStr),
  292. "VersionName": "1",
  293. "Code": 1,
  294. })
  295. } else {
  296. ctx.JSON(http.StatusOK, map[string]interface{}{
  297. "Message": "",
  298. "VersionName": "1",
  299. "Code": 0,
  300. })
  301. }
  302. }
  303. func InferencJobResultList(ctx *context.APIContext) {
  304. jobID := ctx.Params(":jobid")
  305. parentDir := ctx.Query("parentDir")
  306. dirArray := strings.Split(parentDir, "/")
  307. task, err := models.GetCloudbrainByJobID(jobID)
  308. if err != nil {
  309. log.Error("get cloud brain err:", err)
  310. ctx.ServerError("get cloud brain information failed:", err)
  311. }
  312. //get dirs
  313. dirs, err := routerRepo.GetResultDirs(task.JobName, parentDir)
  314. if err != nil {
  315. log.Error("GetModelDirs failed:%v", err.Error(), ctx.Data["msgID"])
  316. ctx.ServerError("GetModelDirs failed:", err)
  317. return
  318. }
  319. var fileInfos []storage.FileInfo
  320. err = json.Unmarshal([]byte(dirs), &fileInfos)
  321. if err != nil {
  322. log.Error("json.Unmarshal failed:%v", err.Error(), ctx.Data["msgID"])
  323. ctx.ServerError("json.Unmarshal failed:", err)
  324. return
  325. }
  326. for i, fileInfo := range fileInfos {
  327. temp, _ := time.Parse("2006-01-02 15:04:05", fileInfo.ModTime)
  328. fileInfos[i].ModTime = temp.Local().Format("2006-01-02 15:04:05")
  329. }
  330. sort.Slice(fileInfos, func(i, j int) bool {
  331. return fileInfos[i].ModTime > fileInfos[j].ModTime
  332. })
  333. ctx.JSON(http.StatusOK, map[string]interface{}{
  334. "JobID": jobID,
  335. "StatusOK": 0,
  336. "Path": dirArray,
  337. "Dirs": fileInfos,
  338. "task": task,
  339. "PageIsCloudBrain": true,
  340. })
  341. }
  342. func GetCloudbrainModelConvertTask(ctx *context.APIContext) {
  343. var (
  344. err error
  345. )
  346. ID := ctx.Params(":id")
  347. job, err := models.QueryModelConvertById(ID)
  348. if err != nil {
  349. ctx.NotFound(err)
  350. log.Error("GetCloudbrainByID failed:", err)
  351. return
  352. }
  353. if job.IsGpuTrainTask() {
  354. jobResult, err := cloudbrain.GetJob(job.CloudBrainTaskId)
  355. if err != nil {
  356. ctx.NotFound(err)
  357. log.Error("GetJob failed:", err)
  358. return
  359. }
  360. result, _ := models.ConvertToJobResultPayload(jobResult.Payload)
  361. if err != nil {
  362. ctx.NotFound(err)
  363. log.Error("ConvertToJobResultPayload failed:", err)
  364. return
  365. }
  366. job.Status = result.JobStatus.State
  367. taskRoles := result.TaskRoles
  368. taskRes, _ := models.ConvertToTaskPod(taskRoles[cloudbrain.SubTaskName].(map[string]interface{}))
  369. if result.JobStatus.State != string(models.JobWaiting) && result.JobStatus.State != string(models.JobFailed) {
  370. job.ContainerIp = taskRes.TaskStatuses[0].ContainerIP
  371. job.ContainerID = taskRes.TaskStatuses[0].ContainerID
  372. job.Status = taskRes.TaskStatuses[0].State
  373. }
  374. if result.JobStatus.State != string(models.JobWaiting) {
  375. models.ModelComputeAndSetDuration(job, result)
  376. err = models.UpdateModelConvert(job)
  377. if err != nil {
  378. log.Error("UpdateJob failed:", err)
  379. }
  380. }
  381. ctx.JSON(http.StatusOK, map[string]interface{}{
  382. "ID": ID,
  383. "JobName": result.Config.JobName,
  384. "JobStatus": result.JobStatus.State,
  385. "SubState": result.JobStatus.SubState,
  386. "CreatedTime": time.Unix(result.JobStatus.CreatedTime/1000, 0).Format("2006-01-02 15:04:05"),
  387. "CompletedTime": time.Unix(result.JobStatus.CompletedTime/1000, 0).Format("2006-01-02 15:04:05"),
  388. })
  389. } else {
  390. result, err := modelarts.GetTrainJob(job.CloudBrainTaskId, job.ModelArtsVersionId)
  391. if err != nil {
  392. log.Error("get modelart job failed:", err)
  393. ctx.NotFound(err)
  394. return
  395. }
  396. job.Status = modelarts.TransTrainJobStatus(result.IntStatus)
  397. job.RunTime = result.Duration / 1000
  398. job.TrainJobDuration = models.ConvertDurationToStr(job.RunTime)
  399. err = models.UpdateModelConvert(job)
  400. if err != nil {
  401. log.Error("UpdateJob failed:", err)
  402. }
  403. ctx.JSON(http.StatusOK, map[string]interface{}{
  404. "ID": ID,
  405. "JobStatus": job.Status,
  406. })
  407. }
  408. }
  409. func CloudbrainGetLogByJobId(jobId string, jobName string) map[string]interface{} {
  410. var hits []models.Hits
  411. result, err := cloudbrain.GetJobLog(jobId)
  412. if err != nil {
  413. log.Error("GetJobLog failed: %v", err)
  414. return nil
  415. }
  416. hits = result.Hits.Hits
  417. //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)
  418. if len(result.Hits.Hits) >= cloudbrain.LogPageSize {
  419. for {
  420. resultNext, err := cloudbrain.GetJobAllLog(result.ScrollID)
  421. if err != nil {
  422. log.Error("GetJobAllLog failed: %v", err)
  423. } else {
  424. for _, hit := range resultNext.Hits.Hits {
  425. hits = append(hits, hit)
  426. }
  427. }
  428. if len(resultNext.Hits.Hits) < cloudbrain.LogPageSize {
  429. log.Info("get all log already")
  430. break
  431. }
  432. }
  433. }
  434. cloudbrain.DeleteJobLogToken(result.ScrollID)
  435. sort.Slice(hits, func(i, j int) bool {
  436. return hits[i].Sort[0] < hits[j].Sort[0]
  437. })
  438. var content string
  439. for _, log := range hits {
  440. content += log.Source.Message + "\n"
  441. }
  442. return map[string]interface{}{
  443. "JobName": jobName,
  444. "Content": content,
  445. }
  446. }
  447. func CloudbrainForModelConvertGetLog(ctx *context.Context) {
  448. ID := ctx.Params(":id")
  449. job, err := models.QueryModelConvertById(ID)
  450. if err != nil {
  451. log.Error("GetCloudbrainByJobName failed: %v", err, ctx.Data["MsgID"])
  452. ctx.ServerError(err.Error(), err)
  453. return
  454. }
  455. result := CloudbrainGetLogByJobId(job.CloudBrainTaskId, job.Name)
  456. if result == nil {
  457. log.Error("GetJobLog failed: %v", err, ctx.Data["MsgID"])
  458. ctx.ServerError(err.Error(), err)
  459. return
  460. }
  461. ctx.JSON(http.StatusOK, result)
  462. }
  463. func ModelSafetyGetLog(ctx *context.APIContext) {
  464. ID := ctx.Params(":id")
  465. job, err := models.GetCloudbrainByID(ID)
  466. if err != nil {
  467. log.Error("GetCloudbrainByJobName failed: %v", err, ctx.Data["MsgID"])
  468. ctx.ServerError(err.Error(), err)
  469. return
  470. }
  471. if job.JobType == string(models.JobTypeModelSafety) {
  472. if job.Type == models.TypeCloudBrainTwo {
  473. //TrainJobForModelConvertGetLog(ctx)
  474. var baseLine = ctx.Query("base_line")
  475. var order = ctx.Query("order")
  476. var lines = ctx.Query("lines")
  477. lines_int, err := strconv.Atoi(lines)
  478. if err != nil {
  479. log.Error("change lines(%d) string to int failed", lines_int)
  480. }
  481. if order != modelarts.OrderDesc && order != modelarts.OrderAsc {
  482. log.Error("order(%s) check failed", order)
  483. ctx.JSON(http.StatusBadRequest, map[string]interface{}{
  484. "err_msg": "order check failed",
  485. })
  486. return
  487. }
  488. resultLogFile, err := modelarts.GetTrainJobLogFileNames(job.JobID, strconv.FormatInt(job.VersionID, 10))
  489. if err != nil {
  490. log.Error("GetTrainJobLogFileNames(%s) failed:%v", job.JobID, err.Error())
  491. }
  492. result, err := modelarts.GetTrainJobLog(job.JobID, strconv.FormatInt(job.VersionID, 10), baseLine, resultLogFile.LogFileList[0], order, lines_int)
  493. if err != nil {
  494. log.Error("GetTrainJobLog(%s) failed:%v", job.JobID, err.Error())
  495. }
  496. if err != nil {
  497. log.Error("trainJobGetLog(%s) failed:%v", job.JobID, err.Error())
  498. // ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobShow, nil)
  499. ctx.JSON(http.StatusOK, map[string]interface{}{
  500. "JobID": job.JobID,
  501. "LogFileName": "",
  502. "StartLine": "0",
  503. "EndLine": "0",
  504. "Content": "",
  505. "Lines": 0,
  506. "CanLogDownload": false,
  507. })
  508. return
  509. }
  510. prefix := strings.TrimPrefix(path.Join(setting.TrainJobModelPath, job.JobName, modelarts.LogPath, job.VersionName), "/") + "/job"
  511. _, err = storage.GetObsLogFileName(prefix)
  512. canLogDownload := isCanDownloadLog(ctx, job)
  513. if err != nil {
  514. canLogDownload = false
  515. }
  516. ctx.Data["log_file_name"] = resultLogFile.LogFileList[0]
  517. ctx.JSON(http.StatusOK, map[string]interface{}{
  518. "JobID": job.JobID,
  519. "LogFileName": resultLogFile.LogFileList[0],
  520. "StartLine": result.StartLine,
  521. "EndLine": result.EndLine,
  522. "Content": result.Content,
  523. "Lines": result.Lines,
  524. "CanLogDownload": canLogDownload,
  525. "StartTime": job.StartTime,
  526. })
  527. }
  528. }
  529. //result := ""
  530. //ctx.JSON(http.StatusOK, result)
  531. }
  532. func isCanDownloadLog(ctx *context.APIContext, job *models.Cloudbrain) bool {
  533. if !ctx.IsSigned {
  534. return false
  535. }
  536. return ctx.IsUserSiteAdmin() || ctx.User.ID == job.UserID
  537. }
  538. func ModelSafetyDownloadLogFile(ctx *context.Context) {
  539. ID := ctx.Params(":id")
  540. job, err := models.GetCloudbrainByID(ID)
  541. if err != nil {
  542. log.Error("GetCloudbrainByJobName failed: %v", err, ctx.Data["MsgID"])
  543. ctx.ServerError(err.Error(), err)
  544. return
  545. }
  546. if job.JobType == string(models.JobTypeModelSafety) {
  547. if job.Type == models.TypeCloudBrainOne {
  548. CloudbrainDownloadLogFile(ctx)
  549. } else if job.Type == models.TypeCloudBrainTwo {
  550. ctx.SetParams("jobid", job.JobID)
  551. ctx.Req.Form.Set("version_name", job.VersionName)
  552. routerRepo.TrainJobDownloadLogFile(ctx)
  553. }
  554. }
  555. }
  556. func CloudbrainDownloadLogFile(ctx *context.Context) {
  557. ID := ctx.Params(":id")
  558. job, err := models.GetCloudbrainByID(ID)
  559. if err != nil {
  560. log.Error("GetCloudbrainByJobName failed: %v", err, ctx.Data["MsgID"])
  561. ctx.ServerError(err.Error(), err)
  562. return
  563. }
  564. if job.JobType == string(models.JobTypeModelSafety) {
  565. if job.Type == models.TypeCloudBrainTwo {
  566. ModelSafetyDownloadLogFile(ctx)
  567. return
  568. }
  569. }
  570. existStr := ""
  571. if job.JobType == string(models.JobTypeTrain) || job.JobType == string(models.JobTypeInference) {
  572. if job.Type == models.TypeCloudBrainOne {
  573. result, err := cloudbrain.GetJob(job.JobID)
  574. if err == nil && result != nil {
  575. jobRes, _ := models.ConvertToJobResultPayload(result.Payload)
  576. taskRoles := jobRes.TaskRoles
  577. taskRes, _ := models.ConvertToTaskPod(taskRoles[cloudbrain.SubTaskName].(map[string]interface{}))
  578. existStr = taskRes.TaskStatuses[0].ExitDiagnostics
  579. }
  580. }
  581. }
  582. logDir := "/model"
  583. if job.JobType == string(models.JobTypeInference) || job.JobType == string(models.JobTypeModelSafety) {
  584. logDir = cloudbrain.ResultPath
  585. }
  586. files, err := storage.GetOneLevelAllObjectUnderDirMinio(setting.Attachment.Minio.Bucket, setting.CBCodePathPrefix+job.JobName+logDir, "")
  587. if err != nil {
  588. log.Error("query cloudbrain model failed: %v", err)
  589. return
  590. }
  591. fileName := ""
  592. for _, file := range files {
  593. if strings.HasSuffix(file.FileName, "log.txt") {
  594. fileName = file.FileName
  595. break
  596. }
  597. }
  598. if fileName != "" {
  599. prefix := "/" + setting.CBCodePathPrefix + job.JobName + "/model"
  600. filePath := setting.Attachment.Minio.RealPath + setting.Attachment.Minio.Bucket + prefix + "/" + fileName
  601. // Read the file contents into a byte slice
  602. data, err := ioutil.ReadFile(filePath)
  603. if err != nil {
  604. ctx.ServerError("ReadFile", err)
  605. return
  606. }
  607. // Set the appropriate response headers
  608. ctx.Resp.Header().Set("Content-Type", "application/octet-stream")
  609. ctx.Resp.Header().Set("Content-Disposition", "attachment; filename="+fileName)
  610. // Write the file contents to the response
  611. if _, err := ctx.Resp.Write(data); err != nil {
  612. ctx.ServerError("Write", err)
  613. return
  614. }
  615. if _, err := ctx.Resp.Write([]byte(existStr)); err != nil {
  616. log.Error("Write failed: %v", err.Error(), ctx.Data["msgID"])
  617. return
  618. }
  619. } else {
  620. log.Info("fileName is null.")
  621. }
  622. }
  623. func CloudbrainGetLog(ctx *context.APIContext) {
  624. ID := ctx.Params(":id")
  625. job, err := models.GetCloudbrainByID(ID)
  626. if err != nil {
  627. log.Error("GetCloudbrainByJobName failed: %v", err, ctx.Data["MsgID"])
  628. ctx.ServerError(err.Error(), err)
  629. return
  630. }
  631. if job.JobType == string(models.JobTypeModelSafety) {
  632. if job.Type == models.TypeCloudBrainOne {
  633. result, err := cloudbrain.GetJob(job.JobID)
  634. existStr := ""
  635. if err == nil && result != nil {
  636. jobRes, _ := models.ConvertToJobResultPayload(result.Payload)
  637. taskRoles := jobRes.TaskRoles
  638. taskRes, _ := models.ConvertToTaskPod(taskRoles[cloudbrain.SubTaskName].(map[string]interface{}))
  639. existStr = taskRes.TaskStatuses[0].ExitDiagnostics
  640. }
  641. ctx.Data["existStr"] = existStr
  642. } else {
  643. ModelSafetyGetLog(ctx)
  644. return
  645. }
  646. }
  647. if job.JobType == string(models.JobTypeTrain) || job.JobType == string(models.JobTypeInference) {
  648. if job.Type == models.TypeCloudBrainOne {
  649. result, err := cloudbrain.GetJob(job.JobID)
  650. existStr := ""
  651. if err == nil && result != nil {
  652. jobRes, _ := models.ConvertToJobResultPayload(result.Payload)
  653. taskRoles := jobRes.TaskRoles
  654. taskRes, _ := models.ConvertToTaskPod(taskRoles[cloudbrain.SubTaskName].(map[string]interface{}))
  655. existStr = taskRes.TaskStatuses[0].ExitDiagnostics
  656. }
  657. ctx.Data["existStr"] = existStr
  658. }
  659. }
  660. lines := ctx.QueryInt("lines")
  661. baseLine := ctx.Query("base_line")
  662. order := ctx.Query("order")
  663. var result map[string]interface{}
  664. resultPath := "/model"
  665. if job.JobType == string(models.JobTypeInference) || job.JobType == string(models.JobTypeModelSafety) {
  666. resultPath = "/result"
  667. }
  668. if baseLine == "" && order == "desc" {
  669. result = getLastLogFromModelDir(job.JobName, lines, resultPath)
  670. } else {
  671. startLine := ctx.QueryInt("base_line")
  672. endLine := startLine + lines
  673. if order == "asc" {
  674. if baseLine == "" {
  675. startLine = 0
  676. endLine = lines
  677. } else {
  678. endLine = startLine
  679. startLine = endLine - lines
  680. if startLine < 0 {
  681. startLine = 0
  682. }
  683. }
  684. } else {
  685. if startLine > 0 {
  686. startLine += 1
  687. endLine += 1
  688. }
  689. }
  690. result = getLogFromModelDir(job.JobName, startLine, endLine, resultPath)
  691. if result == nil {
  692. log.Error("GetJobLog failed: %v", err, ctx.Data["MsgID"])
  693. //ctx.ServerError(err.Error(), err)
  694. return
  695. }
  696. }
  697. content := ""
  698. if result["Content"] != nil {
  699. content = result["Content"].(string)
  700. }
  701. if (job.JobType == string(models.JobTypeTrain) || job.JobType == string(models.JobTypeInference)) && job.Type == models.TypeCloudBrainOne && job.Status == string(models.JobFailed) {
  702. if ctx.Data["existStr"] != nil {
  703. if baseLine == "" && order == "desc" && result["Lines"].(int) == 0 {
  704. result["Lines"] = 1
  705. result["EndLine"] = 1
  706. content = content + ctx.Data["existStr"].(string)
  707. }
  708. if result["Lines"].(int) == 0 && result["StartLine"] == result["EndLine"] && result["StartLine"].(int) != 0 {
  709. content = content + ctx.Data["existStr"].(string)
  710. result["Lines"] = 1
  711. result["StartLine"] = result["StartLine"].(int) - 1
  712. }
  713. if result["Lines"].(int) == 1 && result["StartLine"] == result["EndLine"] {
  714. result["Lines"] = 0
  715. result["StartLine"] = result["StartLine"].(int) + 1
  716. }
  717. }
  718. } else {
  719. if ctx.Data["existStr"] != nil && result["Lines"].(int) < 50 {
  720. content = content + ctx.Data["existStr"].(string)
  721. }
  722. }
  723. logFileName := result["FileName"]
  724. //Logs can only be downloaded if the file exists
  725. //and the current user is an administrator or the creator of the task
  726. canLogDownload := logFileName != nil && logFileName != "" && job.IsUserHasRight(ctx.User)
  727. re := map[string]interface{}{
  728. "JobID": ID,
  729. "LogFileName": logFileName,
  730. "StartLine": result["StartLine"],
  731. "EndLine": result["EndLine"],
  732. "Content": content,
  733. "Lines": result["Lines"],
  734. "CanLogDownload": canLogDownload,
  735. "StartTime": job.StartTime,
  736. }
  737. //result := CloudbrainGetLogByJobId(job.JobID, job.JobName)
  738. ctx.JSON(http.StatusOK, re)
  739. }
  740. func getAllLineFromFile(path string) int {
  741. count := 0
  742. reader, err := os.Open(path)
  743. defer reader.Close()
  744. if err == nil {
  745. r := bufio.NewReader(reader)
  746. for {
  747. _, error := r.ReadString('\n')
  748. if error == io.EOF {
  749. log.Info("read file completed.")
  750. break
  751. }
  752. if error != nil {
  753. log.Info("read file error." + error.Error())
  754. break
  755. }
  756. count = count + 1
  757. }
  758. } else {
  759. log.Info("error:" + err.Error())
  760. }
  761. return count
  762. }
  763. func getLastLogFromModelDir(jobName string, lines int, resultPath string) map[string]interface{} {
  764. prefix := setting.CBCodePathPrefix + jobName + resultPath
  765. files, err := storage.GetOneLevelAllObjectUnderDirMinio(setting.Attachment.Minio.Bucket, prefix, "")
  766. if err != nil {
  767. log.Error("query cloudbrain model failed: %v", err)
  768. return nil
  769. }
  770. re := ""
  771. fileName := ""
  772. count := 0
  773. allLines := 0
  774. startLine := 0
  775. for _, file := range files {
  776. if strings.HasSuffix(file.FileName, "log.txt") {
  777. fileName = file.FileName
  778. path := storage.GetMinioPath(jobName+resultPath+"/", file.FileName)
  779. allLines = getAllLineFromFile(path)
  780. startLine = allLines - lines
  781. if startLine < 0 {
  782. startLine = 0
  783. }
  784. count = allLines - startLine
  785. log.Info("path=" + path)
  786. reader, err := os.Open(path)
  787. defer reader.Close()
  788. if err == nil {
  789. r := bufio.NewReader(reader)
  790. for i := 0; i < allLines; i++ {
  791. line, error := r.ReadString('\n')
  792. if error == io.EOF {
  793. log.Info("read file completed.")
  794. break
  795. }
  796. if error != nil {
  797. log.Info("read file error." + error.Error())
  798. break
  799. }
  800. if error == nil {
  801. if i >= startLine {
  802. re = re + line
  803. }
  804. }
  805. }
  806. } else {
  807. log.Info("error:" + err.Error())
  808. }
  809. break
  810. }
  811. }
  812. return map[string]interface{}{
  813. "JobName": jobName,
  814. "Content": re,
  815. "FileName": fileName,
  816. "Lines": count,
  817. "EndLine": allLines,
  818. "StartLine": startLine,
  819. }
  820. }
  821. func getLogFromModelDir(jobName string, startLine int, endLine int, resultPath string) map[string]interface{} {
  822. prefix := setting.CBCodePathPrefix + jobName + resultPath
  823. files, err := storage.GetOneLevelAllObjectUnderDirMinio(setting.Attachment.Minio.Bucket, prefix, "")
  824. if err != nil {
  825. log.Error("query cloudbrain model failed: %v", err)
  826. return nil
  827. }
  828. if startLine == endLine {
  829. return map[string]interface{}{
  830. "JobName": jobName,
  831. "Content": "",
  832. "FileName": "",
  833. "Lines": 0,
  834. "EndLine": startLine,
  835. "StartLine": startLine,
  836. }
  837. }
  838. re := ""
  839. fileName := ""
  840. count := 0
  841. fileEndLine := endLine
  842. for _, file := range files {
  843. if strings.HasSuffix(file.FileName, "log.txt") {
  844. fileName = file.FileName
  845. path := storage.GetMinioPath(jobName+resultPath+"/", file.FileName)
  846. log.Info("path=" + path)
  847. reader, err := os.Open(path)
  848. defer reader.Close()
  849. if err == nil {
  850. r := bufio.NewReader(reader)
  851. for i := 0; i < endLine; i++ {
  852. line, error := r.ReadString('\n')
  853. if error == io.EOF {
  854. if i >= startLine {
  855. re = re + line
  856. count++
  857. }
  858. fileEndLine = i + 1
  859. log.Info("read file completed.")
  860. break
  861. }
  862. if error != nil {
  863. log.Info("read file error." + error.Error())
  864. break
  865. }
  866. if error == nil {
  867. if i >= startLine {
  868. fileEndLine = i + 1
  869. re = re + line
  870. count++
  871. }
  872. }
  873. }
  874. } else {
  875. log.Info("error:" + err.Error())
  876. }
  877. break
  878. }
  879. }
  880. return map[string]interface{}{
  881. "JobName": jobName,
  882. "Content": re,
  883. "FileName": fileName,
  884. "Lines": count,
  885. "EndLine": fileEndLine,
  886. "StartLine": startLine,
  887. }
  888. }
  889. func CloudBrainModelConvertList(ctx *context.APIContext) {
  890. var (
  891. err error
  892. )
  893. ID := ctx.Params(":id")
  894. parentDir := ctx.Query("parentDir")
  895. dirArray := strings.Split(parentDir, "/")
  896. job, err := models.QueryModelConvertById(ID)
  897. if err != nil {
  898. log.Error("GetCloudbrainByJobID(%s) failed:%v", job.Name, err.Error())
  899. return
  900. }
  901. if job.IsGpuTrainTask() {
  902. //get dirs
  903. dirs, err := routerRepo.GetModelDirs(job.ID, parentDir)
  904. if err != nil {
  905. log.Error("GetModelDirs failed:%v", err.Error(), ctx.Data["msgID"])
  906. ctx.ServerError("GetModelDirs failed:", err)
  907. return
  908. }
  909. var fileInfos []storage.FileInfo
  910. err = json.Unmarshal([]byte(dirs), &fileInfos)
  911. if err != nil {
  912. log.Error("json.Unmarshal failed:%v", err.Error(), ctx.Data["msgID"])
  913. //ctx.ServerError("json.Unmarshal failed:", err)
  914. return
  915. }
  916. for i, fileInfo := range fileInfos {
  917. temp, _ := time.Parse("2006-01-02 15:04:05", fileInfo.ModTime)
  918. fileInfos[i].ModTime = temp.Local().Format("2006-01-02 15:04:05")
  919. }
  920. sort.Slice(fileInfos, func(i, j int) bool {
  921. return fileInfos[i].ModTime > fileInfos[j].ModTime
  922. })
  923. ctx.JSON(http.StatusOK, map[string]interface{}{
  924. "JobID": ID,
  925. "VersionName": "",
  926. "StatusOK": 0,
  927. "Path": dirArray,
  928. "Dirs": fileInfos,
  929. "task": job,
  930. "PageIsCloudBrain": true,
  931. })
  932. } else {
  933. var jobID = ctx.Params(":id")
  934. var versionName = "V0001"
  935. parentDir := ctx.Query("parentDir")
  936. dirArray := strings.Split(parentDir, "/")
  937. models, err := storage.GetObsListObject(job.ID, "output/", parentDir, versionName)
  938. if err != nil {
  939. log.Info("get TrainJobListModel failed:", err)
  940. //ctx.ServerError("GetObsListObject:", err)
  941. return
  942. }
  943. ctx.JSON(http.StatusOK, map[string]interface{}{
  944. "JobID": jobID,
  945. "VersionName": versionName,
  946. "StatusOK": 0,
  947. "Path": dirArray,
  948. "Dirs": models,
  949. "task": job,
  950. "PageIsCloudBrain": true,
  951. })
  952. }
  953. }
  954. func CloudBrainModelList(ctx *context.APIContext) {
  955. var (
  956. err error
  957. )
  958. var jobID = ctx.Params(":jobid")
  959. var versionName = ctx.Query("version_name")
  960. parentDir := ctx.Query("parentDir")
  961. dirArray := strings.Split(parentDir, "/")
  962. task, err := models.GetCloudbrainByJobIDAndVersionName(jobID, versionName)
  963. if err != nil {
  964. log.Error("GetCloudbrainByJobID(%s) failed:%v", task.JobName, err.Error())
  965. return
  966. }
  967. //get dirs
  968. dirs, err := routerRepo.GetModelDirs(task.JobName, parentDir)
  969. if err != nil {
  970. log.Error("GetModelDirs failed:%v", err.Error(), ctx.Data["msgID"])
  971. ctx.ServerError("GetModelDirs failed:", err)
  972. return
  973. }
  974. var fileInfos []storage.FileInfo
  975. err = json.Unmarshal([]byte(dirs), &fileInfos)
  976. if err != nil {
  977. log.Error("json.Unmarshal failed:%v", err.Error(), ctx.Data["msgID"])
  978. //ctx.ServerError("json.Unmarshal failed:", err)
  979. return
  980. }
  981. for i, fileInfo := range fileInfos {
  982. temp, _ := time.Parse("2006-01-02 15:04:05", fileInfo.ModTime)
  983. fileInfos[i].ModTime = temp.Local().Format("2006-01-02 15:04:05")
  984. }
  985. sort.Slice(fileInfos, func(i, j int) bool {
  986. return fileInfos[i].ModTime > fileInfos[j].ModTime
  987. })
  988. ctx.JSON(http.StatusOK, map[string]interface{}{
  989. "JobID": jobID,
  990. "VersionName": versionName,
  991. "StatusOK": 0,
  992. "Path": dirArray,
  993. "Dirs": fileInfos,
  994. "task": task,
  995. "PageIsCloudBrain": true,
  996. })
  997. }
  998. type JobInfo struct {
  999. JobName string `json:"job_name"`
  1000. AiCenterId int `json:"ai_center_id"`
  1001. }
  1002. func GetNewestJobs(ctx *context.APIContext) {
  1003. idsC2Net, err := models.GetNewestJobsByAiCenter()
  1004. if err != nil {
  1005. log.Error("GetNewestJobsByAiCenter(%s) failed:%v", err.Error())
  1006. return
  1007. }
  1008. idsCloudbrain, err := models.GetNewestJobsByType()
  1009. if err != nil {
  1010. log.Error("GetNewestJobsByType(%s) failed:%v", err.Error())
  1011. return
  1012. }
  1013. ids := make([]int64, len(idsC2Net), cap(idsC2Net)*2)
  1014. copy(ids, idsC2Net)
  1015. for _, id := range idsCloudbrain {
  1016. ids = append(ids, id)
  1017. }
  1018. jobs, err := models.GetCloudbrainByIDs(ids)
  1019. if err != nil {
  1020. log.Error("GetCloudbrainByIDs(%s) failed:%v", err.Error())
  1021. return
  1022. }
  1023. jobInfos := make([]JobInfo, 0)
  1024. for _, job := range jobs {
  1025. var id int
  1026. var content string
  1027. switch job.Type {
  1028. case models.TypeCloudBrainOne:
  1029. id, content = getAICenterID("cloudbrain_one")
  1030. if content == "" {
  1031. log.Error("job(%s) has no match config info", job.DisplayJobName)
  1032. continue
  1033. }
  1034. case models.TypeCloudBrainTwo:
  1035. id, content = getAICenterID("cloudbrain_two")
  1036. if content == "" {
  1037. log.Error("job(%s) has no match config info", job.DisplayJobName)
  1038. continue
  1039. }
  1040. case models.TypeC2Net:
  1041. centerInfo := strings.Split(job.AiCenter, "+")
  1042. if len(centerInfo) != 2 {
  1043. log.Error("job(%s):ai_center(%s) is wrong", job.DisplayJobName, job.AiCenter)
  1044. continue
  1045. }
  1046. id, content = getAICenterID(centerInfo[0])
  1047. if content == "" {
  1048. log.Error("job(%s) has no match config info", job.DisplayJobName)
  1049. continue
  1050. }
  1051. default:
  1052. log.Error("no match info")
  1053. continue
  1054. }
  1055. jobInfos = append(jobInfos, JobInfo{
  1056. JobName: job.DisplayJobName,
  1057. AiCenterId: id,
  1058. })
  1059. }
  1060. ctx.JSON(http.StatusOK, jobInfos)
  1061. }
  1062. func GetAICenterInfo(ctx *context.APIContext) {
  1063. if setting.C2NetInfos == nil {
  1064. log.Error("C2NET_SEQUENCE is incorrect")
  1065. return
  1066. }
  1067. ctx.JSON(http.StatusOK, setting.C2NetInfos.C2NetSqInfo)
  1068. }
  1069. func getAICenterID(name string) (int, string) {
  1070. for _, info := range setting.C2NetInfos.C2NetSqInfo {
  1071. if name == info.Name {
  1072. return info.ID, info.Content
  1073. }
  1074. }
  1075. return 0, ""
  1076. }