From d56e4bd69f4a37ab5258788f606928adfede76ed Mon Sep 17 00:00:00 2001 From: tzwang Date: Tue, 26 Mar 2024 17:36:04 +0800 Subject: [PATCH] updated shuguangAi GetResourceStats Former-commit-id: 2e69f551307dd1be671f0cc535b541760ff59aa6 --- api/internal/storeLink/shuguangai.go | 51 ++++++++++++++++++++++++---- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/api/internal/storeLink/shuguangai.go b/api/internal/storeLink/shuguangai.go index f9d8b918..654e12b4 100644 --- a/api/internal/storeLink/shuguangai.go +++ b/api/internal/storeLink/shuguangai.go @@ -267,12 +267,15 @@ func (s *ShuguangAi) QuerySpecs() (interface{}, error) { } func (s *ShuguangAi) GetResourceStats() (*collector.ResourceStats, error) { + //balance userReq := &hpcAC.GetUserInfoReq{} userinfo, err := s.svcCtx.ACRpc.GetUserInfo(s.ctx, userReq) if err != nil { return nil, err } + balance, _ := strconv.ParseFloat(userinfo.Data.AccountBalance, 64) + //resource limit limitReq := &hpcAC.QueueReq{} limitResp, err := s.svcCtx.ACRpc.QueryUserQuotasLimit(s.ctx, limitReq) if err != nil { @@ -281,20 +284,54 @@ func (s *ShuguangAi) GetResourceStats() (*collector.ResourceStats, error) { totalCpu := limitResp.Data.AccountMaxCpu totalDcu := limitResp.Data.AccountMaxDcu + //disk diskReq := &hpcAC.ParaStorQuotaReq{} diskResp, err := s.svcCtx.ACRpc.ParaStorQuota(s.ctx, diskReq) if err != nil { return nil, err } - totalDisk := common.RoundFloat(diskResp.Data[0].Threshold*KB*KB, 3) - availDisk := common.RoundFloat((diskResp.Data[0].Threshold-diskResp.Data[0].Usage)*KB*KB, 3) + totalDisk := common.RoundFloat(diskResp.Data[0].Threshold*KB*KB*KB, 3) + availDisk := common.RoundFloat((diskResp.Data[0].Threshold-diskResp.Data[0].Usage)*KB*KB*KB, 3) - generalInfo, err := s.svcCtx.ACRpc.GetGeneralInfo(s.ctx, nil) - memSize := common.RoundFloat(float64(generalInfo.MemoryInGib)*KB*KB, 3) + //memory + nodeResp, err := s.svcCtx.ACRpc.GetNodeResources(s.ctx, nil) + if err != nil { + return nil, err + } + memSize := common.RoundFloat(float64(nodeResp.Data.MemorySize)*KB*KB, 3) // MB to BYTES + //resources being occupied + memberJobResp, err := s.svcCtx.ACRpc.GetMemberJobs(s.ctx, nil) + if err != nil { + return nil, err + } + var CpuCoreAvail int64 + var MemAvail float64 + if len(memberJobResp.Data) != 0 { + CpuCoreAvail = totalCpu + MemAvail = memSize + } else { + var cpuCoreUsed int64 + var memUsed float64 + for _, datum := range memberJobResp.Data { + cpuCoreUsed += datum.CpuCore + } + memUsed = float64(cpuCoreUsed * 2 * KB * KB * KB) // 2 GB per cpu core + if cpuCoreUsed > totalCpu { + CpuCoreAvail = 0 + } else { + CpuCoreAvail = totalCpu - cpuCoreUsed + } + if memUsed > memSize { + MemAvail = 0 + } else { + MemAvail = memSize - memUsed + } + } + + //usable hours var cards []*collector.Card - balance, _ := strconv.ParseFloat(userinfo.Data.AccountBalance, 64) cardHours := common.RoundFloat(balance/DCUPRICEPERHOUR, 3) cpuHours := common.RoundFloat(balance/CPUCOREPRICEPERHOUR, 3) @@ -312,11 +349,11 @@ func (s *ShuguangAi) GetResourceStats() (*collector.ResourceStats, error) { Name: s.platform, Balance: balance, CpuCoreTotal: totalCpu, - CpuCoreAvail: 0, + CpuCoreAvail: CpuCoreAvail, DiskTotal: totalDisk, DiskAvail: availDisk, MemTotal: memSize, - MemAvail: 0, + MemAvail: MemAvail, CpuCoreHours: cpuHours, CardsAvail: cards, }