Skip to content

Commit

Permalink
fix: added ci test case for min-max in case of gradient-guage
Browse files Browse the repository at this point in the history
  • Loading branch information
Hardikl committed Feb 22, 2023
1 parent 5eab65b commit f104c08
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions cmd/tools/grafana/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestUnitsAndExprMatch(t *testing.T) {
mt := newMetricsTable()
visitDashboards([]string{"../../../grafana/dashboards/cmode", "../../../grafana/dashboards/storagegrid"},
func(path string, data []byte) {
checkUnits(path, mt, data)
checkUnits(t, path, mt, data)
})

// Exceptions are meant to reduce false negatives
Expand Down Expand Up @@ -161,12 +161,12 @@ func newMetricsTable() *metricsTable {
}
}

func checkUnits(dashboardPath string, mt *metricsTable, data []byte) {
func checkUnits(t *testing.T, dashboardPath string, mt *metricsTable, data []byte) {
gjson.GetBytes(data, "panels").ForEach(func(key, value gjson.Result) bool {
doPanel("", key, value, mt, dashboardPath)
doPanel(t, "", key, value, mt, dashboardPath)
value.Get("panels").ForEach(func(key2, value2 gjson.Result) bool {
pathPrefix := fmt.Sprintf("panels[%d].", key.Int())
doPanel(pathPrefix, key2, value2, mt, dashboardPath)
doPanel(t, pathPrefix, key2, value2, mt, dashboardPath)
return true
})
return true
Expand All @@ -184,7 +184,7 @@ var metricDivideMetric2 = regexp.MustCompile(`(\w+)/.*?(\w+){`)
// detects arrays
var metricWithArray = regexp.MustCompile(`metric=~*"(.*?)"`)

func doPanel(pathPrefix string, key gjson.Result, value gjson.Result, mt *metricsTable, dashboardPath string) bool {
func doPanel(t *testing.T, pathPrefix string, key gjson.Result, value gjson.Result, mt *metricsTable, dashboardPath string) bool {
kind := value.Get("type").String()
if kind == "row" {
return true
Expand All @@ -197,17 +197,22 @@ func doPanel(pathPrefix string, key gjson.Result, value gjson.Result, mt *metric
title := value.Get("title").String()
sPath := shortPath(dashboardPath)

propertiesMap := make(map[string]map[string]string)
overrides := make([]override, 0, len(overridesSlice))
expressions := make([]expression, 0)
valueToName := make(map[string]string) // only used with panels[*].transformations[*].options.renameByName

for oi, overrideN := range overridesSlice {
matcherID := overrideN.Get("matcher.id")
// make sure that mapKey is unique for each override element
propertiesMapKey := matcherID.String() + strconv.Itoa(oi)
propertiesMap[propertiesMapKey] = make(map[string]string)
matcherOptions := overrideN.Get("matcher.options")
propertiesN := overrideN.Get("properties").Array()
for pi, propN := range propertiesN {
propID := propN.Get("id").String()
propVal := propN.Get("value").String()
propertiesMap[propertiesMapKey][propID] = propVal
if propID == "unit" {
o := override{
id: matcherID.String(),
Expand All @@ -221,6 +226,18 @@ func doPanel(pathPrefix string, key gjson.Result, value gjson.Result, mt *metric
}
}

// In case of gradient-gauge and percent(0.0-1.0), we must override min and max value
for _, properties := range propertiesMap {
if properties["unit"] == "percentunit" && properties["custom.displayMode"] == "gradient-gauge" {
if maxVal, exist := properties["max"]; !exist || maxVal != "1" {
t.Errorf("dashboard=%s, title=%s should have max value 1", sPath, title)
}
if minVal, exist := properties["min"]; !exist || minVal != "0" {
t.Errorf("dashboard=%s, title=%s should have min value 0", sPath, title)
}
}
}

// Heatmap units are saved in a different place
if kind == "heatmap" && defaultUnit == "" {
defaultUnit = value.Get("yAxis.format").String()
Expand Down

0 comments on commit f104c08

Please sign in to comment.