From f6111c6315d9fe0bd568103c79f8968e2ace7832 Mon Sep 17 00:00:00 2001 From: lewis <747342561@qq.com> Date: Tue, 18 Jan 2022 19:51:33 +0800 Subject: [PATCH] get log --- models/cloudbrain.go | 37 +++++++++++++++++++++++++++++++ modules/cloudbrain/resty.go | 28 +++++++++++++++++++++++ routers/api/v1/api.go | 1 + routers/api/v1/repo/cloudbrain.go | 23 +++++++++++++++++++ routers/repo/cloudbrain.go | 8 +++---- routers/routes/routes.go | 1 - 6 files changed, 93 insertions(+), 5 deletions(-) diff --git a/models/cloudbrain.go b/models/cloudbrain.go index 11e77e49f..4dd7069e6 100755 --- a/models/cloudbrain.go +++ b/models/cloudbrain.go @@ -453,6 +453,43 @@ type CommitImageResult struct { Payload map[string]interface{} `json:"payload"` } +type GetJobLogParams struct { + QueryInfo QueryInfo `json:"query"` +} + +type QueryInfo struct { + Size string `json:"size"` + Sort string `json:"sort"` + MatchInfo MatchInfo `json:"match"` +} + +type MatchInfo struct { + PodName string `json:"kubernetes.pod.name"` +} + +type GetJobLogResult struct { + ScrollID string `json:"_scroll_id"` + Took string `json:"took"` + TimedOut bool `json:"timed_out"` + Shards struct { + Total int `json:"total"` + Successful int `json:"successful"` + Skipped int `json:"skipped"` + Failed int `json:"failed"` + } `json:"_shards"` + Hits struct { + Hits []struct { + Index string `json:"_index"` + Type string `json:"_type"` + ID string `json:"_id"` + Source struct { + Message string `json:"message"` + } `json:"_source"` + Sort []int `json:"sort"` + } `json:"hits"` + } `json:"hits"` +} + type CloudBrainResult struct { Code string `json:"code"` Msg string `json:"msg"` diff --git a/modules/cloudbrain/resty.go b/modules/cloudbrain/resty.go index 4e30ea0e4..a105b1f52 100755 --- a/modules/cloudbrain/resty.go +++ b/modules/cloudbrain/resty.go @@ -270,3 +270,31 @@ sendjob: return nil } + +func GetJobLog(jobID string) (*models.GetJobLogResult, error) { + checkSetting() + client := getRestyClient() + var result models.GetJobLogResult + req := models.GetJobLogParams{ + QueryInfo: models.QueryInfo{ + Size: "5000", + Sort: "log.offset", + MatchInfo: models.MatchInfo{ + PodName: jobID + "task1-0", + }, + }, + } + + res, err := client.R(). + SetHeader("Content-Type", "application/json"). + SetAuthToken(TOKEN). + SetBody(req). + SetResult(&result). + Post(HOST + "/es/_search?_source=message&scroll=5m") + + if err != nil { + return &result, fmt.Errorf("resty GetJobLog: %v, %s", err, res.String()) + } + + return &result, nil +} diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index dcea46ed6..2c9629bd9 100755 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -878,6 +878,7 @@ func RegisterRoutes(m *macaron.Macaron) { }, reqAnyRepoReader()) m.Group("/cloudbrain", func() { m.Get("/:jobid", repo.GetCloudbrainTask) + m.Get("/:jobid/log", repo.CloudbrainGetLog) }, reqRepoReader(models.UnitTypeCloudBrain)) m.Group("/modelarts", func() { m.Group("/notebook", func() { diff --git a/routers/api/v1/repo/cloudbrain.go b/routers/api/v1/repo/cloudbrain.go index bfba5236b..1b5ddf20f 100755 --- a/routers/api/v1/repo/cloudbrain.go +++ b/routers/api/v1/repo/cloudbrain.go @@ -91,3 +91,26 @@ func GetCloudbrainTask(ctx *context.APIContext) { }) } + +func CloudbrainGetLog(ctx *context.Context) { + jobID := ctx.Params(":jobid") + _, err := models.GetCloudbrainByJobID(jobID) + if err != nil { + log.Error("GetCloudbrainByJobID failed: %v", err, ctx.Data["msgID"]) + ctx.ServerError(err.Error(), err) + return + } + + result, err := cloudbrain.GetJobLog(jobID) + if err != nil{ + log.Error("GetJobLog failed: %v", err, ctx.Data["MsgID"]) + return + } + + ctx.JSON(http.StatusOK, map[string]interface{}{ + "JobID": jobID, + "Content": result.Hits, + }) + + return +} diff --git a/routers/repo/cloudbrain.go b/routers/repo/cloudbrain.go index e0b60a867..03ae51d86 100755 --- a/routers/repo/cloudbrain.go +++ b/routers/repo/cloudbrain.go @@ -1072,10 +1072,10 @@ func getBenchmarkAttachment(benchmarkTypeID, benchmarkChildTypeID int) (string, } var isExist bool - for id, benchmarkType := range benchmarkTypes.BenchmarkType { - if id == benchmarkTypeID { - for childID, childType := range benchmarkType.Second { - if childID == benchmarkChildTypeID { + for _, benchmarkType := range benchmarkTypes.BenchmarkType { + if benchmarkType.Id == benchmarkTypeID { + for _, childType := range benchmarkType.Second { + if childType.Id == benchmarkChildTypeID { uuid = childType.Attachment isExist = true break diff --git a/routers/routes/routes.go b/routers/routes/routes.go index c32ec204e..8f31539f0 100755 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -987,7 +987,6 @@ func RegisterRoutes(m *macaron.Macaron) { m.Post("/stop", cloudbrain.AdminOrOwnerOrJobCreaterRight, repo.CloudBrainStop) m.Post("/del", cloudbrain.AdminOrOwnerOrJobCreaterRight, repo.BenchmarkDel) m.Get("/rate", reqRepoCloudBrainReader, repo.GetRate) - //m.Get("/log", reqRepoCloudBrainReader, repo.BenchmarkGetLog) }) m.Get("/create", reqRepoCloudBrainWriter, repo.CloudBrainBenchmarkNew) m.Post("/create", reqRepoCloudBrainWriter, bindIgnErr(auth.CreateCloudBrainForm{}), repo.CloudBrainBenchmarkCreate)