diff options
| author | Paul Buetow <paul@buetow.org> | 2026-05-24 00:19:56 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-05-24 00:19:56 +0300 |
| commit | a3acc6f607b4422b83e023c030892ff39c0cb0aa (patch) | |
| tree | ae57098f63faa5368e35ce8fedf4c77457c55e54 | |
| parent | 7e1c99cc79ba8338a05e37aaac498dd759f614f3 (diff) | |
rpn: implement custom show to display custom metric details
Previously, 'custom show' delegated to MetricShow(), which displayed
metric info for the top stack value — identical to 'metric show' and
making 'custom show' a pointless alias.
Now 'custom show' lists all custom metrics with name, category, base
unit, and factor. Optionally accepts a metric name argument to show
details for a specific custom metric only.
- Add CustomShow(stack, name) method to Operator interface
- Implement CustomShow on Operations with per-metric detail output
- Update handleCustomCommand to call CustomShow instead of MetricShow
| -rw-r--r-- | internal/rpn/operations_interfaces.go | 1 | ||||
| -rw-r--r-- | internal/rpn/operations_metric_cmd.go | 27 | ||||
| -rw-r--r-- | internal/rpn/rpn_parse.go | 6 |
3 files changed, 33 insertions, 1 deletions
diff --git a/internal/rpn/operations_interfaces.go b/internal/rpn/operations_interfaces.go index 002ec33..6af0973 100644 --- a/internal/rpn/operations_interfaces.go +++ b/internal/rpn/operations_interfaces.go @@ -98,6 +98,7 @@ type Operator interface { MetricCategory(stack *Stack, categoryName string) (string, error) MetricCompatible(stack *Stack) (string, error) // Custom metric commands + CustomShow(stack *Stack, name string) (string, error) CustomList(stack *Stack) (string, error) CustomDefine(name string, factor float64, category string) error CustomUndefine(name string) error diff --git a/internal/rpn/operations_metric_cmd.go b/internal/rpn/operations_metric_cmd.go index 1915507..61e3f82 100644 --- a/internal/rpn/operations_metric_cmd.go +++ b/internal/rpn/operations_metric_cmd.go @@ -93,6 +93,33 @@ func parseCategory(name string) (Category, bool) { return 0, false } +// CustomShow returns detailed info for custom metrics. +// If name is empty, shows all custom metrics; otherwise shows the named one. +func (o *Operations) CustomShow(stack *Stack, name string) (string, error) { + reg := o.metricRegistry + if name != "" { + m, ok := reg.Find(name) + if !ok { + return "", fmt.Errorf("unknown custom metric %q", name) + } + if !m.IsCustom { + return "", fmt.Errorf("metric %q is not a custom metric", name) + } + factor := m.Factor(o.GetPrefixMode()) + return fmt.Sprintf("%s, category: %s, base: %s, factor: %.10g", m.Name, m.Category, m.BaseUnit, factor), nil + } + metrics := reg.ListByCategory(Custom) + if len(metrics) == 0 { + return "no custom metrics defined", nil + } + var lines []string + for _, m := range metrics { + factor := m.Factor(o.GetPrefixMode()) + lines = append(lines, fmt.Sprintf(" %s, category: %s, base: %s, factor: %.10g", m.Name, m.Category, m.BaseUnit, factor)) + } + return strings.Join(lines, "\n"), nil +} + // CustomList returns all custom metric names. func (o *Operations) CustomList(stack *Stack) (string, error) { reg := o.metricRegistry diff --git a/internal/rpn/rpn_parse.go b/internal/rpn/rpn_parse.go index 615e804..9906f98 100644 --- a/internal/rpn/rpn_parse.go +++ b/internal/rpn/rpn_parse.go @@ -575,7 +575,11 @@ func (r *RPN) handleCustomCommand(stack *Stack, tokens []string, i int) (string, subCmd := tokens[i+1] switch subCmd { case "show": - result, err := r.ops.MetricShow(stack) + name := "" + if i+2 < len(tokens) { + name = tokens[i+2] + } + result, err := r.ops.CustomShow(stack, name) if err != nil { return "", true, fmt.Errorf("rpn: custom show: %w", err) } |
