-
Notifications
You must be signed in to change notification settings - Fork 994
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Alert define add export and import function (#1499)
Signed-off-by: tomsun28 <tomsun28@outlook.com> Co-authored-by: a-little-fool <2030509072@qq.com> Co-authored-by: 淞筱 <105542329+a-little-fool@users.noreply.github.com> Co-authored-by: tomsun28 <tomsun28@outlook.com>
- Loading branch information
1 parent
9b6e45c
commit 33a8114
Showing
19 changed files
with
864 additions
and
14 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
48 changes: 48 additions & 0 deletions
48
alerter/src/main/java/org/dromara/hertzbeat/alert/service/AlertDefineImExportService.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,48 @@ | ||
package org.dromara.hertzbeat.alert.service; | ||
|
||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.util.List; | ||
|
||
|
||
/** | ||
* Configuration Import Export | ||
* 配置导入导出 | ||
* | ||
* @author a-little-fool | ||
* Created by a-little-fool on 2023/12/25 | ||
*/ | ||
public interface AlertDefineImExportService { | ||
/** | ||
* Import Configuration | ||
* 导入配置 | ||
* | ||
* @param is 输入流 | ||
*/ | ||
void importConfig(InputStream is); | ||
|
||
/** | ||
* Export Configuration | ||
* 导出配置 | ||
* | ||
* @param os 输出流 | ||
* @param configList 配置列表 | ||
*/ | ||
void exportConfig(OutputStream os, List<Long> configList); | ||
|
||
/** | ||
* Export file type | ||
* 导出文件类型 | ||
* | ||
* @return 文件类型 | ||
*/ | ||
String type(); | ||
|
||
/** | ||
* Get Export File Name | ||
* 获取导出文件名 | ||
* | ||
* @return 文件名 | ||
*/ | ||
String getFileName(); | ||
} |
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
138 changes: 138 additions & 0 deletions
138
...java/org/dromara/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.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,138 @@ | ||
package org.dromara.hertzbeat.alert.service.impl; | ||
|
||
|
||
import cn.afterturn.easypoi.excel.annotation.Excel; | ||
import cn.afterturn.easypoi.excel.annotation.ExcelTarget; | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import lombok.Data; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.dromara.hertzbeat.alert.service.AlertDefineImExportService; | ||
import org.dromara.hertzbeat.alert.service.AlertDefineService; | ||
import org.dromara.hertzbeat.common.entity.alerter.AlertDefine; | ||
import org.dromara.hertzbeat.common.entity.manager.TagItem; | ||
import org.springframework.beans.BeanUtils; | ||
import org.springframework.context.annotation.Lazy; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import javax.annotation.Resource; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.time.LocalDate; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* Configuration Import Export | ||
* 配置导入导出 | ||
* | ||
* @author a-little-fool | ||
* Created by a-little-fool on 2023/12/25 | ||
*/ | ||
@Slf4j | ||
public abstract class AlertDefineAbstractImExportServiceImpl implements AlertDefineImExportService { | ||
@Resource | ||
@Lazy | ||
private AlertDefineService alertDefineService; | ||
|
||
@Override | ||
public void importConfig(InputStream is) { | ||
var formList = parseImport(is) | ||
.stream() | ||
.map(this::convert) | ||
.collect(Collectors.toUnmodifiableList()); | ||
if (!CollectionUtils.isEmpty(formList)) { | ||
formList.forEach(alertDefine -> { | ||
alertDefineService.validate(alertDefine, false); | ||
alertDefineService.addAlertDefine(alertDefine); | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
public void exportConfig(OutputStream os, List<Long> configList) { | ||
var monitorList = configList.stream() | ||
.map(it -> alertDefineService.getAlertDefine(it)) | ||
.map(this::convert) | ||
.collect(Collectors.toUnmodifiableList()); | ||
writeOs(monitorList, os); | ||
} | ||
|
||
|
||
/** | ||
* Parsing an input stream into a form | ||
* 将输入流解析为表单 | ||
* | ||
* @param is 输入流 | ||
* @return 表单 | ||
*/ | ||
abstract List<ExportAlertDefineDTO> parseImport(InputStream is); | ||
|
||
/** | ||
* Export Configuration to Output Stream | ||
* 导出配置到输出流 | ||
* | ||
* @param exportAlertDefineList 配置列表 | ||
* @param os 输出流 | ||
*/ | ||
abstract void writeOs(List<ExportAlertDefineDTO> exportAlertDefineList, OutputStream os); | ||
|
||
|
||
private ExportAlertDefineDTO convert(AlertDefine alertDefine) { | ||
var exportAlertDefine = new ExportAlertDefineDTO(); | ||
var alertDefineDTO = new AlertDefineDTO(); | ||
BeanUtils.copyProperties(alertDefine, alertDefineDTO); | ||
exportAlertDefine.setAlertDefine(alertDefineDTO); | ||
return exportAlertDefine; | ||
} | ||
|
||
private AlertDefine convert(ExportAlertDefineDTO exportAlertDefineDTO) { | ||
var alertDefine = new AlertDefine(); | ||
var alertDefineDTO = exportAlertDefineDTO.getAlertDefine(); | ||
BeanUtils.copyProperties(alertDefineDTO, alertDefine); | ||
return alertDefine; | ||
} | ||
|
||
protected String | ||
fileNamePrefix() { | ||
return "hertzbeat_alertDefine_" + LocalDate.now(); | ||
} | ||
|
||
@Data | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@ExcelTarget(value = "ExportAlertDefineDTO") | ||
protected static class ExportAlertDefineDTO { | ||
@Excel(name = "AlertDefine") | ||
private AlertDefineDTO alertDefine; | ||
} | ||
|
||
@Data | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@ExcelTarget(value = "AlertDefineDTO") | ||
protected static class AlertDefineDTO { | ||
@Excel(name = "App") | ||
private String app; | ||
@Excel(name = "Metric") | ||
private String metric; | ||
@Excel(name = "Field") | ||
private String field; | ||
@Excel(name = "Preset") | ||
private Boolean preset; | ||
@Excel(name = "Expr") | ||
private String expr; | ||
@Excel(name = "Priority") | ||
private Byte priority; | ||
@Excel(name = "Times") | ||
private Integer times; | ||
@Excel(name = "Tags") | ||
private List<TagItem> tags; | ||
@Excel(name = "Enable") | ||
private Boolean enable; | ||
@Excel(name = "RecoverNotice") | ||
private Boolean recoverNotice; | ||
@Excel(name = "Template") | ||
private String template; | ||
} | ||
} |
Oops, something went wrong.