Skip to content

Commit

Permalink
[improve] extract some common methods (#2691)
Browse files Browse the repository at this point in the history
Signed-off-by: yuluo-yx <[email protected]>
  • Loading branch information
yuluo-yx authored Sep 9, 2024
1 parent c4c56c6 commit cd028d8
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 86 deletions.
15 changes: 0 additions & 15 deletions alerter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
<artifactId>hertzbeat-alerter</artifactId>
<name>${project.artifactId}</name>

<properties>
</properties>

<dependencies>
<!-- common -->
<dependency>
Expand Down Expand Up @@ -76,18 +73,6 @@
<version>${easy-poi.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -178,30 +177,12 @@ private AlertDefineDTO extractAlertDefineDataFromRow(Row row) {
@Override
public void writeOs(List<ExportAlertDefineDTO> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -56,11 +54,8 @@ public List<ExportAlertDefineDTO> parseImport(InputStream is) {

@Override
public void writeOs(List<ExportAlertDefineDTO> 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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down
14 changes: 14 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,19 @@
<artifactId>jutf7</artifactId>
<version>${jutf7.version}</version>
</dependency>

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>${poi.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -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 <T extends Class<?>> 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<String> 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;
}

}
Original file line number Diff line number Diff line change
@@ -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 <T> void exportWriteOs(List<T> 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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -218,29 +217,12 @@ private Byte getCellValueAsByte(Cell cell) {
@Override
public void writeOs(List<ExportMonitorDTO> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -74,11 +72,8 @@ public List<ExportMonitorDTO> parseImport(InputStream is) {
*/
@Override
public void writeOs(List<ExportMonitorDTO> 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);
}

}

0 comments on commit cd028d8

Please sign in to comment.