Skip to content

Commit

Permalink
#452 作业总览增加根据四种状态分页
Browse files Browse the repository at this point in the history
  • Loading branch information
leewcc committed Jul 18, 2018
1 parent ee9d35b commit 6b42754
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

Expand Down Expand Up @@ -50,7 +51,7 @@ public class JobOverviewController extends AbstractGUIController {
@ApiResponses(value = { @ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class) })
@GetMapping
public SuccessResponseEntity getJobsWithCondition(final HttpServletRequest request, @PathVariable String namespace,
@RequestParam Map<String, String> condition, @RequestParam(required = false, defaultValue = "1") int page,
@RequestParam Map<String, Object> condition, @RequestParam(required = false, defaultValue = "1") int page,
@RequestParam(required = false, defaultValue = "25") int size) throws SaturnJobConsoleException {
return new SuccessResponseEntity(getJobOverviewByPage(namespace, condition, page, size));
}
Expand Down Expand Up @@ -78,17 +79,25 @@ public SuccessResponseEntity getJobNames(@PathVariable String namespace) throws
return new SuccessResponseEntity(jobService.getJobNames(namespace));
}

public JobOverviewVo getJobOverviewByPage(String namespace, Map<String, String> condition, int page,
public JobOverviewVo getJobOverviewByPage(String namespace, Map<String, Object> condition, int page,
int size) throws SaturnJobConsoleException {
JobOverviewVo jobOverviewVo = new JobOverviewVo();
try {
preHandleCondition(condition);
List<JobOverviewJobVo> jobList = new ArrayList<>();
List<JobConfig> unSystemJobs = jobService.getUnSystemJobsWithCondition(namespace, condition, page, size);
if (unSystemJobs != null) {
updateJobOverviewDetail(namespace, jobList, unSystemJobs);
// 当查询条件ready、running、stopping 状态时,获取作业列表会返回满足状态的所有作业
// 若查询条件不为前面三种状态,获取作业列表返回相应页的作业集合
Pageable pageable = PageableUtil.generatePageble(page, size);
JobStatus jobStatus = (JobStatus) condition.get("jobStatus");
if (jobStatus == null || JobStatus.STOPPED.equals(jobStatus)) {
jobOverviewVo.setTotalNumber(jobService.countUnSystemJobsWithCondition(namespace, condition));
} else {
jobOverviewVo.setTotalNumber(unSystemJobs.size());
unSystemJobs = unSystemJobs.subList(pageable.getOffset(), pageable.getPageSize());
}
updateJobOverviewDetail(namespace, jobList, unSystemJobs, jobStatus);
jobOverviewVo.setJobs(jobList);
jobOverviewVo.setTotalNumber(jobService.countUnSystemJobsWithCondition(namespace, condition));
} catch (SaturnJobConsoleException e) {
throw e;
} catch (Exception e) {
Expand All @@ -98,6 +107,26 @@ public JobOverviewVo getJobOverviewByPage(String namespace, Map<String, String>
return jobOverviewVo;
}



private void preHandleCondition(Map<String, Object> condition) throws SaturnJobConsoleException {
if (condition.containsKey("groups") && SaturnConstants.NO_GROUPS_LABEL.equals(condition.get("groups"))) {
condition.put("groups", "");
}
if (condition.containsKey("status")) {
JobStatus jobStatus = JobStatus
.getJobStatus(checkAndGetParametersValueAsString(condition, "status", false));
condition.put("jobStatus", jobStatus);
if (jobStatus != null) {
if (JobStatus.STOPPED.equals(jobStatus)) {
condition.put("isEnabled", 0);
} else {
condition.put("isEnabled", 1);
}
}
}
}

private void updateAbnormalJobSizeInOverview(String namespace, JobOverviewVo jobOverviewVo) {
try {
List<AbnormalJob> abnormalJobList = alarmStatisticsService.getAbnormalJobListByNamespace(namespace);
Expand All @@ -108,7 +137,7 @@ private void updateAbnormalJobSizeInOverview(String namespace, JobOverviewVo job
}

private void updateJobOverviewDetail(String namespace, List<JobOverviewJobVo> jobList,
List<JobConfig> unSystemJobs) {
List<JobConfig> unSystemJobs, JobStatus jobStatus) {
for (JobConfig jobConfig : unSystemJobs) {
try {
jobConfig.setDefaultValues();
Expand All @@ -122,7 +151,9 @@ private void updateJobOverviewDetail(String namespace, List<JobOverviewJobVo> jo
jobOverviewJobVo.setGroups(SaturnConstants.NO_GROUPS_LABEL);
}

JobStatus jobStatus = jobService.getJobStatus(namespace, jobConfig.getJobName());
if (jobStatus == null) {
jobStatus = jobService.getJobStatus(namespace, jobConfig.getJobName());
}
jobOverviewJobVo.setStatus(jobStatus);

if (!JobStatus.STOPPED.equals(jobStatus)) {// 作业如果是STOPPED状态,不需要显示已分配的executor
Expand Down Expand Up @@ -158,7 +189,7 @@ private void updateShardingListInOverview(String namespace, JobConfig jobConfig,
public JobOverviewVo countJobOverviewVo(String namespace) throws SaturnJobConsoleException{
JobOverviewVo jobOverviewVo = new JobOverviewVo();
jobOverviewVo.setTotalNumber(jobService.countUnSystemJobsWithCondition(namespace,
Maps.<String, String>newHashMap()));
Maps.<String, Object>newHashMap()));
jobOverviewVo.setEnabledNumber(jobService.countEnabledUnSystemJobs(namespace));
// 获取该域下的异常作业数量,捕获所有异常,打日志,不抛到前台
updateAbnormalJobSizeInOverview(namespace, jobOverviewVo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@
*/
public enum JobStatus {

READY, STOPPED, RUNNING, STOPPING
READY, STOPPED, RUNNING, STOPPING;

public static JobStatus getJobStatus(String jobStatus) {
switch (jobStatus) {
case "0":
return STOPPED;
case "1":
return READY;
case "2":
return RUNNING;
case "3":
return STOPPING;
default:
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public interface CurrentJobConfigRepository {
List<JobConfig4DB> findConfigsByNamespace(@Param("namespace") String namespace);

List<JobConfig4DB> findConfigsByNamespaceWithCondition(@Param("namespace") String namespace,
@Param("condition") Map<String, String> condition, @Param("pageable") Pageable pageable);
@Param("condition") Map<String, Object> condition, @Param("pageable") Pageable pageable);

int countConfigsByNamespaceWithCondition(@Param("namespace") String namespace,
@Param("condition") Map<String, String> condition);
@Param("condition") Map<String, Object> condition);

int countEnabledUnSystemJobsByNamespace(@Param("namespace") String namespace, @Param("isEnabled")int isEnabled);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ public interface CurrentJobConfigService {

List<JobConfig4DB> findConfigsByNamespace(String namespace);

List<JobConfig4DB> findConfigsByNamespaceWithCondition(String namespace, Map<String, String> condition,
List<JobConfig4DB> findConfigsByNamespaceWithCondition(String namespace, Map<String, Object> condition,
Pageable pageable);

int countConfigsByNamespaceWithCondition(String namespace, Map<String, String> condition);
int countConfigsByNamespaceWithCondition(String namespace, Map<String, Object> condition);

int countEnabledUnSystemJobsByNamespace(String namespace);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,13 @@ public List<JobConfig4DB> findConfigsByNamespace(String namespace) {
}

@Override
public List<JobConfig4DB> findConfigsByNamespaceWithCondition(String namespace, Map<String, String> condition,
public List<JobConfig4DB> findConfigsByNamespaceWithCondition(String namespace, Map<String, Object> condition,
Pageable pageable) {
return currentJobConfigRepo.findConfigsByNamespaceWithCondition(namespace, condition, pageable);
}

@Override
public int countConfigsByNamespaceWithCondition(String namespace, Map<String, String> condition) {
public int countConfigsByNamespaceWithCondition(String namespace, Map<String, Object> condition) {
return currentJobConfigRepo.countConfigsByNamespaceWithCondition(namespace, condition);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public interface JobService {

List<JobConfig> getUnSystemJobs(String namespace) throws SaturnJobConsoleException;

List<JobConfig> getUnSystemJobsWithCondition(String namespace, Map<String, String> condition, int offset, int size)
List<JobConfig> getUnSystemJobsWithCondition(String namespace, Map<String, Object> condition, int page, int size)
throws SaturnJobConsoleException;

int countUnSystemJobsWithCondition(String namespace, Map<String, String> condition);
int countUnSystemJobsWithCondition(String namespace, Map<String, Object> condition);

int countEnabledUnSystemJobs(String namespace);
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.vip.saturn.job.console.vo.GetJobConfigVo;
import com.vip.saturn.job.console.vo.UpdateJobConfigVo;
import com.vip.saturn.job.sharding.node.SaturnExecutorsNode;
import java.awt.print.Pageable;
import jxl.Cell;
import jxl.CellType;
import jxl.Sheet;
Expand Down Expand Up @@ -709,12 +708,10 @@ public List<JobConfig> getUnSystemJobs(String namespace) {
}

@Override
public List<JobConfig> getUnSystemJobsWithCondition(String namespace, Map<String, String> condition,
public List<JobConfig> getUnSystemJobsWithCondition(String namespace, Map<String, Object> condition,
int page, int size) throws SaturnJobConsoleException {
List<JobConfig> unSystemJobs = new ArrayList<>();
convertUnallocatedCondition(condition);
List<JobConfig4DB> jobConfig4DBList = currentJobConfigService.findConfigsByNamespaceWithCondition(namespace,
condition, PageableUtil.generatePageble(page, size));
List<JobConfig4DB> jobConfig4DBList = getJobConfigByStatusWithCondition(namespace, condition, page, size);
if (jobConfig4DBList != null) {
for (JobConfig4DB jobConfig4DB : jobConfig4DBList) {
if (!(StringUtils.isNotBlank(jobConfig4DB.getJobMode()) && jobConfig4DB.getJobMode()
Expand All @@ -728,14 +725,28 @@ public List<JobConfig> getUnSystemJobsWithCondition(String namespace, Map<String
return unSystemJobs;
}

private void convertUnallocatedCondition(Map<String, String> condition) {
if (condition.containsKey("groups") && SaturnConstants.NO_GROUPS_LABEL.equals(condition.get("groups"))) {
condition.put("groups", "");
private List<JobConfig4DB> getJobConfigByStatusWithCondition(String namespace, Map<String, Object> condition,
int page, int size) throws SaturnJobConsoleException {
JobStatus jobStatus = (JobStatus) condition.get("jobStatus");
if (jobStatus == null || JobStatus.STOPPED.equals(jobStatus)) {
return currentJobConfigService.findConfigsByNamespaceWithCondition(namespace, condition,
PageableUtil.generatePageble(page, size));
}

List<JobConfig4DB> jobConfig4DBList = new ArrayList<>();
List<JobConfig4DB> enabledJobConfigList = currentJobConfigService.findConfigsByNamespaceWithCondition(namespace,
condition, null);
for (JobConfig4DB jobConfig4DB : enabledJobConfigList) {
JobStatus currentJobStatus = getJobStatus(namespace, jobConfig4DB.getJobName());
if (jobStatus.equals(currentJobStatus)) {
jobConfig4DBList.add(jobConfig4DB);
}
}
return jobConfig4DBList;
}

@Override
public int countUnSystemJobsWithCondition(String namespace, Map<String, String> condition) {
public int countUnSystemJobsWithCondition(String namespace, Map<String, Object> condition) {
return currentJobConfigService.countConfigsByNamespaceWithCondition(namespace, condition);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,34 @@
import org.springframework.data.domain.Sort;

public class PageableUtil {
private static final int DEFAULT_PAGE = 1;
private static final int DEFAULT_SIZE = 25;

private PageableUtil() {

}

public static Pageable generatePageble(final int page, final int size) {
return new Pageable() {
@Override
public int getPageNumber() {
if (page <= 0) {
return DEFAULT_PAGE;
}
return page;
}

@Override
public int getPageSize() {
if (size <= 0) {
return DEFAULT_SIZE;
}
return size;
}

@Override
public int getOffset() {
return size * (page - 1);
return getPageSize() * (getPageNumber() - 1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@
from job_config
<where>
namespace = #{namespace,jdbcType=VARCHAR}
<if test="condition.groups != null">
and groups = #{condition.groups}
</if>
<if test="condition.isEnabled != null">
and is_enabled = #{condition.isEnabled}
</if>
<choose>
<when test="condition.jobName != null">
and job_name like CONCAT('%',#{condition.jobName},'%')
Expand All @@ -74,20 +80,16 @@
and description like CONCAT('%',#{condition.description},'%')
</when>
</choose>
<if test="condition.groups != null">
and groups = #{condition.groups}
</if>
<if test="condition.isEnabled != null">
and is_enabled = #{condition.isEnabled}
</if>
</where>
<if test="condition.order != null">
order by job_name
<if test="condition.order == 'descending'">
desc
</if>
</if>
limit #{pageable.offset}, #{pageable.pageSize}
<if test="pageable != null">
limit #{pageable.offset}, #{pageable.pageSize}
</if>
</select>

<select id="countConfigsByNamespaceWithCondition" resultType="Integer">
Expand Down

0 comments on commit 6b42754

Please sign in to comment.