diff --git a/alerter/pom.xml b/alerter/pom.xml
index 896bbba9fb7..50b87af6715 100644
--- a/alerter/pom.xml
+++ b/alerter/pom.xml
@@ -28,9 +28,6 @@
hertzbeat-alerter
${project.artifactId}
-
-
-
@@ -76,18 +73,6 @@
${easy-poi.version}
compile
-
- org.apache.poi
- poi
- ${poi.version}
- compile
-
-
- org.apache.poi
- poi-ooxml
- ${poi.version}
- compile
-
diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineExcelImExportServiceImpl.java b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineExcelImExportServiceImpl.java
index 5ea86d30d63..50540042ec8 100644
--- a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineExcelImExportServiceImpl.java
+++ b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineExcelImExportServiceImpl.java
@@ -31,12 +31,11 @@
import org.apache.hertzbeat.alert.dto.ExportAlertDefineDTO;
import org.apache.hertzbeat.common.entity.manager.TagItem;
import org.apache.hertzbeat.common.util.JsonUtil;
+import org.apache.hertzbeat.common.util.export.ExcelExportUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
-import org.apache.poi.ss.usermodel.Font;
-import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
@@ -178,30 +177,12 @@ private AlertDefineDTO extractAlertDefineDataFromRow(Row row) {
@Override
public void writeOs(List exportAlertDefineList, OutputStream os) {
try {
+
Workbook workbook = new HSSFWorkbook();
String sheetName = "Export AlertDefine";
- Sheet sheet = workbook.createSheet(sheetName);
- sheet.setDefaultColumnWidth(20);
- sheet.setColumnWidth(9, 40 * 256);
- sheet.setColumnWidth(10, 40 * 256);
- // set header style
- CellStyle headerCellStyle = workbook.createCellStyle();
- Font headerFont = workbook.createFont();
- headerFont.setBold(true);
- headerCellStyle.setFont(headerFont);
- headerCellStyle.setAlignment(HorizontalAlignment.CENTER);
+ Sheet sheet = ExcelExportUtils.setSheet(sheetName, workbook, AlertDefineDTO.class);
// set cell style
- CellStyle cellStyle = workbook.createCellStyle();
- cellStyle.setAlignment(HorizontalAlignment.CENTER);
- // set header
- String[] headers = {"app", "metric", "field", "preset", "expr", "priority", "times", "tags",
- "enable", "recoverNotice", "template"};
- Row headerRow = sheet.createRow(0);
- for (int i = 0; i < headers.length; i++) {
- Cell cell = headerRow.createCell(i);
- cell.setCellValue(headers[i]);
- cell.setCellStyle(headerCellStyle);
- }
+ CellStyle cellStyle = ExcelExportUtils.setCellStyle(workbook);
// Traverse the threshold rule list, each threshold rule object corresponds to a row of data
int rowIndex = 1;
diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineYamlImExportServiceImpl.java b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineYamlImExportServiceImpl.java
index f0598e8e5d7..1cee5dd3b69 100644
--- a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineYamlImExportServiceImpl.java
+++ b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineYamlImExportServiceImpl.java
@@ -21,13 +21,11 @@
import static org.apache.hertzbeat.common.constants.ExportFileConstants.YamlFile.TYPE;
import java.io.InputStream;
import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.nio.charset.StandardCharsets;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.alert.dto.ExportAlertDefineDTO;
import org.springframework.stereotype.Service;
-import org.yaml.snakeyaml.DumperOptions;
+import org.apache.hertzbeat.common.util.export.YamlExportUtils;
import org.yaml.snakeyaml.Yaml;
/**
@@ -56,11 +54,8 @@ public List parseImport(InputStream is) {
@Override
public void writeOs(List exportAlertDefineList, OutputStream os) {
- DumperOptions options = new DumperOptions();
- options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
- options.setIndent(2);
- options.setPrettyFlow(true);
- Yaml yaml = new Yaml(options);
- yaml.dump(exportAlertDefineList, new OutputStreamWriter(os, StandardCharsets.UTF_8));
+
+ YamlExportUtils.exportWriteOs(exportAlertDefineList, os);
}
+
}
diff --git a/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertDefineExcelImExportServiceTest.java b/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertDefineExcelImExportServiceTest.java
index deb9090b705..dd46b3e2230 100644
--- a/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertDefineExcelImExportServiceTest.java
+++ b/alerter/src/test/java/org/apache/hertzbeat/alert/service/AlertDefineExcelImExportServiceTest.java
@@ -28,7 +28,7 @@
import org.apache.hertzbeat.alert.dto.ExportAlertDefineDTO;
import org.apache.hertzbeat.alert.service.impl.AlertDefineExcelImExportServiceImpl;
import org.apache.hertzbeat.common.entity.manager.TagItem;
-import org.apache.poi.ss.usermodel.Cell;
+import org.apache.hertzbeat.common.util.export.ExcelExportUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
@@ -56,13 +56,7 @@ public class AlertDefineExcelImExportServiceTest {
public void setUp() throws IOException {
Workbook initialWorkbook = WorkbookFactory.create(true);
- Sheet initialSheet = initialWorkbook.createSheet();
- Row headerRow = initialSheet.createRow(0);
- String[] headers = {"app", "metric", "field", "preset", "expr", "priority", "times", "tags", "enable", "recoverNotice", "template"};
- for (int i = 0; i < headers.length; i++) {
- Cell cell = headerRow.createCell(i);
- cell.setCellValue(headers[i]);
- }
+ Sheet initialSheet = ExcelExportUtils.setSheet("Test sheet", initialWorkbook, AlertDefineDTO.class);
Row row = initialSheet.createRow(1);
row.createCell(0).setCellValue("app1");
diff --git a/common/pom.xml b/common/pom.xml
index 770733af86b..bfa3faa7c28 100644
--- a/common/pom.xml
+++ b/common/pom.xml
@@ -128,5 +128,19 @@
jutf7
${jutf7.version}
+
+
+ org.apache.poi
+ poi
+ ${poi.version}
+ compile
+
+
+ org.apache.poi
+ poi-ooxml
+ ${poi.version}
+ compile
+
+
diff --git a/common/src/main/java/org/apache/hertzbeat/common/util/export/ExcelExportUtils.java b/common/src/main/java/org/apache/hertzbeat/common/util/export/ExcelExportUtils.java
new file mode 100644
index 00000000000..ce0dc27632c
--- /dev/null
+++ b/common/src/main/java/org/apache/hertzbeat/common/util/export/ExcelExportUtils.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hertzbeat.common.util.export;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.Font;
+import org.apache.poi.ss.usermodel.HorizontalAlignment;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.springframework.util.ReflectionUtils;
+
+/**
+ * Excel export utils
+ */
+
+public final class ExcelExportUtils {
+
+ private ExcelExportUtils() {
+ }
+
+ /**
+ * set cell style
+ * @param workbook workbook entity
+ */
+ public static CellStyle setCellStyle(Workbook workbook) {
+
+ CellStyle cellStyle = workbook.createCellStyle();
+ cellStyle.setAlignment(HorizontalAlignment.CENTER);
+ return cellStyle;
+ }
+
+ /**
+ * @param clazz Export entity class
+ */
+ public static > Sheet setSheet(String sheetName, Workbook workbook, T clazz) {
+
+ var sheet = workbook.createSheet(sheetName);
+ sheet.setDefaultColumnWidth(20);
+ sheet.setColumnWidth(9, 40 * 256);
+ sheet.setColumnWidth(10, 40 * 256);
+
+ // set header style
+ CellStyle headerCellStyle = workbook.createCellStyle();
+ Font headerFont = workbook.createFont();
+ headerFont.setBold(true);
+ headerCellStyle.setFont(headerFont);
+ headerCellStyle.setAlignment(HorizontalAlignment.CENTER);
+
+ List headers = new ArrayList<>();
+ ReflectionUtils.doWithFields(clazz, field -> {
+
+ field.setAccessible(true);
+ headers.add(field.getName());
+ });
+
+ // set header
+ Row headerRow = sheet.createRow(0);
+ String[] headerArray = headers.toArray(new String[0]);
+ for (int i = 0; i < headerArray.length; i++) {
+ Cell cell = headerRow.createCell(i);
+ cell.setCellValue(headerArray[i]);
+ cell.setCellStyle(headerCellStyle);
+ }
+
+ return sheet;
+ }
+
+}
diff --git a/common/src/main/java/org/apache/hertzbeat/common/util/export/YamlExportUtils.java b/common/src/main/java/org/apache/hertzbeat/common/util/export/YamlExportUtils.java
new file mode 100644
index 00000000000..a084d726adf
--- /dev/null
+++ b/common/src/main/java/org/apache/hertzbeat/common/util/export/YamlExportUtils.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hertzbeat.common.util.export;
+
+import java.io.OutputStream;
+import java.io.OutputStreamWriter;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import org.yaml.snakeyaml.DumperOptions;
+import org.yaml.snakeyaml.Yaml;
+
+/**
+ * Yaml Export Utils
+ */
+
+public final class YamlExportUtils {
+
+ private YamlExportUtils() {
+ }
+
+ public static void exportWriteOs(List list, OutputStream os) {
+
+ var options = new DumperOptions();
+ options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
+ options.setIndent(2);
+ options.setPrettyFlow(true);
+ Yaml yaml = new Yaml(options);
+ yaml.dump(list, new OutputStreamWriter(os, StandardCharsets.UTF_8));
+ }
+
+}
diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ExcelImExportServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ExcelImExportServiceImpl.java
index 671d2a6a4a0..b0ab778c384 100644
--- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ExcelImExportServiceImpl.java
+++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/ExcelImExportServiceImpl.java
@@ -31,12 +31,11 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
+import org.apache.hertzbeat.common.util.export.ExcelExportUtils;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CellType;
-import org.apache.poi.ss.usermodel.Font;
-import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
@@ -218,29 +217,12 @@ private Byte getCellValueAsByte(Cell cell) {
@Override
public void writeOs(List monitorList, OutputStream os) {
try {
+
Workbook workbook = WorkbookFactory.create(true);
String sheetName = "Export Monitor";
- Sheet sheet = workbook.createSheet(sheetName);
- sheet.setDefaultColumnWidth(20);
- sheet.setColumnWidth(9, 40 * 256);
- sheet.setColumnWidth(10, 40 * 256);
- // set header style
- CellStyle headerCellStyle = workbook.createCellStyle();
- Font headerFont = workbook.createFont();
- headerFont.setBold(true);
- headerCellStyle.setFont(headerFont);
- headerCellStyle.setAlignment(HorizontalAlignment.CENTER);
+ Sheet sheet = ExcelExportUtils.setSheet(sheetName, workbook, ExportMonitorDTO.class);
// set cell style
- CellStyle cellStyle = workbook.createCellStyle();
- cellStyle.setAlignment(HorizontalAlignment.CENTER);
- // set header
- String[] headers = { "name", "app", "host", "intervals", "status", "description", "tags", "collector(default null if system dispatch)", "field", "type", "value", "metrics", "detected" };
- Row headerRow = sheet.createRow(0);
- for (int i = 0; i < headers.length; i++) {
- Cell cell = headerRow.createCell(i);
- cell.setCellValue(headers[i]);
- cell.setCellStyle(headerCellStyle);
- }
+ CellStyle cellStyle = ExcelExportUtils.setCellStyle(workbook);
// foreach monitor, each monitor object corresponds to a row of data
int rowIndex = 1;
diff --git a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/YamlImExportServiceImpl.java b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/YamlImExportServiceImpl.java
index 7ff529a787d..f43983b69b3 100644
--- a/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/YamlImExportServiceImpl.java
+++ b/manager/src/main/java/org/apache/hertzbeat/manager/service/impl/YamlImExportServiceImpl.java
@@ -21,12 +21,10 @@
import static org.apache.hertzbeat.common.constants.ExportFileConstants.YamlFile.TYPE;
import java.io.InputStream;
import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.nio.charset.StandardCharsets;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
-import org.yaml.snakeyaml.DumperOptions;
+import org.apache.hertzbeat.common.util.export.YamlExportUtils;
import org.yaml.snakeyaml.Yaml;
/**
@@ -74,11 +72,8 @@ public List parseImport(InputStream is) {
*/
@Override
public void writeOs(List monitorList, OutputStream os) {
- DumperOptions options = new DumperOptions();
- options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
- options.setIndent(2);
- options.setPrettyFlow(true);
- Yaml yaml = new Yaml(options);
- yaml.dump(monitorList, new OutputStreamWriter(os, StandardCharsets.UTF_8));
+
+ YamlExportUtils.exportWriteOs(monitorList, os);
}
+
}