From 9d769a0d65bee75b861ff24686f3714a214479c3 Mon Sep 17 00:00:00 2001 From: medcl Date: Sat, 27 Feb 2021 17:21:55 +0800 Subject: [PATCH] update endpoint to es --- api/cluster/manage.go | 157 ------------------------------- api/cluster/version.go | 16 ---- api/index_management/document.go | 1 + api/init.go | 11 --- ui.go | 2 +- web/mock/cluster/cluster.js | 10 +- web/mock/cluster/metrics.js | 3 +- web/src/pages/Cluster/Metrics.js | 6 +- web/src/services/cluster.js | 12 +-- 9 files changed, 20 insertions(+), 198 deletions(-) delete mode 100644 api/cluster/manage.go delete mode 100644 api/cluster/version.go diff --git a/api/cluster/manage.go b/api/cluster/manage.go deleted file mode 100644 index b4d8acd1..00000000 --- a/api/cluster/manage.go +++ /dev/null @@ -1,157 +0,0 @@ -package cluster - -import ( - "fmt" - "infini.sh/framework/core/api" - httprouter "infini.sh/framework/core/api/router" - "infini.sh/framework/core/elastic" - "infini.sh/framework/core/util" - "infini.sh/search-center/config" - "infini.sh/framework/core/orm" - "net/http" - "strings" - "time" -) - -type APIHandler struct { - Config *config.AppConfig - api.Handler -} - -func (h *APIHandler) HandleCreateClusterAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params){ - var conf = &elastic.ElasticsearchConfig{} - resBody := map[string] interface{}{ - } - err := h.DecodeJSON(req, conf) - if err != nil { - resBody["error"] = err - h.WriteJSON(w, resBody, http.StatusInternalServerError) - return - } - // TODO validate data format - esClient := elastic.GetClient(h.Config.Elasticsearch) - id := util.GetUUID() - conf.Created = time.Now() - conf.Enabled=true - conf.Updated = conf.Created - //conf.ID = id - index:=orm.GetIndexName(elastic.ElasticsearchConfig{}) - _, err = esClient.Index(index, "", id, conf) - if err != nil { - resBody["error"] = err - h.WriteJSON(w, resBody, http.StatusInternalServerError) - return - } - - //conf.ID = ir.ID - - resBody["_source"] = conf - resBody["_id"] = id - resBody["result"] = "created" - - h.WriteJSON(w, resBody,http.StatusOK) - -} - -func (h *APIHandler) HandleUpdateClusterAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params){ - var conf = map[string]interface{}{} - resBody := map[string] interface{}{ - } - err := h.DecodeJSON(req, &conf) - if err != nil { - resBody["error"] = err.Error() - h.WriteJSON(w, resBody, http.StatusInternalServerError) - return - } - id := ps.ByName("id") - esClient := elastic.GetClient(h.Config.Elasticsearch) - indexName := orm.GetIndexName(elastic.ElasticsearchConfig{}) - originConf, err := esClient.Get(indexName, "", id) - if err != nil { - resBody["error"] = err.Error() - h.WriteJSON(w, resBody, http.StatusInternalServerError) - return - } - source := originConf.Source - for k, v := range conf { - if k == "id" { - continue - } - source[k] = v - } - conf["updated"] = time.Now() - _, err = esClient.Index(indexName, "", id, source) - if err != nil { - resBody["error"] = err.Error() - h.WriteJSON(w, resBody, http.StatusInternalServerError) - return - } - resBody["_source"] = conf - resBody["_id"] = id - resBody["result"] = "updated" - - h.WriteJSON(w, resBody,http.StatusOK)} - -func (h *APIHandler) HandleDeleteClusterAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params){ - resBody := map[string] interface{}{ - } - id := ps.ByName("id") - esClient := elastic.GetClient(h.Config.Elasticsearch) - response, err := esClient.Delete(orm.GetIndexName(elastic.ElasticsearchConfig{}), "", id) - - if err != nil { - resBody["error"] = err.Error() - h.WriteJSON(w, resBody, http.StatusInternalServerError) - return - } - - resBody["_id"] = id - resBody["result"] = response.Result - h.WriteJSON(w, resBody, response.StatusCode) -} - -func (h *APIHandler) HandleSearchClusterAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params){ - resBody := map[string] interface{}{ - } - var ( - name = h.GetParameterOrDefault(req, "name", "") - enabled = h.GetParameterOrDefault(req, "enabled", "") - queryDSL = `{"query":{"bool":{"must":[%s]}}}` - mustBuilder = &strings.Builder{} - ) - if name != ""{ - mustBuilder.WriteString(fmt.Sprintf(`{"match":{"name": "%s"}}`, name)) - } - if enabled != "" { - if enabled != "true" { - enabled = "false" - } - if mustBuilder.Len() > 0 { - mustBuilder.WriteString(",") - } - mustBuilder.WriteString(fmt.Sprintf(`{"match":{"enabled": %s}}`, enabled)) - } - - queryDSL = fmt.Sprintf(queryDSL, mustBuilder.String()) - esClient := elastic.GetClient(h.Config.Elasticsearch) - res, err := esClient.SearchWithRawQueryDSL(orm.GetIndexName(elastic.ElasticsearchConfig{}), []byte(queryDSL)) - if err != nil { - resBody["error"] = err.Error() - h.WriteJSON(w, resBody, http.StatusInternalServerError) - return - } - - h.WriteJSON(w, res, http.StatusOK) -} - -//new -func (h *APIHandler) HandleClusterMetricsAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params){ - resBody := map[string] interface{}{} - id := ps.ByName("id") - - //esClient := elastic.GetClient(h.Config.Elasticsearch) - - //resBody["summary"] = conf - resBody["metrics"] = id - - h.WriteJSON(w, resBody,http.StatusOK)} diff --git a/api/cluster/version.go b/api/cluster/version.go deleted file mode 100644 index 3ca52cbf..00000000 --- a/api/cluster/version.go +++ /dev/null @@ -1,16 +0,0 @@ -package cluster - -import ( - httprouter "infini.sh/framework/core/api/router" - "net/http" -) - -func (handler APIHandler) GetClusterVersion(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { - //client := elastic.GetClient(handler.Config.Elasticsearch) - //ver := client.GetMajorVersion() - //resBody := handler.newResponseBody() - //resBody["payload"] = map[string]int{ - // "major": ver, - //} - //handler.WriteJSON(w, resBody, http.StatusOK) -} diff --git a/api/index_management/document.go b/api/index_management/document.go index e891874f..8cbd7e1e 100644 --- a/api/index_management/document.go +++ b/api/index_management/document.go @@ -104,6 +104,7 @@ func (handler APIHandler) HandleDeleteDocumentAction(w http.ResponseWriter, req resResult["payload"] = true handler.WriteJSON(w, resResult, http.StatusOK) } + func (handler APIHandler) HandleSearchDocumentAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { client := elastic.GetClient(handler.Config.Elasticsearch) reqBody := docReqBody{} diff --git a/api/init.go b/api/init.go index f0e7d3a0..6a8ebc05 100644 --- a/api/init.go +++ b/api/init.go @@ -6,7 +6,6 @@ import ( "infini.sh/framework/core/task" "infini.sh/framework/core/ui" "infini.sh/search-center/api/index_management" - "infini.sh/search-center/api/cluster" "infini.sh/search-center/config" "path" ) @@ -48,14 +47,4 @@ func Init(cfg *config.AppConfig) { }, }) - shdl := cluster.APIHandler{ - Config: cfg, - } - ui.HandleUIMethod(api.GET, path.Join(pathPrefix, "/cluster/:id/version"), shdl.GetClusterVersion) - ui.HandleUIMethod(api.GET, path.Join(pathPrefix, "/cluster/:id/metrics"), shdl.HandleClusterMetricsAction) - ui.HandleUIMethod(api.POST, path.Join(pathPrefix, "/cluster"), shdl.HandleCreateClusterAction) - ui.HandleUIMethod(api.PUT, path.Join(pathPrefix, "/cluster/:id"), shdl.HandleUpdateClusterAction) - ui.HandleUIMethod(api.DELETE, path.Join(pathPrefix, "/cluster/:id"), shdl.HandleDeleteClusterAction) - ui.HandleUIMethod(api.GET, path.Join(pathPrefix, "/cluster/_search"), shdl.HandleSearchClusterAction) - ui.HandleUIMethod(api.POST, path.Join(pathPrefix, "/cluster/_search"), shdl.HandleSearchClusterAction) } diff --git a/ui.go b/ui.go index 8452e87f..57935a9b 100644 --- a/ui.go +++ b/ui.go @@ -39,6 +39,6 @@ func (h UI) InitUI() { response := map[string]interface{}{} response["request"] = string(request) - w.Write(util.ToJSONBytes(request)) + w.Write(util.MustToJSONBytes(request)) }) } diff --git a/web/mock/cluster/cluster.js b/web/mock/cluster/cluster.js index d1823292..539f3756 100644 --- a/web/mock/cluster/cluster.js +++ b/web/mock/cluster/cluster.js @@ -1,5 +1,5 @@ export default { - 'GET /_search-center/cluster/_search': function(req, res){ + 'GET /elasticsearch/_search': function(req, res){ res.send({ "took": 0, "timed_out": false, @@ -48,7 +48,7 @@ export default { } }) }, - 'POST /_search-center/cluster/_search': function(req, res){ + 'POST /elasticsearch/_search': function(req, res){ res.send({ "took": 0, "timed_out": false, @@ -80,7 +80,7 @@ export default { } }) }, - 'POST /_search-center/cluster': function(req, res){ + 'POST /elasticsearch': function(req, res){ res.send({ "_id": "c0oc4kkgq9s8qss2uk50", "_source": { @@ -98,7 +98,7 @@ export default { "result": "created" }); }, - 'PUT /_search-center/cluster/:id': function(req, res){ + 'PUT /elasticsearch/:id': function(req, res){ res.send({ "_id": "c0oc4kkgq9s8qss2uk50", "_source": { @@ -115,7 +115,7 @@ export default { "result": "updated" }); }, - 'DELETE /_search-center/cluster/:id': function(req, res){ + 'DELETE /elasticsearch/:id': function(req, res){ res.send({ "_id": "c0oc4kkgq9s8qss2uk50", "result": "deleted" diff --git a/web/mock/cluster/metrics.js b/web/mock/cluster/metrics.js index 5e4f5d4f..ce33567b 100644 --- a/web/mock/cluster/metrics.js +++ b/web/mock/cluster/metrics.js @@ -1,7 +1,8 @@ export default { - 'GET /_search-center/cluster/:id/metrics': function(req, res){ + 'GET /elasticsearch/:id/metrics': function(req, res){ res.send({ "summary" : { + "cluster_name" : "test-cluster", "status" : "green", "used_store_bytes" : 2440421140, "max_store_bytes" : 4440421140, diff --git a/web/src/pages/Cluster/Metrics.js b/web/src/pages/Cluster/Metrics.js index 704281ce..ac358b07 100644 --- a/web/src/pages/Cluster/Metrics.js +++ b/web/src/pages/Cluster/Metrics.js @@ -358,6 +358,7 @@ class ClusterMonitor extends PureComponent { if (clusterMonitor.summary) { let rawStats = clusterMonitor.summary; clusterStats = { + cluster_name: rawStats.cluster_name, status: rawStats.status, version: rawStats.version, nodes_count: rawStats.nodes_count, @@ -425,7 +426,7 @@ class ClusterMonitor extends PureComponent { // // */} - + this.refreshNum = el}/>