update endpoint to es
This commit is contained in:
parent
d05c039b48
commit
9d769a0d65
|
@ -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)}
|
|
@ -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)
|
||||
}
|
|
@ -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{}
|
||||
|
|
11
api/init.go
11
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)
|
||||
}
|
||||
|
|
2
ui.go
2
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))
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 {
|
|||
// <Menu.Divider/>
|
||||
// <Menu.Item key="5"> */}
|
||||
<Input.Group compact>
|
||||
<Button style={{cursor: "default"}}>自动刷新间隔</Button>
|
||||
<Button style={{cursor: "default"}}>刷新间隔</Button>
|
||||
<InputNumber min={1} defaultValue={10} ref={el => this.refreshNum = el}/>
|
||||
<Select defaultValue="seconds" ref={el => this.refreshUnit = el}>
|
||||
<Select.Option value="seconds">秒</Select.Option>
|
||||
|
@ -472,6 +473,9 @@ class ClusterMonitor extends PureComponent {
|
|||
// title={this.props.selectedCluster?this.props.selectedCluster.name:''}
|
||||
style={{marginBottom: 5}}>
|
||||
<Row>
|
||||
<Col md={2} xs={4}>
|
||||
<Statistic valueStyle={vstyle} title="集群名称" value={clusterStats.cluster_name}/>
|
||||
</Col>
|
||||
<Col md={2} xs={4}>
|
||||
<Statistic valueStyle={vstyle} title="在线时长" value={clusterStats.uptime}/>
|
||||
</Col>
|
||||
|
|
|
@ -2,7 +2,7 @@ import request from '@/utils/request';
|
|||
import {buildQueryArgs, pathPrefix} from './common';
|
||||
|
||||
export async function getClusterVersion(params) {
|
||||
return request(`${pathPrefix}/cluster/${params.cluster}/version`, {
|
||||
return request(`/elasticsearch/${params.cluster}/version`, {
|
||||
method: 'GET'
|
||||
});
|
||||
}
|
||||
|
@ -10,13 +10,13 @@ export async function getClusterVersion(params) {
|
|||
export async function getClusterMetrics(params) {
|
||||
let id = params.cluster_id;
|
||||
delete(params['cluster_id']);
|
||||
return request(`${pathPrefix}/cluster/${id}/metrics`, {
|
||||
return request(`/elasticsearch/${id}/metrics`, {
|
||||
method: 'GET'
|
||||
});
|
||||
}
|
||||
|
||||
export async function createClusterConfig(params) {
|
||||
return request(`${pathPrefix}/cluster`, {
|
||||
return request(`/elasticsearch`, {
|
||||
method: 'POST',
|
||||
body: params,
|
||||
});
|
||||
|
@ -25,21 +25,21 @@ export async function createClusterConfig(params) {
|
|||
export async function updateClusterConfig(params) {
|
||||
let id = params.id;
|
||||
delete(params['id']);
|
||||
return request(`${pathPrefix}/cluster/${id}`, {
|
||||
return request(`/elasticsearch/${id}`, {
|
||||
method: 'PUT',
|
||||
body: params,
|
||||
});
|
||||
}
|
||||
|
||||
export async function deleteClusterConfig(params) {
|
||||
return request(`${pathPrefix}/cluster/${params.id}`, {
|
||||
return request(`/elasticsearch/${params.id}`, {
|
||||
method: 'DELETE',
|
||||
body: params,
|
||||
});
|
||||
}
|
||||
|
||||
export async function searchClusterConfig(params) {
|
||||
let url = `${pathPrefix}/cluster/_search`;
|
||||
let url = `/elasticsearch/_search`;
|
||||
let args = buildQueryArgs({
|
||||
name: params.name,
|
||||
enabled: params.enabled
|
||||
|
|
Loading…
Reference in New Issue