From d05c039b48a9f202dc7418bcf722d7f58bc1eeeb Mon Sep 17 00:00:00 2001 From: medcl Date: Wed, 24 Feb 2021 00:09:13 +0800 Subject: [PATCH] refactoring elasticsearch model --- .../cluster_config.go => cluster/manage.go} | 27 +++++++++++++------ api/cluster/version.go | 16 +++++++++++ api/index_management/cluster.go | 17 ------------ api/init.go | 7 ++--- main.go | 3 ++- model/cluster.go | 16 +++++++++++ model/cluster_config.go | 18 ------------- web/mock/cluster/cluster.js | 17 ++++++++++++ web/src/pages/Cluster/Metrics.js | 14 +++++++--- web/src/pages/Cluster/Overview.js | 3 ++- web/src/services/cluster.js | 4 +-- 11 files changed, 89 insertions(+), 53 deletions(-) rename api/{system/cluster_config.go => cluster/manage.go} (83%) create mode 100644 api/cluster/version.go delete mode 100644 api/index_management/cluster.go create mode 100644 model/cluster.go delete mode 100644 model/cluster_config.go diff --git a/api/system/cluster_config.go b/api/cluster/manage.go similarity index 83% rename from api/system/cluster_config.go rename to api/cluster/manage.go index 0d6bebcf..b4d8acd1 100644 --- a/api/system/cluster_config.go +++ b/api/cluster/manage.go @@ -1,4 +1,4 @@ -package system +package cluster import ( "fmt" @@ -7,7 +7,6 @@ import ( "infini.sh/framework/core/elastic" "infini.sh/framework/core/util" "infini.sh/search-center/config" - "infini.sh/search-center/model" "infini.sh/framework/core/orm" "net/http" "strings" @@ -20,7 +19,7 @@ type APIHandler struct { } func (h *APIHandler) HandleCreateClusterAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params){ - var conf = &model.ClusterConfig{} + var conf = &elastic.ElasticsearchConfig{} resBody := map[string] interface{}{ } err := h.DecodeJSON(req, conf) @@ -36,7 +35,7 @@ func (h *APIHandler) HandleCreateClusterAction(w http.ResponseWriter, req *http. conf.Enabled=true conf.Updated = conf.Created //conf.ID = id - index:=orm.GetIndexName(model.ClusterConfig{}) + index:=orm.GetIndexName(elastic.ElasticsearchConfig{}) _, err = esClient.Index(index, "", id, conf) if err != nil { resBody["error"] = err @@ -66,7 +65,7 @@ func (h *APIHandler) HandleUpdateClusterAction(w http.ResponseWriter, req *http. } id := ps.ByName("id") esClient := elastic.GetClient(h.Config.Elasticsearch) - indexName := orm.GetIndexName(model.ClusterConfig{}) + indexName := orm.GetIndexName(elastic.ElasticsearchConfig{}) originConf, err := esClient.Get(indexName, "", id) if err != nil { resBody["error"] = err.Error() @@ -98,7 +97,7 @@ func (h *APIHandler) HandleDeleteClusterAction(w http.ResponseWriter, req *http. } id := ps.ByName("id") esClient := elastic.GetClient(h.Config.Elasticsearch) - response, err := esClient.Delete(orm.GetIndexName(model.ClusterConfig{}), "", id) + response, err := esClient.Delete(orm.GetIndexName(elastic.ElasticsearchConfig{}), "", id) if err != nil { resBody["error"] = err.Error() @@ -135,7 +134,7 @@ func (h *APIHandler) HandleSearchClusterAction(w http.ResponseWriter, req *http. queryDSL = fmt.Sprintf(queryDSL, mustBuilder.String()) esClient := elastic.GetClient(h.Config.Elasticsearch) - res, err := esClient.SearchWithRawQueryDSL(orm.GetIndexName(model.ClusterConfig{}), []byte(queryDSL)) + res, err := esClient.SearchWithRawQueryDSL(orm.GetIndexName(elastic.ElasticsearchConfig{}), []byte(queryDSL)) if err != nil { resBody["error"] = err.Error() h.WriteJSON(w, resBody, http.StatusInternalServerError) @@ -143,4 +142,16 @@ func (h *APIHandler) HandleSearchClusterAction(w http.ResponseWriter, req *http. } h.WriteJSON(w, res, http.StatusOK) -} \ No newline at end of file +} + +//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 new file mode 100644 index 00000000..3ca52cbf --- /dev/null +++ b/api/cluster/version.go @@ -0,0 +1,16 @@ +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/cluster.go b/api/index_management/cluster.go deleted file mode 100644 index bfe7b506..00000000 --- a/api/index_management/cluster.go +++ /dev/null @@ -1,17 +0,0 @@ -package index_management - -import ( - "net/http" - httprouter "infini.sh/framework/core/api/router" - "infini.sh/framework/core/elastic" -) - -func (handler APIHandler) GetClusterVersion(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { - client := elastic.GetClient(handler.Config.Elasticsearch) - ver := client.GetMajorVersion() - resBody := newResponseBody() - resBody["payload"] = map[string]int{ - "major": ver, - } - handler.WriteJSON(w, resBody, http.StatusOK) -} \ No newline at end of file diff --git a/api/init.go b/api/init.go index 531033e7..f0e7d3a0 100644 --- a/api/init.go +++ b/api/init.go @@ -6,7 +6,7 @@ import ( "infini.sh/framework/core/task" "infini.sh/framework/core/ui" "infini.sh/search-center/api/index_management" - "infini.sh/search-center/api/system" + "infini.sh/search-center/api/cluster" "infini.sh/search-center/config" "path" ) @@ -48,10 +48,11 @@ func Init(cfg *config.AppConfig) { }, }) - shdl := system.APIHandler{ + shdl := cluster.APIHandler{ Config: cfg, } - ui.HandleUIMethod(api.GET, path.Join(pathPrefix, "/cluster/:cluster/version"), handler.GetClusterVersion) + 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) diff --git a/main.go b/main.go index 0b75873f..898c60b4 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "errors" _ "expvar" + "infini.sh/framework/core/elastic" "infini.sh/framework" "infini.sh/framework/core/env" @@ -71,7 +72,7 @@ func main() { }, func() { orm.RegisterSchemaWithIndexName(model.Dict{}, "dict") orm.RegisterSchemaWithIndexName(model.Reindex{}, "reindex") - orm.RegisterSchemaWithIndexName(model.ClusterConfig{}, "cluster") + orm.RegisterSchemaWithIndexName(elastic.ElasticsearchConfig{}, "cluster") }) } diff --git a/model/cluster.go b/model/cluster.go new file mode 100644 index 00000000..2dc11f54 --- /dev/null +++ b/model/cluster.go @@ -0,0 +1,16 @@ +package model + +//type ClusterConfig struct { +// //ID string `json:"id" elastic_meta:"_id"` +// Name string `json:"name" elastic_mapping:"name:{type:text}"` +// Endpoint string `json:"endpoint" elastic_mapping:"endpoint:{type:text}"` +// BasicAuth struct { +// UserName string `json:"username,omitempty" elastic_mapping:"username:{type:keyword}"` +// Password string `json:"password,omitempty" elastic_mapping:"password:{type:keyword}" ` +// } `json:"basic_auth,omitempty" elastic_mapping:"basic_auth:{type:object}"` +// Order int `json:"order,omitempty" elastic_mapping:"order:{type:integer}"` +// Description string `json:"description,omitempty" elastic_mapping:"description:{type:text}"` +// Enabled bool `json:"enabled" elastic_mapping:"enabled:{type:boolean}"` +// Created time.Time `json:"created,omitempty" elastic_mapping:"created:{type:date}"` +// Updated time.Time `json:"updated,omitempty" elastic_mapping:"updated:{type:date}"` +//} diff --git a/model/cluster_config.go b/model/cluster_config.go deleted file mode 100644 index 07a7cd38..00000000 --- a/model/cluster_config.go +++ /dev/null @@ -1,18 +0,0 @@ -package model - -import "time" - -type ClusterConfig struct { - //ID string `json:"id" elastic_meta:"_id"` - Name string `json:"name" elastic_mapping:"name:{type:text}"` - Endpoint string `json:"endpoint" elastic_mapping:"endpoint:{type:text}"` - BasicAuth struct { - UserName string `json:"username,omitempty" elastic_mapping:"username:{type:keyword}"` - Password string `json:"password,omitempty" elastic_mapping:"password:{type:keyword}" ` - } `json:"basic_auth,omitempty" elastic_mapping:"basic_auth:{type:object}"` - Order int `json:"order,omitempty" elastic_mapping:"order:{type:integer}"` - Description string `json:"description,omitempty" elastic_mapping:"description:{type:text}"` - Enabled bool `json:"enabled" elastic_mapping:"enabled:{type:boolean}"` - Created time.Time `json:"created,omitempty" elastic_mapping:"created:{type:date}"` - Updated time.Time `json:"updated,omitempty" elastic_mapping:"updated:{type:date}"` -} diff --git a/web/mock/cluster/cluster.js b/web/mock/cluster/cluster.js index b54297c4..d1823292 100644 --- a/web/mock/cluster/cluster.js +++ b/web/mock/cluster/cluster.js @@ -26,6 +26,23 @@ export default { "name": "cluster1", "updated": "2021-02-20T16:03:30.867084+08:00" } + }, + { + "_index": ".infini-search-center_cluster", + "_type": "_doc", + "_id": "c0oc4kkgq9s8qss2uk51", + "_source": { + "basic_auth": { + "password": "123", + "username": "medcl" + }, + "created": "2021-02-20T16:03:30.867084+08:00", + "description": "xx业务集群2", + "enabled": false, + "endpoint": "http://localhost:9201", + "name": "cluster2", + "updated": "2021-02-20T16:03:30.867084+08:00" + } } ] } diff --git a/web/src/pages/Cluster/Metrics.js b/web/src/pages/Cluster/Metrics.js index b2fc670d..704281ce 100644 --- a/web/src/pages/Cluster/Metrics.js +++ b/web/src/pages/Cluster/Metrics.js @@ -138,8 +138,9 @@ const vstyle = { marginRight: "5px" }; -@connect(({clusterMonitor}) => ({ - clusterMonitor +@connect(({clusterMonitor,global}) => ({ + clusterMonitor, + selectedCluster: global.selectedCluster })) class ClusterMonitor extends PureComponent { @@ -183,10 +184,12 @@ class ClusterMonitor extends PureComponent { timeMask = 'YY-MM-DD' } this.setState({timeScale: {min: timeRange.min, max: timeRange.max, mask: timeMask}}); + // console.log(this.props.selectedCluster) dispatch({ type: 'clusterMonitor/fetchClusterMetrics', payload: { timeRange: timeRange, + cluster_id:this.props.selectedCluster?this.props.selectedCluster.id:'' }, }); } @@ -197,6 +200,9 @@ class ClusterMonitor extends PureComponent { componentDidMount() { const {match, location} = this.props; + + + let min = location.query.start || '2020-12-10 15:00'; let max = location.query.end || '2020-12-10 16:00'; min = moment(min, 'YYYY-MM-DD HH:mm'); @@ -462,7 +468,9 @@ class ClusterMonitor extends PureComponent { - + diff --git a/web/src/pages/Cluster/Overview.js b/web/src/pages/Cluster/Overview.js index ae7ef354..c10969c6 100644 --- a/web/src/pages/Cluster/Overview.js +++ b/web/src/pages/Cluster/Overview.js @@ -22,6 +22,7 @@ let HealthCircle = (props)=>{ @connect(({global}) => ({ selectedCluster: global.selectedCluster })) + class Overview extends React.Component { state = { data: [{id:"JFpIbacZQamv9hkgQEDZ2Q", name:"single-es", endpoint:"http://localhost:9200", health: "green", version: "7.10.0", uptime:"320883955"}] @@ -187,4 +188,4 @@ class Overview extends React.Component { } } -export default Overview; \ No newline at end of file +export default Overview; diff --git a/web/src/services/cluster.js b/web/src/services/cluster.js index fbe7195c..349f3eab 100644 --- a/web/src/services/cluster.js +++ b/web/src/services/cluster.js @@ -8,8 +8,8 @@ export async function getClusterVersion(params) { } export async function getClusterMetrics(params) { - let id = params.id; - delete(params['id']); + let id = params.cluster_id; + delete(params['cluster_id']); return request(`${pathPrefix}/cluster/${id}/metrics`, { method: 'GET' });