This is an automated email from the ASF dual-hosted git repository.

kezhenxu94 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-cli.git


The following commit(s) were added to refs/heads/master by this push:
     new d06d1eb  fix `metrics top` can't infer the scope automatically (#94)
d06d1eb is described below

commit d06d1ebf565756885650061ddd2e1fe527417052
Author: Hoshea Jiang <[email protected]>
AuthorDate: Thu Apr 29 09:37:11 2021 +0800

    fix `metrics top` can't infer the scope automatically (#94)
---
 internal/commands/metrics/aggregation/topn.go |  7 ++++++-
 pkg/graphql/utils/parser.go                   | 20 +++++++++++++++++++-
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/internal/commands/metrics/aggregation/topn.go 
b/internal/commands/metrics/aggregation/topn.go
index 8d47c63..5092299 100644
--- a/internal/commands/metrics/aggregation/topn.go
+++ b/internal/commands/metrics/aggregation/topn.go
@@ -32,6 +32,7 @@ import (
        "github.com/apache/skywalking-cli/pkg/display"
        "github.com/apache/skywalking-cli/pkg/display/displayable"
        "github.com/apache/skywalking-cli/pkg/graphql/metrics"
+       "github.com/apache/skywalking-cli/pkg/graphql/utils"
 )
 
 var TopN = cli.Command{
@@ -64,7 +65,7 @@ var TopN = cli.Command{
 
                metricsName := ctx.String("name")
                normal := ctx.BoolT("isNormal")
-               scope := ctx.Generic("scope").(*model.ScopeEnumValue).Selected
+               scope := utils.ParseScopeInTop(metricsName)
                order := ctx.Generic("order").(*model.OrderEnumValue).Selected
                topN := 5
                parentService := ctx.String("service")
@@ -83,6 +84,10 @@ var TopN = cli.Command{
                        Step:  step,
                }
 
+               if ctx.Bool("debug") {
+                       logger.Log.Debugln(metricsName, scope, topN)
+               }
+
                metricsValues, err := metrics.SortMetrics(ctx, 
api.TopNCondition{
                        Name:          metricsName,
                        ParentService: &parentService,
diff --git a/pkg/graphql/utils/parser.go b/pkg/graphql/utils/parser.go
index cf5ece9..037cedd 100644
--- a/pkg/graphql/utils/parser.go
+++ b/pkg/graphql/utils/parser.go
@@ -17,7 +17,11 @@
 
 package utils
 
-import api "skywalking.apache.org/repo/goapi/query"
+import (
+       api "skywalking.apache.org/repo/goapi/query"
+
+       "strings"
+)
 
 // ParseScope defines the scope based on the input parameters.
 func ParseScope(entity *api.Entity) api.Scope {
@@ -39,3 +43,17 @@ func ParseScope(entity *api.Entity) api.Scope {
 
        return scope
 }
+
+// ParseScopeInTop defines the scope based on the metrics' name.
+// The scope can only be `Service` or `ServiceInstance` or `Endpoint`.
+func ParseScopeInTop(metricsName string) api.Scope {
+       scope := api.ScopeService
+
+       if strings.HasPrefix(metricsName, "service_instance") {
+               scope = api.ScopeServiceInstance
+       } else if strings.HasPrefix(metricsName, "endpoint") {
+               scope = api.ScopeEndpoint
+       }
+
+       return scope
+}

Reply via email to