From 4476923079fbcd0c5464c15a90fa398172152b76 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Sun, 8 Sep 2024 17:56:47 +0800 Subject: [PATCH 01/12] [improve] extract some common methods Signed-off-by: yuluo-yx --- alerter/pom.xml | 15 ---- .../hertzbeat/alert/dto/AlertDefineDTO.java | 3 + .../AlertDefineExcelImExportServiceImpl.java | 58 ++++++--------- .../AlertDefineYamlImExportServiceImpl.java | 13 ++-- common/pom.xml | 14 ++++ .../common/util/export/ExcelExportUtils.java | 70 +++++++++++++++++++ .../common/util/export/YamlExportUtils.java | 44 ++++++++++++ manager/pom.xml | 5 -- .../impl/ExcelImExportServiceImpl.java | 24 ++----- .../service/impl/YamlImExportServiceImpl.java | 11 ++- 10 files changed, 166 insertions(+), 91 deletions(-) create mode 100644 common/src/main/java/org/apache/hertzbeat/common/util/export/ExcelExportUtils.java create mode 100644 common/src/main/java/org/apache/hertzbeat/common/util/export/YamlExportUtils.java 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/dto/AlertDefineDTO.java b/alerter/src/main/java/org/apache/hertzbeat/alert/dto/AlertDefineDTO.java index 0cf72fedae8..46480700e8d 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/dto/AlertDefineDTO.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/dto/AlertDefineDTO.java @@ -22,6 +22,8 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; import java.util.List; + +import lombok.Builder; import lombok.Data; import org.apache.hertzbeat.common.entity.manager.TagItem; @@ -30,6 +32,7 @@ */ @Data +@Builder @JsonInclude(JsonInclude.Include.NON_NULL) @JsonIgnoreProperties(ignoreUnknown = true) @ExcelTarget(value = "AlertDefineDTO") 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..b8be63acf02 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; @@ -154,19 +153,24 @@ private Byte getCellValueAsByte(Cell cell) { private AlertDefineDTO extractAlertDefineDataFromRow(Row row) { - AlertDefineDTO alertDefineDTO = new AlertDefineDTO(); - alertDefineDTO.setApp(getCellValueAsString(row.getCell(0))); - alertDefineDTO.setMetric(getCellValueAsString(row.getCell(1))); - alertDefineDTO.setField(getCellValueAsString(row.getCell(2))); - alertDefineDTO.setPreset(getCellValueAsBoolean(row.getCell(3))); - alertDefineDTO.setExpr(getCellValueAsString(row.getCell(4))); - alertDefineDTO.setPriority(getCellValueAsByte(row.getCell(5))); - alertDefineDTO.setTimes(getCellValueAsInteger(row.getCell(6))); - alertDefineDTO.setTags(extractTagDataFromRow(row.getCell(7))); - alertDefineDTO.setEnable(getCellValueAsBoolean(row.getCell(8))); - alertDefineDTO.setRecoverNotice(getCellValueAsBoolean(row.getCell(9))); - alertDefineDTO.setTemplate(getCellValueAsString(row.getCell(10))); - return alertDefineDTO; + + if (Objects.isNull(row)) { + return null; + } + + return AlertDefineDTO.builder() + .app(getCellValueAsString(row.getCell(0))) + .metric(getCellValueAsString(row.getCell(1))) + .field(getCellValueAsString(row.getCell(2))) + .preset(getCellValueAsBoolean(row.getCell(3))) + .expr(getCellValueAsString(row.getCell(4))) + .priority(getCellValueAsByte(row.getCell(5))) + .times(getCellValueAsInteger(row.getCell(6))) + .tags(extractTagDataFromRow(row.getCell(7))) + .enable(getCellValueAsBoolean(row.getCell(8))) + .recoverNotice(getCellValueAsBoolean(row.getCell(9))) + .template(getCellValueAsString(row.getCell(10))) + .build(); } @@ -178,30 +182,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, ExportAlertDefineDTO.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/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..02807b796c2 --- /dev/null +++ b/common/src/main/java/org/apache/hertzbeat/common/util/export/ExcelExportUtils.java @@ -0,0 +1,70 @@ +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()); + }); + headers.forEach(System.out::println); + + // set header + Row headerRow = sheet.createRow(0); + headers.forEach(header -> { + Cell cell = headerRow.createCell(headers.indexOf(header)); + cell.setCellValue(header); + 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/pom.xml b/manager/pom.xml index 63999336b2c..4f4c68350f4 100644 --- a/manager/pom.xml +++ b/manager/pom.xml @@ -175,11 +175,6 @@ h2 runtime - - cn.afterturn - easypoi-spring-boot-starter - ${easy-poi.version} - com.huaweicloud.sdk huaweicloud-sdk-smn 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..0b273e4dbaa 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,6 +31,7 @@ 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; @@ -218,29 +219,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..aa2c4c9e2c1 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 @@ -26,6 +26,8 @@ import java.util.List; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; + +import org.apache.hertzbeat.common.util.export.YamlExportUtils; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; @@ -74,11 +76,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); } + } From f32e0644403d98bf8f0b24e285953ffe8864b527 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Sun, 8 Sep 2024 17:59:39 +0800 Subject: [PATCH 02/12] fix Signed-off-by: yuluo-yx --- .../common/util/export/ExcelExportUtils.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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 index 02807b796c2..160538e518d 100644 --- 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 @@ -1,3 +1,18 @@ +/* + * 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; From ab1c7bf1ddf483899d62c5f7c5bf47d056102075 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Sun, 8 Sep 2024 18:02:10 +0800 Subject: [PATCH 03/12] fix Signed-off-by: yuluo-yx --- .../manager/service/impl/ExcelImExportServiceImpl.java | 2 -- .../hertzbeat/manager/service/impl/YamlImExportServiceImpl.java | 2 -- 2 files changed, 4 deletions(-) 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 0b273e4dbaa..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 @@ -36,8 +36,6 @@ 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; 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 aa2c4c9e2c1..c1543a9fe5f 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,8 +21,6 @@ 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; From 61479f45eea56fe7bdd71e4a7c8282a696311ec9 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Sun, 8 Sep 2024 18:09:08 +0800 Subject: [PATCH 04/12] fix Signed-off-by: yuluo-yx --- .../service/impl/AlertDefineAbstractImExportServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java index 121e17a9a5e..c6f0f0cdd79 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java @@ -83,7 +83,7 @@ public void exportConfig(OutputStream os, List configList) { private ExportAlertDefineDTO convert(AlertDefine alertDefine) { var exportAlertDefine = new ExportAlertDefineDTO(); - var alertDefineDTO = new AlertDefineDTO(); + var alertDefineDTO = AlertDefineDTO.builder().build(); BeanUtils.copyProperties(alertDefine, alertDefineDTO); exportAlertDefine.setAlertDefine(alertDefineDTO); return exportAlertDefine; From 40db1a61693794af640c6903d5105ddd15b92adf Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Sun, 8 Sep 2024 18:12:28 +0800 Subject: [PATCH 05/12] fix Signed-off-by: yuluo-yx --- .../hertzbeat/alert/dto/AlertDefineDTO.java | 1 - .../AlertDefineExcelImExportServiceImpl.java | 31 ++++++++----------- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/dto/AlertDefineDTO.java b/alerter/src/main/java/org/apache/hertzbeat/alert/dto/AlertDefineDTO.java index 46480700e8d..29b17ea56b8 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/dto/AlertDefineDTO.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/dto/AlertDefineDTO.java @@ -32,7 +32,6 @@ */ @Data -@Builder @JsonInclude(JsonInclude.Include.NON_NULL) @JsonIgnoreProperties(ignoreUnknown = true) @ExcelTarget(value = "AlertDefineDTO") 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 b8be63acf02..2a194b9638e 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 @@ -153,24 +153,19 @@ private Byte getCellValueAsByte(Cell cell) { private AlertDefineDTO extractAlertDefineDataFromRow(Row row) { - - if (Objects.isNull(row)) { - return null; - } - - return AlertDefineDTO.builder() - .app(getCellValueAsString(row.getCell(0))) - .metric(getCellValueAsString(row.getCell(1))) - .field(getCellValueAsString(row.getCell(2))) - .preset(getCellValueAsBoolean(row.getCell(3))) - .expr(getCellValueAsString(row.getCell(4))) - .priority(getCellValueAsByte(row.getCell(5))) - .times(getCellValueAsInteger(row.getCell(6))) - .tags(extractTagDataFromRow(row.getCell(7))) - .enable(getCellValueAsBoolean(row.getCell(8))) - .recoverNotice(getCellValueAsBoolean(row.getCell(9))) - .template(getCellValueAsString(row.getCell(10))) - .build(); + AlertDefineDTO alertDefineDTO = new AlertDefineDTO(); + alertDefineDTO.setApp(getCellValueAsString(row.getCell(0))); + alertDefineDTO.setMetric(getCellValueAsString(row.getCell(1))); + alertDefineDTO.setField(getCellValueAsString(row.getCell(2))); + alertDefineDTO.setPreset(getCellValueAsBoolean(row.getCell(3))); + alertDefineDTO.setExpr(getCellValueAsString(row.getCell(4))); + alertDefineDTO.setPriority(getCellValueAsByte(row.getCell(5))); + alertDefineDTO.setTimes(getCellValueAsInteger(row.getCell(6))); + alertDefineDTO.setTags(extractTagDataFromRow(row.getCell(7))); + alertDefineDTO.setEnable(getCellValueAsBoolean(row.getCell(8))); + alertDefineDTO.setRecoverNotice(getCellValueAsBoolean(row.getCell(9))); + alertDefineDTO.setTemplate(getCellValueAsString(row.getCell(10))); + return alertDefineDTO; } From a6a1911f3299047fc25edd091f21eff3495e0d72 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Sun, 8 Sep 2024 18:16:55 +0800 Subject: [PATCH 06/12] fix Signed-off-by: yuluo-yx --- .../java/org/apache/hertzbeat/alert/dto/AlertDefineDTO.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/dto/AlertDefineDTO.java b/alerter/src/main/java/org/apache/hertzbeat/alert/dto/AlertDefineDTO.java index 29b17ea56b8..0cf72fedae8 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/dto/AlertDefineDTO.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/dto/AlertDefineDTO.java @@ -22,8 +22,6 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; import java.util.List; - -import lombok.Builder; import lombok.Data; import org.apache.hertzbeat.common.entity.manager.TagItem; From ce087cda5e2ca7da86a37efa0e009b7067a80eb3 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Sun, 8 Sep 2024 18:19:37 +0800 Subject: [PATCH 07/12] fix Signed-off-by: yuluo-yx --- .../service/impl/AlertDefineAbstractImExportServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java index c6f0f0cdd79..121e17a9a5e 100644 --- a/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java +++ b/alerter/src/main/java/org/apache/hertzbeat/alert/service/impl/AlertDefineAbstractImExportServiceImpl.java @@ -83,7 +83,7 @@ public void exportConfig(OutputStream os, List configList) { private ExportAlertDefineDTO convert(AlertDefine alertDefine) { var exportAlertDefine = new ExportAlertDefineDTO(); - var alertDefineDTO = AlertDefineDTO.builder().build(); + var alertDefineDTO = new AlertDefineDTO(); BeanUtils.copyProperties(alertDefine, alertDefineDTO); exportAlertDefine.setAlertDefine(alertDefineDTO); return exportAlertDefine; From 7c7b283e04b4bcf1249f4058fee5eb642e215a94 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Sun, 8 Sep 2024 19:06:27 +0800 Subject: [PATCH 08/12] fix Signed-off-by: yuluo-yx --- .../AlertDefineExcelImExportServiceImpl.java | 2 +- .../AlertDefineExcelImExportServiceTest.java | 17 ++++++++++------- .../common/util/export/ExcelExportUtils.java | 14 +++++++++----- 3 files changed, 20 insertions(+), 13 deletions(-) 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 2a194b9638e..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 @@ -180,7 +180,7 @@ public void writeOs(List exportAlertDefineList, OutputStre Workbook workbook = new HSSFWorkbook(); String sheetName = "Export AlertDefine"; - Sheet sheet = ExcelExportUtils.setSheet(sheetName, workbook, ExportAlertDefineDTO.class); + Sheet sheet = ExcelExportUtils.setSheet(sheetName, workbook, AlertDefineDTO.class); // set cell style CellStyle cellStyle = ExcelExportUtils.setCellStyle(workbook); 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..de4a48a428f 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,6 +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.hertzbeat.common.util.export.ExcelExportUtils; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; @@ -56,13 +57,15 @@ 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 = 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/src/main/java/org/apache/hertzbeat/common/util/export/ExcelExportUtils.java b/common/src/main/java/org/apache/hertzbeat/common/util/export/ExcelExportUtils.java index 160538e518d..4332d140968 100644 --- 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 @@ -16,7 +16,11 @@ package org.apache.hertzbeat.common.util.export; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; + +import org.apache.hertzbeat.common.entity.alerter.Alert; +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.Font; @@ -69,15 +73,15 @@ public static > Sheet setSheet(String sheetName, Workbook wor field.setAccessible(true); headers.add(field.getName()); }); - headers.forEach(System.out::println); // set header Row headerRow = sheet.createRow(0); - headers.forEach(header -> { - Cell cell = headerRow.createCell(headers.indexOf(header)); - cell.setCellValue(header); + 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; } From 4fd65a508ed5d215f4eb8e4573076e58b2238ab5 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Sun, 8 Sep 2024 19:08:58 +0800 Subject: [PATCH 09/12] fix Signed-off-by: yuluo-yx --- .../apache/hertzbeat/common/util/export/ExcelExportUtils.java | 4 ---- 1 file changed, 4 deletions(-) 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 index 4332d140968..ce0dc27632c 100644 --- 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 @@ -16,11 +16,7 @@ package org.apache.hertzbeat.common.util.export; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; - -import org.apache.hertzbeat.common.entity.alerter.Alert; -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.Font; From ba92d6414521eb52836fbc420f135d13e8a7deec Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Sun, 8 Sep 2024 19:11:23 +0800 Subject: [PATCH 10/12] fix Signed-off-by: yuluo-yx --- .../service/AlertDefineExcelImExportServiceTest.java | 9 --------- 1 file changed, 9 deletions(-) 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 de4a48a428f..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 @@ -29,7 +29,6 @@ import org.apache.hertzbeat.alert.service.impl.AlertDefineExcelImExportServiceImpl; import org.apache.hertzbeat.common.entity.manager.TagItem; import org.apache.hertzbeat.common.util.export.ExcelExportUtils; -import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; @@ -57,14 +56,6 @@ 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); From aec34a64778bb62d973a830cbaa72a9e4cb1c0cd Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Sun, 8 Sep 2024 19:16:27 +0800 Subject: [PATCH 11/12] fix Signed-off-by: yuluo-yx --- .../hertzbeat/manager/service/impl/YamlImExportServiceImpl.java | 2 -- 1 file changed, 2 deletions(-) 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 c1543a9fe5f..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 @@ -24,9 +24,7 @@ import java.util.List; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; - import org.apache.hertzbeat.common.util.export.YamlExportUtils; -import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; /** From 8e97a604c4f7acdfb21121bcc33eb04c35c13b47 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Sun, 8 Sep 2024 19:21:17 +0800 Subject: [PATCH 12/12] fix Signed-off-by: yuluo-yx --- manager/pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/manager/pom.xml b/manager/pom.xml index 4f4c68350f4..63999336b2c 100644 --- a/manager/pom.xml +++ b/manager/pom.xml @@ -175,6 +175,11 @@ h2 runtime + + cn.afterturn + easypoi-spring-boot-starter + ${easy-poi.version} + com.huaweicloud.sdk huaweicloud-sdk-smn