From 4a9b9d3c0651e1a950b30eef057e6eaf66e547ad Mon Sep 17 00:00:00 2001 From: devad Date: Fri, 24 Nov 2023 15:42:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=97=E8=A1=A8=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: devad Former-commit-id: f0de1a71a21b8cd65a99493c6486a1dbc199f26f --- api/desc/core/pcm-core.api | 46 ++++++++++++- api/desc/pcm.api | 15 +++++ api/etc/pcm.yaml | 2 +- api/internal/handler/apps/appdetailhandler.go | 24 +++++++ api/internal/handler/apps/applisthandler.go | 24 +++++++ api/internal/handler/routes.go | 17 +++++ api/internal/logic/apps/appdetaillogic.go | 30 +++++++++ api/internal/logic/apps/applistlogic.go | 64 +++++++++++++++++++ api/internal/types/types.go | 40 ++++++++++++ 9 files changed, 260 insertions(+), 2 deletions(-) create mode 100644 api/internal/handler/apps/appdetailhandler.go create mode 100644 api/internal/handler/apps/applisthandler.go create mode 100644 api/internal/logic/apps/appdetaillogic.go create mode 100644 api/internal/logic/apps/applistlogic.go diff --git a/api/desc/core/pcm-core.api b/api/desc/core/pcm-core.api index 6affef39..67b55917 100644 --- a/api/desc/core/pcm-core.api +++ b/api/desc/core/pcm-core.api @@ -565,4 +565,48 @@ type Participant { metricsUrl string `json:"metricsUrl"` tenantName string `json:"tenantName"` typeName string `json:"typeName"` -} \ No newline at end of file +} + + +// apps列表参数 +type ( + AppListReq { + Namespace string `form:"namespace"` + } + AppListResp { + TotalCount int64 `json:"totalCount"` // 任务总数 + Apps []App `json:"apps"` //应用列表 + } + App { + Id int64 `json:"id"` + Name string `json:"name"` + Status string `json:"status"` + TaskType string `json:"taskType"` + StartTime string `json:"startTime"` + EndTime string `json:"endTime"` + ParticipantStatus string `json:"participantStatus"` + ParticipantId int64 `json:"participantId"` + ParticipantName string `json:"participantName"` + Storage string `json:"storage"` + CreateTime string `json:"createTime"` + } +) + +//apps 详情参数 +type ( + AppDetailReq { + Name string `path:"name"` + NsID string `param:"nsID"` + } + AppDetailResp { + CpuCores float64 `json:"cpuCores"` + CpuRate float64 `json:"cpuRate"` + CpuLimit float64 `json:"cpuLimit"` + GpuCores float64 `json:"gpuCores"` + GpuRate float64 `json:"gpuRate"` + GpuLimit float64 `json:"gpuLimit"` + MemoryTotal float64 `json:"memoryTotal"` + MemoryRate float64 `json:"memoryRate"` + MemoryLimit float64 `json:"memoryLimit"` + } +) \ No newline at end of file diff --git a/api/desc/pcm.api b/api/desc/pcm.api index da2b18dc..fcea9471 100644 --- a/api/desc/pcm.api +++ b/api/desc/pcm.api @@ -519,4 +519,19 @@ service pcm { @handler GetAISpecsHandler get /storelink/getResourceSpecs (GetResourceSpecsReq) returns (GetResourceSpecsResp) +} + +// 接口 +@server( + prefix: pcm/v1 + group : apps +) +service pcm { + @doc "应用列表" + @handler AppListHandler + get /apps/list (AppListReq) returns (AppListResp) + + @doc "应用详情" + @handler AppDetailHandler + get /apps/detail (AppDetailReq) returns (AppDetailResp) } \ No newline at end of file diff --git a/api/etc/pcm.yaml b/api/etc/pcm.yaml index 84d70409..149fb1d2 100644 --- a/api/etc/pcm.yaml +++ b/api/etc/pcm.yaml @@ -4,7 +4,7 @@ NacosConfig: ServerConfigs: # - IpAddr: 127.0.0.1 # Port: 8848 - - IpAddr: nacos.jcce.dev + - IpAddr: 127.0.0.1 Port: 8848 ClientConfig: NamespaceId: test diff --git a/api/internal/handler/apps/appdetailhandler.go b/api/internal/handler/apps/appdetailhandler.go new file mode 100644 index 00000000..27268583 --- /dev/null +++ b/api/internal/handler/apps/appdetailhandler.go @@ -0,0 +1,24 @@ +package apps + +import ( + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/apps" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types" + "gitlink.org.cn/jcce-pcm/utils/result" + "net/http" +) + +func AppDetailHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.AppDetailReq + if err := httpx.Parse(r, &req); err != nil { + result.ParamErrorResult(r, w, err) + return + } + + l := apps.NewAppDetailLogic(r.Context(), svcCtx) + resp, err := l.AppDetail(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/api/internal/handler/apps/applisthandler.go b/api/internal/handler/apps/applisthandler.go new file mode 100644 index 00000000..3e42b3c0 --- /dev/null +++ b/api/internal/handler/apps/applisthandler.go @@ -0,0 +1,24 @@ +package apps + +import ( + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/logic/apps" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types" + "gitlink.org.cn/jcce-pcm/utils/result" + "net/http" +) + +func AppListHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.AppListReq + if err := httpx.Parse(r, &req); err != nil { + result.ParamErrorResult(r, w, err) + return + } + + l := apps.NewAppListLogic(r.Context(), svcCtx) + resp, err := l.AppList(&req) + result.HttpResult(r, w, resp, err) + } +} diff --git a/api/internal/handler/routes.go b/api/internal/handler/routes.go index 510e12e2..2164883f 100644 --- a/api/internal/handler/routes.go +++ b/api/internal/handler/routes.go @@ -5,6 +5,7 @@ import ( "net/http" ai "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/handler/ai" + apps "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/handler/apps" cloud "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/handler/cloud" core "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/handler/core" hpc "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/handler/hpc" @@ -630,4 +631,20 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { }, rest.WithPrefix("/pcm/v1"), ) + + server.AddRoutes( + []rest.Route{ + { + Method: http.MethodGet, + Path: "/apps/list", + Handler: apps.AppListHandler(serverCtx), + }, + { + Method: http.MethodGet, + Path: "/apps/detail", + Handler: apps.AppDetailHandler(serverCtx), + }, + }, + rest.WithPrefix("/pcm/v1"), + ) } diff --git a/api/internal/logic/apps/appdetaillogic.go b/api/internal/logic/apps/appdetaillogic.go new file mode 100644 index 00000000..f10c2569 --- /dev/null +++ b/api/internal/logic/apps/appdetaillogic.go @@ -0,0 +1,30 @@ +package apps + +import ( + "context" + + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type AppDetailLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewAppDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AppDetailLogic { + return &AppDetailLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *AppDetailLogic) AppDetail(req *types.AppDetailReq) (resp *types.AppDetailResp, err error) { + // todo: add your logic here and delete this line + + return +} diff --git a/api/internal/logic/apps/applistlogic.go b/api/internal/logic/apps/applistlogic.go new file mode 100644 index 00000000..90bc922d --- /dev/null +++ b/api/internal/logic/apps/applistlogic.go @@ -0,0 +1,64 @@ +package apps + +import ( + "context" + "gorm.io/gorm" + "time" + + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type AppListLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewAppListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AppListLogic { + return &AppListLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +type Task struct { + Id int64 `db:"id"` // id + Name string `db:"name"` // 作业名称 + Description string `db:"description"` // 作业描述 + Status string `db:"status"` // 作业状态 + Strategy int64 `db:"strategy"` // 策略 + SynergyStatus int64 `db:"synergy_status"` // 协同状态(0-未协同、1-已协同) + CommitTime time.Time `db:"commit_time"` // 提交时间 + StartTime string `db:"start_time"` // 开始时间 + EndTime string `db:"end_time"` // 结束运行时间 + RunningTime int64 `db:"running_time"` // 已运行时间(单位秒) + YamlString string `db:"yaml_string"` + Result string `db:"result"` // 作业结果 + DeletedAt gorm.DeletedAt `gorm:"index"` + NsID string `db:"ns_id"` + PName string `db:"p_name"` // p端名称 + PId int64 `db:"p_id"` // p端id +} + +func (l *AppListLogic) AppList(req *types.AppListReq) (resp *types.AppListResp, err error) { + var tasks []Task + resp = &types.AppListResp{} + l.svcCtx.DbEngin.Raw("SELECT t.*,phy.name as p_name,phy.id as p_id FROM task t LEFT JOIN cloud c ON c.task_id = t.id join sc_participant_phy_info phy on c.participant_id = phy.id WHERE c.kind in ('Deployment', 'StatefulSet', 'Ingress', 'Service') AND t.`ns_id` = ? AND t.`deleted_at` IS NULL ORDER BY t.created_time Desc", req.Namespace).Scan(&tasks) + for _, task := range tasks { + resp.Apps = append(resp.Apps, types.App{ + Id: task.Id, + Name: task.Name, + Status: task.Status, + StartTime: task.StartTime, + EndTime: task.EndTime, + CreateTime: task.CommitTime.Format("2006-01-02 15:04:05"), + ParticipantId: task.PId, + ParticipantName: task.PName, + }) + } + return +} diff --git a/api/internal/types/types.go b/api/internal/types/types.go index 6f8abfd2..51aa0cc5 100644 --- a/api/internal/types/types.go +++ b/api/internal/types/types.go @@ -532,6 +532,46 @@ type Participant struct { TypeName string `json:"typeName"` } +type AppListReq struct { + Namespace string `form:"namespace"` +} + +type AppListResp struct { + TotalCount int64 `json:"totalCount"` // 任务总数 + Apps []App `json:"apps"` //应用列表 +} + +type App struct { + Id int64 `json:"id"` + Name string `json:"name"` + Status string `json:"status"` + TaskType string `json:"taskType"` + StartTime string `json:"startTime"` + EndTime string `json:"endTime"` + ParticipantStatus string `json:"participantStatus"` + ParticipantId int64 `json:"participantId"` + ParticipantName string `json:"participantName"` + Storage string `json:"storage"` + CreateTime string `json:"createTime"` +} + +type AppDetailReq struct { + Name string `path:"name"` + NsID string `param:"nsID"` +} + +type AppDetailResp struct { + CpuCores float64 `json:"cpuCores"` + CpuRate float64 `json:"cpuRate"` + CpuLimit float64 `json:"cpuLimit"` + GpuCores float64 `json:"gpuCores"` + GpuRate float64 `json:"gpuRate"` + GpuLimit float64 `json:"gpuLimit"` + MemoryTotal float64 `json:"memoryTotal"` + MemoryRate float64 `json:"memoryRate"` + MemoryLimit float64 `json:"memoryLimit"` +} + type Job struct { SlurmVersion string `json:"slurmVersion"` Name string `json:"name"`