refactoring elasticsearch model

This commit is contained in:
medcl 2021-02-24 00:09:13 +08:00
parent c7f184b466
commit d05c039b48
11 changed files with 89 additions and 53 deletions

View File

@ -1,4 +1,4 @@
package system package cluster
import ( import (
"fmt" "fmt"
@ -7,7 +7,6 @@ import (
"infini.sh/framework/core/elastic" "infini.sh/framework/core/elastic"
"infini.sh/framework/core/util" "infini.sh/framework/core/util"
"infini.sh/search-center/config" "infini.sh/search-center/config"
"infini.sh/search-center/model"
"infini.sh/framework/core/orm" "infini.sh/framework/core/orm"
"net/http" "net/http"
"strings" "strings"
@ -20,7 +19,7 @@ type APIHandler struct {
} }
func (h *APIHandler) HandleCreateClusterAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params){ 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{}{ resBody := map[string] interface{}{
} }
err := h.DecodeJSON(req, conf) err := h.DecodeJSON(req, conf)
@ -36,7 +35,7 @@ func (h *APIHandler) HandleCreateClusterAction(w http.ResponseWriter, req *http.
conf.Enabled=true conf.Enabled=true
conf.Updated = conf.Created conf.Updated = conf.Created
//conf.ID = id //conf.ID = id
index:=orm.GetIndexName(model.ClusterConfig{}) index:=orm.GetIndexName(elastic.ElasticsearchConfig{})
_, err = esClient.Index(index, "", id, conf) _, err = esClient.Index(index, "", id, conf)
if err != nil { if err != nil {
resBody["error"] = err resBody["error"] = err
@ -66,7 +65,7 @@ func (h *APIHandler) HandleUpdateClusterAction(w http.ResponseWriter, req *http.
} }
id := ps.ByName("id") id := ps.ByName("id")
esClient := elastic.GetClient(h.Config.Elasticsearch) esClient := elastic.GetClient(h.Config.Elasticsearch)
indexName := orm.GetIndexName(model.ClusterConfig{}) indexName := orm.GetIndexName(elastic.ElasticsearchConfig{})
originConf, err := esClient.Get(indexName, "", id) originConf, err := esClient.Get(indexName, "", id)
if err != nil { if err != nil {
resBody["error"] = err.Error() resBody["error"] = err.Error()
@ -98,7 +97,7 @@ func (h *APIHandler) HandleDeleteClusterAction(w http.ResponseWriter, req *http.
} }
id := ps.ByName("id") id := ps.ByName("id")
esClient := elastic.GetClient(h.Config.Elasticsearch) 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 { if err != nil {
resBody["error"] = err.Error() resBody["error"] = err.Error()
@ -135,7 +134,7 @@ func (h *APIHandler) HandleSearchClusterAction(w http.ResponseWriter, req *http.
queryDSL = fmt.Sprintf(queryDSL, mustBuilder.String()) queryDSL = fmt.Sprintf(queryDSL, mustBuilder.String())
esClient := elastic.GetClient(h.Config.Elasticsearch) 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 { if err != nil {
resBody["error"] = err.Error() resBody["error"] = err.Error()
h.WriteJSON(w, resBody, http.StatusInternalServerError) 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) 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)}

16
api/cluster/version.go Normal file
View File

@ -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)
}

View File

@ -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)
}

View File

@ -6,7 +6,7 @@ import (
"infini.sh/framework/core/task" "infini.sh/framework/core/task"
"infini.sh/framework/core/ui" "infini.sh/framework/core/ui"
"infini.sh/search-center/api/index_management" "infini.sh/search-center/api/index_management"
"infini.sh/search-center/api/system" "infini.sh/search-center/api/cluster"
"infini.sh/search-center/config" "infini.sh/search-center/config"
"path" "path"
) )
@ -48,10 +48,11 @@ func Init(cfg *config.AppConfig) {
}, },
}) })
shdl := system.APIHandler{ shdl := cluster.APIHandler{
Config: cfg, 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.POST, path.Join(pathPrefix, "/cluster"), shdl.HandleCreateClusterAction)
ui.HandleUIMethod(api.PUT, path.Join(pathPrefix, "/cluster/:id"), shdl.HandleUpdateClusterAction) 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.DELETE, path.Join(pathPrefix, "/cluster/:id"), shdl.HandleDeleteClusterAction)

View File

