diff --git a/server/elasticClient/client.go b/server/elasticClient/client.go index dbd9421dc824d982581318635964b858331738bc..a90fe9215018871f5b2ffca1df8be28e9265b78e 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 0000000000000000000000000000000000000000..17ca241cf1969063f5f63b07cab84f9028bacbf3 --- /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