perf(定时报告): 发送完消息删除临时文件

This commit is contained in:
fit2cloud-chenyw 2022-10-31 14:20:04 +08:00
parent 6596ca9d71
commit 47bb7d29ce
4 changed files with 25 additions and 7 deletions

View File

@ -18,8 +18,9 @@ import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelUtils {
private static final String suffix = ".xlsx";
private static final String BASE_ROOT = "/opt/dataease/data/";
public static File exportExcel(List<ExcelSheetModel> sheets, String fileName) throws Exception {
public static File exportExcel(List<ExcelSheetModel> sheets, String fileName, String folderId) throws Exception {
AtomicReference<String> realFileName = new AtomicReference<>(fileName);
Workbook wb = new XSSFWorkbook();
@ -63,7 +64,14 @@ public class ExcelUtils {
if (!StringUtils.endsWith(fileName, suffix)) {
realFileName.set(realFileName.get() + suffix);
}
File result = new File("/opt/dataease/data/" + realFileName.get());
String folderPath = BASE_ROOT;
if (StringUtils.isNotBlank(folderId)) {
folderPath = BASE_ROOT + folderId + "/";
}
if (!FileUtil.exist(folderPath)) {
FileUtil.mkdir(folderPath);
}
File result = new File(folderPath + realFileName.get());
BufferedOutputStream outputStream = FileUtil.getOutputStream(result);
try {
wb.write(outputStream);

View File

@ -1,5 +1,6 @@
package io.dataease.job.sechedule.strategy.impl;
import cn.hutool.core.io.FileUtil;
import io.dataease.auth.entity.SysUserEntity;
import io.dataease.auth.entity.TokenInfo;
import io.dataease.auth.service.AuthUserService;
@ -160,6 +161,7 @@ public class EmailTaskHandler extends TaskHandler implements Job {
EmailXpackService emailXpackService = SpringContextUtil.getBean(EmailXpackService.class);
AuthUserServiceImpl userService = SpringContextUtil.getBean(AuthUserServiceImpl.class);
SysUserService sysUserService = SpringContextUtil.getBean(SysUserService.class);
List<File> files = null;
try {
XpackEmailTaskRequest taskForm = emailXpackService.taskForm(taskInstance.getTaskId());
if (ObjectUtils.isEmpty(taskForm) || CronUtils.taskExpire(taskForm.getEndTime())) {
@ -199,7 +201,7 @@ public class EmailTaskHandler extends TaskHandler implements Job {
contentStr = new String(content, "UTF-8");
}
List<File> files = null;
String viewIds = emailTemplateDTO.getViewIds();
ChartViewService chartViewService = SpringContextUtil.getBean(ChartViewService.class);
List<ViewOption> viewOptions = chartViewService.viewOptions(panelId);
@ -343,6 +345,14 @@ public class EmailTaskHandler extends TaskHandler implements Job {
} catch (Exception e) {
error(taskInstance, e);
LogUtil.error(e.getMessage(), e);
} finally {
if (CollectionUtils.isNotEmpty(files)) {
files.forEach(file -> {
if (file.exists()) {
FileUtil.del(file);
}
});
}
}
}

View File

@ -233,7 +233,7 @@ public class XEmailTaskServer {
ExcelSheetModel excelSheetModel = excelSheetModel(instanceDTOS);
List<ExcelSheetModel> sheetModels = new ArrayList<>();
sheetModels.add(excelSheetModel);
File file = ExcelUtils.exportExcel(sheetModels, null);
File file = ExcelUtils.exportExcel(sheetModels, null, null);
InputStream inputStream = new FileInputStream(file);
HttpServletResponse response = ServletUtils.response();
try {

View File

@ -51,7 +51,7 @@ public class ViewExportExcel {
Map<String, ChartExtRequest> stringChartExtRequestMap = buildViewRequest(panelDto, justView);
List<File> results = new ArrayList<>();
List<ExcelSheetModel> sheets = viewIds.stream().map(viewId -> viewFiles(viewId, stringChartExtRequestMap.get(viewId))).collect(Collectors.toList());
File excelFile = ExcelUtils.exportExcel(sheets, panelDto.getName());
File excelFile = ExcelUtils.exportExcel(sheets, panelDto.getName(), panelDto.getId());
results.add(excelFile);
return results;
}
@ -62,7 +62,7 @@ public class ViewExportExcel {
List<Map<String, Object>> components = gson.fromJson(componentsJson, tokenType);
String panelStyle = panelDto.getPanelStyle();
Map map = gson.fromJson(panelStyle, Map.class);
Map panelMap = (LinkedTreeMap)map.get("panel");
Map panelMap = (LinkedTreeMap) map.get("panel");
double resultCount = Double.parseDouble(panelMap.get("resultCount").toString());
String resultMode = panelMap.get("resultMode").toString();
@ -73,7 +73,7 @@ public class ViewExportExcel {
ChartExtRequest chartExtRequest = new ChartExtRequest();
chartExtRequest.setQueryFrom("panel");
chartExtRequest.setFilter(chartExtFilterRequests);
chartExtRequest.setResultCount((int)resultCount);
chartExtRequest.setResultCount((int) resultCount);
chartExtRequest.setResultMode(resultMode);
result.put(entry.getKey(), chartExtRequest);
}