fix(region): add gpu unused usage (#22372)

This commit is contained in:
屈轩
2025-04-08 11:40:47 +08:00
committed by GitHub
parent adbe8e3af7
commit 636f579069
2 changed files with 44 additions and 18 deletions
+41 -18
View File
@@ -1169,8 +1169,16 @@ func (manager *SIsolatedDeviceManager) totalCountQ(
}
type IsolatedDeviceCountStat struct {
Devices int
Gpus int
Devices int
Gpus int
DevicesUnused int
GpusUnused int
}
type IsolatedDeviceStat struct {
DevType string
GuestId string
Count int
}
func (manager *SIsolatedDeviceManager) totalCount(
@@ -1185,8 +1193,8 @@ func (manager *SIsolatedDeviceManager) totalCount(
cloudEnv string,
rangeObjs []db.IStandaloneModel,
policyResult rbacutils.SPolicyResult,
) (int, error) {
return manager.totalCountQ(
) ([]IsolatedDeviceStat, error) {
iq := manager.totalCountQ(
ctx,
scope,
ownerId,
@@ -1198,7 +1206,20 @@ func (manager *SIsolatedDeviceManager) totalCount(
cloudEnv,
rangeObjs,
policyResult,
).CountWithError()
)
sq := iq.SubQuery()
q := sq.Query(
sq.Field("dev_type"),
sq.Field("guest_id"),
sqlchemy.COUNT("count", sq.Field("id")),
)
q = q.GroupBy(q.Field("dev_type"), q.Field("guest_id"))
ret := []IsolatedDeviceStat{}
err := q.All(&ret)
if err != nil {
return nil, err
}
return ret, nil
}
func (manager *SIsolatedDeviceManager) TotalCount(
@@ -1213,26 +1234,28 @@ func (manager *SIsolatedDeviceManager) TotalCount(
rangeObjs []db.IStandaloneModel,
policyResult rbacutils.SPolicyResult,
) (IsolatedDeviceCountStat, error) {
stat := IsolatedDeviceCountStat{}
devCnt, err := manager.totalCount(
ret := IsolatedDeviceCountStat{}
stat, err := manager.totalCount(
ctx,
scope, ownerId, nil, hostType, resourceTypes,
providers, brands, cloudEnv,
rangeObjs, policyResult)
if err != nil {
return stat, err
return ret, err
}
gpuCnt, err := manager.totalCount(
ctx,
scope, ownerId, VALID_GPU_TYPES, hostType, resourceTypes,
providers, brands, cloudEnv,
rangeObjs, policyResult)
if err != nil {
return stat, err
for _, s := range stat {
ret.Devices += s.Count
if utils.IsInStringArray(s.DevType, VALID_GPU_TYPES) {
ret.Gpus += s.Count
}
if len(s.GuestId) == 0 {
ret.DevicesUnused += s.Count
if utils.IsInStringArray(s.DevType, VALID_GPU_TYPES) {
ret.GpusUnused += s.Count
}
}
}
stat.Devices = devCnt
stat.Gpus = gpuCnt
return stat, nil
return ret, nil
}
func (self *SIsolatedDevice) getDesc() *api.IsolatedDeviceJsonDesc {
+3
View File
@@ -1044,6 +1044,9 @@ func IsolatedDeviceUsage(ctx context.Context, userToken mcclient.TokenCredential
}
ret, _ := models.IsolatedDeviceManager.TotalCount(ctx, scope, userCred, hostType, resourceTypes, providers, brands, cloudEnv, rangeObjs, results)
count[prefix] = ret.Devices
count[prefix+".unused"] = ret.DevicesUnused
count[prefix+".gpu"] = ret.Gpus
count[prefix+".gpu.unused"] = ret.GpusUnused
return count
}