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.
This commit is contained in:
George Robinson
2023-02-16 17:16:36 +01:00
committed by GitHub
parent 0c99730c20
commit 9e86916d48
5 changed files with 104 additions and 105 deletions
@@ -0,0 +1,41 @@
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)
}
)