added getclusterbalance api

Former-commit-id: 856cdfb1b2c380bf848782bbf85275f34e925cfd
This commit is contained in:
tzwang 2024-05-20 16:48:09 +08:00
parent ff117751b0
commit 766c862af7
6 changed files with 45 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package schedule package schedule
import ( import (
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result"
"net/http" "net/http"
"github.com/zeromicro/go-zero/rest/httpx" "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) { return func(w http.ResponseWriter, r *http.Request) {
var req types.GetClusterBalanceByIdReq var req types.GetClusterBalanceByIdReq
if err := httpx.Parse(r, &req); err != nil { if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err) result.ParamErrorResult(r, w, err)
return return
} }
l := schedule.NewGetClusterBalanceByIdLogic(r.Context(), svcCtx) l := schedule.NewGetClusterBalanceByIdLogic(r.Context(), svcCtx)
resp, err := l.GetClusterBalanceById(&req) resp, err := l.GetClusterBalanceById(&req)
if err != nil { result.HttpResult(r, w, resp, err)
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
} }
} }

View File

@ -24,7 +24,12 @@ func NewGetClusterBalanceByIdLogic(ctx context.Context, svcCtx *svc.ServiceConte
} }
func (l *GetClusterBalanceByIdLogic) GetClusterBalanceById(req *types.GetClusterBalanceByIdReq) (resp *types.GetClusterBalanceByIdResp, err error) { 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
} }

View File

@ -11,6 +11,7 @@ type AiCollector interface {
DownloadAlgorithmCode(ctx context.Context, resourceType string, card string, taskType string, dataset string, algorithm string) (string, error) 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 UploadAlgorithmCode(ctx context.Context, resourceType string, card string, taskType string, dataset string, algorithm string, code string) error
GetComputeCards(ctx context.Context) ([]string, error) GetComputeCards(ctx context.Context) ([]string, error)
GetUserBalance(ctx context.Context) (float64, error)
} }
type ResourceStats struct { type ResourceStats struct {

View File

@ -187,6 +187,10 @@ func (m *ModelArtsLink) GetComputeCards(ctx context.Context) ([]string, error) {
return nil, nil 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) { func (m *ModelArtsLink) DownloadAlgorithmCode(ctx context.Context, resourceType string, card string, taskType string, dataset string, algorithm string) (string, error) {
return "", nil return "", nil
} }

View File

@ -359,6 +359,25 @@ func (o *OctopusLink) GetComputeCards(ctx context.Context) ([]string, error) {
return cards, nil 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) { func (o *OctopusLink) DownloadAlgorithmCode(ctx context.Context, resourceType string, card string, taskType string, dataset string, algorithm string) (string, error) {
var name string var name string
if resourceType == CARD { if resourceType == CARD {

View File

@ -453,6 +453,16 @@ func (s *ShuguangAi) GetComputeCards(ctx context.Context) ([]string, error) {
return cards, nil 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) { func (s *ShuguangAi) DownloadAlgorithmCode(ctx context.Context, resourceType string, card string, taskType string, dataset string, algorithm string) (string, error) {
algoName := dataset + DASH + algorithm algoName := dataset + DASH + algorithm
req := &hpcAC.GetFileReq{ req := &hpcAC.GetFileReq{