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