From d1455412041e1fa4cd868fef8c37b68a1b9910eb Mon Sep 17 00:00:00 2001 From: CMGS Date: Wed, 9 Sep 2026 17:48:08 +0900 Subject: [PATCH] review: simplify and reuse lenses over the batch ToResourcePercent divides through core's AdvancedDivide instead of a local twin; the node status command keeps its one-shot and periodic paths apart; nodePercentOf is the one place a node's percentages are computed, so the stream path no longer builds a one-element slice per node. --- cmd/node/status.go | 5 ++--- describe/node.go | 23 +++++++++++++---------- describe/utils.go | 16 +++++----------- 3 files changed, 20 insertions(+), 24 deletions(-) diff --git a/cmd/node/status.go b/cmd/node/status.go index d3fd601..edbda19 100644 --- a/cmd/node/status.go +++ b/cmd/node/status.go @@ -21,13 +21,12 @@ type setNodeStatusOptions struct { } func (o *setNodeStatusOptions) run(ctx context.Context) error { - err := o.heartbeat(ctx) if o.interval == 0 { - return err + return o.heartbeat(ctx) } logger := log.WithFunc("node.setNodeStatusOptions.run") - logger.Error(ctx, err, "heartbeat") + logger.Error(ctx, o.heartbeat(ctx), "heartbeat") ticker := time.NewTicker(time.Duration(o.interval) * time.Second) defer ticker.Stop() diff --git a/describe/node.go b/describe/node.go index 1e29d33..721c66b 100644 --- a/describe/node.go +++ b/describe/node.go @@ -89,28 +89,31 @@ type nodePercent struct { } func nodePercents(ctx context.Context, resources ...*corepb.NodeResource) []nodePercent { - logger := log.WithFunc("describe.nodePercents") rv := make([]nodePercent, 0, len(resources)) for _, resource := range resources { - cr, sr, err := ToResourcePercent(resource) - if err != nil { - logger.Errorf(ctx, err, "resource percent of node %s", resource.Name) - continue + if percent, ok := nodePercentOf(ctx, resource); ok { + rv = append(rv, percent) } - rv = append(rv, nodePercent{resource, cr, sr}) } return rv } +func nodePercentOf(ctx context.Context, resource *corepb.NodeResource) (nodePercent, bool) { + cr, sr, err := ToResourcePercent(resource) + if err != nil { + log.WithFunc("describe.nodePercentOf").Errorf(ctx, err, "resource percent of node %s", resource.Name) + return nodePercent{}, false + } + return nodePercent{resource, cr, sr}, true +} + func nodePercentChan(ctx context.Context, resources <-chan *corepb.NodeResource, keep NodeResourceFilter) <-chan nodePercent { rv := make(chan nodePercent) go func() { defer close(rv) for resource := range resources { - for _, percent := range nodePercents(ctx, resource) { - if keep == nil || keep(percent.cpumem, percent.storage) { - rv <- percent - } + if percent, ok := nodePercentOf(ctx, resource); ok && (keep == nil || keep(percent.cpumem, percent.storage)) { + rv <- percent } } }() diff --git a/describe/utils.go b/describe/utils.go index e2d8f93..13efaac 100644 --- a/describe/utils.go +++ b/describe/utils.go @@ -11,6 +11,7 @@ import ( "github.com/jedib0t/go-pretty/v6/table" resourcetypes "github.com/projecteru2/core/resource/types" corepb "github.com/projecteru2/core/rpc/gen" + coreutils "github.com/projecteru2/core/utils" "sigs.k8s.io/yaml" "github.com/projecteru2/cli/cmd/utils" @@ -37,23 +38,16 @@ func ToResourcePercent(resource *corepb.NodeResource) (cpumem, storage map[strin storageCap := resCap[utils.ResourceStorage] cr, sr := map[string]float64{}, map[string]float64{} if cpumemUsage != nil && cpumemCap != nil { - cr["cpu"] = ratio(cpumemUsage.Float64("cpu"), cpumemCap.Float64("cpu")) - cr["memory"] = ratio(cpumemUsage.Float64("memory"), cpumemCap.Float64("memory")) + cr["cpu"] = coreutils.AdvancedDivide(cpumemUsage.Float64("cpu"), cpumemCap.Float64("cpu")) + cr["memory"] = coreutils.AdvancedDivide(cpumemUsage.Float64("memory"), cpumemCap.Float64("memory")) } if storageUsage != nil && storageCap != nil { - sr["storage"] = ratio(storageUsage.Float64("storage"), storageCap.Float64("storage")) - sr["volumes"] = ratio(sumParams(storageUsage.RawParams("volumes")), sumParams(storageCap.RawParams("volumes"))) + sr["storage"] = coreutils.AdvancedDivide(storageUsage.Float64("storage"), storageCap.Float64("storage")) + sr["volumes"] = coreutils.AdvancedDivide(sumParams(storageUsage.RawParams("volumes")), sumParams(storageCap.RawParams("volumes"))) } return cr, sr, nil } -func ratio(usage, capacity float64) float64 { - if capacity == 0 { - return 0 - } - return usage / capacity -} - func sumParams(params resourcetypes.RawParams) float64 { sum := 0.0 for key := range params {