| @@ -8,6 +8,7 @@ package repo | |||||
| import ( | import ( | ||||
| "bufio" | "bufio" | ||||
| "encoding/json" | "encoding/json" | ||||
| "fmt" | |||||
| "io" | "io" | ||||
| "net/http" | "net/http" | ||||
| "os" | "os" | ||||
| @@ -413,41 +414,37 @@ func CloudbrainGetLog(ctx *context.Context) { | |||||
| } | } | ||||
| lines := ctx.QueryInt("lines") | lines := ctx.QueryInt("lines") | ||||
| baseLine := ctx.Query("base_line") | baseLine := ctx.Query("base_line") | ||||
| var result map[string]interface{} | |||||
| if baseLine == "" { | if baseLine == "" { | ||||
| re := getLastLogFromModelDir(job.JobName, lines) | |||||
| ctx.JSON(http.StatusOK, re) | |||||
| return | |||||
| } | |||||
| startLine := ctx.QueryInt("base_line") | |||||
| endLine := startLine + lines | |||||
| order := ctx.Query("order") | |||||
| if order == "asc" { | |||||
| endLine = startLine | |||||
| startLine = endLine - lines | |||||
| if startLine < 0 { | |||||
| startLine = 0 | |||||
| result = getLastLogFromModelDir(job.JobName, lines) | |||||
| } else { | |||||
| startLine := ctx.QueryInt("base_line") | |||||
| endLine := startLine + lines | |||||
| order := ctx.Query("order") | |||||
| if order == "asc" { | |||||
| endLine = startLine | |||||
| startLine = endLine - lines | |||||
| if startLine < 0 { | |||||
| startLine = 0 | |||||
| } | |||||
| } | |||||
| result = getLogFromModelDir(job.JobName, startLine, endLine) | |||||
| if result == nil { | |||||
| log.Error("GetJobLog failed: %v", err, ctx.Data["MsgID"]) | |||||
| ctx.ServerError(err.Error(), err) | |||||
| return | |||||
| } | } | ||||
| } | } | ||||
| result := getLogFromModelDir(job.JobName, startLine, endLine) | |||||
| if result == nil { | |||||
| log.Error("GetJobLog failed: %v", err, ctx.Data["MsgID"]) | |||||
| ctx.ServerError(err.Error(), err) | |||||
| return | |||||
| } | |||||
| re := map[string]interface{}{ | re := map[string]interface{}{ | ||||
| "JobID": ID, | "JobID": ID, | ||||
| "LogFileName": result["FileName"], | "LogFileName": result["FileName"], | ||||
| "StartLine": startLine, | |||||
| "EndLine": result["endLine"], | |||||
| "StartLine": result["StartLine"], | |||||
| "EndLine": result["EndLine"], | |||||
| "Content": result["Content"], | "Content": result["Content"], | ||||
| "Lines": result["lines"], | |||||
| "Lines": result["Lines"], | |||||
| "CanLogDownload": result["FileName"] != "", | "CanLogDownload": result["FileName"] != "", | ||||
| } | } | ||||
| //result := CloudbrainGetLogByJobId(job.JobID, job.JobName) | //result := CloudbrainGetLogByJobId(job.JobID, job.JobName) | ||||
| ctx.JSON(http.StatusOK, re) | ctx.JSON(http.StatusOK, re) | ||||
| } | } | ||||
| @@ -487,12 +484,13 @@ func getLastLogFromModelDir(jobName string, lines int) map[string]interface{} { | |||||
| fileName := "" | fileName := "" | ||||
| count := 0 | count := 0 | ||||
| allLines := 0 | allLines := 0 | ||||
| startLine := 0 | |||||
| for _, file := range files { | for _, file := range files { | ||||
| if strings.HasSuffix(file.FileName, "log.txt") { | if strings.HasSuffix(file.FileName, "log.txt") { | ||||
| fileName = file.FileName | fileName = file.FileName | ||||
| path := storage.GetMinioPath(jobName+"/model/", file.FileName) | path := storage.GetMinioPath(jobName+"/model/", file.FileName) | ||||
| allLines = getAllLineFromFile(path) | allLines = getAllLineFromFile(path) | ||||
| startLine := allLines - 50 | |||||
| startLine = allLines - lines | |||||
| if startLine < 0 { | if startLine < 0 { | ||||
| startLine = 0 | startLine = 0 | ||||
| } | } | ||||
| @@ -504,7 +502,6 @@ func getLastLogFromModelDir(jobName string, lines int) map[string]interface{} { | |||||
| r := bufio.NewReader(reader) | r := bufio.NewReader(reader) | ||||
| for i := 0; i < allLines; i++ { | for i := 0; i < allLines; i++ { | ||||
| line, error := r.ReadString('\n') | line, error := r.ReadString('\n') | ||||
| log.Info("line=" + line) | |||||
| if error == io.EOF { | if error == io.EOF { | ||||
| log.Info("read file completed.") | log.Info("read file completed.") | ||||
| break | break | ||||
| @@ -515,6 +512,7 @@ func getLastLogFromModelDir(jobName string, lines int) map[string]interface{} { | |||||
| } | } | ||||
| if error == nil { | if error == nil { | ||||
| if i >= startLine { | if i >= startLine { | ||||
| log.Info("i=" + fmt.Sprint(i)) | |||||
| re = re + line | re = re + line | ||||
| } | } | ||||
| } | } | ||||
| @@ -527,11 +525,12 @@ func getLastLogFromModelDir(jobName string, lines int) map[string]interface{} { | |||||
| } | } | ||||
| return map[string]interface{}{ | return map[string]interface{}{ | ||||
| "JobName": jobName, | |||||
| "Content": re, | |||||
| "FileName": fileName, | |||||
| "lines": count, | |||||
| "endLine": allLines, | |||||
| "JobName": jobName, | |||||
| "Content": re, | |||||
| "FileName": fileName, | |||||
| "Lines": count, | |||||
| "EndLine": allLines, | |||||
| "StartLine": startLine, | |||||
| } | } | ||||
| } | } | ||||
| @@ -583,11 +582,12 @@ func getLogFromModelDir(jobName string, startLine int, endLine int) map[string]i | |||||
| } | } | ||||
| return map[string]interface{}{ | return map[string]interface{}{ | ||||
| "JobName": jobName, | |||||
| "Content": re, | |||||
| "FileName": fileName, | |||||
| "lines": count, | |||||
| "endLine": fileEndLine, | |||||
| "JobName": jobName, | |||||
| "Content": re, | |||||
| "FileName": fileName, | |||||
| "Lines": count, | |||||
| "EndLine": fileEndLine, | |||||
| "StartLine": startLine, | |||||
| } | } | ||||
| } | } | ||||