mirror of
https://github.com/dromara/hertzbeat.git
synced 2026-09-17 09:40:58 +00:00
fix(alert): keep datasource in alert rule export/import (#4264)
Co-authored-by: Duansg <siguoduan@gmail.com>
This commit is contained in:
@@ -51,4 +51,6 @@ public class AlertDefineDTO {
|
||||
private String template;
|
||||
@Excel(name = "Enable")
|
||||
private Boolean enable;
|
||||
@Excel(name = "Datasource")
|
||||
private String datasource;
|
||||
}
|
||||
|
||||
+5
-1
@@ -155,6 +155,7 @@ public class AlertDefineExcelImExportServiceImpl extends AlertDefineAbstractImEx
|
||||
alertDefineDTO.setAnnotations(JsonUtil.fromJson(getCellValueAsString(row.getCell(6)), typeReference));
|
||||
alertDefineDTO.setTemplate(getCellValueAsString(row.getCell(7)));
|
||||
alertDefineDTO.setEnable(getCellValueAsBoolean(row.getCell(8)));
|
||||
alertDefineDTO.setDatasource(getCellValueAsString(row.getCell(9)));
|
||||
return alertDefineDTO;
|
||||
}
|
||||
|
||||
@@ -186,7 +187,7 @@ public class AlertDefineExcelImExportServiceImpl extends AlertDefineAbstractImEx
|
||||
CellStyle cellStyle = workbook.createCellStyle();
|
||||
cellStyle.setAlignment(HorizontalAlignment.CENTER);
|
||||
// set header
|
||||
String[] headers = {"Name", "Type", "Expr", "Period", "Times", "Labels", "Annotations", "Template", "Enable"};
|
||||
String[] headers = {"Name", "Type", "Expr", "Period", "Times", "Labels", "Annotations", "Template", "Enable", "Datasource"};
|
||||
Row headerRow = sheet.createRow(0);
|
||||
for (int i = 0; i < headers.length; i++) {
|
||||
Cell cell = headerRow.createCell(i);
|
||||
@@ -227,6 +228,9 @@ public class AlertDefineExcelImExportServiceImpl extends AlertDefineAbstractImEx
|
||||
Cell enableCell = row.createCell(8);
|
||||
enableCell.setCellValue(alertDefineDTO.getEnable());
|
||||
enableCell.setCellStyle(cellStyle);
|
||||
Cell datasourceCell = row.createCell(9);
|
||||
datasourceCell.setCellValue(alertDefineDTO.getDatasource());
|
||||
datasourceCell.setCellStyle(cellStyle);
|
||||
}
|
||||
workbook.write(os);
|
||||
os.close();
|
||||
|
||||
+5
@@ -69,6 +69,7 @@ public class AlertDefineExcelImExportServiceTest {
|
||||
row.createCell(6).setCellValue(JsonUtil.toJson(Map.of("key", "value")));
|
||||
row.createCell(7).setCellValue("template1");
|
||||
row.createCell(8).setCellValue(true);
|
||||
row.createCell(9).setCellValue("promql");
|
||||
|
||||
ByteArrayInputStream inputStream = new ByteArrayInputStream(toByteArray(initialWorkbook));
|
||||
|
||||
@@ -93,6 +94,7 @@ public class AlertDefineExcelImExportServiceTest {
|
||||
assertEquals(Map.of("key", "value"), alertDefineDTO.getAnnotations());
|
||||
assertEquals("template1", alertDefineDTO.getTemplate());
|
||||
assertTrue(alertDefineDTO.getEnable());
|
||||
assertEquals("promql", alertDefineDTO.getDatasource());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -111,6 +113,7 @@ public class AlertDefineExcelImExportServiceTest {
|
||||
alertDefineDTO.setAnnotations(Map.of("key", "value"));
|
||||
alertDefineDTO.setTemplate("template1");
|
||||
alertDefineDTO.setEnable(true);
|
||||
alertDefineDTO.setDatasource("promql");
|
||||
exportAlertDefineDTO.setAlertDefine(alertDefineDTO);
|
||||
exportAlertDefineList.add(exportAlertDefineDTO);
|
||||
|
||||
@@ -129,6 +132,7 @@ public class AlertDefineExcelImExportServiceTest {
|
||||
assertEquals("Annotations", headerRow.getCell(6).getStringCellValue());
|
||||
assertEquals("Template", headerRow.getCell(7).getStringCellValue());
|
||||
assertEquals("Enable", headerRow.getCell(8).getStringCellValue());
|
||||
assertEquals("Datasource", headerRow.getCell(9).getStringCellValue());
|
||||
|
||||
Row dataRow = resultSheet.getRow(1);
|
||||
assertEquals("app1", dataRow.getCell(0).getStringCellValue());
|
||||
@@ -140,6 +144,7 @@ public class AlertDefineExcelImExportServiceTest {
|
||||
assertEquals(JsonUtil.toJson(Map.of("key", "value")), dataRow.getCell(6).getStringCellValue());
|
||||
assertEquals("template1", dataRow.getCell(7).getStringCellValue());
|
||||
assertTrue(dataRow.getCell(8).getBooleanCellValue());
|
||||
assertEquals("promql", dataRow.getCell(9).getStringCellValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+29
-1
@@ -21,6 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
@@ -29,8 +31,10 @@ import java.util.List;
|
||||
import org.apache.hertzbeat.alert.dto.AlertDefineDTO;
|
||||
import org.apache.hertzbeat.alert.dto.ExportAlertDefineDTO;
|
||||
import org.apache.hertzbeat.alert.service.impl.AlertDefineJsonImExportServiceImpl;
|
||||
import org.apache.hertzbeat.common.entity.alerter.AlertDefine;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
/**
|
||||
* test case for {@link AlertDefineJsonImExportServiceImpl}
|
||||
@@ -43,7 +47,7 @@ class AlertDefineJsonImExportServiceTest {
|
||||
@SuppressWarnings("checkstyle:OperatorWrap")
|
||||
private static final String JSON_DATA = "[{\"alertDefine\":{\"name\":\"App1\",\"type\":\"realtime\"," +
|
||||
"\"expr\":\"Expr1\",\"period\":3000,\"times\":3," +
|
||||
"\"enable\":true,\"template\":\"Template1\"}}]";
|
||||
"\"enable\":true,\"template\":\"Template1\",\"datasource\":\"promql\"}}]";
|
||||
|
||||
private InputStream inputStream;
|
||||
private List<ExportAlertDefineDTO> alertDefineList;
|
||||
@@ -77,6 +81,7 @@ class AlertDefineJsonImExportServiceTest {
|
||||
assertEquals(1, result.size());
|
||||
assertEquals("App1", result.get(0).getAlertDefine().getName());
|
||||
assertEquals("realtime", result.get(0).getAlertDefine().getType());
|
||||
assertEquals("promql", result.get(0).getAlertDefine().getDatasource());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -100,6 +105,29 @@ class AlertDefineJsonImExportServiceTest {
|
||||
assertTrue(result.contains("realtime"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void testExportKeepsDatasource() {
|
||||
AlertDefineService alertDefineService = mock(AlertDefineService.class);
|
||||
AlertDefine define = AlertDefine.builder()
|
||||
.name("test")
|
||||
.type("periodic_metric")
|
||||
.expr("cpu_usage{instance=\"server1\"} > 80")
|
||||
.datasource("promql")
|
||||
.period(300)
|
||||
.times(3)
|
||||
.template("test")
|
||||
.enable(true)
|
||||
.build();
|
||||
when(alertDefineService.getAlertDefine(1L)).thenReturn(define);
|
||||
ReflectionTestUtils.setField(service, "alertDefineService", alertDefineService);
|
||||
|
||||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
service.exportConfig(outputStream, List.of(1L));
|
||||
|
||||
String result = outputStream.toString(StandardCharsets.UTF_8);
|
||||
assertTrue(result.contains("promql"), "exported config should keep datasource, but got: " + result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testType() {
|
||||
assertEquals("JSON", service.type());
|
||||
|
||||
+3
@@ -64,6 +64,7 @@ class AlertDefineYamlImExportServiceTest {
|
||||
times: 3
|
||||
enable: true
|
||||
template: Template1
|
||||
datasource: promql
|
||||
""";
|
||||
|
||||
private InputStream inputStream;
|
||||
@@ -82,6 +83,7 @@ class AlertDefineYamlImExportServiceTest {
|
||||
alertDefine.setExpr("Expr1");
|
||||
alertDefine.setEnable(true);
|
||||
alertDefine.setTemplate("Template1");
|
||||
alertDefine.setDatasource("promql");
|
||||
|
||||
ExportAlertDefineDTO exportAlertDefine = new ExportAlertDefineDTO();
|
||||
exportAlertDefine.setAlertDefine(alertDefine);
|
||||
@@ -135,6 +137,7 @@ class AlertDefineYamlImExportServiceTest {
|
||||
assertTrue(yamlOutput.contains("name: App1"));
|
||||
assertTrue(yamlOutput.contains("type: realtime"));
|
||||
assertTrue(yamlOutput.contains("expr: Expr1"));
|
||||
assertTrue(yamlOutput.contains("datasource: promql"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user