fix(region): add eip and disk usage

This commit is contained in:
ioito
2021-12-16 16:34:40 +08:00
parent 35704864bc
commit be997f90df
3 changed files with 125 additions and 19 deletions
+31 -9
View File
@@ -1515,9 +1515,11 @@ func (self *SElasticip) DoChangeBandwidth(userCred mcclient.TokenCredential, ban
}
type EipUsage struct {
PublicIPCount int
EIPCount int
EIPUsedCount int
PublicIPCount int
PublicIpBandwidth int
EIPCount int
EipBandwidth int
EIPUsedCount int
}
func (u EipUsage) Total() int {
@@ -1540,11 +1542,22 @@ func (manager *SElasticipManager) usageQ(q *sqlchemy.SQuery, rangeObjs []db.ISta
func (manager *SElasticipManager) TotalCount(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, rangeObjs []db.IStandaloneModel, providers []string, brands []string, cloudEnv string) EipUsage {
usage := EipUsage{}
q1 := manager.Query().Equals("mode", api.EIP_MODE_INSTANCE_PUBLICIP)
q1sq := manager.Query().SubQuery()
q1 := q1sq.Query(
sqlchemy.COUNT("public_ip_count", q1sq.Field("id")),
sqlchemy.SUM("public_ip_bandwidth", q1sq.Field("bandwidth")),
).Equals("mode", api.EIP_MODE_INSTANCE_PUBLICIP)
q1 = manager.usageQ(q1, rangeObjs, providers, brands, cloudEnv)
q2 := manager.Query().Equals("mode", api.EIP_MODE_STANDALONE_EIP)
q2sq := manager.Query().SubQuery()
q2 := q2sq.Query(
sqlchemy.COUNT("eip_count", q2sq.Field("id")),
sqlchemy.SUM("eip_bandwidth", q2sq.Field("bandwidth")),
).Equals("mode", api.EIP_MODE_STANDALONE_EIP)
q2 = manager.usageQ(q2, rangeObjs, providers, brands, cloudEnv)
q3 := manager.Query().Equals("mode", api.EIP_MODE_STANDALONE_EIP).IsNotEmpty("associate_id")
q3sq := manager.Query().SubQuery()
q3 := q3sq.Query(
sqlchemy.COUNT("eip_used_count", q3sq.Field("id")),
).Equals("mode", api.EIP_MODE_STANDALONE_EIP).IsNotEmpty("associate_id")
q3 = manager.usageQ(q3, rangeObjs, providers, brands, cloudEnv)
switch scope {
case rbacutils.ScopeSystem:
@@ -1558,9 +1571,18 @@ func (manager *SElasticipManager) TotalCount(scope rbacutils.TRbacScope, ownerId
q2 = q2.Equals("tenant_id", ownerId.GetProjectId())
q3 = q3.Equals("tenant_id", ownerId.GetProjectId())
}
usage.PublicIPCount, _ = q1.CountWithError()
usage.EIPCount, _ = q2.CountWithError()
usage.EIPUsedCount, _ = q3.CountWithError()
err := q1.First(&usage)
if err != nil {
log.Errorf("q.First error: %v", err)
}
err = q2.First(&usage)
if err != nil {
log.Errorf("q.First error: %v", err)
}
err = q3.First(&usage)
if err != nil {
log.Errorf("q.First error: %v", err)
}
return usage
}
+55 -10
View File
@@ -1025,6 +1025,8 @@ func (manager *SStorageManager) totalCapacityQ(
storages.Field("capacity"),
storages.Field("reserved"),
storages.Field("cmtbound"),
storages.Field("storage_type"),
storages.Field("medium_type"),
stmt.Field("used_capacity"),
stmt.Field("used_count"),
stmt2.Field("failed_capacity"),
@@ -1070,6 +1072,8 @@ type StorageStat struct {
Capacity int
Reserved int
Cmtbound float32
StorageType string
MediumType string
UsedCapacity int
UsedCount int
FailedCapacity int
@@ -1091,6 +1095,15 @@ type StoragesCapacityStat struct {
CountAttached int
DetachedCapacity int64
CountDetached int
MediumeCapacity map[string]int64
StorageTypeCapacity map[string]int64
MediumeCapacityUsed map[string]int64
StorageTypeCapacityUsed map[string]int64
AttachedMediumeCapacity map[string]int64
AttachedStorageTypeCapacity map[string]int64
DetachedMediumeCapacity map[string]int64
DetachedStorageTypeCapacity map[string]int64
}
func (manager *SStorageManager) calculateCapacity(q *sqlchemy.SQuery) StoragesCapacityStat {
@@ -1110,33 +1123,65 @@ func (manager *SStorageManager) calculateCapacity(q *sqlchemy.SQuery) StoragesCa
atCount int = 0
dtCapa int64 = 0
dtCount int = 0
mCapa = map[string]int64{}
sCapa = map[string]int64{}
mFailed = map[string]int64{}
sFailed = map[string]int64{}
matCapa = map[string]int64{}
satCapa = map[string]int64{}
mdtCapa = map[string]int64{}
sdtCapa = map[string]int64{}
)
var add = func(m, s map[string]int64, mediumType, storageType string, capa int64) (map[string]int64, map[string]int64) {
_, ok := m[mediumType]
if !ok {
m[mediumType] = 0
}
m[mediumType] += capa
_, ok = s[storageType]
if !ok {
s[storageType] = 0
}
s[storageType] += capa
return m, s
}
for _, stat := range stats {
tCapa += int64(stat.Capacity - stat.Reserved)
if stat.Cmtbound == 0 {
stat.Cmtbound = options.Options.DefaultStorageOvercommitBound
}
mCapa, sCapa = add(mCapa, sCapa, stat.MediumType, stat.StorageType, int64(stat.Capacity-stat.Reserved))
tVCapa += float64(stat.Capacity-stat.Reserved) * float64(stat.Cmtbound)
tUsed += int64(stat.UsedCapacity)
cUsed += stat.UsedCount
tFailed += int64(stat.FailedCapacity)
mFailed, sFailed = add(mFailed, sFailed, stat.MediumType, stat.StorageType, int64(stat.FailedCapacity))
cFailed += stat.FailedCount
atCapa += int64(stat.AttachedUsedCapacity)
matCapa, satCapa = add(matCapa, satCapa, stat.MediumType, stat.StorageType, int64(stat.AttachedUsedCapacity))
atCount += stat.AttachedCount
dtCapa += int64(stat.DetachedUsedCapacity)
mdtCapa, sdtCapa = add(mdtCapa, sdtCapa, stat.MediumType, stat.StorageType, int64(stat.DetachedUsedCapacity))
dtCount += stat.DetachedCount
}
return StoragesCapacityStat{
Capacity: tCapa,
CapacityVirtual: tVCapa,
CapacityUsed: tUsed,
CountUsed: cUsed,
CapacityUnready: tFailed,
CountUnready: cFailed,
AttachedCapacity: atCapa,
CountAttached: atCount,
DetachedCapacity: dtCapa,
CountDetached: dtCount,
Capacity: tCapa,
MediumeCapacity: mCapa,
StorageTypeCapacity: sCapa,
CapacityVirtual: tVCapa,
CapacityUsed: tUsed,
CountUsed: cUsed,
CapacityUnready: tFailed,
CountUnready: cFailed,
AttachedCapacity: atCapa,
AttachedMediumeCapacity: matCapa,
AttachedStorageTypeCapacity: satCapa,
CountAttached: atCount,
DetachedCapacity: dtCapa,
DetachedMediumeCapacity: mdtCapa,
DetachedStorageTypeCapacity: sdtCapa,
CountDetached: dtCount,
}
}
+39
View File
@@ -610,14 +610,32 @@ func StorageUsage(
true,
)
count[sPrefix] = result.Capacity
for s, capa := range result.StorageTypeCapacity {
count[fmt.Sprintf("%s.storage_type.%s", sPrefix, s)] = capa
}
for m, capa := range result.MediumeCapacity {
count[fmt.Sprintf("%s.medium_type.%s", sPrefix, m)] = capa
}
count[fmt.Sprintf("%s.virtual", sPrefix)] = result.CapacityVirtual
count[fmt.Sprintf("%s.owner", dPrefix)] = result.CapacityUsed
count[fmt.Sprintf("%s.count.owner", dPrefix)] = result.CountUsed
count[fmt.Sprintf("%s.unready.owner", dPrefix)] = result.CapacityUnready
count[fmt.Sprintf("%s.unready.count.owner", dPrefix)] = result.CountUnready
count[fmt.Sprintf("%s.attached.owner", dPrefix)] = result.AttachedCapacity
for s, capa := range result.AttachedStorageTypeCapacity {
count[fmt.Sprintf("%s.attached.owner.storage_type.%s", dPrefix, s)] = capa
}
for m, capa := range result.AttachedMediumeCapacity {
count[fmt.Sprintf("%s.attached.owner.medium_type.%s", dPrefix, m)] = capa
}
count[fmt.Sprintf("%s.attached.count.owner", dPrefix)] = result.CountAttached
count[fmt.Sprintf("%s.detached.owner", dPrefix)] = result.DetachedCapacity
for s, capa := range result.DetachedStorageTypeCapacity {
count[fmt.Sprintf("%s.detached.owner.storage_type.%s", dPrefix, s)] = capa
}
for m, capa := range result.DetachedMediumeCapacity {
count[fmt.Sprintf("%s.detached.owner.medium_type.%s", dPrefix, m)] = capa
}
count[fmt.Sprintf("%s.detached.count.owner", dPrefix)] = result.CountDetached
count[fmt.Sprintf("%s.mounted.owner", dPrefix)] = result.AttachedCapacity
count[fmt.Sprintf("%s.mounted.count.owner", dPrefix)] = result.CountAttached
@@ -640,12 +658,30 @@ func StorageUsage(
)
count[fmt.Sprintf("%s", dPrefix)] = result.CapacityUsed
for s, capa := range result.StorageTypeCapacity {
count[fmt.Sprintf("%s.storage_type.%s", dPrefix, s)] = capa
}
for m, capa := range result.MediumeCapacity {
count[fmt.Sprintf("%s.medium_type.%s", dPrefix, m)] = capa
}
count[fmt.Sprintf("%s.count", dPrefix)] = result.CountUsed
count[fmt.Sprintf("%s.unready", dPrefix)] = result.CapacityUnready
count[fmt.Sprintf("%s.unready.count", dPrefix)] = result.CountUnready
count[fmt.Sprintf("%s.attached", dPrefix)] = result.AttachedCapacity
for s, capa := range result.AttachedStorageTypeCapacity {
count[fmt.Sprintf("%s.attached.storage_type.%s", dPrefix, s)] = capa
}
for m, capa := range result.AttachedMediumeCapacity {
count[fmt.Sprintf("%s.attached.medium_type.%s", dPrefix, m)] = capa
}
count[fmt.Sprintf("%s.attached.count", dPrefix)] = result.CountAttached
count[fmt.Sprintf("%s.detached", dPrefix)] = result.DetachedCapacity
for s, capa := range result.DetachedStorageTypeCapacity {
count[fmt.Sprintf("%s.detached.storage_type.%s", dPrefix, s)] = capa
}
for m, capa := range result.DetachedMediumeCapacity {
count[fmt.Sprintf("%s.detached.medium_type.%s", dPrefix, m)] = capa
}
count[fmt.Sprintf("%s.detached.count", dPrefix)] = result.CountDetached
count[fmt.Sprintf("%s.mounted", dPrefix)] = result.AttachedCapacity
count[fmt.Sprintf("%s.mounted.count", dPrefix)] = result.CountAttached
@@ -924,6 +960,9 @@ func EipUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, ra
count := make(map[string]interface{})
count[getKey(scope, "eip")] = eipUsage.Total()
count[getKey(scope, "eip.public_ip")] = eipUsage.PublicIPCount
count[getKey(scope, "eip.public_ip.bandwidth.mb")] = eipUsage.PublicIpBandwidth
count[getKey(scope, "eip.floating_ip")] = eipUsage.EIPCount
count[getKey(scope, "eip.floating_ip.bandwidth.mb")] = eipUsage.EipBandwidth
count[getKey(scope, "eip.floating_ip")] = eipUsage.EIPCount
count[getKey(scope, "eip.floating_ip.used")] = eipUsage.EIPUsedCount
count[getKey(scope, "eip.used")] = eipUsage.EIPUsedCount + eipUsage.PublicIPCount