From 01213f0433056dac7d5d40bc985da7268240a4c8 Mon Sep 17 00:00:00 2001 From: Wangjunqi123 Date: Tue, 25 Jun 2024 15:43:11 +0800 Subject: [PATCH] server/elasticclient: initialize search template --- server/elasticClient/client.go | 24 ++++++++- server/global/template/cluster.go | 86 +++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 server/global/template/cluster.go diff --git a/server/elasticClient/client.go b/server/elasticClient/client.go index dbd9421..a90fe92 100644 --- a/server/elasticClient/client.go +++ b/server/elasticClient/client.go @@ -7,12 +7,14 @@ import ( "io" "net" "net/http" + "strings" "time" "github.com/pkg/errors" "gitee.com/openeuler/PilotGo-plugin-elk/conf" "gitee.com/openeuler/PilotGo-plugin-elk/errormanager" + "gitee.com/openeuler/PilotGo-plugin-elk/global/template" "gitee.com/openeuler/PilotGo-plugin-elk/pluginclient" elastic "github.com/elastic/go-elasticsearch/v7" ) @@ -54,11 +56,29 @@ func InitElasticClient() { Client: es_client, Ctx: pluginclient.Global_Context, } + + Global_elastic.initSearchTemplate() +} + +func (client *ElasticClient_v7) initSearchTemplate() { + for key, value := range template.DSL_template_map { + reqbody := strings.NewReader(value) + _, 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) + } + } } -func (client *ElasticClient_v7) Search(index string, body io.Reader) ([]byte, error) { +func (client *ElasticClient_v7) SearchByQueryRequestBody(index string, body io.Reader) ([]byte, error) { resp, err := client.Client.Search( - client.Client.Search.WithContext(context.Background()), + client.Client.Search.WithContext(client.Ctx), client.Client.Search.WithIndex(index), client.Client.Search.WithBody(body), client.Client.Search.WithTrackTotalHits(true), diff --git a/server/global/template/cluster.go b/server/global/template/cluster.go new file mode 100644 index 0000000..17ca241 --- /dev/null +++ b/server/global/template/cluster.go @@ -0,0 +1,86 @@ +package template + +var ( + DSL_template_map map[string]string +) + +const ( + DSL_log_timeaxis_template = `{ + "script": { + "lang": "mustache", + "source": { + "aggs": { + "1": { + "terms": { + "field": "{{aggs_field}}", + "order": { + "_count": "desc" + } + }, + "aggs": { + "1-1": { + "date_histogram": { + "field": "@timestamp", + "fixed_interval": "1s", + "time_zone": "Asia/Shanghai", + "min_doc_count": 0 + } + } + } + } + }, + "size": "{{size}}", + "script_fields": {}, + "stored_fields": [ + "*" + ], + "runtime_mappings": {}, + "query": { + "bool": { + "must": [], + "filter": [ + { + "bool": { + "should": [ + { + "match": { + "data_stream.dataset": "{{query_data_stream_dataset}}" + } + } + ], + "minimum_should_match": 1 + } + }, + { + "range": { + "@timestamp": { + "format": "strict_date_optional_time", + "gte": "{{query_range_gte}}", + "lte": "{{query_range_lte}}" + } + } + } + ], + "should": [], + "must_not": [] + } + } + }, + "params": { + "query_data_stream_dataset": "system.syslog", + "query_range_gte": "2024-06-24T10:55:36.185Z", + "query_range_lte": "2024-06-24T11:00:36.185Z", + "aggs_field": "host.hostname", + "size": 0 + } + } + } + + }` +) + +func init() { + DSL_template_map = map[string]string{ + "log_timeaxis": DSL_log_timeaxis_template, + } +} \ No newline at end of file -- Gitee