@ -3,6 +3,7 @@ package main
import ( import (
"errors" "errors"
_ "expvar" _ "expvar"
"infini.sh/framework/core/elastic"
"infini.sh/framework" "infini.sh/framework"
"infini.sh/framework/core/env" "infini.sh/framework/core/env"
@ -71,7 +72,7 @@ func main() {
}, func() { }, func() {
orm.RegisterSchemaWithIndexName(model.Dict{}, "dict") orm.RegisterSchemaWithIndexName(model.Dict{}, "dict")
orm.RegisterSchemaWithIndexName(model.Reindex{}, "reindex") orm.RegisterSchemaWithIndexName(model.Reindex{}, "reindex")
orm.RegisterSchemaWithIndexName(model.ClusterConfig{}, "cluster") orm.RegisterSchemaWithIndexName(elastic.ElasticsearchConfig{}, "cluster")
}) })
} }

16
model/cluster.go Normal file
View File

@ -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}"`
//}

View File

@ -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}"`
}

View File

@ -26,6 +26,23 @@ export default {
"name": "cluster1", "name": "cluster1",
"updated": "2021-02-20T16:03:30.867084+08:00" "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"
}
} }
] ]
} }

View File

@ -138,8 +138,9 @@ const vstyle = {
marginRight: "5px" marginRight: "5px"
}; };
@connect(({clusterMonitor}) => ({ @connect(({clusterMonitor,global}) => ({
clusterMonitor clusterMonitor,
selectedCluster: global.selectedCluster
})) }))
class ClusterMonitor extends PureComponent { class ClusterMonitor extends PureComponent {
@ -183,10 +184,12 @@ class ClusterMonitor extends PureComponent {
timeMask = 'YY-MM-DD' timeMask = 'YY-MM-DD'
} }
this.setState({timeScale: {min: timeRange.min, max: timeRange.max, mask: timeMask}}); this.setState({timeScale: {min: timeRange.min, max: timeRange.max, mask: timeMask}});
// console.log(this.props.selectedCluster)
dispatch({ dispatch({
type: 'clusterMonitor/fetchClusterMetrics', type: 'clusterMonitor/fetchClusterMetrics',
payload: { payload: {
timeRange: timeRange, timeRange: timeRange,
cluster_id:this.props.selectedCluster?this.props.selectedCluster.id:''
}, },
}); });
} }
@ -197,6 +200,9 @@ class ClusterMonitor extends PureComponent {
componentDidMount() { componentDidMount() {
const {match, location} = this.props; const {match, location} = this.props;
let min = location.query.start || '2020-12-10 15:00'; let min = location.query.start || '2020-12-10 15:00';
let max = location.query.end || '2020-12-10 16:00'; let max = location.query.end || '2020-12-10 16:00';
min = moment(min, 'YYYY-MM-DD HH:mm'); min = moment(min, 'YYYY-MM-DD HH:mm');
@ -462,7 +468,9 @@ class ClusterMonitor extends PureComponent {
</Input.Group> </Input.Group>
</div> </div>
<Card style={{marginBottom: 5}}> <Card
// title={this.props.selectedCluster?this.props.selectedCluster.name:''}
style={{marginBottom: 5}}>
<Row> <Row>
<Col md={2} xs={4}> <Col md={2} xs={4}>
<Statistic valueStyle={vstyle} title="在线时长" value={clusterStats.uptime}/> <Statistic valueStyle={vstyle} title="在线时长" value={clusterStats.uptime}/>

View File

@ -22,6 +22,7 @@ let HealthCircle = (props)=>{
@connect(({global}) => ({ @connect(({global}) => ({
selectedCluster: global.selectedCluster selectedCluster: global.selectedCluster
})) }))
class Overview extends React.Component { class Overview extends React.Component {
state = { state = {
data: [{id:"JFpIbacZQamv9hkgQEDZ2Q", name:"single-es", endpoint:"http://localhost:9200", health: "green", version: "7.10.0", uptime:"320883955"}] 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; export default Overview;

View File

@ -8,8 +8,8 @@ export async function getClusterVersion(params) {
} }
export async function getClusterMetrics(params) { export async function getClusterMetrics(params) {
let id = params.id; let id = params.cluster_id;
delete(params['id']); delete(params['cluster_id']);
return request(`${pathPrefix}/cluster/${id}/metrics`, { return request(`${pathPrefix}/cluster/${id}/metrics`, {
method: 'GET' method: 'GET'
}); });