Unverified Commit bc30e43b authored by lgcareer's avatar lgcareer Committed by GitHub
Browse files

Merge pull request #49 from lgcareer/dev

Added queue management additions and updates
parents c2b9393b b6e1ac03
Loading
Loading
Loading
Loading
+104 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@
package cn.escheduler.api.controller;


import cn.escheduler.api.enums.Status;
import cn.escheduler.api.service.QueueService;
import cn.escheduler.api.utils.Constants;
import cn.escheduler.api.utils.Result;
@@ -29,7 +30,7 @@ import org.springframework.web.bind.annotation.*;

import java.util.Map;

import static cn.escheduler.api.enums.Status.QUERY_QUEUE_LIST_ERROR;
import static cn.escheduler.api.enums.Status.*;


/**
@@ -63,5 +64,107 @@ public class QueueController extends BaseController{
        }
    }

    /**
     * query queue list paging
     * @param loginUser
     * @return
     */
    @GetMapping(value="/list-paging")
    @ResponseStatus(HttpStatus.OK)
    public Result queryQueueListPaging(@RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                  @RequestParam("pageNo") Integer pageNo,
                                  @RequestParam(value = "searchVal", required = false) String searchVal,
                                  @RequestParam("pageSize") Integer pageSize){
        try{
            logger.info("login user {}, query queue list,search value:{}", loginUser.getUserName(),searchVal);
            Map<String, Object> result = checkPageParams(pageNo, pageSize);
            if(result.get(Constants.STATUS) != Status.SUCCESS){
                return returnDataListPaging(result);
            }

            result = queueService.queryList(loginUser,searchVal,pageNo,pageSize);
            return returnDataListPaging(result);
        }catch (Exception e){
            logger.error(QUERY_QUEUE_LIST_ERROR.getMsg(),e);
            return error(QUERY_QUEUE_LIST_ERROR.getCode(), QUERY_QUEUE_LIST_ERROR.getMsg());
        }
    }

    /**
     * create queue
     *
     * @param loginUser
     * @param queue
     * @param queueName
     * @return
     */
    @PostMapping(value = "/create")
    @ResponseStatus(HttpStatus.CREATED)
    public Result createQueue(@RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                               @RequestParam(value = "queue") String queue,
                               @RequestParam(value = "queueName") String queueName) {
        logger.info("login user {}, create queue, queue: {}, queueName: {}",
                loginUser.getUserName(), queue, queueName);
        try {
            Map<String, Object> result = queueService.createQueue(loginUser,queue,queueName);
            return returnDataList(result);

        }catch (Exception e){
            logger.error(CREATE_QUEUE_ERROR.getMsg(),e);
            return error(CREATE_QUEUE_ERROR.getCode(), CREATE_QUEUE_ERROR.getMsg());
        }
    }

    /**
     * update queue
     *
     * @param loginUser
     * @param queue
     * @param queueName
     * @return
     */
    @PostMapping(value = "/update")
    @ResponseStatus(HttpStatus.CREATED)
    public Result updateQueue(@RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                              @RequestParam(value = "id") int id,
                              @RequestParam(value = "queue") String queue,
                              @RequestParam(value = "queueName") String queueName) {
        logger.info("login user {}, update queue, id: {}, queue: {}, queueName: {}",
                loginUser.getUserName(), id,queue, queueName);
        try {
            Map<String, Object> result = queueService.updateQueue(loginUser,id,queue,queueName);
            return returnDataList(result);

        }catch (Exception e){
            logger.error(UPDATE_QUEUE_ERROR.getMsg(),e);
            return error(UPDATE_QUEUE_ERROR.getCode(), UPDATE_QUEUE_ERROR.getMsg());
        }
    }

    /**
     * verify queue and queue name
     *
     * @param loginUser
     * @param queue
     * @param queueName
     * @return
     */
    @PostMapping(value = "/verify-queue")
    @ResponseStatus(HttpStatus.OK)
    public Result verifyQueue(@RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                   @RequestParam(value ="queue") String queue,
                                   @RequestParam(value ="queueName") String queueName
    ) {

        try{
            logger.info("login user {}, verfiy queue: {} queue name: {}",
                    loginUser.getUserName(),queue,queueName);
            return queueService.verifyQueue(queue,queueName);
        }catch (Exception e){
            logger.error(VERIFY_QUEUE_ERROR.getMsg(),e);
            return error(Status.VERIFY_QUEUE_ERROR.getCode(), Status.VERIFY_QUEUE_ERROR.getMsg());
        }
    }


}
+7 −0
Original line number Diff line number Diff line
@@ -149,6 +149,13 @@ public enum Status {
    TENANT_CODE_HAS_ALREADY_EXISTS(10124,"tenant code has already exists"),
    IP_IS_EMPTY(10125,"ip is empty"),
    SCHEDULE_CRON_REALEASE_NEED_NOT_CHANGE(10126, "schedule release is already {0}"),
    CREATE_QUEUE_ERROR(10127, "create queue error"),
    QUEUE_NOT_EXIST(10128, "queue {0} not exists"),
    QUEUE_VALUE_EXIST(10129, "queue value {0} already exists"),
    QUEUE_NAME_EXIST(10130, "queue name {0} already exists"),
    UPDATE_QUEUE_ERROR(10131, "update queue error"),
    NEED_NOT_UPDATE_QUEUE(10132, "no content changes, no updates are required"),
    VERIFY_QUEUE_ERROR(10133,"verify queue error"),


    UDF_FUNCTION_NOT_EXIST(20001, "UDF function not found"),
+217 −21
Original line number Diff line number Diff line
@@ -18,12 +18,18 @@ package cn.escheduler.api.service;

import cn.escheduler.api.enums.Status;
import cn.escheduler.api.utils.Constants;
import cn.escheduler.api.utils.PageInfo;
import cn.escheduler.api.utils.Result;
import cn.escheduler.dao.mapper.QueueMapper;
import cn.escheduler.dao.model.Queue;
import cn.escheduler.dao.model.User;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -34,6 +40,7 @@ import java.util.Map;
@Service
public class QueueService extends BaseService {

    private static final Logger logger = LoggerFactory.getLogger(TenantService.class);

    @Autowired
    private QueueMapper queueMapper;
@@ -57,4 +64,193 @@ public class QueueService extends BaseService{
        return result;
    }

    /**
     * query queue list paging
     *
     * @param loginUser
     * @param searchVal
     * @param pageNo
     * @param pageSize
     * @return
     */
    public Map<String, Object> queryList(User loginUser, String searchVal, Integer pageNo, Integer pageSize) {
        Map<String, Object> result = new HashMap<>(5);
        if (checkAdmin(loginUser, result)) {
            return result;
        }

        Integer count = queueMapper.countQueuePaging(searchVal);

        PageInfo<Queue> pageInfo = new PageInfo<>(pageNo, pageSize);

        List<Queue> queueList = queueMapper.queryQueuePaging(searchVal, pageInfo.getStart(), pageSize);

        pageInfo.setTotalCount(count);
        pageInfo.setLists(queueList);
        result.put(Constants.DATA_LIST, pageInfo);
        putMsg(result, Status.SUCCESS);

        return result;
    }

    /**
     * create queue
     *
     * @param loginUser
     * @param queue
     * @param queueName
     * @return
     */
    public Map<String, Object> createQueue(User loginUser, String queue, String queueName) {
        Map<String, Object> result = new HashMap<>(5);
        if (checkAdmin(loginUser, result)) {
            return result;
        }

        if(StringUtils.isEmpty(queue)){
            putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, queue);
            return result;
        }

        if(StringUtils.isEmpty(queueName)){
            putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, queueName);
            return result;
        }

        if (checkQueueNameExist(queueName)) {
            putMsg(result, Status.QUEUE_NAME_EXIST, queueName);
            return result;
        }

        if (checkQueueExist(queue)) {
            putMsg(result, Status.QUEUE_VALUE_EXIST, queue);
            return result;
        }

        Queue queueObj = new Queue();
        Date now = new Date();

        queueObj.setQueue(queue);
        queueObj.setQueueName(queueName);
        queueObj.setCreateTime(now);
        queueObj.setUpdateTime(now);

        queueMapper.insert(queueObj);
        putMsg(result, Status.SUCCESS);

        return result;
    }

    /**
     * update queue
     *
     * @param loginUser
     * @param id
     * @param queue
     * @param queueName
     * @return
     */
    public Map<String, Object> updateQueue(User loginUser, int id, String queue, String queueName) {
        Map<String, Object> result = new HashMap<>(5);
        if (checkAdmin(loginUser, result)) {
            return result;
        }

        Queue queueObj = queueMapper.queryById(id);
        if (queueObj == null) {
            putMsg(result, Status.QUEUE_NOT_EXIST, id);
            return result;
        }

        // whether queue value or queueName is changed
        if (queue.equals(queueObj.getQueue()) && queueName.equals(queueObj.getQueueName())) {
            putMsg(result, Status.NEED_NOT_UPDATE_QUEUE);
            return result;
        }

        // check queue name is exist
        if (!queueName.equals(queueObj.getQueueName())) {
            if(checkQueueNameExist(queueName)){
                putMsg(result, Status.QUEUE_NAME_EXIST, queueName);
                return result;
            }
        }

        // check queue value is exist
        if (!queue.equals(queueObj.getQueue())) {
            if(checkQueueExist(queue)){
                putMsg(result, Status.QUEUE_VALUE_EXIST, queue);
                return result;
            }
        }

        // update queue
        Date now = new Date();
        queueObj.setQueue(queue);
        queueObj.setQueueName(queueName);
        queueObj.setUpdateTime(now);

        queueMapper.update(queueObj);
        putMsg(result, Status.SUCCESS);

        return result;
    }

    /**
     * verify queue and queueName
     *
     * @param queue
     * @param queueName
     * @return
     */
    public Result verifyQueue(String queue, String queueName) {
        Result result=new Result();

        if (StringUtils.isEmpty(queue)) {
            putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, queue);
            return result;
        }

        if (StringUtils.isEmpty(queueName)) {
            putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, queueName);
            return result;
        }


        if(checkQueueNameExist(queueName)){
            logger.error("queue name {} has exist, can't create again.", queueName);
            putMsg(result, Status.QUEUE_NAME_EXIST, queueName);
            return result;
        }

        if(checkQueueExist(queue)){
            logger.error("queue value {} has exist, can't create again.", queue);
            putMsg(result, Status.QUEUE_VALUE_EXIST, queue);
            return result;
        }

        putMsg(result, Status.SUCCESS);
        return result;
    }

    /**
     * check queue exist
     *
     * @param queue
     * @return
     */
    private boolean checkQueueExist(String queue) {
        return queueMapper.queryByQueue(queue) == null ? false : true;
    }

    /**
     * check queue name exist
     *
     * @param queueName
     * @return
     */
    private boolean checkQueueNameExist(String queueName) {
        return queueMapper.queryByQueueName(queueName) == null ? false : true;
    }

}
+78 −0
Original line number Diff line number Diff line
@@ -32,9 +32,12 @@ import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@@ -65,4 +68,79 @@ public class QueueControllerTest {
        Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue());
        logger.info(mvcResult.getResponse().getContentAsString());
    }

    @Test
    public void queryPagingList() throws Exception {

        MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
        //paramsMap.add("processInstanceId","1380");
        paramsMap.add("searchVal","");
        paramsMap.add("pageNo","1");
        paramsMap.add("pageSize","20");

        MvcResult mvcResult = mockMvc.perform(get("/queue/list-paging")
                .header("sessionId", "d4541e0d-0349-4f05-9c68-300176cd3c91")
                .params(paramsMap))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                .andReturn();
        Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
        Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue());
        logger.info(mvcResult.getResponse().getContentAsString());
    }

    @Test
    public void createQueue() throws Exception {

        MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
        paramsMap.add("queue","ait111134");
        paramsMap.add("queueName","aitName1");

        MvcResult mvcResult = mockMvc.perform(post("/queue/create")
                .header("sessionId", "d4541e0d-0349-4f05-9c68-300176cd3c91")
                .params(paramsMap))
                .andExpect(status().isCreated())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                .andReturn();
        Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
        //Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue());
        logger.info(mvcResult.getResponse().getContentAsString());
    }

    @Test
    public void updateQueue() throws Exception {

        MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
        paramsMap.add("id","2");
        paramsMap.add("queue","ait12");
        paramsMap.add("queueName","aitName");

        MvcResult mvcResult = mockMvc.perform(post("/queue/update")
                .header("sessionId", "d4541e0d-0349-4f05-9c68-300176cd3c91")
                .params(paramsMap))
                .andExpect(status().isCreated())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                .andReturn();
        Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
        //Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue());
        logger.info(mvcResult.getResponse().getContentAsString());
    }

    @Test
    public void verifyQueue() throws Exception {

        MultiValueMap<String, String> paramsMap = new LinkedMultiValueMap<>();
        paramsMap.add("queue","ait123");
        paramsMap.add("queueName","aitName");

        MvcResult mvcResult = mockMvc.perform(post("/queue/verify-queue")
                .header("sessionId", "d4541e0d-0349-4f05-9c68-300176cd3c91")
                .params(paramsMap))
                .andExpect(status().isOk())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                .andReturn();
        Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class);
        //Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue());
        logger.info(mvcResult.getResponse().getContentAsString());
    }
}
 No newline at end of file
