Skip to content

Commit 4feff12

Browse files
fix(alert): keep datasource in alert rule export/import (#4264)
Co-authored-by: Duansg <siguoduan@gmail.com>
1 parent 3e7c2bc commit 4feff12

5 files changed

Lines changed: 44 additions & 2 deletions

File tree

hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/dto/AlertDefineDTO.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,6 @@ public class AlertDefineDTO {
5151
private String template;
5252
@Excel(name = "Enable")
5353
private Boolean enable;
54+
@Excel(name = "Datasource")
55+
private String datasource;
5456
}

hertzbeat-alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineExcelImExportServiceImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ private AlertDefineDTO extractAlertDefineDataFromRow(Row row) {
155155
alertDefineDTO.setAnnotations(JsonUtil.fromJson(getCellValueAsString(row.getCell(6)), typeReference));
156156
alertDefineDTO.setTemplate(getCellValueAsString(row.getCell(7)));
157157
alertDefineDTO.setEnable(getCellValueAsBoolean(row.getCell(8)));
158+
alertDefineDTO.setDatasource(getCellValueAsString(row.getCell(9)));
158159
return alertDefineDTO;
159160
}
160161

@@ -186,7 +187,7 @@ public void writeOs(List<ExportAlertDefineDTO> exportAlertDefineList, OutputStre
186187
CellStyle cellStyle = workbook.createCellStyle();
187188
cellStyle.setAlignment(HorizontalAlignment.CENTER);
188189
// set header
189-
String[] headers = {"Name", "Type", "Expr", "Period", "Times", "Labels", "Annotations", "Template", "Enable"};
190+
String[] headers = {"Name", "Type", "Expr", "Period", "Times", "Labels", "Annotations", "Template", "Enable", "Datasource"};
190191
Row headerRow = sheet.createRow(0);
191192
for (int i = 0; i < headers.length; i++) {
192193
Cell cell = headerRow.createCell(i);
@@ -227,6 +228,9 @@ public void writeOs(List<ExportAlertDefineDTO> exportAlertDefineList, OutputStre
227228
Cell enableCell = row.createCell(8);
228229
enableCell.setCellValue(alertDefineDTO.getEnable());
229230
enableCell.setCellStyle(cellStyle);
231+
Cell datasourceCell = row.createCell(9);
232+
datasourceCell.setCellValue(alertDefineDTO.getDatasource());
233+
datasourceCell.setCellStyle(cellStyle);
230234
}
231235
workbook.write(os);
232236
os.close();

hertzbeat-alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertDefineExcelImExportServiceTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public void setUp() throws IOException {
6969
row.createCell(6).setCellValue(JsonUtil.toJson(Map.of("key", "value")));
7070
row.createCell(7).setCellValue("template1");
7171
row.createCell(8).setCellValue(true);
72+
row.createCell(9).setCellValue("promql");
7273

7374
ByteArrayInputStream inputStream = new ByteArrayInputStream(toByteArray(initialWorkbook));
7475

@@ -93,6 +94,7 @@ public void testParseImport() throws IOException {
9394
assertEquals(Map.of("key", "value"), alertDefineDTO.getAnnotations());
9495
assertEquals("template1", alertDefineDTO.getTemplate());
9596
assertTrue(alertDefineDTO.getEnable());
97+
assertEquals("promql", alertDefineDTO.getDatasource());
9698
}
9799
}
98100

@@ -111,6 +113,7 @@ public void testWriteOs() throws IOException {
111113
alertDefineDTO.setAnnotations(Map.of("key", "value"));
112114
alertDefineDTO.setTemplate("template1");
113115
alertDefineDTO.setEnable(true);
116+
alertDefineDTO.setDatasource("promql");
114117
exportAlertDefineDTO.setAlertDefine(alertDefineDTO);
115118
exportAlertDefineList.add(exportAlertDefineDTO);
116119

@@ -129,6 +132,7 @@ public void testWriteOs() throws IOException {
129132
assertEquals("Annotations", headerRow.getCell(6).getStringCellValue());
130133
assertEquals("Template", headerRow.getCell(7).getStringCellValue());
131134
assertEquals("Enable", headerRow.getCell(8).getStringCellValue());
135+
assertEquals("Datasource", headerRow.getCell(9).getStringCellValue());
132136

133137
Row dataRow = resultSheet.getRow(1);
134138
assertEquals("app1", dataRow.getCell(0).getStringCellValue());
@@ -140,6 +144,7 @@ public void testWriteOs() throws IOException {
140144
assertEquals(JsonUtil.toJson(Map.of("key", "value")), dataRow.getCell(6).getStringCellValue());
141145
assertEquals("template1", dataRow.getCell(7).getStringCellValue());
142146
assertTrue(dataRow.getCell(8).getBooleanCellValue());
147+
assertEquals("promql", dataRow.getCell(9).getStringCellValue());
143148
}
144149
}
145150
}

hertzbeat-alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertDefineJsonImExportServiceTest.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import static org.junit.jupiter.api.Assertions.assertNotNull;
2222
import static org.junit.jupiter.api.Assertions.assertNull;
2323
import static org.junit.jupiter.api.Assertions.assertTrue;
24+
import static org.mockito.Mockito.mock;
25+
import static org.mockito.Mockito.when;
2426
import java.io.ByteArrayInputStream;
2527
import java.io.ByteArrayOutputStream;
2628
import java.io.InputStream;
@@ -29,8 +31,10 @@
2931
import org.apache.hertzbeat.alert.dto.AlertDefineDTO;
3032
import org.apache.hertzbeat.alert.dto.ExportAlertDefineDTO;
3133
import org.apache.hertzbeat.alert.service.impl.AlertDefineJsonImExportServiceImpl;
34+
import org.apache.hertzbeat.common.entity.alerter.AlertDefine;
3235
import org.junit.jupiter.api.BeforeEach;
3336
import org.junit.jupiter.api.Test;
37+
import org.springframework.test.util.ReflectionTestUtils;
3438

3539
/**
3640
* test case for {@link AlertDefineJsonImExportServiceImpl}
@@ -43,7 +47,7 @@ class AlertDefineJsonImExportServiceTest {
4347
@SuppressWarnings("checkstyle:OperatorWrap")
4448
private static final String JSON_DATA = "[{\"alertDefine\":{\"name\":\"App1\",\"type\":\"realtime\"," +
4549
"\"expr\":\"Expr1\",\"period\":3000,\"times\":3," +
46-
"\"enable\":true,\"template\":\"Template1\"}}]";
50+
"\"enable\":true,\"template\":\"Template1\",\"datasource\":\"promql\"}}]";
4751

4852
private InputStream inputStream;
4953
private List<ExportAlertDefineDTO> alertDefineList;
@@ -77,6 +81,7 @@ void testParseImport() {
7781
assertEquals(1, result.size());
7882
assertEquals("App1", result.get(0).getAlertDefine().getName());
7983
assertEquals("realtime", result.get(0).getAlertDefine().getType());
84+
assertEquals("promql", result.get(0).getAlertDefine().getDatasource());
8085
}
8186

8287
@Test
@@ -100,6 +105,29 @@ void testWriteOs() {
100105
assertTrue(result.contains("realtime"));
101106
}
102107

108+
@Test
109+
void testExportKeepsDatasource() {
110+
AlertDefineService alertDefineService = mock(AlertDefineService.class);
111+
AlertDefine define = AlertDefine.builder()
112+
.name("test")
113+
.type("periodic_metric")
114+
.expr("cpu_usage{instance=\"server1\"} > 80")
115+
.datasource("promql")
116+
.period(300)
117+
.times(3)
118+
.template("test")
119+
.enable(true)
120+
.build();
121+
when(alertDefineService.getAlertDefine(1L)).thenReturn(define);
122+
ReflectionTestUtils.setField(service, "alertDefineService", alertDefineService);
123+
124+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
125+
service.exportConfig(outputStream, List.of(1L));
126+
127+
String result = outputStream.toString(StandardCharsets.UTF_8);
128+
assertTrue(result.contains("promql"), "exported config should keep datasource, but got: " + result);
129+
}
130+
103131
@Test
104132
void testType() {
105133
assertEquals("JSON", service.type());

hertzbeat-alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertDefineYamlImExportServiceTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class AlertDefineYamlImExportServiceTest {
6464
times: 3
6565
enable: true
6666
template: Template1
67+
datasource: promql
6768
""";
6869

6970
private InputStream inputStream;
@@ -82,6 +83,7 @@ public void setup() {
8283
alertDefine.setExpr("Expr1");
8384
alertDefine.setEnable(true);
8485
alertDefine.setTemplate("Template1");
86+
alertDefine.setDatasource("promql");
8587

8688
ExportAlertDefineDTO exportAlertDefine = new ExportAlertDefineDTO();
8789
exportAlertDefine.setAlertDefine(alertDefine);
@@ -135,6 +137,7 @@ void testWriteOs() {
135137
assertTrue(yamlOutput.contains("name: App1"));
136138
assertTrue(yamlOutput.contains("type: realtime"));
137139
assertTrue(yamlOutput.contains("expr: Expr1"));
140+
assertTrue(yamlOutput.contains("datasource: promql"));
138141
}
139142

140143
@Test

0 commit comments

Comments
 (0)