From ae889bb0aef52f9df1f9621dc9ce638536075d5e Mon Sep 17 00:00:00 2001 From: jerryjzhang Date: Sat, 21 Sep 2024 18:13:31 +0800 Subject: [PATCH] [improvement][project]Optimize certain code structures. --- .../DefaultAuthenticationInterceptor.java | 1 + .../common/pojo/enums/ErrorCode.java | 15 ---------- .../common/pojo/enums/SinkDbEnum.java | 30 ------------------- .../common/pojo/enums/TimeMode.java | 7 ++--- .../supersonic/common/util/DateUtils.java | 27 +++++++++-------- .../chat/corrector/S2SqlDateHelper.java | 2 +- .../headless/core/utils/SqlUtils.java | 4 +-- .../service/impl/DownloadServiceImpl.java | 8 ++--- 8 files changed, 26 insertions(+), 68 deletions(-) delete mode 100644 common/src/main/java/com/tencent/supersonic/common/pojo/enums/ErrorCode.java delete mode 100644 common/src/main/java/com/tencent/supersonic/common/pojo/enums/SinkDbEnum.java diff --git a/auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/interceptor/DefaultAuthenticationInterceptor.java b/auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/interceptor/DefaultAuthenticationInterceptor.java index 13cd6755e..b0bdbcd1d 100644 --- a/auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/interceptor/DefaultAuthenticationInterceptor.java +++ b/auth/authentication/src/main/java/com/tencent/supersonic/auth/authentication/interceptor/DefaultAuthenticationInterceptor.java @@ -2,6 +2,7 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; + import com.tencent.supersonic.auth.api.authentication.annotation.AuthenticationIgnore; import com.tencent.supersonic.auth.api.authentication.config.AuthenticationConfig; import com.tencent.supersonic.auth.api.authentication.pojo.User; diff --git a/common/src/main/java/com/tencent/supersonic/common/pojo/enums/ErrorCode.java b/common/src/main/java/com/tencent/supersonic/common/pojo/enums/ErrorCode.java deleted file mode 100644 index 3762e0243..000000000 --- a/common/src/main/java/com/tencent/supersonic/common/pojo/enums/ErrorCode.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.tencent.supersonic.common.pojo.enums; - -public enum ErrorCode { - MULTIPLE_ERRORS_PLACEHOLDER, - MULTIPLE_ERRORS, - NULL_POINTER, - ILLEGAL_ARGUMENT, - ILLEGAL_STATE, - NO_PERMISSION, - INDEX_OUT_OF_BOUND, - DUPLICATED_THEME, - UNKNOWN; - - private ErrorCode() {} -} diff --git a/common/src/main/java/com/tencent/supersonic/common/pojo/enums/SinkDbEnum.java b/common/src/main/java/com/tencent/supersonic/common/pojo/enums/SinkDbEnum.java deleted file mode 100644 index b5e94996a..000000000 --- a/common/src/main/java/com/tencent/supersonic/common/pojo/enums/SinkDbEnum.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.tencent.supersonic.common.pojo.enums; - -public enum SinkDbEnum { - TDW("TDW"), - - DORIS("DORIS"), - - ICEBERY("ICEBERY"), - - NOT_SUPPORT("NOT_SUPPORT"); - - private String db; - - SinkDbEnum(String db) { - this.db = db; - } - - public String getDb() { - return db; - } - - public static SinkDbEnum of(String name) { - for (SinkDbEnum item : SinkDbEnum.values()) { - if (item.db.equalsIgnoreCase(name)) { - return item; - } - } - return SinkDbEnum.NOT_SUPPORT; - } -} diff --git a/common/src/main/java/com/tencent/supersonic/common/pojo/enums/TimeMode.java b/common/src/main/java/com/tencent/supersonic/common/pojo/enums/TimeMode.java index a2f0ae3cf..87dce1e95 100644 --- a/common/src/main/java/com/tencent/supersonic/common/pojo/enums/TimeMode.java +++ b/common/src/main/java/com/tencent/supersonic/common/pojo/enums/TimeMode.java @@ -1,11 +1,10 @@ package com.tencent.supersonic.common.pojo.enums; public enum TimeMode { - - // a specific date at N days ago + // a single date at N days ago LAST, - // a period of time from N days ago to today + // a period of date from N days ago to today RECENT, - // a period of time from the first day of current month/year to today + // a period of date from the first day of current month/year to today CURRENT } diff --git a/common/src/main/java/com/tencent/supersonic/common/util/DateUtils.java b/common/src/main/java/com/tencent/supersonic/common/util/DateUtils.java index 9e463fa86..84891c31c 100644 --- a/common/src/main/java/com/tencent/supersonic/common/util/DateUtils.java +++ b/common/src/main/java/com/tencent/supersonic/common/util/DateUtils.java @@ -24,11 +24,14 @@ @Slf4j public class DateUtils { - public static final String DATE_FORMAT = "yyyy-MM-dd"; - public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; - public static final String FORMAT = "yyyyMMddHHmmss"; - private static final DateTimeFormatter dateTimeFormatter = - DateTimeFormatter.ofPattern(DATE_FORMAT); + public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd"; + public static final String DEFAULT_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; + private static final DateTimeFormatter DEFAULT_DATE_FORMATTER2 = + DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT); + private static final SimpleDateFormat DEFAULT_DATE_FORMATTER = + new SimpleDateFormat(DEFAULT_DATE_FORMAT); + private static final SimpleDateFormat DEFAULT_TIME_FORMATTER = + new SimpleDateFormat(DEFAULT_DATE_FORMAT); public static DateTimeFormatter getDateFormatter(String date, String[] formats) { for (int i = 0; i < formats.length; i++) { @@ -66,13 +69,13 @@ public static String getBeforeDate(int intervalDay, DatePeriodEnum datePeriodEnu if (Objects.isNull(datePeriodEnum)) { datePeriodEnum = DatePeriodEnum.DAY; } - SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); + SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT); String currentDate = dateFormat.format(new Date()); return getBeforeDate(currentDate, intervalDay, datePeriodEnum); } public static String getBeforeDate(String currentDate, DatePeriodEnum datePeriodEnum) { - LocalDate specifiedDate = LocalDate.parse(currentDate, dateTimeFormatter); + LocalDate specifiedDate = LocalDate.parse(currentDate, DEFAULT_DATE_FORMATTER2); LocalDate startDate; switch (datePeriodEnum) { case MONTH: @@ -85,12 +88,12 @@ public static String getBeforeDate(String currentDate, DatePeriodEnum datePeriod startDate = specifiedDate; } - return startDate.format(dateTimeFormatter); + return startDate.format(DEFAULT_DATE_FORMATTER2); } public static String getBeforeDate( String currentDate, int intervalDay, DatePeriodEnum datePeriodEnum) { - LocalDate specifiedDate = LocalDate.parse(currentDate, dateTimeFormatter); + LocalDate specifiedDate = LocalDate.parse(currentDate, DEFAULT_DATE_FORMATTER2); LocalDate result = null; switch (datePeriodEnum) { case DAY: @@ -133,7 +136,7 @@ public static String getBeforeDate( default: } if (Objects.nonNull(result)) { - return result.format(DateTimeFormatter.ofPattern(DATE_FORMAT)); + return result.format(DEFAULT_DATE_FORMATTER2); } return null; @@ -142,9 +145,9 @@ public static String getBeforeDate( public static String format(Date date) { DateFormat dateFormat; if (containsTime(date)) { - dateFormat = new SimpleDateFormat(DateUtils.TIME_FORMAT); + dateFormat = DEFAULT_TIME_FORMATTER; } else { - dateFormat = new SimpleDateFormat(DateUtils.DATE_FORMAT); + dateFormat = DEFAULT_DATE_FORMATTER; } return dateFormat.format(date); } diff --git a/headless/chat/src/main/java/com/tencent/supersonic/headless/chat/corrector/S2SqlDateHelper.java b/headless/chat/src/main/java/com/tencent/supersonic/headless/chat/corrector/S2SqlDateHelper.java index 0022bca77..9108ad4a0 100644 --- a/headless/chat/src/main/java/com/tencent/supersonic/headless/chat/corrector/S2SqlDateHelper.java +++ b/headless/chat/src/main/java/com/tencent/supersonic/headless/chat/corrector/S2SqlDateHelper.java @@ -55,7 +55,7 @@ public static Pair calculateDateRange( private static String reformatDate(String dateStr, String format) { try { // Assuming the input date format is "yyyy-MM-dd" - SimpleDateFormat inputFormat = new SimpleDateFormat(DateUtils.DATE_FORMAT); + SimpleDateFormat inputFormat = new SimpleDateFormat(DateUtils.DEFAULT_DATE_FORMAT); Date date = inputFormat.parse(dateStr); SimpleDateFormat outputFormat = new SimpleDateFormat(format); return outputFormat.format(date); diff --git a/headless/core/src/main/java/com/tencent/supersonic/headless/core/utils/SqlUtils.java b/headless/core/src/main/java/com/tencent/supersonic/headless/core/utils/SqlUtils.java index 9385154ba..306ea1498 100644 --- a/headless/core/src/main/java/com/tencent/supersonic/headless/core/utils/SqlUtils.java +++ b/headless/core/src/main/java/com/tencent/supersonic/headless/core/utils/SqlUtils.java @@ -155,10 +155,10 @@ private Map getLineData(ResultSet rs, List queryCol private Object getValue(Object value) { if (value instanceof LocalDate) { LocalDate localDate = (LocalDate) value; - return localDate.format(DateTimeFormatter.ofPattern(DateUtils.DATE_FORMAT)); + return localDate.format(DateTimeFormatter.ofPattern(DateUtils.DEFAULT_DATE_FORMAT)); } else if (value instanceof LocalDateTime) { LocalDateTime localDateTime = (LocalDateTime) value; - return localDateTime.format(DateTimeFormatter.ofPattern(DateUtils.TIME_FORMAT)); + return localDateTime.format(DateTimeFormatter.ofPattern(DateUtils.DEFAULT_TIME_FORMAT)); } else if (value instanceof Date) { Date date = (Date) value; return DateUtils.format(date); diff --git a/headless/server/src/main/java/com/tencent/supersonic/headless/server/service/impl/DownloadServiceImpl.java b/headless/server/src/main/java/com/tencent/supersonic/headless/server/service/impl/DownloadServiceImpl.java index bf3b58837..26fde97d0 100644 --- a/headless/server/src/main/java/com/tencent/supersonic/headless/server/service/impl/DownloadServiceImpl.java +++ b/headless/server/src/main/java/com/tencent/supersonic/headless/server/service/impl/DownloadServiceImpl.java @@ -60,6 +60,8 @@ public class DownloadServiceImpl implements DownloadService { private static final long downloadSize = 10000; + private static final String dateFormat = "yyyyMMddHHmmss"; + private MetricService metricService; private DimensionService dimensionService; @@ -80,8 +82,7 @@ public void downloadByStruct( DownloadMetricReq downloadMetricReq, User user, HttpServletResponse response) throws Exception { String fileName = - String.format( - "%s_%s.xlsx", "supersonic", DateUtils.format(new Date(), DateUtils.FORMAT)); + String.format("%s_%s.xlsx", "supersonic", DateUtils.format(new Date(), dateFormat)); File file = FileUtils.createTmpFile(fileName); try { QueryStructReq queryStructReq = metricService.convert(downloadMetricReq); @@ -108,8 +109,7 @@ public void batchDownload( BatchDownloadReq batchDownloadReq, User user, HttpServletResponse response) throws Exception { String fileName = - String.format( - "%s_%s.xlsx", "supersonic", DateUtils.format(new Date(), DateUtils.FORMAT)); + String.format("%s_%s.xlsx", "supersonic", DateUtils.format(new Date(), dateFormat)); File file = FileUtils.createTmpFile(fileName); List metricIds = batchDownloadReq.getMetricIds(); if (CollectionUtils.isEmpty(metricIds)) {