diff --git a/api/client/task.go b/api/client/task.go index 3367ab95..9456e5d0 100644 --- a/api/client/task.go +++ b/api/client/task.go @@ -9,5 +9,5 @@ type TaskOptions struct { type Task interface { PullTaskInfo(pullTaskInfoReq PullTaskInfoReq) (*PullTaskInfoResp, error) PushTaskInfo(pushTaskInfoReq PushTaskInfoReq) (*PushTaskInfoResp, error) - PushResourceInfo(pushResourceInfoReq PushResourceInfoReq) error + PushResourceInfo(pushResourceInfoReq PushResourceInfoReq) (*PushResourceInfoResp, error) } diff --git a/api/client/task_impl.go b/api/client/task_impl.go index a339c298..31aebbd0 100644 --- a/api/client/task_impl.go +++ b/api/client/task_impl.go @@ -50,8 +50,8 @@ func (t *task) PushTaskInfo(pushTaskInfoReq PushTaskInfoReq) (*PushTaskInfoResp, url := t.client.url + "/pcm/v1/core/pushTaskInfo" method := "POST" - infoReq := PullTaskInfoReq{AdapterId: pushTaskInfoReq.AdapterId} - jsonStr, _ := json.Marshal(infoReq) + //infoReq := PullTaskInfoReq{AdapterId: pushTaskInfoReq.AdapterId} + jsonStr, _ := json.Marshal(pushTaskInfoReq) payload := strings.NewReader(string(jsonStr)) client := &http.Client{} @@ -66,7 +66,22 @@ func (t *task) PushTaskInfo(pushTaskInfoReq PushTaskInfoReq) (*PushTaskInfoResp, return &resp, nil } -func (t *task) PushResourceInfo(pushResourceInfoReq PushResourceInfoReq) error { - //TODO implement me - panic("implement me") +func (t *task) PushResourceInfo(pushResourceInfoReq PushResourceInfoReq) (*PushResourceInfoResp, error) { + + url := t.client.url + "/pcm/v1/core/pushResourceInfo" + method := "POST" + //infoReq := PushResourceInfoReq{AdapterId: pushResourceInfoReq.AdapterId} + jsonStr, _ := json.Marshal(pushResourceInfoReq) + payload := strings.NewReader(string(jsonStr)) + + client := &http.Client{} + req, _ := http.NewRequest(method, url, payload) + req.Header.Add("Content-Type", "application/json") + res, _ := client.Do(req) + defer res.Body.Close() + + body, _ := ioutil.ReadAll(res.Body) + var resp PushResourceInfoResp + json.Unmarshal(body, &resp) + return &resp, nil } diff --git a/api/client/types.go b/api/client/types.go index 0b8250f8..0888f950 100644 --- a/api/client/types.go +++ b/api/client/types.go @@ -30,9 +30,14 @@ type PushTaskInfoResp struct { } type PushResourceInfoReq struct { - AdapterId int64 `json:"adapterId"` + AdapterId int64 `json:"adapterId"` + ResourceStats []ResourceStats `json:"resourceStats"` } +type PushResourceInfoResp struct { + Code int64 + Msg string +} type HpcInfo struct { Id int64 `json:"id"` // id TaskId int64 `json:"task_id"` // 任务id @@ -121,3 +126,27 @@ type VmInfo struct { DeleteOnTermination bool `json:"delete_on_termination,omitempty"` State string `json:"state,omitempty"` } + +type ResourceStats struct { + ClusterId int64 + Name string + CpuCoreAvail int64 + CpuCoreTotal int64 + MemAvail float64 + MemTotal float64 + DiskAvail float64 + DiskTotal float64 + GpuAvail int64 + CardsAvail []*Card + CpuCoreHours float64 + Balance float64 +} + +type Card struct { + Platform string + Type string + Name string + TOpsAtFp16 float64 + CardHours float64 + CardNum int32 +} diff --git a/api/desc/pcm.api b/api/desc/pcm.api index 6165299c..d96f4a83 100644 --- a/api/desc/pcm.api +++ b/api/desc/pcm.api @@ -150,6 +150,10 @@ service pcm { @handler jobHandler get /hpc/job (hpcJobReq) returns (hpcJobResp) + @doc "超算资源总览" + @handler resourceHandler + get /hpc/resource (hpcResourceReq) returns (hpcResourceResp) + @doc "超算查询资产列表" @handler queueAssetsHandler get /hpc/queueAssets returns (QueueAssetsResp) diff --git a/api/internal/handler/hpc/resourcehandler.go b/api/internal/handler/hpc/resourcehandler.go new file mode 100644 index 00000000..2f321824 --- /dev/null +++ b/api/internal/handler/hpc/resourcehandler.go @@ -0,0 +1,28 @@ +package hpc + +import ( + "net/http" + + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/hpc" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" +) + +func ResourceHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.HpcResourceReq + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := hpc.NewResourceLogic(r.Context(), svcCtx) + resp, err := l.Resource(&req) + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } + } +} diff --git a/api/internal/handler/routes.go b/api/internal/handler/routes.go index aecdd7ab..08b9ed98 100644 --- a/api/internal/handler/routes.go +++ b/api/internal/handler/routes.go @@ -176,6 +176,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/hpc/job", Handler: hpc.JobHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/hpc/resource", + Handler: hpc.ResourceHandler(serverCtx), + }, { Method: http.MethodGet, Path: "/hpc/queueAssets", diff --git a/api/internal/logic/hpc/resourcelogic.go b/api/internal/logic/hpc/resourcelogic.go new file mode 100644 index 00000000..b88e94a1 --- /dev/null +++ b/api/internal/logic/hpc/resourcelogic.go @@ -0,0 +1,48 @@ +package hpc + +import ( + "context" + + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type ResourceLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewResourceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResourceLogic { + return &ResourceLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *ResourceLogic) Resource(req *types.HpcResourceReq) (resp *types.HpcResourceResp, err error) { + + l.svcCtx.DbEngin.Raw("SELECT th.NAME as job_name,t.description as job_desc,t.commit_time as submit_time,th.STATUS as job_status,ta.name as adapter_name,tc.name as cluster_name,tc.label as cluster_type FROM task_hpc th LEFT JOIN task t ON t.id = th.task_id JOIN t_cluster tc on th.cluster_id = tc.id JOIN t_adapter ta on tc.adapter_id = ta.id") + + hpcResource := types.HPCResource{ + GPUCardsTotal: 0, + CPUCoresTotal: 0, + RAMTotal: 0, + GPUCardsUsed: 0, + CPUCoresUsed: 0, + RAMUsed: 0, + GPURate: 0, + CPURate: 0, + RAMRate: 0, + } + + resp = &types.HpcResourceResp{ + Code: 200, + Msg: "success", + HPCResource: hpcResource, + } + return resp, nil +}