Skip to content

Commit

Permalink
feat: percentage panels should clamp min/max to 0/100%
Browse files Browse the repository at this point in the history
Fixes: #1782
  • Loading branch information
cgrinds committed Mar 1, 2023
1 parent 2ea4141 commit 77b38c3
Show file tree
Hide file tree
Showing 13 changed files with 237 additions and 98 deletions.
37 changes: 37 additions & 0 deletions cmd/tools/grafana/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,3 +782,40 @@ func asTitle(id string) string {
replacer := strings.NewReplacer("[", ".", "]", "")
return replacer.Replace(path)
}

func TestPercentHasMinMax(t *testing.T) {
visitDashboards(
[]string{"../../../grafana/dashboards/cmode", "../../../grafana/dashboards/storagegrid"},
func(path string, data []byte) {
checkPercentHasMinMax(t, path, data)
})
}

func checkPercentHasMinMax(t *testing.T, path string, data []byte) {
dashPath := shortPath(path)

visitAllPanels(data, func(path string, key, value gjson.Result) {
panelType := value.Get("type").String()
if panelType != "timeseries" {
return
}
defaultUnit := value.Get("fieldConfig.defaults.unit").String()
if defaultUnit != "percent" && defaultUnit != "percentunit" {
return
}
min := value.Get("fieldConfig.defaults.min").String()
max := value.Get("fieldConfig.defaults.max").String()
if min != "0" {
t.Errorf(`dashboard=%s path=%s panel="%s" has unit=%s, min should be 0 got=%s`,
dashPath, path, value.Get("title").String(), defaultUnit, min)
}
if defaultUnit == "percent" && max != "100" {
t.Errorf(`dashboard=%s path=%s panel="%s" has unit=%s, max should be 100 got=%s`,
dashPath, path, value.Get("title").String(), defaultUnit, max)
}
if defaultUnit == "percentunit" && max != "1" {
t.Errorf(`dashboard=%s path=%s panel="%s" has unit=%s, max should be 1 got=%s`,
dashPath, path, value.Get("title").String(), defaultUnit, max)
}
})
}
21 changes: 17 additions & 4 deletions grafana/dashboards/cmode/aggregate.json
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,7 @@
},
"mappings": [],
"min": 0,
"max": 100,
"thresholds": {
"mode": "absolute",
"steps": [
Expand Down Expand Up @@ -1484,6 +1485,7 @@
},
"mappings": [],
"min": 0,
"max": 100,
"thresholds": {
"mode": "absolute",
"steps": [
Expand Down Expand Up @@ -1863,6 +1865,7 @@
},
"mappings": [],
"min": 0,
"max": 100,
"thresholds": {
"mode": "absolute",
"steps": [
Expand Down Expand Up @@ -1953,6 +1956,7 @@
},
"mappings": [],
"min": 0,
"max": 100,
"thresholds": {
"mode": "absolute",
"steps": [
Expand Down Expand Up @@ -2043,6 +2047,7 @@
},
"mappings": [],
"min": 0,
"max": 100,
"thresholds": {
"mode": "absolute",
"steps": [
Expand Down Expand Up @@ -3172,7 +3177,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": []
},
Expand Down Expand Up @@ -3273,7 +3280,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": [
{
Expand Down Expand Up @@ -3943,7 +3952,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": []
},
Expand Down Expand Up @@ -4129,7 +4140,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": []
},
Expand Down
28 changes: 21 additions & 7 deletions grafana/dashboards/cmode/cdot.json
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": []
},
Expand Down Expand Up @@ -597,7 +599,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": [
{
Expand Down Expand Up @@ -725,7 +729,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": [
{
Expand Down Expand Up @@ -853,7 +859,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": [
{
Expand Down Expand Up @@ -1006,7 +1014,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": []
},
Expand Down Expand Up @@ -1093,7 +1103,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": []
},
Expand Down Expand Up @@ -1180,7 +1192,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": []
},
Expand Down
17 changes: 12 additions & 5 deletions grafana/dashboards/cmode/cluster.json
Original file line number Diff line number Diff line change
Expand Up @@ -1919,7 +1919,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": []
},
Expand Down Expand Up @@ -2295,7 +2297,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": []
},
Expand Down Expand Up @@ -2373,7 +2377,6 @@
},
"links": [],
"mappings": [],
"min": 0,
"thresholds": {
"mode": "absolute",
"steps": [
Expand All @@ -2387,7 +2390,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": []
},
Expand Down Expand Up @@ -2478,7 +2483,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": []
},
Expand Down
12 changes: 9 additions & 3 deletions grafana/dashboards/cmode/disk.json
Original file line number Diff line number Diff line change
Expand Up @@ -1009,7 +1009,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": []
},
Expand Down Expand Up @@ -2174,7 +2176,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": [
{
Expand Down Expand Up @@ -2301,7 +2305,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": [
{
Expand Down
26 changes: 18 additions & 8 deletions grafana/dashboards/cmode/lun.json
Original file line number Diff line number Diff line change
Expand Up @@ -3333,7 +3333,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": []
},
Expand Down Expand Up @@ -3409,7 +3411,6 @@
}
},
"mappings": [],
"min": 0,
"thresholds": {
"mode": "absolute",
"steps": [
Expand All @@ -3423,7 +3424,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": []
},
Expand Down Expand Up @@ -3512,7 +3515,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": []
},
Expand Down Expand Up @@ -3588,7 +3593,6 @@
}
},
"mappings": [],
"min": 0,
"thresholds": {
"mode": "absolute",
"steps": [
Expand All @@ -3602,7 +3606,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": []
},
Expand Down Expand Up @@ -3691,7 +3697,9 @@
}
]
},
"unit": "percentunit"
"unit": "percentunit",
"min": 0,
"max": 1
},
"overrides": []
},
Expand Down Expand Up @@ -3782,7 +3790,9 @@
}
]
},
"unit": "percentunit"
"unit": "percentunit",
"min": 0,
"max": 1
},
"overrides": []
},
Expand Down
5 changes: 3 additions & 2 deletions grafana/dashboards/cmode/mcc_cluster.json
Original file line number Diff line number Diff line change
Expand Up @@ -1188,7 +1188,6 @@
}
},
"mappings": [],
"min": 0,
"thresholds": {
"mode": "absolute",
"steps": [
Expand All @@ -1206,7 +1205,9 @@
}
]
},
"unit": "percent"
"unit": "percent",
"min": 0,
"max": 100
},
"overrides": []
},
Expand Down
Loading

0 comments on commit 77b38c3

Please sign in to comment.