From 416b5d34708608bdc6cf74559927abe5851c30d1 Mon Sep 17 00:00:00 2001 From: caojiaqiang Date: Wed, 26 Jun 2024 19:34:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dn9e=E5=88=9B=E5=BB=BA?= =?UTF-8?q?=E7=B4=A2=E5=BC=95=E6=A8=A1=E5=BC=8F=E6=97=B6=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E5=88=B0=E5=AF=B9=E5=BA=94=E7=9A=84=E8=A1=A8?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ly/ckibana/handlers/IndicesHandler.java | 72 ++++++++++++++ .../ly/ckibana/handlers/MappingHandler.java | 94 +++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 src/main/java/com/ly/ckibana/handlers/IndicesHandler.java create mode 100644 src/main/java/com/ly/ckibana/handlers/MappingHandler.java diff --git a/src/main/java/com/ly/ckibana/handlers/IndicesHandler.java b/src/main/java/com/ly/ckibana/handlers/IndicesHandler.java new file mode 100644 index 0000000..12e29ce --- /dev/null +++ b/src/main/java/com/ly/ckibana/handlers/IndicesHandler.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2023 LY.com All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.ly.ckibana.handlers; + +import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; +import com.ly.ckibana.configure.web.route.HttpRoute; +import com.ly.ckibana.constants.Constants; +import com.ly.ckibana.model.exception.FallbackToEsException; +import com.ly.ckibana.model.request.RequestContext; +import com.ly.ckibana.parser.ParamParser; +import com.ly.ckibana.service.EsClientUtil; +import com.ly.ckibana.util.JSONUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.http.HttpMethod; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.List; + +/** + * @author caojiaqiang + */ +@SuppressWarnings("rawtypes") +@Component +public class IndicesHandler extends BaseHandler { + + @Resource + private ParamParser paramParser; + + @Override + public List routes() { + return List.of( + HttpRoute.newRoute().path("_cat/indices/{index}").methods(HttpMethod.GET, HttpMethod.POST) + ); + } + + @SuppressWarnings("unchecked") + @Override + public String doHandle(RequestContext context) throws Exception { + String index = context.getIndex(); + // es6.x 查询索引列表时,传入index默认为 * 或 *:*,此时解析请求体,判断是否查询索引列表,如果是的话,就查询ck和es,merge后返回 + if (StringUtils.isEmpty(index)) { + throw new FallbackToEsException(); + } + if (!index.equals(Constants.MATCH_ALL) && !context.isCkIndex()) { + return EsClientUtil.doRequest(context); + } + + JSONArray result = new JSONArray(); + JSONObject object = new JSONObject(); + object.put("health", "green"); + object.put("status", "open"); + object.put("index", index); + result.add(object); + return JSONUtils.serialize(result); + } + +} diff --git a/src/main/java/com/ly/ckibana/handlers/MappingHandler.java b/src/main/java/com/ly/ckibana/handlers/MappingHandler.java new file mode 100644 index 0000000..2c3800b --- /dev/null +++ b/src/main/java/com/ly/ckibana/handlers/MappingHandler.java @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2023 LY.com All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.ly.ckibana.handlers; + +import com.alibaba.fastjson2.JSONObject; +import com.ly.ckibana.configure.web.route.HttpRoute; +import com.ly.ckibana.constants.Constants; +import com.ly.ckibana.model.compute.indexpattern.IndexPattern; +import com.ly.ckibana.model.exception.FallbackToEsException; +import com.ly.ckibana.model.request.ProxyConfig; +import com.ly.ckibana.model.request.RequestContext; +import com.ly.ckibana.parser.ParamParser; +import com.ly.ckibana.service.EsClientUtil; +import com.ly.ckibana.util.JSONUtils; +import org.apache.commons.lang3.StringUtils; +import org.springframework.http.HttpMethod; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.List; +import java.util.Map; + +/** + * @author caojiaqiang + */ +@SuppressWarnings("rawtypes") +@Component +public class MappingHandler extends BaseHandler { + + @Resource + private ParamParser paramParser; + + @Override + public List routes() { + return List.of( + HttpRoute.newRoute().path("/{index}/_mapping").methods(HttpMethod.GET, HttpMethod.POST) + ); + } + + @SuppressWarnings("unchecked") + @Override + public String doHandle(RequestContext context) throws Exception { + String index = context.getIndex(); + // es6.x 查询索引列表时,传入index默认为 * 或 *:*,此时解析请求体,判断是否查询索引列表,如果是的话,就查询ck和es,merge后返回 + if (StringUtils.isEmpty(index)) { + throw new FallbackToEsException(); + } + if (!index.equals(Constants.MATCH_ALL) && !context.isCkIndex()) { + return EsClientUtil.doRequest(context); + } + + ProxyConfig proxyConfig = context.getProxyConfig(); + IndexPattern indexPattern = proxyConfig.buildIndexPattern(context.getOriginalIndex(), index); + indexPattern.setTimeField(EsClientUtil.getIndexPatternMeta(context.getProxyConfig().getRestClient()).getOrDefault(index, null)); + Map fields = paramParser.queryIndexPatternFields(context, indexPattern, true); + + JSONObject properties = new JSONObject(); + for (Map.Entry entry : fields.entrySet()) { + JSONObject entryValue = entry.getValue(); + for (String key : entryValue.keySet()) { + properties.put(entry.getKey(), entryValue.getJSONObject(key)); + } + } + + JSONObject data = new JSONObject(); + data.put("properties", properties); + data.put("dynamic", false); + data.put("date_detection", false); + + JSONObject info = new JSONObject(); + info.put("info", data); + + JSONObject mapping = new JSONObject(); + mapping.put("mappings", info); + + JSONObject result = new JSONObject(); + result.put(index, mapping); + return JSONUtils.serialize(result); + } + +}