pcm-ac/internal/logic/listjoblogic.go

130 lines
3.5 KiB
Go

package logic
import (
"context"
"github.com/bitly/go-simplejson"
"gitlink.org.cn/JointCloud/pcm-ac/hpcAC"
"gitlink.org.cn/JointCloud/pcm-ac/internal/common"
"gitlink.org.cn/JointCloud/pcm-ac/internal/svc"
"io"
"log"
"net/http"
"net/url"
"strconv"
"time"
"github.com/zeromicro/go-zero/core/logx"
)
type ListJobLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewListJobLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListJobLogic {
return &ListJobLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// ListJob list all jobs
func (l *ListJobLogic) ListJob(in *hpcAC.ListJobReq) (*hpcAC.ListJobResp, error) {
var resp hpcAC.ListJobResp
jobUrl := "/hpc/openapi/v2/jobs?"
getTokenLogic := NewGetACTokenLogic(l.ctx, l.svcCtx)
tokenResp, _ := getTokenLogic.GetACToken(&hpcAC.ACTokenReq{})
token := tokenResp.GetData().Token
getACClusterIdLogic := NewGetACClusterIdLogic(l.ctx, l.svcCtx)
clusterIdResp, _ := getACClusterIdLogic.GetACClusterId(&hpcAC.ACClusterReq{Token: token})
clusterId := clusterIdResp.GetData().Id
c := http.Client{Timeout: time.Duration(3) * time.Second}
params := url.Values{}
params.Add("strClusterIDList", strconv.FormatInt(clusterId, 10))
reqUrl, err := http.NewRequest("GET", common.HpcCenterUrlPrefix()+jobUrl+params.Encode(), nil)
if err != nil {
log.Fatal(err)
}
reqUrl.Header.Add("token", token)
respUrl, err := c.Do(reqUrl)
if err != nil {
log.Fatal(err)
}
body, err := io.ReadAll(respUrl.Body)
jsonResult, err := simplejson.NewJson(body)
jsonData := jsonResult.Get("data")
if jsonData.Get("total").MustInt() == 0 {
resp.Code = uint32(200)
resp.Msg = "success"
resp.RecordCount = 0
return &resp, nil
}
jobList := jsonResult.Get("data").Get("list")
rows, err := jobList.Array()
if err != nil {
log.Fatal(err)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(respUrl.Body)
var Jobs []*hpcAC.Job
for index := range rows {
jobShuguang := jobList.GetIndex(index)
var job hpcAC.Job
job.AppType = jobShuguang.Get("appType").MustString()
job.ErrorPath = jobShuguang.Get("errorPath").MustString()
job.JobId = jobShuguang.Get("jobId").MustString()
job.JobName = jobShuguang.Get("jobName").MustString()
job.JobRunTime = jobShuguang.Get("jobRunTime").MustString()
job.JobStartTime = jobShuguang.Get("jobStartTime").MustString()
job.JobStatus = jobShuguang.Get("jobStatus").MustString()
job.JobManagerId = jobShuguang.Get("jobmanagerId").MustString()
job.JobManagerName = jobShuguang.Get("jobmanagerName").MustString()
job.JobManagerType = jobShuguang.Get("jobmanagerType").MustString()
job.NodeUsed = jobShuguang.Get("nodeUsed").MustString()
job.OutputPath = jobShuguang.Get("outputPath").MustString()
job.ProcNumUsed = int32(jobShuguang.Get("procNumUsed").MustInt64())
job.Queue = jobShuguang.Get("queue").MustString()
job.Reason = jobShuguang.Get("reason").MustString()
job.User = jobShuguang.Get("user").MustString()
job.WorkDir = jobShuguang.Get("workDir").MustString()
startTime, err := time.Parse(l.svcCtx.Config.ShuguangConf.Layout, jobShuguang.Get("jobStartTime").MustString())
if err == nil {
job.JobStartTime = startTime.String()
}
Jobs = append(Jobs, &job)
}
if jsonResult.Get("code").MustInt() == 0 {
resp.Code = uint32(200)
}
resp.Msg = jsonResult.Get("msg").MustString()
resp.RecordCount = uint32(jsonData.Get("total").MustInt())
resp.Jobs = Jobs
return &resp, nil
}