From 99f03fc574d62142fcc5ca102e41bea0c6d0451a Mon Sep 17 00:00:00 2001 From: liugq Date: Thu, 16 Mar 2023 10:55:21 +0800 Subject: [PATCH] add elastic status overview api --- plugin/api/index_management/elasticsearch.go | 53 ++++++++++++++++++++ plugin/api/init.go | 1 + 2 files changed, 54 insertions(+) diff --git a/plugin/api/index_management/elasticsearch.go b/plugin/api/index_management/elasticsearch.go index 2966d4a9..7b50e25d 100644 --- a/plugin/api/index_management/elasticsearch.go +++ b/plugin/api/index_management/elasticsearch.go @@ -233,3 +233,56 @@ func (handler APIHandler) getLastActiveHostCount() (int, error){ return len(searchRes.Aggregations["week_active_host"].Buckets), nil } +func (handler APIHandler) ElasticsearchStatusSummaryAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params){ + clusterGrp, err := handler.getGroupMetric(orm.GetIndexName(elastic.ElasticsearchConfig{}), "labels.health_status") + if err != nil { + log.Error(err) + handler.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + nodeGrp, err := handler.getGroupMetric(orm.GetIndexName(elastic.NodeConfig{}), "metadata.labels.status") + if err != nil { + log.Error(err) + handler.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + hostCount, err := handler.getMetricCount(orm.GetIndexName(elastic.NodeConfig{}), "metadata.host", nil) + if err != nil { + log.Error(err) + handler.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + handler.WriteJSON(w, util.MapStr{ + "cluster": clusterGrp, + "node": nodeGrp, + "host": util.MapStr{ + "online": hostCount, + }, + }, http.StatusOK) +} + +func (handler APIHandler) getGroupMetric(indexName, field string) (interface{}, error){ + client := elastic.GetClient(global.MustLookupString(elastic.GlobalSystemElasticsearchID)) + queryDSL := util.MapStr{ + "size": 0, + "aggs": util.MapStr{ + "group": util.MapStr{ + "terms": util.MapStr{ + "field": field, + }, + }, + }, + } + searchRes, err := client.SearchWithRawQueryDSL(indexName, util.MustToJSONBytes(queryDSL)) + if err != nil { + log.Error(err) + return 0, err + } + groups := map[string]interface{}{} + for _, bk := range searchRes.Aggregations["group"].Buckets { + if key, ok := bk["key"].(string); ok { + groups[key] = bk["doc_count"] + } + } + return groups, nil +} diff --git a/plugin/api/init.go b/plugin/api/init.go index a6a897fd..a2f0b2e4 100644 --- a/plugin/api/init.go +++ b/plugin/api/init.go @@ -49,6 +49,7 @@ func Init(cfg *config.AppConfig) { api.HandleAPIMethod(api.PUT, path.Join(pathPrefix, "elasticsearch/command/:cid"), handler.RequirePermission(handler.HandleSaveCommonCommandAction, enum.PermissionCommandWrite)) api.HandleAPIMethod(api.GET, path.Join(pathPrefix, "elasticsearch/command"), handler.RequirePermission(handler.HandleQueryCommonCommandAction, enum.PermissionCommandRead)) api.HandleAPIMethod(api.DELETE, path.Join(pathPrefix, "elasticsearch/command/:cid"), handler.RequirePermission(handler.HandleDeleteCommonCommandAction,enum.PermissionCommandWrite)) + api.HandleAPIMethod(api.GET, "/elasticsearch/overview/status", handler.RequireLogin(handler.ElasticsearchStatusSummaryAction)) //task.RegisterScheduleTask(task.ScheduleTask{ // Description: "sync reindex task result",