-
Notifications
You must be signed in to change notification settings - Fork 999
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[alerter] Enhanced reporting of external general alert API (#1326)
- Loading branch information
Showing
6 changed files
with
206 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
alerter/src/main/java/org/dromara/hertzbeat/alert/dto/GeneralCloudAlertReport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package org.dromara.hertzbeat.alert.dto; | ||
|
||
import lombok.*; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.dromara.hertzbeat.alert.util.DateUtil; | ||
import org.dromara.hertzbeat.common.entity.dto.AlertReport; | ||
|
||
/** | ||
* 通用云端告警实体类 | ||
*/ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@EqualsAndHashCode(callSuper = true) | ||
public class GeneralCloudAlertReport extends AlertReport { | ||
|
||
/** | ||
* 告警日期时间 | ||
*/ | ||
private String alertDateTime; | ||
|
||
/** | ||
* 日期时间格式 | ||
*/ | ||
private String dateTimeFormat; | ||
|
||
/** | ||
* 可通过增强属性刷新告警时间的时间戳 | ||
*/ | ||
public void refreshAlertTime() { | ||
// 有时间戳,取时间戳 | ||
if (getAlertTime() != 0L) { | ||
return; | ||
} | ||
// 没有时间戳,判断是否有字符串配置 | ||
if (StringUtils.isNotBlank(alertDateTime)) { | ||
Long timeStamp = null; | ||
// 优先用户配置 | ||
if (StringUtils.isNotBlank(dateTimeFormat)) { | ||
timeStamp = DateUtil.getTimeStampFromFormat(alertDateTime, dateTimeFormat); | ||
} | ||
// 默认支持日期格式 | ||
if (timeStamp == null) { | ||
timeStamp = DateUtil.getTimeStampFromSomeFormats(alertDateTime); | ||
} | ||
// 解析成功 | ||
if (timeStamp != null) { | ||
setAlertTime(timeStamp); | ||
return; | ||
} | ||
} | ||
throw new RuntimeException("告警时间解析异常"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
alerter/src/main/java/org/dromara/hertzbeat/alert/util/DateUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.dromara.hertzbeat.alert.util; | ||
|
||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
|
||
/** | ||
* 记录一些常用的日期格式 | ||
*/ | ||
public class DateUtil { | ||
|
||
private static final String[] DATE_FORMATS = { | ||
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", | ||
"yyyy-MM-dd HH:mm:ss"}; | ||
|
||
/** | ||
* 常用日期格式转时间戳 | ||
*/ | ||
public static Long getTimeStampFromSomeFormats(String date) { | ||
SimpleDateFormat sdf; | ||
for (String dateFormat : DATE_FORMATS) { | ||
try { | ||
sdf = new SimpleDateFormat(dateFormat); | ||
return sdf.parse(date).getTime(); | ||
} catch (ParseException e) {} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* 指定日期格式转换时间戳 | ||
*/ | ||
public static Long getTimeStampFromFormat(String date, String format) { | ||
SimpleDateFormat sdf = new SimpleDateFormat(format); | ||
try { | ||
return sdf.parse(date).getTime(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("时间格式解析异常!"); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.