Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/java/com/ly/ckibana/util/RestUtils.java
  • Loading branch information
qzhello committed Jan 29, 2024
2 parents ce4b18a + a91a4bf commit 63797fe
Show file tree
Hide file tree
Showing 11 changed files with 73 additions and 62 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/ly/ckibana/handlers/ParamHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public String doHandle(RequestContext context) {
}

String body = proxyConfigLoader.getYaml().dumpAs(kibanaProperty, Tag.MAP, DumperOptions.FlowStyle.BLOCK);
String response = EsClientUtil.saveOne(proxyConfigLoader.getMetadataRestClient(), proxyConfigLoader.getSettingsIndexName(),
String response = EsClientUtil.saveOne(proxyConfigLoader.getMetadataRestClient(), proxyConfigLoader.getSettingsIndexName(),
"kibana",
Map.of(
"key", "kibana",
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ly/ckibana/parser/HitsResultParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ private HitsOptimizedResult doOptimizeSearchTimeRange(CkRequestContext ckRequest
*/
private CkRequest getByMinuteRequest(SortType sortType, CkRequestContext ckRequestContext, Range orgTimeClause) {
CkRequest byMinuteRequest = ProxyUtils.buildRequest(ckRequestContext.getTableName(), SqlConstants.COUNT_QUERY);
String timeField = ProxyUtils.generateTimeFieldSqlWithFormatUnixTimestamp64Milli(ckRequestContext.getIndexPattern().getTimeField(), orgTimeClause.getCkFieldType());
String timeField = ProxyUtils.generateTimeFieldSqlWithFormatUnixTimestamp64(ckRequestContext.getIndexPattern().getTimeField(), orgTimeClause.getCkFieldType());
byMinuteRequest.appendSelect(String.format(SqlConstants.TIME_AGG_BY_MINUTE_TEMPLATE, timeField, SqlConstants.CK_MINUTE_NAME));
byMinuteRequest.initGroupBy(SqlConstants.CK_MINUTE_NAME);
byMinuteRequest.orderBy(SqlConstants.CK_MINUTE_NAME + " " + sortType.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public List<SqlConverter> buildSelectSqlConvertors(Range timeRange) {
FieldSqlConverter fieldAggregation = new FieldSqlConverter();
fieldAggregation.setCondition(
SqlUtils.getFunctionString(SqlConstants.TO_INT64, "(%s) / %d",
ProxyUtils.generateTimeFieldSqlWithFormatUnixTimestamp64Milli(getField(), timeRange.getCkFieldType()),
ProxyUtils.generateTimeFieldSqlWithFormatUnixTimestamp64(getField(), timeRange.getCkFieldType()),
getInterval())
);
fieldAggregation.setName(queryFieldName());
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/ly/ckibana/util/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.sql.Timestamp;

@Slf4j
public class DateUtils {
Expand Down
32 changes: 6 additions & 26 deletions src/main/java/com/ly/ckibana/util/ProxyUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,10 @@ public static String getErrorResponse(String message) {
/**
* 时间查询sql转换,支持字符串(耗性能,不推荐)和数值类型,DateTime, DateTime64类型.
*/
public static String generateTimeFieldSqlWithFormatUnixTimestamp64Milli(String ckFieldName, String ckFieldType) {
if (isDateTime64Ms(ckFieldType)) {
public static String generateTimeFieldSqlWithFormatUnixTimestamp64(String ckFieldName, String ckFieldType) {
if (SqlUtils.isDateTime64(ckFieldType)) {
return String.format("toUnixTimestamp64Milli(%s)", getFieldSqlPart(ckFieldName));
} else if (isDateTime(ckFieldType)) {
} else if (SqlUtils.isDateTime(ckFieldType)) {
return String.format("toUnixTimestamp(%s)*1000", getFieldSqlPart(ckFieldName));
} else {
return getFieldSqlPart(ckFieldName);
Expand Down Expand Up @@ -274,33 +274,13 @@ public static Range getRangeWrappedBySqlFunction(Range orgRange, boolean isTimeF
return rangeConverted;
}

/**
* 是否为DateTime64时间类型.
*
* @param ckFieldType ck字段类型
* @return true:是DateTime64类型
*/
public static boolean isDateTime64Ms(String ckFieldType) {
return StringUtils.startsWith(ckFieldType, SqlConstants.TYPE_DATETIME64);
}

/**
* 是否为DateTime时间类型.
*
* @param ckFieldType ck字段类型
* @return true:是DateTime类型
*/
public static boolean isDateTime(String ckFieldType) {
return StringUtils.startsWith(ckFieldType, SqlConstants.TYPE_DATETIME);
}

/**
* 时间字段转换。作为值 or 字段.
*/
public static String generateTimeFieldSqlWithFormatDateTime64ZoneShangHai(Object value, String ckFieldType) {
if (isDateTime64Ms(ckFieldType)) {
return String.format("toDateTime64(%s/1000.0)", value.toString());
} else if (isDateTime(ckFieldType)) {
if (SqlUtils.isDateTime64(ckFieldType)) {
return String.format("toDateTime64(%s/1000,%d)", value.toString(), SqlUtils.getDateTime64Scale(ckFieldType));
} else if (SqlUtils.isDateTime(ckFieldType)) {
return String.format("toDateTime(%s/1000)", value.toString());
} else {
return value.toString();
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/ly/ckibana/util/RestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ public static RequestContext createRequestContext(String urls, Map<String, Strin
}

public static class EsHttpConfigCallback implements RestClientBuilder.HttpClientConfigCallback {

@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT)
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/ly/ckibana/util/SqlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,37 @@ public static String generateIpSql(Object value, IPType ipType, boolean isQueryV
public static String getSelectTemplate(String selectSql, String table) {
return String.format("SELECT %s FROM %s", selectSql, table);
}

/**
* 是否为DateTime64时间类型.
*
* @param ckFieldType ck字段类型
* @return true:是DateTime64类型
*/
public static boolean isDateTime64(String ckFieldType) {
return StringUtils.startsWith(ckFieldType, SqlConstants.TYPE_DATETIME64);
}

/**
* DateTime64时间类型,提取精度.
*
* @param ckFieldType ck字段类型,如DateTime64(3)
* @return 精度,如3
*/
public static int getDateTime64Scale(String ckFieldType) {
String scale = ckFieldType.replace(SqlConstants.TYPE_DATETIME64, StringUtils.EMPTY)
.replace(Constants.Symbol.LEFT_PARENTHESIS, StringUtils.EMPTY)
.replace(Constants.Symbol.RIGHT_PARENTHESIS, StringUtils.EMPTY);
return Integer.parseInt(scale);
}
/**
* 是否为DateTime时间类型.
*
* @param ckFieldType ck字段类型
* @return true:是DateTime类型
*/
public static boolean isDateTime(String ckFieldType) {
return StringUtils.startsWith(ckFieldType, SqlConstants.TYPE_DATETIME);
}

}
Loading

0 comments on commit 63797fe

Please sign in to comment.