From 4cc69c8e53fa500f709464abe3f117640195d947 Mon Sep 17 00:00:00 2001 From: liugq Date: Tue, 8 Aug 2023 11:32:01 +0800 Subject: [PATCH] add alert template function md_to_html --- plugin/api/alerting/channel.go | 21 +++++++++++++++++++++ plugin/api/email/server.go | 4 ++-- service/alerting/funcs/function.go | 1 + service/alerting/funcs/strings.go | 21 ++++++++++++++++++++- 4 files changed, 44 insertions(+), 3 deletions(-) diff --git a/plugin/api/alerting/channel.go b/plugin/api/alerting/channel.go index e1b5cb15..6999098b 100644 --- a/plugin/api/alerting/channel.go +++ b/plugin/api/alerting/channel.go @@ -12,6 +12,7 @@ import ( "infini.sh/framework/core/global" "net/http" "strconv" + "strings" "time" log "github.com/cihub/seelog" @@ -190,6 +191,7 @@ func (h *AlertAPI) searchChannel(w http.ResponseWriter, req *http.Request, ps ht strFrom = h.GetParameterOrDefault(req, "from", "0") subType = h.GetParameterOrDefault(req, "sub_type", "") typ = h.GetParameterOrDefault(req, "type", "") + sort = h.GetParameterOrDefault(req, "sort", "updated:desc") ) mustQ := []interface{}{} if keyword != "" { @@ -223,6 +225,18 @@ func (h *AlertAPI) searchChannel(w http.ResponseWriter, req *http.Request, ps ht if from < 0 { from = 0 } + var ( + sortField string + sortDirection string + ) + sortParts := strings.Split(sort, ":") + sortField = sortParts[0] + if len(sortParts) >= 2 { + sortDirection = sortParts[1] + } + if sortDirection == "" { + sortDirection = "asc" + } query := util.MapStr{ "size": size, "from": from, @@ -231,6 +245,13 @@ func (h *AlertAPI) searchChannel(w http.ResponseWriter, req *http.Request, ps ht "must": mustQ, }, }, + "sort": []util.MapStr{ + { + sortField: util.MapStr{ + "order": sortDirection, + }, + }, + }, } q := orm.Query{ diff --git a/plugin/api/email/server.go b/plugin/api/email/server.go index 8a6637b6..2043cdec 100644 --- a/plugin/api/email/server.go +++ b/plugin/api/email/server.go @@ -17,8 +17,8 @@ import ( "infini.sh/framework/core/orm" "infini.sh/framework/core/util" "net/http" - "src/github.com/buger/jsonparser" - "src/github.com/gopkg.in/gomail.v2" + "github.com/buger/jsonparser" + "github.com/gopkg.in/gomail.v2" "strconv" "time" ) diff --git a/service/alerting/funcs/function.go b/service/alerting/funcs/function.go index 37978fa9..13cd5f0f 100644 --- a/service/alerting/funcs/function.go +++ b/service/alerting/funcs/function.go @@ -33,5 +33,6 @@ var genericMap = map[string]interface{}{ "mul": mul, "lookup": lookup, "str_replace": replace, + "md_to_html": mdToHTML, //"get_keystore_secret": getKeystoreSecret, } diff --git a/service/alerting/funcs/strings.go b/service/alerting/funcs/strings.go index 86a25bbe..a14be25c 100644 --- a/service/alerting/funcs/strings.go +++ b/service/alerting/funcs/strings.go @@ -4,7 +4,12 @@ package funcs -import "strings" +import ( + "github.com/gomarkdown/markdown" + "github.com/gomarkdown/markdown/html" + "github.com/gomarkdown/markdown/parser" + "strings" +) func substring(start, end int, s string) string { runes := []rune(s) @@ -17,4 +22,18 @@ func substring(start, end int, s string) string { func replace(old, new, src string) string { return strings.Replace(src, old, new, -1) +} + +func mdToHTML(mdText string) string { + extensions := parser.CommonExtensions | parser.AutoHeadingIDs | parser.NoEmptyLineBeforeBlock + p := parser.NewWithExtensions(extensions) + doc := p.Parse([]byte(mdText)) + + // create HTML renderer with extensions + htmlFlags := html.CommonFlags | html.HrefTargetBlank + opts := html.RendererOptions{Flags: htmlFlags} + renderer := html.NewRenderer(opts) + + buf := markdown.Render(doc, renderer) + return string(buf) } \ No newline at end of file