Skip to content

Commit

Permalink
Merge pull request #144 from mackerelio/add-dashboard-widget-fields
Browse files Browse the repository at this point in the history
Add ReferenceLines and FormatRules fields to Widget
  • Loading branch information
mechairoi authored May 16, 2023
2 parents c838963 + f31a8af commit b2e5f06
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 10 deletions.
33 changes: 23 additions & 10 deletions dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,19 @@ type Dashboard struct {

// Widget information
type Widget struct {
Type string `json:"type"`
Title string `json:"title"`
Layout Layout `json:"layout"`
Metric Metric `json:"metric,omitempty"`
Graph Graph `json:"graph,omitempty"`
Range Range `json:"range,omitempty"`
Markdown string `json:"markdown,omitempty"`
Type string `json:"type"`
Title string `json:"title"`
Layout Layout `json:"layout"`
Metric Metric `json:"metric,omitempty"`
Graph Graph `json:"graph,omitempty"`
Range Range `json:"range,omitempty"`
Markdown string `json:"markdown,omitempty"`
ReferenceLines []ReferenceLine `json:"referenceLines,omitempty"`
// If this field is nil, it will be treated as a two-digit display after the decimal point.
FractionSize *int64 `json:"fractionSize,omitempty"`
Suffix string `json:"suffix,omitempty"`
RoleFullName string `json:"roleFullname,omitempty"`
FractionSize *int64 `json:"fractionSize,omitempty"`
Suffix string `json:"suffix,omitempty"`
FormatRules []FormatRule `json:"formatRules,omitempty"`
RoleFullName string `json:"roleFullname,omitempty"`
}

// Metric information
Expand All @@ -133,6 +135,12 @@ func (m Metric) MarshalJSON() ([]byte, error) {
return json.Marshal(Alias(m))
}

type FormatRule struct {
Name string `json:"name"`
Threshold float64 `json:"threshold"`
Operator string `json:"operator"`
}

// Graph information
type Graph struct {
Type string `json:"type"`
Expand Down Expand Up @@ -190,6 +198,11 @@ func (r Range) MarshalJSON() ([]byte, error) {
}
}

type ReferenceLine struct {
Label string `json:"label"`
Value float64 `json:"value"`
}

// Layout information
type Layout struct {
X int64 `json:"x"`
Expand Down
33 changes: 33 additions & 0 deletions dashboards_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,25 @@ func TestFindDashboard(t *testing.T) {
"width": 8,
"height": 10,
},
"referenceLines": []map[string]interface{}{
{
"label": "critical",
"value": 1.23,
},
},
},
{
"type": "value",
"title": "value_widget",
"fractionSize": 2,
"suffix": "total",
"formatRules": []map[string]interface{}{
{
"name": "SLO",
"threshold": 2.34,
"operator": ">",
},
},
"metric": map[string]interface{}{
"type": "expression",
"expression": "alias(scale(\nsum(\n group(\n host(2u4PP3TJqbx,loadavg.*)\n )\n),\n1\n), 'test')",
Expand Down Expand Up @@ -218,6 +231,14 @@ func TestFindDashboard(t *testing.T) {
t.Error("request sends json including widgets.graph.name but:", dashboard)
}

if dashboard.Widgets[1].ReferenceLines[0].Label != "critical" {
t.Error("request sends json including widgets.graph.referenceLines.label but:", dashboard)
}

if dashboard.Widgets[1].ReferenceLines[0].Value != 1.23 {
t.Error("request sends json including widgets.graph.referenceLines.value but:", dashboard)
}

// Widget Test : Metric ( && Expression Type)

if dashboard.Widgets[2].Metric.Type != "expression" {
Expand All @@ -236,6 +257,18 @@ func TestFindDashboard(t *testing.T) {
t.Error("request sends json including widgets.suffix but:", dashboard)
}

if dashboard.Widgets[2].FormatRules[0].Name != "SLO" {
t.Error("request sends json including widgets.formatRules.name but:", dashboard)
}

if dashboard.Widgets[2].FormatRules[0].Threshold != 2.34 {
t.Error("request sends json including widgets.formatRules.threshold but:", dashboard)
}

if dashboard.Widgets[2].FormatRules[0].Operator != ">" {
t.Error("request sends json including widgets.formatRules.operator but:", dashboard)
}

// Widget Test : AlertStatus

if dashboard.Widgets[3].RoleFullName != "test:dashboard" {
Expand Down

0 comments on commit b2e5f06

Please sign in to comment.