diff --git a/server/elasticClient/client.go b/server/elasticClient/client.go index e69ac2e498e7b396185bd099e346e01c9f6a744f..dbd9421dc824d982581318635964b858331738bc 100644 --- a/server/elasticClient/client.go +++ b/server/elasticClient/client.go @@ -57,12 +57,12 @@ func InitElasticClient() { } func (client *ElasticClient_v7) Search(index string, body io.Reader) ([]byte, error) { - resp, err := Global_elastic.Client.Search( - Global_elastic.Client.Search.WithContext(context.Background()), - Global_elastic.Client.Search.WithIndex(index), - Global_elastic.Client.Search.WithBody(body), - Global_elastic.Client.Search.WithTrackTotalHits(true), - Global_elastic.Client.Search.WithPretty(), + resp, err := client.Client.Search( + client.Client.Search.WithContext(context.Background()), + client.Client.Search.WithIndex(index), + client.Client.Search.WithBody(body), + client.Client.Search.WithTrackTotalHits(true), + client.Client.Search.WithPretty(), ) if err != nil { err = errors.Errorf("%+v **errstack**0", err.Error()) diff --git a/server/handler/meta.go b/server/handler/meta.go new file mode 100644 index 0000000000000000000000000000000000000000..f023928775fb251ea5e8f3b81699ee46b17feb06 --- /dev/null +++ b/server/handler/meta.go @@ -0,0 +1,21 @@ +package handler + +import ( + "gitee.com/openeuler/PilotGo-plugin-elk/errormanager" + "gitee.com/openeuler/PilotGo-plugin-elk/pluginclient" + "gitee.com/openeuler/PilotGo/sdk/response" + "github.com/gin-gonic/gin" + "github.com/pkg/errors" +) + +func newError(ctx *gin.Context, err error) { + err = errors.Errorf("fail to search data: %s **errstack**0", err.Error()) // err top + errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) + response.Fail(ctx, nil, err.Error()) +} + +func wrapError(ctx *gin.Context, err error) { + err = errors.Wrap(err, " **0") // err top + errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) + response.Fail(ctx, nil, err.Error()) +} diff --git a/server/handler/router.go b/server/handler/router.go index d0c9de16bd16f1e19478d1e9caac6bf717611719..5a3401b1e8267dfb8fd3dc7dc7b112c7c9932b4b 100644 --- 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("/search", SearchHandle) + api.POST("/log_time_axis_data", Search_LogTimeAxisDataHandle) } timeoutapi := router.Group("/plugin/elk/api") diff --git a/server/handler/search.go b/server/handler/search.go deleted file mode 100644 index 462541a50349479fc0cffdc00ba5a375da3802ce..0000000000000000000000000000000000000000 --- a/server/handler/search.go +++ /dev/null @@ -1,64 +0,0 @@ -package handler - -import ( - "context" - "encoding/json" - "io" - - "github.com/pkg/errors" - - "gitee.com/openeuler/PilotGo-plugin-elk/elasticClient" - "gitee.com/openeuler/PilotGo-plugin-elk/errormanager" - "gitee.com/openeuler/PilotGo-plugin-elk/pluginclient" - "gitee.com/openeuler/PilotGo/sdk/response" - "github.com/gin-gonic/gin" -) - -func SearchHandle(ctx *gin.Context) { - index := ctx.Query("index") - if index == "" { - err := errors.Errorf("%+v **warn**0", "index is null") - errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) - response.Fail(ctx, nil, err.Error()) - return - } - - defer ctx.Request.Body.Close() - if elasticClient.Global_elastic.Client == nil { - err := errors.Errorf("%+v **warn**0", "global_elastic is null") - errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) - response.Fail(ctx, nil, err.Error()) - return - } - resp, err := elasticClient.Global_elastic.Client.Search( - elasticClient.Global_elastic.Client.Search.WithContext(context.Background()), - elasticClient.Global_elastic.Client.Search.WithIndex(index), - elasticClient.Global_elastic.Client.Search.WithBody(ctx.Request.Body), - elasticClient.Global_elastic.Client.Search.WithTrackTotalHits(true), - elasticClient.Global_elastic.Client.Search.WithPretty(), - ) - defer resp.Body.Close() - if err != nil { - err = errors.Errorf("%+v **warn**0", err.Error()) - errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) - response.Fail(ctx, nil, errors.Cause(err).Error()) - return - } - if resp.IsError() { - err = errors.Errorf("%+v **warn**0", resp.String()) - errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) - response.Fail(ctx, nil, resp.String()) - return - } else { - resp_body_data := map[string]interface{}{} - resp_body_bytes, err := io.ReadAll(resp.Body) - if err != nil { - err = errors.Errorf("%+v **warn**0", err.Error()) - errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) - response.Fail(ctx, nil, resp.String()) - return - } - json.Unmarshal(resp_body_bytes, &resp_body_data) - response.Success(ctx, resp_body_data, "") - } -} diff --git a/server/handler/searchHandle.go b/server/handler/searchHandle.go new file mode 100644 index 0000000000000000000000000000000000000000..5c6de5fa81e05763e0f3af5fbc1efd9549f68548 --- /dev/null +++ b/server/handler/searchHandle.go @@ -0,0 +1,59 @@ +package handler + +import ( + "bytes" + "encoding/json" + "io" + + "github.com/pkg/errors" + + "gitee.com/openeuler/PilotGo-plugin-elk/elasticClient" + "gitee.com/openeuler/PilotGo-plugin-elk/errormanager" + "gitee.com/openeuler/PilotGo-plugin-elk/pluginclient" + "gitee.com/openeuler/PilotGo-plugin-elk/service/cluster" + "gitee.com/openeuler/PilotGo/sdk/response" + "github.com/gin-gonic/gin" +) + +// 查询日志时间轴相关数据 +func Search_LogTimeAxisDataHandle(ctx *gin.Context) { + if elasticClient.Global_elastic.Client == nil { + err := errors.New("global_elastic is null **warn**0") // err top + errormanager.ErrorTransmit(pluginclient.Global_Context, err, false) + response.Fail(ctx, nil, err.Error()) + return + } + // process request body + defer ctx.Request.Body.Close() + req_body_bytes, err := io.ReadAll(ctx.Request.Body) + if err != nil { + newError(ctx, err) + return + } + req_body := struct { + Index string `json:"index"` + DSL interface{} `json:"dsl"` + }{} + err = json.Unmarshal(req_body_bytes, &req_body) + if err != nil { + newError(ctx, err) + return + } + query_body_bytes, err := json.Marshal(req_body.DSL) + if err != nil { + newError(ctx, err) + return + } + + resp_body_bytes, err := elasticClient.Global_elastic.Search(req_body.Index, bytes.NewReader(query_body_bytes)) + if err != nil { + wrapError(ctx, err) + return + } + data, err := cluster.ProcessLogTimeAixsData(resp_body_bytes) + if err != nil { + wrapError(ctx, err) + return + } + response.Success(ctx, data, "") +}