From 130b116b5216ead96005035e8dc4f9f4c796e978 Mon Sep 17 00:00:00 2001 From: tzwang Date: Mon, 20 May 2024 15:46:49 +0800 Subject: [PATCH 1/3] added get balance api Former-commit-id: 2c8df408652a3e2bede8652ff11e9460ba8fc7e0 --- api/desc/pcm.api | 3 +++ api/desc/schedule/pcm-schedule.api | 9 +++++++++ api/internal/logic/core/pagelisttasklogic.go | 5 +++++ 3 files changed, 17 insertions(+) diff --git a/api/desc/pcm.api b/api/desc/pcm.api index 2cb1e06c..f7290a96 100644 --- a/api/desc/pcm.api +++ b/api/desc/pcm.api @@ -978,6 +978,9 @@ service pcm { @handler GetComputeCardsByClusterHandler get /schedule/getComputeCardsByCluster/:adapterId/:clusterId (GetComputeCardsByClusterReq) returns (GetComputeCardsByClusterResp) + + @handler GetClusterBalanceByIdHandler + get /schedule/getClusterBalanceById/:adapterId/:clusterId (GetClusterBalanceByIdReq) returns (GetClusterBalanceByIdResp) } @server( diff --git a/api/desc/schedule/pcm-schedule.api b/api/desc/schedule/pcm-schedule.api index 9be2c829..e612f9fb 100644 --- a/api/desc/schedule/pcm-schedule.api +++ b/api/desc/schedule/pcm-schedule.api @@ -141,4 +141,13 @@ type ( GetComputeCardsByClusterResp { Cards []string `json:"cards"` } + + GetClusterBalanceByIdReq{ + AdapterId string `path:"adapterId"` + ClusterId string `path:"clusterId"` + } + + GetClusterBalanceByIdResp{ + Balance float64 `json:"balance"` + } ) \ No newline at end of file diff --git a/api/internal/logic/core/pagelisttasklogic.go b/api/internal/logic/core/pagelisttasklogic.go index 32cc2240..f1858a4a 100644 --- a/api/internal/logic/core/pagelisttasklogic.go +++ b/api/internal/logic/core/pagelisttasklogic.go @@ -143,6 +143,11 @@ func (l *PageListTaskLogic) updateTaskStatus(tasks []*types.TaskModel, ch chan<- } } + if len(aiTask) == 0 { + ch <- struct{}{} + return + } + start, _ := time.ParseInLocation(constants.Layout, aiTask[0].StartTime, time.Local) end, _ := time.ParseInLocation(constants.Layout, aiTask[0].EndTime, time.Local) From ff117751b05ca33b15a0a80616264b8f0e187961 Mon Sep 17 00:00:00 2001 From: tzwang Date: Mon, 20 May 2024 16:19:06 +0800 Subject: [PATCH 2/3] updated types Former-commit-id: 898388856c31253f46aca54df8b6d72b5d15333a --- api/internal/handler/routes.go | 5 ++++ .../schedule/getclusterbalancebyidhandler.go | 28 +++++++++++++++++ .../schedule/getclusterbalancebyidlogic.go | 30 +++++++++++++++++++ api/internal/types/types.go | 9 ++++++ 4 files changed, 72 insertions(+) create mode 100644 api/internal/handler/schedule/getclusterbalancebyidhandler.go create mode 100644 api/internal/logic/schedule/getclusterbalancebyidlogic.go diff --git a/api/internal/handler/routes.go b/api/internal/handler/routes.go index 11006a83..05583d79 100644 --- a/api/internal/handler/routes.go +++ b/api/internal/handler/routes.go @@ -1225,6 +1225,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/schedule/getComputeCardsByCluster/:adapterId/:clusterId", Handler: schedule.GetComputeCardsByClusterHandler(serverCtx), }, + { + Method: http.MethodGet, + Path: "/schedule/getClusterBalanceById/:adapterId/:clusterId", + Handler: schedule.GetClusterBalanceByIdHandler(serverCtx), + }, }, rest.WithPrefix("/pcm/v1"), ) diff --git a/api/internal/handler/schedule/getclusterbalancebyidhandler.go b/api/internal/handler/schedule/getclusterbalancebyidhandler.go new file mode 100644 index 00000000..ace98e2f --- /dev/null +++ b/api/internal/handler/schedule/getclusterbalancebyidhandler.go @@ -0,0 +1,28 @@ +package schedule + +import ( + "net/http" + + "github.com/zeromicro/go-zero/rest/httpx" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/logic/schedule" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types" +) + +func GetClusterBalanceByIdHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + var req types.GetClusterBalanceByIdReq + if err := httpx.Parse(r, &req); err != nil { + httpx.ErrorCtx(r.Context(), w, err) + return + } + + l := schedule.NewGetClusterBalanceByIdLogic(r.Context(), svcCtx) + resp, err := l.GetClusterBalanceById(&req) + if err != nil { + httpx.ErrorCtx(r.Context(), w, err) + } else { + httpx.OkJsonCtx(r.Context(), w, resp) + } + } +} diff --git a/api/internal/logic/schedule/getclusterbalancebyidlogic.go b/api/internal/logic/schedule/getclusterbalancebyidlogic.go new file mode 100644 index 00000000..1f38f351 --- /dev/null +++ b/api/internal/logic/schedule/getclusterbalancebyidlogic.go @@ -0,0 +1,30 @@ +package schedule + +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 GetClusterBalanceByIdLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewGetClusterBalanceByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetClusterBalanceByIdLogic { + return &GetClusterBalanceByIdLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +func (l *GetClusterBalanceByIdLogic) GetClusterBalanceById(req *types.GetClusterBalanceByIdReq) (resp *types.GetClusterBalanceByIdResp, err error) { + // todo: add your logic here and delete this line + + return +} diff --git a/api/internal/types/types.go b/api/internal/types/types.go index 54a3c5d1..fed98b44 100644 --- a/api/internal/types/types.go +++ b/api/internal/types/types.go @@ -5746,6 +5746,15 @@ type GetComputeCardsByClusterResp struct { Cards []string `json:"cards"` } +type GetClusterBalanceByIdReq struct { + AdapterId string `path:"adapterId"` + ClusterId string `path:"clusterId"` +} + +type GetClusterBalanceByIdResp struct { + Balance float64 `json:"balance"` +} + type CreateAlertRuleReq struct { CLusterId string `json:"clusterId"` ClusterName string `json:"clusterName"` From 766c862af7ab90cfb71cd28b9c93619ec915dca1 Mon Sep 17 00:00:00 2001 From: tzwang Date: Mon, 20 May 2024 16:48:09 +0800 Subject: [PATCH 3/3] added getclusterbalance api Former-commit-id: 856cdfb1b2c380bf848782bbf85275f34e925cfd --- .../schedule/getclusterbalancebyidhandler.go | 10 ++++------ .../schedule/getclusterbalancebyidlogic.go | 9 +++++++-- .../scheduler/service/collector/collector.go | 1 + api/internal/storeLink/modelarts.go | 4 ++++ api/internal/storeLink/octopus.go | 19 +++++++++++++++++++ api/internal/storeLink/shuguangai.go | 10 ++++++++++ 6 files changed, 45 insertions(+), 8 deletions(-) diff --git a/api/internal/handler/schedule/getclusterbalancebyidhandler.go b/api/internal/handler/schedule/getclusterbalancebyidhandler.go index ace98e2f..27e273f4 100644 --- a/api/internal/handler/schedule/getclusterbalancebyidhandler.go +++ b/api/internal/handler/schedule/getclusterbalancebyidhandler.go @@ -1,6 +1,7 @@ package schedule import ( + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" "net/http" "github.com/zeromicro/go-zero/rest/httpx" @@ -13,16 +14,13 @@ func GetClusterBalanceByIdHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.GetClusterBalanceByIdReq if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := schedule.NewGetClusterBalanceByIdLogic(r.Context(), svcCtx) resp, err := l.GetClusterBalanceById(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) + } } diff --git a/api/internal/logic/schedule/getclusterbalancebyidlogic.go b/api/internal/logic/schedule/getclusterbalancebyidlogic.go index 1f38f351..09f70b2c 100644 --- a/api/internal/logic/schedule/getclusterbalancebyidlogic.go +++ b/api/internal/logic/schedule/getclusterbalancebyidlogic.go @@ -24,7 +24,12 @@ func NewGetClusterBalanceByIdLogic(ctx context.Context, svcCtx *svc.ServiceConte } func (l *GetClusterBalanceByIdLogic) GetClusterBalanceById(req *types.GetClusterBalanceByIdReq) (resp *types.GetClusterBalanceByIdResp, err error) { - // todo: add your logic here and delete this line + resp = &types.GetClusterBalanceByIdResp{} + balance, err := l.svcCtx.Scheduler.AiService.AiCollectorAdapterMap[req.AdapterId][req.ClusterId].GetUserBalance(l.ctx) + if err != nil { + return nil, err + } + resp.Balance = balance - return + return resp, nil } diff --git a/api/internal/scheduler/service/collector/collector.go b/api/internal/scheduler/service/collector/collector.go index 5e6a7940..2c8d51a8 100644 --- a/api/internal/scheduler/service/collector/collector.go +++ b/api/internal/scheduler/service/collector/collector.go @@ -11,6 +11,7 @@ type AiCollector interface { DownloadAlgorithmCode(ctx context.Context, resourceType string, card string, taskType string, dataset string, algorithm string) (string, error) UploadAlgorithmCode(ctx context.Context, resourceType string, card string, taskType string, dataset string, algorithm string, code string) error GetComputeCards(ctx context.Context) ([]string, error) + GetUserBalance(ctx context.Context) (float64, error) } type ResourceStats struct { diff --git a/api/internal/storeLink/modelarts.go b/api/internal/storeLink/modelarts.go index 2186addb..ee8df706 100644 --- a/api/internal/storeLink/modelarts.go +++ b/api/internal/storeLink/modelarts.go @@ -187,6 +187,10 @@ func (m *ModelArtsLink) GetComputeCards(ctx context.Context) ([]string, error) { return nil, nil } +func (m *ModelArtsLink) GetUserBalance(ctx context.Context) (float64, error) { + return 0, nil +} + func (m *ModelArtsLink) DownloadAlgorithmCode(ctx context.Context, resourceType string, card string, taskType string, dataset string, algorithm string) (string, error) { return "", nil } diff --git a/api/internal/storeLink/octopus.go b/api/internal/storeLink/octopus.go index 4457ccd2..77d957d9 100644 --- a/api/internal/storeLink/octopus.go +++ b/api/internal/storeLink/octopus.go @@ -359,6 +359,25 @@ func (o *OctopusLink) GetComputeCards(ctx context.Context) ([]string, error) { return cards, nil } +func (o *OctopusLink) GetUserBalance(ctx context.Context) (float64, error) { + balanceReq := &octopus.GetUserBalanceReq{ + Platform: o.platform, + } + balanceResp, err := o.octopusRpc.GetUserBalance(ctx, balanceReq) + if err != nil { + return 0, err + } + if !balanceResp.Success { + if balanceResp.Error != nil { + return 0, errors.New(balanceResp.Error.Message) + } else { + return 0, errors.New("failed to get user balance") + } + } + balance := float64(balanceResp.Payload.BillingUser.Amount) + return balance, nil +} + func (o *OctopusLink) DownloadAlgorithmCode(ctx context.Context, resourceType string, card string, taskType string, dataset string, algorithm string) (string, error) { var name string if resourceType == CARD { diff --git a/api/internal/storeLink/shuguangai.go b/api/internal/storeLink/shuguangai.go index 415f14b1..cd17229a 100644 --- a/api/internal/storeLink/shuguangai.go +++ b/api/internal/storeLink/shuguangai.go @@ -453,6 +453,16 @@ func (s *ShuguangAi) GetComputeCards(ctx context.Context) ([]string, error) { return cards, nil } +func (s *ShuguangAi) GetUserBalance(ctx context.Context) (float64, error) { + userReq := &hpcAC.GetUserInfoReq{} + userinfo, err := s.aCRpc.GetUserInfo(ctx, userReq) + if err != nil { + return 0, err + } + balance, _ := strconv.ParseFloat(userinfo.Data.AccountBalance, 64) + return balance, nil +} + func (s *ShuguangAi) DownloadAlgorithmCode(ctx context.Context, resourceType string, card string, taskType string, dataset string, algorithm string) (string, error) { algoName := dataset + DASH + algorithm req := &hpcAC.GetFileReq{