mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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.
42 lines
1.1 KiB
Go
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)
|
|
}
|
|
)
|