Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: ensure rate calculations are not 1m #1683

Merged
merged 1 commit into from
Feb 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 37 additions & 15 deletions cmd/tools/grafana/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,21 +347,7 @@ func checkUnusedVariables(t *testing.T, path string, data []byte) {
return true
})

// collect all expressions
expressions := make([]string, 0)
gjson.GetBytes(data, "panels").ForEach(func(key, value gjson.Result) bool {
doExpr("", key, value, func(path string, expr string) {
expressions = append(expressions, expr)
})
value.Get("panels").ForEach(func(key2, value2 gjson.Result) bool {
pathPrefix := fmt.Sprintf("panels[%d].", key.Int())
doExpr(pathPrefix, key2, value2, func(path string, expr string) {
expressions = append(expressions, expr)
})
return true
})
return true
})
expressions := allExpressions(data)

// check that each variable is used in at least one expression
varLoop:
Expand All @@ -381,6 +367,24 @@ varLoop:
}
}

func allExpressions(data []byte) []string {
expressions := make([]string, 0)
gjson.GetBytes(data, "panels").ForEach(func(key, value gjson.Result) bool {
doExpr("", key, value, func(path string, expr string) {
expressions = append(expressions, expr)
})
value.Get("panels").ForEach(func(key2, value2 gjson.Result) bool {
pathPrefix := fmt.Sprintf("panels[%d].", key.Int())
doExpr(pathPrefix, key2, value2, func(path string, expr string) {
expressions = append(expressions, expr)
})
return true
})
return true
})
return expressions
}

func doExpr(pathPrefix string, key gjson.Result, value gjson.Result, exprFunc func(path string, expr string)) {
kind := value.Get("type").String()
if kind == "row" {
Expand Down Expand Up @@ -674,3 +678,21 @@ func checkPanelChildPanels(t *testing.T, path string, data []byte) {
return true
})
}

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

func checkRate1m(t *testing.T, path string, data []byte) {
expressions := allExpressions(data)
for _, expr := range expressions {
if strings.Contains(expr, "[1m]") {
t.Errorf("dashboard=%s, expr should not use rate of [1m] expr=%s", path, expr)
}
}
}