Merge branch 'master' of ssh://git.infini.ltd:64221/infini/search-center

This commit is contained in:
silenceqi 2021-02-27 23:37:46 +08:00
commit 1ba374a048
9 changed files with 20 additions and 198 deletions

View File

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

View File

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

View File

@ -104,6 +104,7 @@ func (handler APIHandler) HandleDeleteDocumentAction(w http.ResponseWriter, req
resResult["payload"] = true resResult["payload"] = true
handler.WriteJSON(w, resResult, http.StatusOK) handler.WriteJSON(w, resResult, http.StatusOK)
} }
func (handler APIHandler) HandleSearchDocumentAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { func (handler APIHandler) HandleSearchDocumentAction(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
client := elastic.GetClient(handler.Config.Elasticsearch) client := elastic.GetClient(handler.Config.Elasticsearch)
reqBody := docReqBody{} reqBody := docReqBody{}

View File

@ -6,7 +6,6 @@ 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/cluster"
"infini.sh/search-center/config" "infini.sh/search-center/config"
"path" "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
View File

@ -39,6 +39,6 @@ func (h UI) InitUI() {
response := map[string]interface{}{} response := map[string]interface{}{}
response["request"] = string(request) response["request"] = string(request)
w.Write(util.ToJSONBytes(request)) w.Write(util.MustToJSONBytes(request))
}) })
} }

View File

@ -1,5 +1,5 @@
export default { export default {
'GET /_search-center/cluster/_search': function(req, res){ 'GET /elasticsearch/_search': function(req, res){
res.send({ res.send({
"took": 0, "took": 0,
"timed_out": false, "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({ res.send({
"took": 0, "took": 0,
"timed_out": false, "timed_out": false,
@ -80,7 +80,7 @@ export default {
} }
}) })
}, },
'POST /_search-center/cluster': function(req, res){ 'POST /elasticsearch': function(req, res){
res.send({ res.send({
"_id": "c0oc4kkgq9s8qss2uk50", "_id": "c0oc4kkgq9s8qss2uk50",
"_source": { "_source": {
@ -98,7 +98,7 @@ export default {
"result": "created" "result": "created"
}); });
}, },
'PUT /_search-center/cluster/:id': function(req, res){ 'PUT /elasticsearch/:id': function(req, res){
res.send({ res.send({
"_id": "c0oc4kkgq9s8qss2uk50", "_id": "c0oc4kkgq9s8qss2uk50",
"_source": { "_source": {
@ -115,7 +115,7 @@ export default {
"result": "updated" "result": "updated"
}); });
}, },
'DELETE /_search-center/cluster/:id': function(req, res){ 'DELETE /elasticsearch/:id': function(req, res){
res.send({ res.send({
"_id": "c0oc4kkgq9s8qss2uk50", "_id": "c0oc4kkgq9s8qss2uk50",
"result": "deleted" "result": "deleted"

View File

@ -1,7 +1,8 @@
export default { export default {
'GET /_search-center/cluster/:id/metrics': function(req, res){ 'GET /elasticsearch/:id/metrics': function(req, res){
res.send({ res.send({
"summary" : { "summary" : {
"cluster_name" : "test-cluster",
"status" : "green", "status" : "green",
"used_store_bytes" : 2440421140, "used_store_bytes" : 2440421140,
"max_store_bytes" : 4440421140, "max_store_bytes" : 4440421140,

View File

@ -358,6 +358,7 @@ class ClusterMonitor extends PureComponent {
if (clusterMonitor.summary) { if (clusterMonitor.summary) {
let rawStats = clusterMonitor.summary; let rawStats = clusterMonitor.summary;
clusterStats = { clusterStats = {
cluster_name: rawStats.cluster_name,
status: rawStats.status, status: rawStats.status,
version: rawStats.version, version: rawStats.version,
nodes_count: rawStats.nodes_count, nodes_count: rawStats.nodes_count,
@ -425,7 +426,7 @@ class ClusterMonitor extends PureComponent {
// <Menu.Divider/> // <Menu.Divider/>
// <Menu.Item key="5"> */} // <Menu.Item key="5"> */}
<Input.Group compact> <Input.Group compact>
<Button style={{cursor: "default"}}>自动刷新间隔</Button> <Button style={{cursor: "default"}}>刷新间隔</Button>
<InputNumber min={1} defaultValue={10} ref={el => this.refreshNum = el}/> <InputNumber min={1} defaultValue={10} ref={el => this.refreshNum = el}/>
<Select defaultValue="seconds" ref={el => this.refreshUnit = el}> <Select defaultValue="seconds" ref={el => this.refreshUnit = el}>
<Select.Option value="seconds"></Select.Option> <Select.Option value="seconds"></Select.Option>
@ -472,6 +473,9 @@ class ClusterMonitor extends PureComponent {
// title={this.props.selectedCluster?this.props.selectedCluster.name:''} // title={this.props.selectedCluster?this.props.selectedCluster.name:''}
style={{marginBottom: 5}}> style={{marginBottom: 5}}>
<Row> <Row>
<Col md={2} xs={4}>
<Statistic valueStyle={vstyle} title="集群名称" value={clusterStats.cluster_name}/>
</Col>
<Col md={2} xs={4}> <Col md={2} xs={4}>
<Statistic valueStyle={vstyle} title="在线时长" value={clusterStats.uptime}/> <Statistic valueStyle={vstyle} title="在线时长" value={clusterStats.uptime}/>
</Col> </Col>

View File

@ -2,7 +2,7 @@ import request from '@/utils/request';
import {buildQueryArgs, pathPrefix} from './common'; import {buildQueryArgs, pathPrefix} from './common';
export async function getClusterVersion(params) { export async function getClusterVersion(params) {
return request(`${pathPrefix}/cluster/${params.cluster}/version`, { return request(`/elasticsearch/${params.cluster}/version`, {
method: 'GET' method: 'GET'
}); });
} }
@ -10,13 +10,13 @@ export async function getClusterVersion(params) {
export async function getClusterMetrics(params) { export async function getClusterMetrics(params) {
let id = params.cluster_id; let id = params.cluster_id;
delete(params['cluster_id']); delete(params['cluster_id']);
return request(`${pathPrefix}/cluster/${id}/metrics`, { return request(`/elasticsearch/${id}/metrics`, {
method: 'GET' method: 'GET'
}); });
} }
export async function createClusterConfig(params) { export async function createClusterConfig(params) {
return request(`${pathPrefix}/cluster`, { return request(`/elasticsearch`, {
method: 'POST', method: 'POST',
body: params, body: params,
}); });
@ -25,21 +25,21 @@ export async function createClusterConfig(params) {
export async function updateClusterConfig(params) { export async function updateClusterConfig(params) {
let id = params.id; let id = params.id;
delete(params['id']); delete(params['id']);
return request(`${pathPrefix}/cluster/${id}`, { return request(`/elasticsearch/${id}`, {
method: 'PUT', method: 'PUT',
body: params, body: params,
}); });
} }
export async function deleteClusterConfig(params) { export async function deleteClusterConfig(params) {
return request(`${pathPrefix}/cluster/${params.id}`, { return request(`/elasticsearch/${params.id}`, {
method: 'DELETE', method: 'DELETE',
body: params, body: params,
}); });
} }
export async function searchClusterConfig(params) { export async function searchClusterConfig(params) {
let url = `${pathPrefix}/cluster/_search`; let url = `/elasticsearch/_search`;
let args = buildQueryArgs({ let args = buildQueryArgs({
name: params.name, name: params.name,
enabled: params.enabled enabled: params.enabled