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 61a8081  Fix `dashboard global` command cannot return json result (#49)
61a8081 is described below

commit 61a80813f45d5b10cfb4c25c0191e03f6497bf66
Author: Hoshea Jiang <[email protected]>
AuthorDate: Thu Jul 30 23:12:05 2020 +0800

    Fix `dashboard global` command cannot return json result (#49)
---
 .../dashboard/LabeledMetricsValues.graphql         |  4 +--
 commands/dashboard/global/global.go                | 12 ++++++-
 graphql/dashboard/global.go                        | 38 +++++++++++++++++++---
 3 files changed, 46 insertions(+), 8 deletions(-)

diff --git a/assets/graphqls/dashboard/LabeledMetricsValues.graphql 
b/assets/graphqls/dashboard/LabeledMetricsValues.graphql
index 9b8c2ed..25fa78e 100644
--- a/assets/graphqls/dashboard/LabeledMetricsValues.graphql
+++ b/assets/graphqls/dashboard/LabeledMetricsValues.graphql
@@ -15,8 +15,8 @@
 # specific language governing permissions and limitations
 # under the License.
 
-query ($condition: MetricsCondition!, $label: [String!]!, $duration: 
Duration!) {
-    result: readLabeledMetricsValues(condition: $condition, labels: $label, 
duration: $duration) {
+query ($condition: MetricsCondition!, $labels: [String!]!, $duration: 
Duration!) {
+    result: readLabeledMetricsValues(condition: $condition, labels: $labels, 
duration: $duration) {
         label
         values {
             values {
diff --git a/commands/dashboard/global/global.go 
b/commands/dashboard/global/global.go
index 2a1bc04..baa0226 100644
--- a/commands/dashboard/global/global.go
+++ b/commands/dashboard/global/global.go
@@ -36,7 +36,17 @@ var GlobalCommand = cli.Command{
        ShortName:   "g",
        Usage:       "Display global data",
        Description: "Display global data",
-       Flags:       flags.DurationFlags,
+       Flags: flags.Flags(
+               flags.DurationFlags,
+               []cli.Flag{
+                       cli.StringFlag{
+                               Name:     "template",
+                               Usage:    "load dashboard UI template",
+                               Required: false,
+                               Value:    dashboard.DefaultTemplatePath,
+                       },
+               },
+       ),
        Before: interceptor.BeforeChain([]cli.BeforeFunc{
                interceptor.TimezoneInterceptor,
                interceptor.DurationInterceptor,
diff --git a/graphql/dashboard/global.go b/graphql/dashboard/global.go
index eb8cf13..c0d2d8d 100644
--- a/graphql/dashboard/global.go
+++ b/graphql/dashboard/global.go
@@ -21,6 +21,7 @@ import (
        "encoding/json"
        "io/ioutil"
        "os"
+       "strings"
 
        "github.com/machinebox/graphql"
        "github.com/urfave/cli"
@@ -56,10 +57,18 @@ type GlobalData struct {
        HeatMap         schema.HeatMap             `json:"heatMap"`
 }
 
+// Use singleton pattern to make sure to load template only once.
+var globalTemplate *GlobalTemplate
+
 const DefaultTemplatePath = "templates/Dashboard.Global.json"
 
+// LoadTemplate reads UI template from file.
 func LoadTemplate(filename string) (*GlobalTemplate, error) {
-       var config GlobalTemplate
+       if globalTemplate != nil {
+               return globalTemplate, nil
+       }
+
+       var t GlobalTemplate
        var byteValue []byte
 
        if filename == DefaultTemplatePath {
@@ -78,20 +87,22 @@ func LoadTemplate(filename string) (*GlobalTemplate, error) 
{
                }
        }
 
-       if err := json.Unmarshal(byteValue, &config); err != nil {
+       if err := json.Unmarshal(byteValue, &t); err != nil {
                return nil, err
        }
-       return &config, nil
+       globalTemplate = &t
+       return globalTemplate, nil
 }
 
 func Metrics(ctx *cli.Context, duration schema.Duration) 
[][]*schema.SelectedRecord {
        var ret [][]*schema.SelectedRecord
-       configs, err := LoadTemplate(ctx.String("template"))
+
+       template, err := LoadTemplate(ctx.String("template"))
        if err != nil {
                return nil
        }
 
-       for _, m := range configs.Metrics {
+       for _, m := range template.Metrics {
                var response map[string][]*schema.SelectedRecord
                request := 
graphql.NewRequest(assets.Read("graphqls/dashboard/SortMetrics.graphql"))
                request.Var("condition", m.Condition)
@@ -107,8 +118,19 @@ func Metrics(ctx *cli.Context, duration schema.Duration) 
[][]*schema.SelectedRec
 func responseLatency(ctx *cli.Context, duration schema.Duration) 
[]*schema.MetricsValues {
        var response map[string][]*schema.MetricsValues
 
+       template, err := LoadTemplate(ctx.String("template"))
+       if err != nil {
+               return nil
+       }
+
+       // labels in the template file is string type,
+       // need to convert to string array for graphql query.
+       labels := strings.Split(template.ResponseLatency.Labels, ",")
+
        request := 
graphql.NewRequest(assets.Read("graphqls/dashboard/LabeledMetricsValues.graphql"))
        request.Var("duration", duration)
+       request.Var("condition", template.ResponseLatency.Condition)
+       request.Var("labels", labels)
 
        client.ExecuteQueryOrFail(ctx, request, &response)
 
@@ -118,8 +140,14 @@ func responseLatency(ctx *cli.Context, duration 
schema.Duration) []*schema.Metri
 func heatMap(ctx *cli.Context, duration schema.Duration) schema.HeatMap {
        var response map[string]schema.HeatMap
 
+       template, err := LoadTemplate(ctx.String("template"))
+       if err != nil {
+               return schema.HeatMap{}
+       }
+
        request := 
graphql.NewRequest(assets.Read("graphqls/dashboard/HeatMap.graphql"))
        request.Var("duration", duration)
+       request.Var("condition", template.HeatMap.Condition)
 
        client.ExecuteQueryOrFail(ctx, request, &response)
 

Reply via email to