mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Extract custom template functions (#60695)
extract custom template functions and export the FuncMap
This commit is contained in:
parent
4e2bd63572
commit
05c9af5110
@ -2,16 +2,12 @@ package state
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"math"
|
"math"
|
||||||
"net/url"
|
"net/url"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
text_template "text/template"
|
|
||||||
|
|
||||||
"github.com/prometheus/common/model"
|
"github.com/prometheus/common/model"
|
||||||
"github.com/prometheus/prometheus/pkg/timestamp"
|
"github.com/prometheus/prometheus/pkg/timestamp"
|
||||||
"github.com/prometheus/prometheus/promql"
|
"github.com/prometheus/prometheus/promql"
|
||||||
@ -59,15 +55,7 @@ func expandTemplate(ctx context.Context, name, text string, labels map[string]st
|
|||||||
[]string{"missingkey=invalid"},
|
[]string{"missingkey=invalid"},
|
||||||
)
|
)
|
||||||
|
|
||||||
expander.Funcs(text_template.FuncMap{
|
expander.Funcs(FuncMap)
|
||||||
"graphLink": graphLink,
|
|
||||||
"tableLink": tableLink,
|
|
||||||
|
|
||||||
// This function is a no-op for now.
|
|
||||||
"strvalue": func(value templateCaptureValue) string {
|
|
||||||
return ""
|
|
||||||
},
|
|
||||||
})
|
|
||||||
result, resultErr = expander.Expand()
|
result, resultErr = expander.Expand()
|
||||||
// Replace missing key value to one that does not look like an HTML tag. This can cause problems downstream in some notifiers.
|
// Replace missing key value to one that does not look like an HTML tag. This can cause problems downstream in some notifiers.
|
||||||
// For example, Telegram in HTML mode rejects requests with unsupported tags.
|
// For example, Telegram in HTML mode rejects requests with unsupported tags.
|
||||||
@ -96,29 +84,3 @@ type query struct {
|
|||||||
Datasource string `json:"datasource"`
|
Datasource string `json:"datasource"`
|
||||||
Expr string `json:"expr"`
|
Expr string `json:"expr"`
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
|
46
pkg/services/ngalert/state/template_functions.go
Normal file
46
pkg/services/ngalert/state/template_functions.go
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
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 ""
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user