grafana/pkg/services/ngalert/state/template/funcs.go
George Robinson 9e86916d48
Alerting: Move templating to template package (#63347)
This commit moves templating from the state package to a sub-package
called template. This sub-package will be the logical package for
future ease-of-use improvements to templating custom annotations
and labels.
2023-02-16 17:16:36 +01:00

42 lines
1.1 KiB
Go

package template
import (
"encoding/json"
"fmt"
"net/url"
"text/template"
)
type query struct {
Datasource string `json:"datasource"`
Expr string `json:"expr"`
}
var (
defaultFuncs = template.FuncMap{
"graphLink": graphLink,
"tableLink": tableLink,
}
)
var (
graphLink = func(data string) string {
var q query
if err := json.Unmarshal([]byte(data), &q); err != nil {
return ""
}
datasource := url.QueryEscape(q.Datasource)
expr := url.QueryEscape(q.Expr)
return fmt.Sprintf(`/explore?left={"datasource":%[1]q,"queries":[{"datasource":%[1]q,"expr":%q,"instant":false,"range":true,"refId":"A"}],"range":{"from":"now-1h","to":"now"}}`, datasource, expr)
}
tableLink = func(data string) string {
var q query
if err := json.Unmarshal([]byte(data), &q); err != nil {
return ""
}
datasource := url.QueryEscape(q.Datasource)
expr := url.QueryEscape(q.Expr)
return fmt.Sprintf(`/explore?left={"datasource":%[1]q,"queries":[{"datasource":%[1]q,"expr":%q,"instant":true,"range":false,"refId":"A"}],"range":{"from":"now-1h","to":"now"}}`, datasource, expr)
}
)