未验证 提交 61a80813 编写于 作者: H Hoshea Jiang 提交者: GitHub

Fix `dashboard global` command cannot return json result (#49)

上级 6ae6ca04
......@@ -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 {
......
......@@ -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,
......
......@@ -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)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册