From 3504c16d114e7ba972d572375486b4078b97f4f6 Mon Sep 17 00:00:00 2001 From: Wangjunqi123 Date: Mon, 1 Jul 2024 09:37:39 +0800 Subject: [PATCH] server: using a unified handle function to handle the search template interface --- server/elasticClient/client.go | 25 +-------- server/handler/router.go | 2 +- server/handler/searchHandle.go | 4 +- server/main.go | 3 ++ server/service/cluster/LogTimeAixs.go | 2 +- server/service/meta.go | 22 -------- .../cluster.go => service/template/meta.go} | 33 +++++------- server/service/template/template.go | 51 +++++++++++++++++++ 8 files changed, 73 insertions(+), 69 deletions(-) mode change 100644 => 100755 server/elasticClient/client.go mode change 100644 => 100755 server/handler/router.go mode change 100644 => 100755 server/handler/searchHandle.go mode change 100644 => 100755 server/main.go mode change 100644 => 100755 server/service/cluster/LogTimeAixs.go delete mode 100644 server/service/meta.go rename server/{global/template/cluster.go => service/template/meta.go} (92%) mode change 100644 => 100755 create mode 100755 server/service/template/template.go diff --git a/server/elasticClient/client.go b/server/elasticClient/client.go old mode 100644 new mode 100755 index 2ec838b..2950026 --- a/server/elasticClient/client.go +++ b/server/elasticClient/client.go @@ -9,14 +9,12 @@ import ( "io" "net" "net/http" - "strings" "time" "github.com/pkg/errors" "gitee.com/openeuler/PilotGo-plugin-elk/server/conf" "gitee.com/openeuler/PilotGo-plugin-elk/server/errormanager" - "gitee.com/openeuler/PilotGo-plugin-elk/server/global/template" "gitee.com/openeuler/PilotGo-plugin-elk/server/pluginclient" elastic "github.com/elastic/go-elasticsearch/v7" "github.com/elastic/go-elasticsearch/v7/esapi" @@ -25,8 +23,8 @@ import ( var Global_elastic *ElasticClient_v7 type ElasticClient_v7 struct { - Client *elastic.Client - Ctx context.Context + Client *elastic.Client + Ctx context.Context } func InitElasticClient() { @@ -59,25 +57,6 @@ func InitElasticClient() { Client: es_client, Ctx: pluginclient.Global_Context, } - - Global_elastic.initSearchTemplate() -} - -// 在elasticsearch中添加查询模板 -func (client *ElasticClient_v7) initSearchTemplate() { - for key, value := range template.DSL_template_map { - reqbody := strings.NewReader(value[0]) - _, err := client.Client.PutScript( - key, - reqbody, - client.Client.PutScript.WithContext(client.Ctx), - client.Client.PutScript.WithPretty(), - ) - if err != nil { - err = errors.Errorf("fail to put script: %s, %s **warn**0", key, err.Error()) // err top - errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) - } - } } // 通过dsl查询 diff --git a/server/handler/router.go b/server/handler/router.go old mode 100644 new mode 100755 index 0a1e8af..c17377f --- a/server/handler/router.go +++ b/server/handler/router.go @@ -48,7 +48,7 @@ func InitRouter(router *gin.Engine) { api := router.Group("/plugin/elk/api") { api.POST("/create_policy", CreatePolicyHandle) - + api.POST("/log_clusterhost_timeaxis_data", SearchByTemplateHandle) api.POST("/log_hostprocess_timeaxis_data", SearchByTemplateHandle) api.POST("/log_stream_data", SearchByTemplateHandle) diff --git a/server/handler/searchHandle.go b/server/handler/searchHandle.go old mode 100644 new mode 100755 index 53ee5ba..1257ec1 --- a/server/handler/searchHandle.go +++ b/server/handler/searchHandle.go @@ -11,7 +11,7 @@ import ( "gitee.com/openeuler/PilotGo-plugin-elk/server/errormanager" "gitee.com/openeuler/PilotGo-plugin-elk/server/global" "gitee.com/openeuler/PilotGo-plugin-elk/server/pluginclient" - "gitee.com/openeuler/PilotGo-plugin-elk/server/service" + "gitee.com/openeuler/PilotGo-plugin-elk/server/service/template" "gitee.com/openeuler/PilotGo/sdk/response" "github.com/gin-gonic/gin" ) @@ -56,7 +56,7 @@ func SearchByTemplateHandle(ctx *gin.Context) { "id": req_body.Id, "params": params, } - data, err := service.TemplateHandleFuncMap[query_body["id"].(string)](index, query_body) + data, err := template.QueryTemplateMap[query_body["id"].(string)].Func(index, query_body) if err != nil { wrapError(ctx, err) return diff --git a/server/main.go b/server/main.go old mode 100644 new mode 100755 index 59976c3..875f40f --- a/server/main.go +++ b/server/main.go @@ -9,6 +9,7 @@ import ( kibanaclient "gitee.com/openeuler/PilotGo-plugin-elk/server/kibanaClient/7_17_16" "gitee.com/openeuler/PilotGo-plugin-elk/server/logger" "gitee.com/openeuler/PilotGo-plugin-elk/server/pluginclient" + "gitee.com/openeuler/PilotGo-plugin-elk/server/service/template" "gitee.com/openeuler/PilotGo-plugin-elk/server/signal" ) @@ -45,8 +46,10 @@ func main() { /* init elasticsearch client + init search template */ elasticClient.InitElasticClient() + template.InitSearchTemplate() /* init kibana client diff --git a/server/service/cluster/LogTimeAixs.go b/server/service/cluster/LogTimeAixs.go old mode 100644 new mode 100755 index 169b4c8..69b5cce --- a/server/service/cluster/LogTimeAixs.go +++ b/server/service/cluster/LogTimeAixs.go @@ -8,7 +8,7 @@ import ( "github.com/tidwall/gjson" ) -func ProcessLogTimeAixsData(index string, querybody map[string]interface{}) (interface{}, error) { +func ProcessLogTimeAxisData(index string, querybody map[string]interface{}) (interface{}, error) { search_result_body_bytes, err := elasticClient.Global_elastic.SearchByTemplate(index, querybody) if err != nil { err = errors.Wrap(err, "fail to process log timeaxis data") diff --git a/server/service/meta.go b/server/service/meta.go deleted file mode 100644 index 10fffed..0000000 --- a/server/service/meta.go +++ /dev/null @@ -1,22 +0,0 @@ -package service - -import ( - "gitee.com/openeuler/PilotGo-plugin-elk/server/global/template" - "gitee.com/openeuler/PilotGo-plugin-elk/server/service/cluster" -) - -type SearchTemplateFunc func(string, map[string]interface{}) (interface{}, error) - -var TemplateHandleFuncMap map[string]SearchTemplateFunc - -func init() { - TemplateHandleFuncMap = make(map[string]SearchTemplateFunc) - for template_id, content := range template.DSL_template_map { - switch content[1] { - case "ProcessLogTimeAixsData": - TemplateHandleFuncMap[template_id] = cluster.ProcessLogTimeAixsData - case "ProcessLogStreamData": - TemplateHandleFuncMap[template_id] = cluster.ProcessLogStreamData - } - } -} diff --git a/server/global/template/cluster.go b/server/service/template/meta.go old mode 100644 new mode 100755 similarity index 92% rename from server/global/template/cluster.go rename to server/service/template/meta.go index 994a731..323e0b8 --- a/server/global/template/cluster.go +++ b/server/service/template/meta.go @@ -1,8 +1,18 @@ package template -var ( - DSL_template_map map[string][]string -) +/* +key: 查询模板id + +value: 查询模板文本及处理函数 +*/ +var QueryTemplateMap map[string]QueryTemplateMeta + +type SearchTemplateFunc func(string, map[string]interface{}) (interface{}, error) + +type QueryTemplateMeta struct { + Text string `json:"text"` // 查询模板文本 + Func SearchTemplateFunc `json:"func"` // 查询模板请求处理函数 +} const ( DSL_log_clusterhost_timeaxis_template = `{ @@ -251,20 +261,3 @@ const ( } }` ) - -func init() { - DSL_template_map = map[string][]string{ - "log_clusterhost_timeaxis": { - DSL_log_clusterhost_timeaxis_template, - "ProcessLogTimeAixsData", - }, - "log_hostprocess_timeaxis": { - DSL_log_hostprocess_timeaxis_template, - "ProcessLogTimeAixsData", - }, - "log_stream": { - DSL_log_stream_template, - "ProcessLogStreamData", - }, - } -} diff --git a/server/service/template/template.go b/server/service/template/template.go new file mode 100755 index 0000000..8d54697 --- /dev/null +++ b/server/service/template/template.go @@ -0,0 +1,51 @@ +package template + +import ( + "strings" + + "gitee.com/openeuler/PilotGo-plugin-elk/server/elasticClient" + "gitee.com/openeuler/PilotGo-plugin-elk/server/errormanager" + "gitee.com/openeuler/PilotGo-plugin-elk/server/pluginclient" + "gitee.com/openeuler/PilotGo-plugin-elk/server/service/cluster" + "github.com/pkg/errors" +) + +func init() { + QueryTemplateMap = map[string]QueryTemplateMeta{ + "log_clusterhost_timeaxis": { + Text: DSL_log_clusterhost_timeaxis_template, + Func: cluster.ProcessLogTimeAxisData, + }, + "log_hostprocess_timeaxis": { + Text: DSL_log_hostprocess_timeaxis_template, + Func: cluster.ProcessLogTimeAxisData, + }, + "log_stream": { + Text: DSL_log_stream_template, + Func: cluster.ProcessLogStreamData, + }, + } +} + +// 在elasticsearch中添加查询模板 +func InitSearchTemplate() { + if elasticClient.Global_elastic == nil { + err := errors.Errorf("elasticClient is nil **errstackfatal**0") // err top + errormanager.ErrorTransmit(pluginclient.Global_Context, err, true) + return + } + + for template_id, template_meta := range QueryTemplateMap { + reqbody := strings.NewReader(template_meta.Text) + _, err := elasticClient.Global_elastic.Client.PutScript( + template_id, + reqbody, + elasticClient.Global_elastic.Client.PutScript.WithContext(elasticClient.Global_elastic.Ctx), + elasticClient.Global_elastic.Client.PutScript.WithPretty(), + ) + if err != nil { + err = errors.Errorf("fail to put script: %s, %s **errstackfatal**0", template_id, err.Error()) // err top + errormanager.ErrorTransmit(pluginclient.Global_Context, err, true) + } + } +} -- Gitee