Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ public class AlertDefineDTO {
private String template;
@Excel(name = "Enable")
private Boolean enable;
@Excel(name = "Datasource")
private String datasource;
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ private AlertDefineDTO extractAlertDefineDataFromRow(Row row) {
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;
}

Expand Down Expand Up @@ -186,7 +187,7 @@ public void writeOs(List<ExportAlertDefineDTO> exportAlertDefineList, OutputStre
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);
Expand Down Expand Up @@ -227,6 +228,9 @@ public void writeOs(List<ExportAlertDefineDTO> exportAlertDefineList, OutputStre
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public void setUp() throws IOException {
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));

Expand All @@ -93,6 +94,7 @@ public void testParseImport() throws IOException {
assertEquals(Map.of("key", "value"), alertDefineDTO.getAnnotations());
assertEquals("template1", alertDefineDTO.getTemplate());
assertTrue(alertDefineDTO.getEnable());
assertEquals("promql", alertDefineDTO.getDatasource());
}
}

Expand All @@ -111,6 +113,7 @@ public void testWriteOs() throws IOException {
alertDefineDTO.setAnnotations(Map.of("key", "value"));
alertDefineDTO.setTemplate("template1");
alertDefineDTO.setEnable(true);
alertDefineDTO.setDatasource("promql");
exportAlertDefineDTO.setAlertDefine(alertDefineDTO);
exportAlertDefineList.add(exportAlertDefineDTO);

Expand All @@ -129,6 +132,7 @@ public void testWriteOs() throws IOException {
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());
Expand All @@ -140,6 +144,7 @@ public void testWriteOs() throws IOException {
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());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
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;
Expand All @@ -29,8 +31,10 @@
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}
Expand All @@ -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;
Expand Down Expand Up @@ -77,6 +81,7 @@ void testParseImport() {
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
Expand All @@ -100,6 +105,29 @@ void testWriteOs() {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class AlertDefineYamlImExportServiceTest {
times: 3
enable: true
template: Template1
datasource: promql
""";

private InputStream inputStream;
Expand All @@ -82,6 +83,7 @@ public void setup() {
alertDefine.setExpr("Expr1");
alertDefine.setEnable(true);
alertDefine.setTemplate("Template1");
alertDefine.setDatasource("promql");

ExportAlertDefineDTO exportAlertDefine = new ExportAlertDefineDTO();
exportAlertDefine.setAlertDefine(alertDefine);
Expand Down Expand Up @@ -135,6 +137,7 @@ void testWriteOs() {
assertTrue(yamlOutput.contains("name: App1"));
assertTrue(yamlOutput.contains("type: realtime"));
assertTrue(yamlOutput.contains("expr: Expr1"));
assertTrue(yamlOutput.contains("datasource: promql"));
}

@Test
Expand Down
Loading