grafana/pkg/services/ngalert/state/template_functions.go
Santiago 05c9af5110
Extract custom template functions (#60695)
extract custom template functions and export the FuncMap
2022-12-22 17:31:40 -03:00

47 lines
1.2 KiB
Go

package state
import (
"encoding/json"
"fmt"
"net/url"
text_template "text/template"
)
// FuncMap is a map of custom functions we use for templates.
var FuncMap = text_template.FuncMap{
"graphLink": graphLink,
"tableLink": tableLink,
"strvalue": strValue,
}
func graphLink(rawQuery string) string {
var q query
if err := json.Unmarshal([]byte(rawQuery), &q); err != nil {
return ""
}
escapedExpression := url.QueryEscape(q.Expr)
escapedDatasource := url.QueryEscape(q.Datasource)
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"}}`, escapedDatasource, escapedExpression)
}
func tableLink(rawQuery string) string {
var q query
if err := json.Unmarshal([]byte(rawQuery), &q); err != nil {
return ""
}
escapedExpression := url.QueryEscape(q.Expr)
escapedDatasource := url.QueryEscape(q.Datasource)
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"}}`, escapedDatasource, escapedExpression)
}
// This function is a no-op for now.
func strValue(value templateCaptureValue) string {
return ""
}