From c83397ac5685a8d1d859c11840e96b7574361905 Mon Sep 17 00:00:00 2001 From: palytoxin Date: Mon, 20 Jul 2020 15:37:31 +0800 Subject: [PATCH] parse task result --- models/cloudbrain.go | 91 +++++++++++++++++++++++++++++ routers/repo/cloudbrain.go | 6 +- templates/repo/cloudbrain/show.tmpl | 2 + 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/models/cloudbrain.go b/models/cloudbrain.go index acfb37e08..f76363518 100644 --- a/models/cloudbrain.go +++ b/models/cloudbrain.go @@ -1,8 +1,10 @@ package models import ( + "encoding/json" "errors" "fmt" + "time" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" @@ -81,6 +83,95 @@ type CloudbrainsOptions struct { SortType string CloudbrainIDs []int64 } +type TaskPod struct { + TaskRoleStatus struct { + Name string `json:"name"` + } `json:"taskRoleStatus"` + TaskStatuses []struct { + TaskIndex int `json:"taskIndex"` + PodUID string `json:"podUid"` + PodIP string `json:"podIp"` + PodName string `json:"podName"` + ContainerID string `json:"containerId"` + ContainerIP string `json:"containerIp"` + ContainerGpus string `json:"containerGpus"` + State string `json:"state"` + StartAt time.Time `json:"startAt"` + FinishedAt time.Time `json:"finishedAt"` + ExitCode int `json:"exitCode"` + ExitDiagnostics string `json:"exitDiagnostics"` + RetriedCount int `json:"retriedCount"` + } `json:"taskStatuses"` +} + +func ConvertToTaskPod(input map[string]interface{}) (TaskPod, error) { + data, _ := json.Marshal(input) + var taskPod TaskPod + err := json.Unmarshal(data, &taskPod) + return taskPod, err +} + +type JobResultPayload struct { + ID string `json:"id"` + Name string `json:"name"` + Platform string `json:"platform"` + JobStatus struct { + Username string `json:"username"` + State string `json:"state"` + SubState string `json:"subState"` + ExecutionType string `json:"executionType"` + Retries int `json:"retries"` + CreatedTime int64 `json:"createdTime"` + CompletedTime int64 `json:"completedTime"` + AppID string `json:"appId"` + AppProgress string `json:"appProgress"` + AppTrackingURL string `json:"appTrackingUrl"` + AppLaunchedTime int64 `json:"appLaunchedTime"` + AppCompletedTime int64 `json:"appCompletedTime"` + AppExitCode int `json:"appExitCode"` + AppExitDiagnostics string `json:"appExitDiagnostics"` + AppExitType interface{} `json:"appExitType"` + VirtualCluster string `json:"virtualCluster"` + } `json:"jobStatus"` + TaskRoles map[string]interface{} `json:"taskRoles"` + Resource struct { + CPU int `json:"cpu"` + Memory string `json:"memory"` + NvidiaComGpu int `json:"nvidia.com/gpu"` + } `json:"resource"` + Config struct { + Image string `json:"image"` + JobID string `json:"jobId"` + GpuType string `json:"gpuType"` + JobName string `json:"jobName"` + JobType string `json:"jobType"` + TaskRoles []struct { + Name string `json:"name"` + ShmMB int `json:"shmMB"` + Command string `json:"command"` + MemoryMB int `json:"memoryMB"` + CPUNumber int `json:"cpuNumber"` + GpuNumber int `json:"gpuNumber"` + IsMainRole bool `json:"isMainRole"` + TaskNumber int `json:"taskNumber"` + NeedIBDevice bool `json:"needIBDevice"` + MinFailedTaskCount int `json:"minFailedTaskCount"` + MinSucceededTaskCount int `json:"minSucceededTaskCount"` + } `json:"taskRoles"` + RetryCount int `json:"retryCount"` + } `json:"config"` + Userinfo struct { + User string `json:"user"` + OrgID string `json:"org_id"` + } `json:"userinfo"` +} + +func ConvertToJobResultPayload(input map[string]interface{}) (JobResultPayload, error) { + data, _ := json.Marshal(input) + var jobResultPayload JobResultPayload + err := json.Unmarshal(data, &jobResultPayload) + return jobResultPayload, err +} func Cloudbrains(opts *CloudbrainsOptions) ([]*Cloudbrain, int64, error) { sess := x.NewSession() diff --git a/routers/repo/cloudbrain.go b/routers/repo/cloudbrain.go index 01910d93d..e258eadd7 100644 --- a/routers/repo/cloudbrain.go +++ b/routers/repo/cloudbrain.go @@ -102,7 +102,11 @@ func CloudBrainShow(ctx *context.Context) { } if result != nil { - ctx.Data["result"] = result + jobRes, _ := models.ConvertToJobResultPayload(result.Payload) + ctx.Data["result"] = jobRes + taskRoles := jobRes.TaskRoles + taskRes, _ := models.ConvertToTaskPod(taskRoles["task1"].(map[string]interface{})) + ctx.Data["taskRes"] = taskRes } ctx.Data["jobID"] = jobID diff --git a/templates/repo/cloudbrain/show.tmpl b/templates/repo/cloudbrain/show.tmpl index 29c2f5096..29de55309 100644 --- a/templates/repo/cloudbrain/show.tmpl +++ b/templates/repo/cloudbrain/show.tmpl @@ -12,6 +12,8 @@
{{.JobID}}

{{end}} +

taskRes:

+
{{.taskRes}}

err:

{{.error}}