Skip to content

Commit

Permalink
include system usage
Browse files Browse the repository at this point in the history
  • Loading branch information
wanyaoqi committed Jun 28, 2020
1 parent 561465e commit 89ec83f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 176 deletions.
39 changes: 3 additions & 36 deletions pkg/compute/models/guests.go
Original file line number Diff line number Diff line change
Expand Up @@ -2539,10 +2539,10 @@ func (manager *SGuestManager) TotalCount(
ownerId mcclient.IIdentityProvider,
rangeObjs []db.IStandaloneModel,
status []string, hypervisors []string,
isSystem bool, pendingDelete bool,
includeSystem bool, pendingDelete bool,
hostTypes []string, resourceTypes []string, providers []string, brands []string, cloudEnv string,
) SGuestCountStat {
return usageTotalGuestResouceCount(scope, ownerId, rangeObjs, status, hypervisors, isSystem, pendingDelete, hostTypes, resourceTypes, providers, brands, cloudEnv)
return usageTotalGuestResouceCount(scope, ownerId, rangeObjs, status, hypervisors, includeSystem, pendingDelete, hostTypes, resourceTypes, providers, brands, cloudEnv)
}

func (self *SGuest) detachNetworks(ctx context.Context, userCred mcclient.TokenCredential, gns []SGuestnetwork, reserve bool, deploy bool) error {
Expand Down Expand Up @@ -3025,39 +3025,6 @@ type SGuestCountStat struct {
}

func usageTotalGuestResouceCount(
scope rbacutils.TRbacScope,
ownerId mcclient.IIdentityProvider,
rangeObjs []db.IStandaloneModel,
status []string,
hypervisors []string,
isSystem bool,
pendingDelete bool,
hostTypes []string,
resourceTypes []string,
providers []string, brands []string, cloudEnv string,
) SGuestCountStat {
q, guests := _guestResourceCountQuery(scope, ownerId, rangeObjs, status, hypervisors,
pendingDelete, hostTypes, resourceTypes, providers, brands, cloudEnv)
if isSystem {
q = q.IsTrue("is_system")
} else {
q = q.Filter(sqlchemy.OR(
sqlchemy.IsNull(guests.Field("is_system")), sqlchemy.IsFalse(guests.Field("is_system"))))
}

stat := SGuestCountStat{}
row := q.Row()
err := q.Row2Struct(row, &stat)
if err != nil {
log.Errorf("%s", err)
}
stat.TotalCpuCount += stat.TotalBackupCpuCount
stat.TotalMemSize += stat.TotalBackupMemSize
stat.TotalDiskSize += stat.TotalBackupDiskSize
return stat
}

func totalGuestResourceCount(
scope rbacutils.TRbacScope,
ownerId mcclient.IIdentityProvider,
rangeObjs []db.IStandaloneModel,
Expand All @@ -3071,11 +3038,11 @@ func totalGuestResourceCount(
) SGuestCountStat {
q, guests := _guestResourceCountQuery(scope, ownerId, rangeObjs, status, hypervisors,
pendingDelete, hostTypes, resourceTypes, providers, brands, cloudEnv)

if !includeSystem {
q = q.Filter(sqlchemy.OR(
sqlchemy.IsNull(guests.Field("is_system")), sqlchemy.IsFalse(guests.Field("is_system"))))
}

stat := SGuestCountStat{}
row := q.Row()
err := q.Row2Struct(row, &stat)
Expand Down
2 changes: 1 addition & 1 deletion pkg/compute/models/quotas.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (self *SQuota) FetchUsage(ctx context.Context) error {

diskSize := totalDiskSize(scope, ownerId, tristate.None, tristate.None, false, false, rangeObjs, providers, brands, keys.CloudEnv, hypervisors)

guest := totalGuestResourceCount(scope, ownerId, rangeObjs, nil, hypervisors, false, false, nil, nil, providers, brands, keys.CloudEnv)
guest := usageTotalGuestResouceCount(scope, ownerId, rangeObjs, nil, hypervisors, false, false, nil, nil, providers, brands, keys.CloudEnv)

self.Count = guest.TotalGuestCount
self.Cpu = guest.TotalCpuCount
Expand Down
40 changes: 19 additions & 21 deletions pkg/compute/models/storages.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ type StorageCapacityStat struct {
TotalSizeVirtual float64
}

func filterDisksByScope(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, pendingDeleted bool, isSystem bool) *sqlchemy.SSubQuery {
func filterDisksByScope(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, pendingDeleted bool, includeSystem bool) *sqlchemy.SSubQuery {
q := DiskManager.Query()
switch scope {
case rbacutils.ScopeSystem:
Expand All @@ -848,16 +848,14 @@ func filterDisksByScope(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityPr
} else {
q = q.IsFalse("pending_deleted")
}
if isSystem {
q = q.IsTrue("is_system")
} else {
if !includeSystem {
q = q.IsFalse("is_system")
}
return q.SubQuery()
}

func (manager *SStorageManager) disksReadyQ(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, pendingDeleted bool, isSystem bool) *sqlchemy.SSubQuery {
disks := filterDisksByScope(scope, ownerId, pendingDeleted, isSystem)
func (manager *SStorageManager) disksReadyQ(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, pendingDeleted bool, includeSystem bool) *sqlchemy.SSubQuery {
disks := filterDisksByScope(scope, ownerId, pendingDeleted, includeSystem)
q := disks.Query(
disks.Field("storage_id"),
sqlchemy.SUM("used_capacity", disks.Field("disk_size")),
Expand All @@ -867,7 +865,7 @@ func (manager *SStorageManager) disksReadyQ(scope rbacutils.TRbacScope, ownerId
return q.SubQuery()
}

func (manager *SStorageManager) diskIsAttachedQ(isAttached bool, scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, pendingDeleted bool, isSystem bool) *sqlchemy.SSubQuery {
func (manager *SStorageManager) diskIsAttachedQ(isAttached bool, scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, pendingDeleted bool, includeSystem bool) *sqlchemy.SSubQuery {
sumKey := "attached_used_capacity"
countKey := "attached_count"
cond := sqlchemy.In
Expand All @@ -877,7 +875,7 @@ func (manager *SStorageManager) diskIsAttachedQ(isAttached bool, scope rbacutils
cond = sqlchemy.NotIn
}
sq := GuestdiskManager.Query("disk_id").SubQuery()
disks := filterDisksByScope(scope, ownerId, pendingDeleted, isSystem)
disks := filterDisksByScope(scope, ownerId, pendingDeleted, includeSystem)
disks = disks.Query().Filter(cond(disks.Field("id"), sq)).SubQuery()
q := disks.Query(
disks.Field("storage_id"),
Expand All @@ -887,16 +885,16 @@ func (manager *SStorageManager) diskIsAttachedQ(isAttached bool, scope rbacutils
return q.SubQuery()
}

func (manager *SStorageManager) diskAttachedQ(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, pendingDeleted bool, isSystem bool) *sqlchemy.SSubQuery {
return manager.diskIsAttachedQ(true, scope, ownerId, pendingDeleted, isSystem)
func (manager *SStorageManager) diskAttachedQ(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, pendingDeleted bool, includeSystem bool) *sqlchemy.SSubQuery {
return manager.diskIsAttachedQ(true, scope, ownerId, pendingDeleted, includeSystem)
}

func (manager *SStorageManager) diskDetachedQ(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, pendingDeleted bool, isSystem bool) *sqlchemy.SSubQuery {
return manager.diskIsAttachedQ(false, scope, ownerId, pendingDeleted, isSystem)
func (manager *SStorageManager) diskDetachedQ(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, pendingDeleted bool, includeSystem bool) *sqlchemy.SSubQuery {
return manager.diskIsAttachedQ(false, scope, ownerId, pendingDeleted, includeSystem)
}

func (manager *SStorageManager) disksFailedQ(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, pendingDeleted bool, isSystem bool) *sqlchemy.SSubQuery {
disks := filterDisksByScope(scope, ownerId, pendingDeleted, isSystem)
func (manager *SStorageManager) disksFailedQ(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, pendingDeleted bool, includeSystem bool) *sqlchemy.SSubQuery {
disks := filterDisksByScope(scope, ownerId, pendingDeleted, includeSystem)
q := disks.Query(
disks.Field("storage_id"),
sqlchemy.SUM("failed_capacity", disks.Field("disk_size")),
Expand All @@ -911,13 +909,13 @@ func (manager *SStorageManager) totalCapacityQ(
resourceTypes []string,
providers []string, brands []string, cloudEnv string,
scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider,
pendingDeleted bool, isSystem bool,
pendingDeleted bool, includeSystem bool,
storageOwnership bool,
) *sqlchemy.SQuery {
stmt := manager.disksReadyQ(scope, ownerId, pendingDeleted, isSystem)
stmt2 := manager.disksFailedQ(scope, ownerId, pendingDeleted, isSystem)
attachedDisks := manager.diskAttachedQ(scope, ownerId, pendingDeleted, isSystem)
detachedDisks := manager.diskDetachedQ(scope, ownerId, pendingDeleted, isSystem)
stmt := manager.disksReadyQ(scope, ownerId, pendingDeleted, includeSystem)
stmt2 := manager.disksFailedQ(scope, ownerId, pendingDeleted, includeSystem)
attachedDisks := manager.diskAttachedQ(scope, ownerId, pendingDeleted, includeSystem)
detachedDisks := manager.diskDetachedQ(scope, ownerId, pendingDeleted, includeSystem)
storages := manager.Query().SubQuery()
q := storages.Query(
storages.Field("capacity"),
Expand Down Expand Up @@ -1047,7 +1045,7 @@ func (manager *SStorageManager) TotalCapacity(
providers []string, brands []string, cloudEnv string,
scope rbacutils.TRbacScope,
ownerId mcclient.IIdentityProvider,
pendingDeleted bool, isSystem bool,
pendingDeleted bool, includeSystem bool,
storageOwnership bool,
) StoragesCapacityStat {
res1 := manager.calculateCapacity(
Expand All @@ -1057,7 +1055,7 @@ func (manager *SStorageManager) TotalCapacity(
resourceTypes,
providers, brands, cloudEnv,
scope, ownerId,
pendingDeleted, isSystem,
pendingDeleted, includeSystem,
storageOwnership,
),
)
Expand Down

0 comments on commit 89ec83f

Please sign in to comment.