Skip to content

Commit

Permalink
Alert define add export and import function (#1499)
Browse files Browse the repository at this point in the history
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
4 people committed Mar 10, 2024
1 parent 9b6e45c commit 33a8114
Show file tree
Hide file tree
Showing 19 changed files with 864 additions and 14 deletions.
12 changes: 12 additions & 0 deletions alerter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,18 @@
<artifactId>springdoc-openapi-ui</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.3.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Predicate;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -126,5 +124,19 @@ public ResponseEntity<Message<Void>> deleteAlertDefines(
}
return ResponseEntity.ok(Message.success());
}


@GetMapping("/export")
@Operation(summary = "export alertDefine config", description = "导出告警阀值配置")
public void export(
@Parameter(description = "AlertDefine ID List | 告警阀值ID列表", example = "656937901") @RequestParam List<Long> ids,
@Parameter(description = "Export Type:JSON,EXCEL,YAML") @RequestParam(defaultValue = "JSON") String type,
HttpServletResponse res) throws Exception {
alertDefineService.export(ids, type, res);
}

@PostMapping("/import")
@Operation(summary = "import alertDefine config", description = "导入告警阀值配置")
public void importDefines(MultipartFile file) throws Exception {
alertDefineService.importConfig(file);
}
}
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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -128,4 +130,22 @@ public interface AlertDefineService {
* @return Associated information about the monitoring list
*/
List<AlertDefineMonitorBind> getBindAlertDefineMonitors(long alertDefineId);

/**
* Export file configuration of specified type based on ID list and export file type
* 根据ID列表、导出文件类型导出指定类型文件配置
* @param ids AlertDefine ID
* @param type File Type
* @param res Response
* @throws Exception An exception was thrown during the export
*/
void export(List<Long> ids, String type, HttpServletResponse res) throws Exception;

/**
* Add alarm threshold rules based on the uploaded alarm threshold file
* 根据上传的告警阀值文件,增加告警阀值规则
* @param file Upload File
* @throws Exception An exception was thrown during the importConfig
*/
void importConfig(MultipartFile file) throws Exception;
}
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;
}
}
Loading

0 comments on commit 33a8114

Please sign in to comment.