mirror of
https://github.com/tencentmusic/supersonic.git
synced 2025-12-13 21:17:08 +00:00
[improvement][project]Use DatePeriodEnum to replace DAY/MONTH/YEAR period constants.
This commit is contained in:
@@ -3,70 +3,40 @@ package com.tencent.supersonic.common.pojo;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class Constants {
|
||||
|
||||
public static final String COMMA = ",";
|
||||
public static final String DOUBLE_SLASH = "//";
|
||||
public static final String EMPTY = "";
|
||||
public static final String AT_SYMBOL = "@";
|
||||
public static final String DOT = ".";
|
||||
public static String SPACE = " ";
|
||||
public static String POUND = "#";
|
||||
public static final String SPACE = " ";
|
||||
public static final String POUND = "#";
|
||||
public static final String COLON = ":";
|
||||
public static final String MINUS = "-";
|
||||
public static final String UNDERLINE = "_";
|
||||
public static final String DICT_VALUE = "v";
|
||||
public static final String UNDERLINE_DOUBLE = "__";
|
||||
public static final String PARENTHESES_START = "(";
|
||||
public static final String PARENTHESES_END = ")";
|
||||
public static final String APOSTROPHE = "'";
|
||||
public static final String PERCENT_SIGN = "%";
|
||||
public static String LIMIT_UPPER = "LIMIT";
|
||||
public static String ORDER_UPPER = "ORDER";
|
||||
public static String DESC_UPPER = "DESC";
|
||||
public static String ASC_UPPER = "ASC";
|
||||
public static String GROUP_UPPER = "GROUP";
|
||||
public static String NULL_UPPER = "NULL";
|
||||
public static String AND_UPPER = "AND";
|
||||
public static String END_SUBQUERY = ") subq_";
|
||||
public static String SYS_VAR = "sys_var";
|
||||
public static final String DESC_UPPER = "DESC";
|
||||
public static final String ASC_UPPER = "ASC";
|
||||
public static final String AND_UPPER = "AND";
|
||||
public static final String SYS_VAR = "sys_var";
|
||||
public static final String NEW_LINE_CHAR = "\n";
|
||||
public static final String UNIONALL = " union all ";
|
||||
public static final String YAML_FILES_SUFFIX = ".yaml";
|
||||
public static final String JDBC_PREFIX_FORMATTER = "jdbc:%s:";
|
||||
public static final Pattern PATTERN_JDBC_TYPE = Pattern.compile("jdbc:\\w+");
|
||||
public static final String STATISTIC = "statistic";
|
||||
public static final String DORIS_LOWER = "doris";
|
||||
public static final String MYSQL_LOWER = "mysql";
|
||||
public static final String ADMIN_LOWER = "admin";
|
||||
|
||||
public static final String DAY = "DAY";
|
||||
public static final String DAY_FORMAT = "yyyy-MM-dd";
|
||||
public static final String MONTH_FORMAT = "yyyy-MM";
|
||||
public static final String TIMES_FORMAT = "yyyy-MM-dd HH:mm:ss.S";
|
||||
public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
|
||||
public static final String DAY_FORMAT_INT = "YYYYMMDD";
|
||||
public static final String MONTH_FORMAT_INT = "YYYYMM";
|
||||
public static final String MONTH = "MONTH";
|
||||
public static final String WEEK = "WEEK";
|
||||
public static final String YEAR = "YEAR";
|
||||
|
||||
public static final String JOIN_UNDERLINE = "__";
|
||||
|
||||
public static final String LIST_LOWER = "list";
|
||||
public static final String TOTAL_LOWER = "total";
|
||||
public static final String PAGESIZE_LOWER = "pageSize";
|
||||
public static final String TRUE_LOWER = "true";
|
||||
|
||||
public static final String NULL = "null";
|
||||
public static final Double MAX_SIMILARITY = 1.0d;
|
||||
public static final String CONTEXT = "CONTEXT";
|
||||
public static final String BRACKETS_START = "[";
|
||||
public static final String BRACKETS_END = "]";
|
||||
|
||||
public static final Long DEFAULT_FREQUENCY = 100000L;
|
||||
|
||||
public static final String TABLE_PREFIX = "t_";
|
||||
|
||||
public static final Long DEFAULT_DETAIL_LIMIT = 500L;
|
||||
public static final Long DEFAULT_METRIC_LIMIT = 200L;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.tencent.supersonic.common.pojo;
|
||||
|
||||
import com.tencent.supersonic.common.pojo.enums.DatePeriodEnum;
|
||||
import com.tencent.supersonic.common.pojo.enums.TimeDimensionEnum;
|
||||
import com.tencent.supersonic.common.util.DateUtils;
|
||||
import lombok.Data;
|
||||
@@ -29,8 +30,8 @@ public class DateConf {
|
||||
/** the last unit time unit, such as the last 7 days, unit = 7 */
|
||||
private Integer unit = 1;
|
||||
|
||||
/** DAY,WEEK,MONTH */
|
||||
private String period = Constants.DAY;
|
||||
/** DAY,WEEK,MONTH,QUARTER,YEAR */
|
||||
private DatePeriodEnum period = DatePeriodEnum.DAY;
|
||||
|
||||
/** the text parse from , example "last 7 days" , "last mouth" */
|
||||
private String detectWord;
|
||||
@@ -49,11 +50,11 @@ public class DateConf {
|
||||
}
|
||||
|
||||
public String getGroupByTimeDimension() {
|
||||
if (Constants.DAY.equals(period)) {
|
||||
if (DatePeriodEnum.DAY.equals(period)) {
|
||||
return TimeDimensionEnum.DAY.getName();
|
||||
} else if (Constants.WEEK.equals(period)) {
|
||||
} else if (DatePeriodEnum.WEEK.equals(period)) {
|
||||
return TimeDimensionEnum.WEEK.getName();
|
||||
} else if (Constants.MONTH.equals(period)) {
|
||||
} else if (DatePeriodEnum.MONTH.equals(period)) {
|
||||
return TimeDimensionEnum.MONTH.getName();
|
||||
} else {
|
||||
return TimeDimensionEnum.DAY.getName();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package com.tencent.supersonic.common.pojo.enums;
|
||||
|
||||
public enum DatePeriodEnum {
|
||||
DAY("日"),
|
||||
DAY("天"),
|
||||
WEEK("周"),
|
||||
MONTH("月"),
|
||||
QUARTER("季度"),
|
||||
@@ -24,4 +24,13 @@ public enum DatePeriodEnum {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static DatePeriodEnum fromChName(String chName) {
|
||||
for (DatePeriodEnum value : values()) {
|
||||
if (value.chName.equals(chName)) {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.tencent.supersonic.common.util;
|
||||
import com.tencent.supersonic.common.pojo.Constants;
|
||||
import com.tencent.supersonic.common.pojo.DateConf;
|
||||
import com.tencent.supersonic.common.pojo.ItemDateResp;
|
||||
import com.tencent.supersonic.common.pojo.enums.DatePeriodEnum;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@@ -23,12 +24,8 @@ import java.util.regex.Pattern;
|
||||
|
||||
import static com.tencent.supersonic.common.pojo.Constants.APOSTROPHE;
|
||||
import static com.tencent.supersonic.common.pojo.Constants.COMMA;
|
||||
import static com.tencent.supersonic.common.pojo.Constants.DAY;
|
||||
import static com.tencent.supersonic.common.pojo.Constants.DAY_FORMAT;
|
||||
import static com.tencent.supersonic.common.pojo.Constants.MONTH;
|
||||
import static com.tencent.supersonic.common.pojo.Constants.MONTH_FORMAT;
|
||||
import static com.tencent.supersonic.common.pojo.Constants.WEEK;
|
||||
import static com.tencent.supersonic.common.pojo.Constants.YEAR;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@@ -50,16 +47,6 @@ public class DateModeUtils {
|
||||
@Value("${s2.query.parameter.sys.zipper.end:end_}")
|
||||
private String sysZipperDateColEnd;
|
||||
|
||||
public Boolean recentMode(DateConf dateInfo) {
|
||||
if (Objects.nonNull(dateInfo)
|
||||
&& DateConf.DateMode.RECENT == dateInfo.getDateMode()
|
||||
&& DAY.equalsIgnoreCase(dateInfo.getPeriod())
|
||||
&& Objects.nonNull(dateInfo.getUnit())) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean hasAvailableDataMode(DateConf dateInfo) {
|
||||
if (Objects.nonNull(dateInfo) && DateConf.DateMode.AVAILABLE == dateInfo.getDateMode()) {
|
||||
return true;
|
||||
@@ -93,7 +80,7 @@ public class DateModeUtils {
|
||||
LocalDate endReq = LocalDate.parse(dateInfo.getEndDate(), formatter);
|
||||
|
||||
if (endReq.isAfter(endData)) {
|
||||
if (DAY.equalsIgnoreCase(dateInfo.getPeriod())) {
|
||||
if (DatePeriodEnum.DAY.equals(dateInfo.getPeriod())) {
|
||||
Long unit =
|
||||
getInterval(
|
||||
dateInfo.getStartDate(),
|
||||
@@ -106,7 +93,7 @@ public class DateModeUtils {
|
||||
"(%s >= '%s' and %s <= '%s')", sysDateCol, dateMin, sysDateCol, dateMax);
|
||||
}
|
||||
|
||||
if (MONTH.equalsIgnoreCase(dateInfo.getPeriod())) {
|
||||
if (DatePeriodEnum.MONTH.equals(dateInfo.getPeriod())) {
|
||||
Long unit =
|
||||
getInterval(
|
||||
dateInfo.getStartDate(),
|
||||
@@ -193,7 +180,8 @@ public class DateModeUtils {
|
||||
dateDate.getEndDate(),
|
||||
DateTimeFormatter.ofPattern(dateDate.getDateFormat()));
|
||||
List<ImmutablePair<String, String>> ret = new ArrayList<>();
|
||||
if (dateDate.getDatePeriod() != null && MONTH.equalsIgnoreCase(dateDate.getDatePeriod())) {
|
||||
if (dateDate.getDatePeriod() != null
|
||||
&& DatePeriodEnum.MONTH.equals(dateDate.getDatePeriod())) {
|
||||
Long unit =
|
||||
getInterval(
|
||||
dateInfo.getStartDate(),
|
||||
@@ -260,13 +248,13 @@ public class DateModeUtils {
|
||||
if (Objects.isNull(dateDate)) {
|
||||
return "";
|
||||
}
|
||||
if (DAY.equalsIgnoreCase(dateInfo.getPeriod())) {
|
||||
if (DatePeriodEnum.DAY.equals(dateInfo.getPeriod())) {
|
||||
return recentDayStr(dateDate, dateInfo);
|
||||
}
|
||||
if (MONTH.equalsIgnoreCase(dateInfo.getPeriod())) {
|
||||
if (DatePeriodEnum.MONTH.equals(dateInfo.getPeriod())) {
|
||||
return recentMonthStr(dateDate, dateInfo);
|
||||
}
|
||||
if (WEEK.equalsIgnoreCase(dateInfo.getPeriod())) {
|
||||
if (DatePeriodEnum.WEEK.equals(dateInfo.getPeriod())) {
|
||||
return recentWeekStr(dateDate, dateInfo);
|
||||
}
|
||||
return "";
|
||||
@@ -279,7 +267,7 @@ public class DateModeUtils {
|
||||
* @return
|
||||
*/
|
||||
public String betweenDateStr(DateConf dateInfo) {
|
||||
if (MONTH.equalsIgnoreCase(dateInfo.getPeriod())) {
|
||||
if (DatePeriodEnum.MONTH.equals(dateInfo.getPeriod())) {
|
||||
// startDate YYYYMM
|
||||
if (!dateInfo.getStartDate().contains(Constants.MINUS)) {
|
||||
return String.format(
|
||||
@@ -302,7 +290,7 @@ public class DateModeUtils {
|
||||
sysDateMonthCol,
|
||||
endData.format(formatter));
|
||||
}
|
||||
if (WEEK.equalsIgnoreCase(dateInfo.getPeriod())) {
|
||||
if (DatePeriodEnum.WEEK.equals(dateInfo.getPeriod())) {
|
||||
return String.format(
|
||||
"%s >= '%s' and %s <= '%s'",
|
||||
sysDateWeekCol, dateInfo.getStartDate(), sysDateWeekCol, dateInfo.getEndDate());
|
||||
@@ -322,10 +310,10 @@ public class DateModeUtils {
|
||||
StringJoiner joiner = new StringJoiner(COMMA);
|
||||
dateInfo.getDateList().stream().forEach(date -> joiner.add(APOSTROPHE + date + APOSTROPHE));
|
||||
String dateCol = sysDateCol;
|
||||
if (MONTH.equalsIgnoreCase(dateInfo.getPeriod())) {
|
||||
if (DatePeriodEnum.MONTH.equals(dateInfo.getPeriod())) {
|
||||
dateCol = sysDateMonthCol;
|
||||
}
|
||||
if (WEEK.equalsIgnoreCase(dateInfo.getPeriod())) {
|
||||
if (DatePeriodEnum.WEEK.equals(dateInfo.getPeriod())) {
|
||||
dateCol = sysDateWeekCol;
|
||||
}
|
||||
return String.format("(%s in (%s))", dateCol, joiner.toString());
|
||||
@@ -344,22 +332,22 @@ public class DateModeUtils {
|
||||
|
||||
Integer unit = dateInfo.getUnit();
|
||||
|
||||
if (DAY.equalsIgnoreCase(dateInfo.getPeriod())) {
|
||||
if (DatePeriodEnum.DAY.equals(dateInfo.getPeriod())) {
|
||||
LocalDate dateMax = LocalDate.now().minusDays(1);
|
||||
LocalDate dateMin = dateMax.minusDays(unit - 1);
|
||||
return String.format(
|
||||
"(%s >= '%s' and %s <= '%s')", sysDateCol, dateMin, sysDateCol, dateMax);
|
||||
}
|
||||
|
||||
if (WEEK.equalsIgnoreCase(dateInfo.getPeriod())) {
|
||||
if (DatePeriodEnum.WEEK.equals(dateInfo.getPeriod())) {
|
||||
LocalDate dateMax = LocalDate.now().minusDays(1);
|
||||
return recentWeekStr(dateMax, unit.longValue());
|
||||
}
|
||||
if (MONTH.equalsIgnoreCase(dateInfo.getPeriod())) {
|
||||
if (DatePeriodEnum.MONTH.equals(dateInfo.getPeriod())) {
|
||||
LocalDate dateMax = LocalDate.now().minusDays(1);
|
||||
return recentMonthStr(dateMax, unit.longValue(), MONTH_FORMAT);
|
||||
}
|
||||
if (YEAR.equalsIgnoreCase(dateInfo.getPeriod())) {
|
||||
if (DatePeriodEnum.YEAR.equals(dateInfo.getPeriod())) {
|
||||
LocalDate dateMax = LocalDate.now().minusDays(1);
|
||||
return recentMonthStr(dateMax, unit.longValue() * 12, MONTH_FORMAT);
|
||||
}
|
||||
@@ -398,13 +386,13 @@ public class DateModeUtils {
|
||||
}
|
||||
|
||||
public String getSysDateCol(DateConf dateInfo) {
|
||||
if (DAY.equalsIgnoreCase(dateInfo.getPeriod())) {
|
||||
if (DatePeriodEnum.DAY.equals(dateInfo.getPeriod())) {
|
||||
return sysDateCol;
|
||||
}
|
||||
if (WEEK.equalsIgnoreCase(dateInfo.getPeriod())) {
|
||||
if (DatePeriodEnum.WEEK.equals(dateInfo.getPeriod())) {
|
||||
return sysDateWeekCol;
|
||||
}
|
||||
if (MONTH.equalsIgnoreCase(dateInfo.getPeriod())) {
|
||||
if (DatePeriodEnum.MONTH.equals(dateInfo.getPeriod())) {
|
||||
return sysDateMonthCol;
|
||||
}
|
||||
return "";
|
||||
@@ -414,16 +402,16 @@ public class DateModeUtils {
|
||||
return Pattern.matches("[\\d\\s-:]+", date);
|
||||
}
|
||||
|
||||
public String getPeriodByCol(String col) {
|
||||
public DatePeriodEnum getPeriodByCol(String col) {
|
||||
if (sysDateCol.equalsIgnoreCase(col)) {
|
||||
return DAY;
|
||||
return DatePeriodEnum.DAY;
|
||||
}
|
||||
if (sysDateWeekCol.equalsIgnoreCase(col)) {
|
||||
return WEEK;
|
||||
return DatePeriodEnum.WEEK;
|
||||
}
|
||||
if (sysDateMonthCol.equalsIgnoreCase(col)) {
|
||||
return MONTH;
|
||||
return DatePeriodEnum.MONTH;
|
||||
}
|
||||
return "";
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package com.tencent.supersonic.common.util;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.tencent.supersonic.common.pojo.Constants;
|
||||
import com.tencent.supersonic.common.pojo.enums.DatePeriodEnum;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@@ -163,7 +162,8 @@ public class DateUtils {
|
||||
return !timeString.equals("00:00:00");
|
||||
}
|
||||
|
||||
public static List<String> getDateList(String startDateStr, String endDateStr, String period) {
|
||||
public static List<String> getDateList(
|
||||
String startDateStr, String endDateStr, DatePeriodEnum period) {
|
||||
try {
|
||||
LocalDate startDate = LocalDate.parse(startDateStr);
|
||||
LocalDate endDate = LocalDate.parse(endDateStr);
|
||||
@@ -171,10 +171,10 @@ public class DateUtils {
|
||||
LocalDate currentDate = startDate;
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
|
||||
while (!currentDate.isAfter(endDate)) {
|
||||
if (Constants.MONTH.equals(period)) {
|
||||
if (DatePeriodEnum.MONTH.equals(period)) {
|
||||
datesInRange.add(currentDate.format(formatter));
|
||||
currentDate = currentDate.plusMonths(1);
|
||||
} else if (Constants.WEEK.equals(period)) {
|
||||
} else if (DatePeriodEnum.WEEK.equals((period))) {
|
||||
datesInRange.add(currentDate.format(DateTimeFormatter.ISO_DATE));
|
||||
currentDate = currentDate.plusWeeks(1);
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user