[improvement][project]Optimize certain code structures.

This commit is contained in:
jerryjzhang
2024-09-21 18:13:31 +08:00
parent 4193b84e83
commit ae889bb0ae
8 changed files with 26 additions and 68 deletions

View File

@@ -2,6 +2,7 @@ package com.tencent.supersonic.auth.authentication.interceptor;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.tencent.supersonic.auth.api.authentication.annotation.AuthenticationIgnore; 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.config.AuthenticationConfig;
import com.tencent.supersonic.auth.api.authentication.pojo.User; import com.tencent.supersonic.auth.api.authentication.pojo.User;

View File

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

View File

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

View File

@@ -1,11 +1,10 @@
package com.tencent.supersonic.common.pojo.enums; package com.tencent.supersonic.common.pojo.enums;
public enum TimeMode { public enum TimeMode {
// a single date at N days ago
// a specific date at N days ago
LAST, LAST,
// a period of time from N days ago to today // a period of date from N days ago to today
RECENT, 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 CURRENT
} }

View File

@@ -24,11 +24,14 @@ import java.util.Objects;
@Slf4j @Slf4j
public class DateUtils { public class DateUtils {
public static final String DATE_FORMAT = "yyyy-MM-dd"; public static final String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";
public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; public static final String DEFAULT_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
public static final String FORMAT = "yyyyMMddHHmmss"; private static final DateTimeFormatter DEFAULT_DATE_FORMATTER2 =
private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMAT);
DateTimeFormatter.ofPattern(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) { public static DateTimeFormatter getDateFormatter(String date, String[] formats) {
for (int i = 0; i < formats.length; i++) { for (int i = 0; i < formats.length; i++) {
@@ -66,13 +69,13 @@ public class DateUtils {
if (Objects.isNull(datePeriodEnum)) { if (Objects.isNull(datePeriodEnum)) {
datePeriodEnum = DatePeriodEnum.DAY; datePeriodEnum = DatePeriodEnum.DAY;
} }
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT); SimpleDateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_FORMAT);
String currentDate = dateFormat.format(new Date()); String currentDate = dateFormat.format(new Date());
return getBeforeDate(currentDate, intervalDay, datePeriodEnum); return getBeforeDate(currentDate, intervalDay, datePeriodEnum);
} }
public static String getBeforeDate(String currentDate, DatePeriodEnum 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; LocalDate startDate;
switch (datePeriodEnum) { switch (datePeriodEnum) {
case MONTH: case MONTH:
@@ -85,12 +88,12 @@ public class DateUtils {
startDate = specifiedDate; startDate = specifiedDate;
} }
return startDate.format(dateTimeFormatter); return startDate.format(DEFAULT_DATE_FORMATTER2);
} }
public static String getBeforeDate( public static String getBeforeDate(
String currentDate, int intervalDay, DatePeriodEnum datePeriodEnum) { String currentDate, int intervalDay, DatePeriodEnum datePeriodEnum) {
LocalDate specifiedDate = LocalDate.parse(currentDate, dateTimeFormatter); LocalDate specifiedDate = LocalDate.parse(currentDate, DEFAULT_DATE_FORMATTER2);
LocalDate result = null; LocalDate result = null;
switch (datePeriodEnum) { switch (datePeriodEnum) {
case DAY: case DAY:
@@ -133,7 +136,7 @@ public class DateUtils {
default: default:
} }
if (Objects.nonNull(result)) { if (Objects.nonNull(result)) {
return result.format(DateTimeFormatter.ofPattern(DATE_FORMAT)); return result.format(DEFAULT_DATE_FORMATTER2);
} }
return null; return null;
@@ -142,9 +145,9 @@ public class DateUtils {
public static String format(Date date) { public static String format(Date date) {
DateFormat dateFormat; DateFormat dateFormat;
if (containsTime(date)) { if (containsTime(date)) {
dateFormat = new SimpleDateFormat(DateUtils.TIME_FORMAT); dateFormat = DEFAULT_TIME_FORMATTER;
} else { } else {
dateFormat = new SimpleDateFormat(DateUtils.DATE_FORMAT); dateFormat = DEFAULT_DATE_FORMATTER;
} }
return dateFormat.format(date); return dateFormat.format(date);
} }

View File

@@ -55,7 +55,7 @@ public class S2SqlDateHelper {
private static String reformatDate(String dateStr, String format) { private static String reformatDate(String dateStr, String format) {
try { try {
// Assuming the input date format is "yyyy-MM-dd" // 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); Date date = inputFormat.parse(dateStr);
SimpleDateFormat outputFormat = new SimpleDateFormat(format); SimpleDateFormat outputFormat = new SimpleDateFormat(format);
return outputFormat.format(date); return outputFormat.format(date);

View File

@@ -155,10 +155,10 @@ public class SqlUtils {
private Object getValue(Object value) { private Object getValue(Object value) {
if (value instanceof LocalDate) { if (value instanceof LocalDate) {
LocalDate localDate = (LocalDate) value; 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) { } else if (value instanceof LocalDateTime) {
LocalDateTime localDateTime = (LocalDateTime) value; 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) { } else if (value instanceof Date) {
Date date = (Date) value; Date date = (Date) value;
return DateUtils.format(date); return DateUtils.format(date);

View File

@@ -60,6 +60,8 @@ public class DownloadServiceImpl implements DownloadService {
private static final long downloadSize = 10000; private static final long downloadSize = 10000;
private static final String dateFormat = "yyyyMMddHHmmss";
private MetricService metricService; private MetricService metricService;
private DimensionService dimensionService; private DimensionService dimensionService;
@@ -80,8 +82,7 @@ public class DownloadServiceImpl implements DownloadService {
DownloadMetricReq downloadMetricReq, User user, HttpServletResponse response) DownloadMetricReq downloadMetricReq, User user, HttpServletResponse response)
throws Exception { throws Exception {
String fileName = String fileName =
String.format( String.format("%s_%s.xlsx", "supersonic", DateUtils.format(new Date(), dateFormat));
"%s_%s.xlsx", "supersonic", DateUtils.format(new Date(), DateUtils.FORMAT));
File file = FileUtils.createTmpFile(fileName); File file = FileUtils.createTmpFile(fileName);
try { try {
QueryStructReq queryStructReq = metricService.convert(downloadMetricReq); QueryStructReq queryStructReq = metricService.convert(downloadMetricReq);
@@ -108,8 +109,7 @@ public class DownloadServiceImpl implements DownloadService {
BatchDownloadReq batchDownloadReq, User user, HttpServletResponse response) BatchDownloadReq batchDownloadReq, User user, HttpServletResponse response)
throws Exception { throws Exception {
String fileName = String fileName =
String.format( String.format("%s_%s.xlsx", "supersonic", DateUtils.format(new Date(), dateFormat));
"%s_%s.xlsx", "supersonic", DateUtils.format(new Date(), DateUtils.FORMAT));
File file = FileUtils.createTmpFile(fileName); File file = FileUtils.createTmpFile(fileName);
List<Long> metricIds = batchDownloadReq.getMetricIds(); List<Long> metricIds = batchDownloadReq.getMetricIds();
if (CollectionUtils.isEmpty(metricIds)) { if (CollectionUtils.isEmpty(metricIds)) {