// Copyright 2016 The Gogs Authors. All rights reserved. // Copyright 2018 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. package repo import ( "net/http" "strconv" "code.gitea.io/gitea/models" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/modelarts" ) func GetModelArtsNotebook(ctx *context.APIContext) { var ( err error ) jobID := ctx.Params(":jobid") repoID := ctx.Repo.Repository.ID job, err := models.GetRepoCloudBrainByJobID(repoID, jobID) if err != nil { ctx.NotFound(err) return } result, err := modelarts.GetJob(jobID) if err != nil { ctx.NotFound(err) return } job.Status = result.Status err = models.UpdateJob(job) if err != nil { log.Error("UpdateJob failed:", err) } ctx.JSON(http.StatusOK, map[string]interface{}{ "JobID": jobID, "JobStatus": result.Status, }) } func GetModelArtsTrainJob(ctx *context.APIContext) { var ( err error ) jobID := ctx.Params(":jobid") repoID := ctx.Repo.Repository.ID job, err := models.GetRepoCloudBrainByJobID(repoID, jobID) if err != nil { ctx.NotFound(err) return } result, err := modelarts.GetTrainJob(jobID, strconv.FormatInt(job.VersionID, 10)) if err != nil { ctx.NotFound(err) return } job.Status = modelarts.TransTrainJobStatus(result.IntStatus) job.Duration = result.Duration job.TrainJobDuration = result.TrainJobDuration err = models.UpdateJob(job) if err != nil { log.Error("UpdateJob failed:", err) } ctx.JSON(http.StatusOK, map[string]interface{}{ "JobID": jobID, "JobStatus": job.Status, "JobDuration": job.Duration, }) } func GetModelArtsTrainJobVersion(ctx *context.APIContext) { var ( err error ) jobID := ctx.Params(":jobid") versionName := ctx.Query("version_name") repoID := ctx.Repo.Repository.ID job, err := models.GetRepoCloudBrainByJobIDAndVersionName(repoID, jobID, versionName) if err != nil { ctx.NotFound(err) return } result, err := modelarts.GetTrainJob(jobID, strconv.FormatInt(job.VersionID, 10)) if err != nil { ctx.NotFound(err) return } job.Status = modelarts.TransTrainJobStatus(result.IntStatus) job.Duration = result.Duration job.TrainJobDuration = result.TrainJobDuration if result.Duration != 0 { job.TrainJobDuration = addZero(result.Duration/3600000) + ":" + addZero(result.Duration%3600000/60000) + ":" + addZero(result.Duration%60000/1000) } else { job.TrainJobDuration = "00:00:00" } err = models.UpdateJob(job) if err != nil { log.Error("UpdateJob failed:", err) } ctx.JSON(http.StatusOK, map[string]interface{}{ "JobID": jobID, "JobStatus": job.Status, "JobDuration": job.TrainJobDuration, }) } func addZero(t int64) (m string) { if t < 10 { m = "0" + strconv.FormatInt(t, 10) return m } else { return strconv.FormatInt(t, 10) } } func TrainJobGetLog(ctx *context.APIContext) { var ( err error ) var jobID = ctx.Params(":jobid") var versionName = ctx.Query("version_name") // var logFileName = ctx.Query("file_name") var baseLine = ctx.Query("base_line") var order = ctx.Query("order") var lines = ctx.Query("lines") lines_int, err := strconv.Atoi(lines) if err != nil { log.Error("change lines(%d) string to int failed", lines_int) } if order != modelarts.OrderDesc && order != modelarts.OrderAsc { log.Error("order(%s) check failed", order) ctx.JSON(http.StatusBadRequest, map[string]interface{}{ "err_msg": "order check failed", }) return } // task, err := models.GetCloudbrainByJobIDAndVersionName(jobID, versionName) // if err != nil { // log.Error("GetCloudbrainByJobIDAndVersionName(%s) failed:%v", jobID, err.Error()) // ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ // "err_msg": "GetCloudbrainByJobIDAndVersionName failed", // }) // return // } resultLogFile, result, err := trainJobGetLogContent(jobID, versionName, baseLine, order, lines_int) if err != nil { log.Error("trainJobGetLog(%s) failed:%v", jobID, err.Error()) // ctx.RenderWithErr(err.Error(), tplModelArtsTrainJobShow, nil) return } ctx.Data["log_file_name"] = resultLogFile.LogFileList[0] // result, err := modelarts.GetTrainJobLog(jobID, strconv.FormatInt(task.VersionID, 10), baseLine, logFileName, order, modelarts.Lines) // if err != nil { // log.Error("GetTrainJobLog(%s) failed:%v", jobID, err.Error()) // ctx.JSON(http.StatusInternalServerError, map[string]interface{}{ // "err_msg": "GetTrainJobLog failed", // }) // return // } ctx.JSON(http.StatusOK, map[string]interface{}{ "JobID": jobID, "StartLine": result.StartLine, "EndLine": result.EndLine, "Content": result.Content, "Lines": result.Lines, }) } func trainJobGetLogContent(jobID string, versionName string, baseLine string, order string, lines int) (*models.GetTrainJobLogFileNamesResult, *models.GetTrainJobLogResult, error) { task, err := models.GetCloudbrainByJobIDAndVersionName(jobID, versionName) if err != nil { log.Error("GetCloudbrainByJobID(%s) failed:%v", jobID, err.Error()) return nil, nil, err } resultLogFile, err := modelarts.GetTrainJobLogFileNames(jobID, strconv.FormatInt(task.VersionID, 10)) if err != nil { log.Error("GetTrainJobLogFileNames(%s) failed:%v", jobID, err.Error()) return nil, nil, err } result, err := modelarts.GetTrainJobLog(jobID, strconv.FormatInt(task.VersionID, 10), baseLine, resultLogFile.LogFileList[0], order, lines) if err != nil { log.Error("GetTrainJobLog(%s) failed:%v", jobID, err.Error()) return nil, nil, err } return resultLogFile, result, err }