Skip to content

Commit

Permalink
vipshop#687 导出选中的作业
Browse files Browse the repository at this point in the history
  • Loading branch information
cmzdandan committed Apr 15, 2020
1 parent 9dad8a3 commit 0872a7c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ public SuccessResponseEntity importJobs(final HttpServletRequest request,
return new SuccessResponseEntity(jobService.importJobs(namespace, file, getCurrentLoginUserName()));
}

// 导出全部的作业
@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
@Audit
@GetMapping(value = "/export")
Expand All @@ -429,6 +430,19 @@ public void exportJobs(final HttpServletRequest request, @AuditParam("namespace"
SaturnConsoleUtils.exportFile(response, exportJobFile, exportFileName, true);
}

// 导出选定的作业
@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
@Audit
@GetMapping(value = "/exportSelected")
public void exportSelectedJobs(final HttpServletRequest request, @AuditParam("namespace") @PathVariable String namespace,
@RequestParam(required = false) List<String> jobList, final HttpServletResponse response) throws SaturnJobConsoleException {
// assertIsPermitted(PermissionKeys.jobExport, namespace);
File exportJobFile = jobService.exportSelectedJobs(namespace, jobList);
String currentTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
String exportFileName = namespace + "_" + currentTime + ".xls";
SaturnConsoleUtils.exportFile(response, exportJobFile, exportFileName, true);
}

@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
@GetMapping(value = "/arrangeLayout")
public SuccessResponseEntity getArrangeLayout(final HttpServletRequest request, @PathVariable String namespace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ List<BatchJobResult> importJobs(String namespace, MultipartFile file, String cre

File exportJobs(String namespace) throws SaturnJobConsoleException;

File exportSelectedJobs(String namespace, List<String> jobList) throws SaturnJobConsoleException;

ArrangeLayout getArrangeLayout(String namespace) throws SaturnJobConsoleException;

JobConfig getJobConfigFromZK(String namespace, String jobName) throws SaturnJobConsoleException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1710,6 +1710,48 @@ public int compare(JobConfig o1, JobConfig o2) {
}
}

@Override
public File exportSelectedJobs(String namespace, List<String> jobList) throws SaturnJobConsoleException {
try {
File tmp = new File(SaturnConstants.CACHES_FILE_PATH,
"tmp_exportFile_" + System.currentTimeMillis() + "_" + random.nextInt(1000) + ".xls");
if (!tmp.exists()) {
FileUtils.forceMkdir(tmp.getParentFile());
tmp.createNewFile();
}
WritableWorkbook writableWorkbook = Workbook.createWorkbook(tmp);
WritableSheet sheet1 = writableWorkbook.createSheet("Sheet1", 0);
setExcelHeader(sheet1);
// 单个域下作业量不会很大,直接复用之前的方法取该域下全部作业再过滤
List<JobConfig> targetJobs = filterTargetJobs(jobList, getUnSystemJobs(namespace));
// sort by jobName
Collections.sort(targetJobs, new Comparator<JobConfig>() {
@Override
public int compare(JobConfig o1, JobConfig o2) {
return o1.getJobName().compareTo(o2.getJobName());
}
});
setExcelContent(namespace, sheet1, targetJobs);

writableWorkbook.write();
writableWorkbook.close();

return tmp;
} catch (Exception e) {
throw new SaturnJobConsoleException(e);
}
}

private List<JobConfig> filterTargetJobs(List<String> jobList, List<JobConfig> jobConfigList) {
List<JobConfig> result = new ArrayList<>();
for (JobConfig jobConfig : jobConfigList) {
if (jobList.contains(jobConfig.getJobName())) {
result.add(jobConfig);
}
}
return result;
}

protected void setExcelContent(String namespace, WritableSheet sheet1, List<JobConfig> unSystemJobs)
throws SaturnJobConsoleException, WriteException {
if (unSystemJobs != null && !unSystemJobs.isEmpty()) {
Expand Down

0 comments on commit 0872a7c

Please sign in to comment.