From de68df439d559f0c87943986e52163aa8b332aed Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Tue, 18 Jun 2019 18:14:41 +0800 Subject: [PATCH] fix: add lb and group ip address usage --- cmd/climc/shell/usages.go | 4 ++++ pkg/compute/models/wires.go | 27 ++++++++++++++++++++++++++- pkg/compute/usages/handler.go | 4 ++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/cmd/climc/shell/usages.go b/cmd/climc/shell/usages.go index 099e5a5056..9cc35e7a75 100644 --- a/cmd/climc/shell/usages.go +++ b/cmd/climc/shell/usages.go @@ -142,6 +142,7 @@ func init() { type ImageUsageOptions struct { Project string `help:"check image usage of a project"` Domain string `help:"check image usage of a domain"` + Scope string `help:"query scope" choices:"project|domain|system"` } R(&ImageUsageOptions{}, "image-usage", "Show general usage of images", func(s *mcclient.ClientSession, args *ImageUsageOptions) error { params := jsonutils.NewDict() @@ -150,6 +151,9 @@ func init() { } else if args.Domain != "" { params.Add(jsonutils.NewString(args.Domain), "domain") } + if args.Scope != "" { + params.Add(jsonutils.NewString(args.Scope), "scope") + } result, err := modules.Images.GetUsage(s, params) if err != nil { return err diff --git a/pkg/compute/models/wires.go b/pkg/compute/models/wires.go index c655e07290..9c11594bc3 100644 --- a/pkg/compute/models/wires.go +++ b/pkg/compute/models/wires.go @@ -361,6 +361,18 @@ func (manager *SWireManager) totalCountQ(rangeObj db.IStandaloneModel, hostTypes sqlchemy.COUNT("rnic_count"), ) + groupNics := GroupnetworkManager.Query().SubQuery() + grpNicSQ := groupNics.Query( + groupNics.Field("network_id"), + sqlchemy.COUNT("grpnic_count"), + ).GroupBy(groupNics.Field("network_id")).SubQuery() + + lbNics := LoadbalancernetworkManager.Query().SubQuery() + lbNicSQ := lbNics.Query( + lbNics.Field("network_id"), + sqlchemy.COUNT("lbnic_count"), + ).GroupBy(lbNics.Field("network_id")).SubQuery() + gNicSQ := gNicQ.GroupBy(gNics.Field("network_id")).SubQuery() hNicSQ := hNicQ.GroupBy(hNics.Field("network_id")).SubQuery() revSQ := revQ.GroupBy(revIps.Field("network_id")).SubQuery() @@ -371,10 +383,15 @@ func (manager *SWireManager) totalCountQ(rangeObj db.IStandaloneModel, hostTypes sqlchemy.COUNT("id").Label("net_count"), sqlchemy.SUM("gnic_count", gNicQ.Field("gnic_count")), sqlchemy.SUM("hnic_count", hNicQ.Field("hnic_count")), - sqlchemy.SUM("rev_count", revQ.Field("rnic_count"))) + sqlchemy.SUM("rev_count", revQ.Field("rnic_count")), + sqlchemy.SUM("grpnic_count", grpNicSQ.Field("grpnic_count")), + sqlchemy.SUM("lbnic_count", lbNicSQ.Field("lbnic_count")), + ) netQ = netQ.LeftJoin(gNicSQ, sqlchemy.Equals(gNicSQ.Field("network_id"), networks.Field("id"))) netQ = netQ.LeftJoin(hNicSQ, sqlchemy.Equals(hNicSQ.Field("network_id"), networks.Field("id"))) netQ = netQ.LeftJoin(revSQ, sqlchemy.Equals(revSQ.Field("network_id"), networks.Field("id"))) + netQ = netQ.LeftJoin(grpNicSQ, sqlchemy.Equals(grpNicSQ.Field("network_id"), networks.Field("id"))) + netQ = netQ.LeftJoin(lbNicSQ, sqlchemy.Equals(lbNicSQ.Field("network_id"), networks.Field("id"))) netQ = netQ.GroupBy(networks.Field("wire_id")) netSQ := netQ.SubQuery() @@ -385,6 +402,8 @@ func (manager *SWireManager) totalCountQ(rangeObj db.IStandaloneModel, hostTypes sqlchemy.SUM("guest_nic_count", netSQ.Field("gnic_count")), sqlchemy.SUM("host_nic_count", netSQ.Field("hnic_count")), sqlchemy.SUM("reserved_count", netSQ.Field("rev_count")), + sqlchemy.SUM("group_nic_count", netSQ.Field("grpnic_count")), + sqlchemy.SUM("lb_nic_count", netSQ.Field("lbnic_count")), ) q = q.LeftJoin(netSQ, sqlchemy.Equals(wires.Field("id"), netSQ.Field("wire_id"))) @@ -434,6 +453,12 @@ type WiresCountStat struct { GuestNicCount int HostNicCount int ReservedCount int + GroupNicCount int + LbNicCount int +} + +func (wstat WiresCountStat) NicCount() int { + return wstat.GuestNicCount + wstat.HostNicCount + wstat.ReservedCount + wstat.GroupNicCount + wstat.LbNicCount } func (manager *SWireManager) TotalCount(rangeObj db.IStandaloneModel, hostTypes []string, providers []string, cloudEnv string) WiresCountStat { diff --git a/pkg/compute/usages/handler.go b/pkg/compute/usages/handler.go index 6e85a22fdb..949ec1e669 100644 --- a/pkg/compute/usages/handler.go +++ b/pkg/compute/usages/handler.go @@ -461,6 +461,9 @@ func WireUsage(rangeObj db.IStandaloneModel, hostTypes []string, providers []str count["all.nics.guest"] = result.GuestNicCount count["all.nics.host"] = result.HostNicCount count["all.nics.reserve"] = result.ReservedCount + count["all.nics.group"] = result.GroupNicCount + count["all.nics.lb"] = result.LbNicCount + count["all.nics"] = result.NicCount() return count } @@ -606,6 +609,7 @@ func EipUsage(scope rbacutils.TRbacScope, ownerId mcclient.IIdentityProvider, ra count[getKey(projectId, "eip.public_ip")] = eipUsage.PublicIPCount count[getKey(projectId, "eip.floating_ip")] = eipUsage.EIPCount count[getKey(projectId, "eip.floating_ip.used")] = eipUsage.EIPUsedCount + count[getKey(projectId, "eip.used")] = eipUsage.EIPUsedCount + eipUsage.PublicIPCount return count }