+33 −2
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import cn.escheduler.dao.model.Queue;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.sql.Timestamp;
import java.util.List;

/**
@@ -64,7 +65,9 @@ public interface QueueMapper {
     */
    @Results(value = {@Result(property = "id", column = "id", id = true, javaType = Integer.class, jdbcType = JdbcType.INTEGER),
            @Result(property = "queueName", column = "queue_name", javaType = String.class, jdbcType = JdbcType.VARCHAR),
            @Result(property = "queue", column = "queue", javaType = String.class, jdbcType = JdbcType.VARCHAR)
            @Result(property = "queue", column = "queue", javaType = String.class, jdbcType = JdbcType.VARCHAR),
            @Result(property = "createTime", column = "create_time", javaType = Timestamp.class, jdbcType = JdbcType.DATE),
            @Result(property = "updateTime", column = "update_time", javaType = Timestamp.class, jdbcType = JdbcType.DATE)
    })
    @SelectProvider(type = QueueMapperProvider.class, method = "queryById")
    Queue queryById(@Param("queueId") int queueId);
@@ -76,13 +79,41 @@ public interface QueueMapper {
     */
    @Results(value = {@Result(property = "id", column = "id", id = true, javaType = Integer.class, jdbcType = JdbcType.INTEGER),
            @Result(property = "queueName", column = "queue_name", javaType = String.class, jdbcType = JdbcType.VARCHAR),
            @Result(property = "queue", column = "queue", javaType = String.class, jdbcType = JdbcType.VARCHAR)
            @Result(property = "queue", column = "queue", javaType = String.class, jdbcType = JdbcType.VARCHAR),
            @Result(property = "createTime", column = "create_time", javaType = Timestamp.class, jdbcType = JdbcType.DATE),
            @Result(property = "updateTime", column = "update_time", javaType = Timestamp.class, jdbcType = JdbcType.DATE)
    })
    @SelectProvider(type = QueueMapperProvider.class, method = "queryAllQueue")
    List<Queue> queryAllQueue();

    /**
     * query all queue list
     * @return
     */
    @Results(value = {@Result(property = "id", column = "id", id = true, javaType = Integer.class, jdbcType = JdbcType.INTEGER),
            @Result(property = "queueName", column = "queue_name", javaType = String.class, jdbcType = JdbcType.VARCHAR),
            @Result(property = "queue", column = "queue", javaType = String.class, jdbcType = JdbcType.VARCHAR),
            @Result(property = "createTime", column = "create_time", javaType = Timestamp.class, jdbcType = JdbcType.DATE),
            @Result(property = "updateTime", column = "update_time", javaType = Timestamp.class, jdbcType = JdbcType.DATE)
    })
    @SelectProvider(type = QueueMapperProvider.class, method = "queryQueuePaging")
    List<Queue> queryQueuePaging(@Param("searchVal") String searchVal,
                                 @Param("offset") Integer offset,
                                 @Param("pageSize") Integer pageSize);

    /**
     * count queue by search value
     * @param searchVal
     * @return
     */
    @SelectProvider(type = QueueMapperProvider.class, method = "countQueuePaging")
    Integer countQueuePaging(@Param("searchVal") String searchVal);

    @SelectProvider(type = QueueMapperProvider.class, method = "queryByQueue")
    Queue queryByQueue(@Param("queue") String queue);

    @SelectProvider(type = QueueMapperProvider.class, method = "queryByQueueName")
    Queue queryByQueueName(@Param("queueName") String queueName);


}
Loading