Search in sources :

Example 21 with Template

use of com.webank.wedatasphere.qualitis.rule.entity.Template in project Qualitis by WeBankFinTech.

the class RuleTemplateServiceImpl method getMultiRuleTemplate.

/**
 * Paging get template
 * @param request
 * @return
 */
@Override
public GeneralResponse<GetAllResponse<RuleTemplateResponse>> getMultiRuleTemplate(TemplatePageRequest request) throws UnExpectedRequestException {
    // Check Arguments
    TemplatePageRequest.checkRequest(request);
    int size = request.getSize();
    int page = request.getPage();
    String dataSourceType = request.getDataSourceType();
    Integer dataSourceTypeCode = TemplateDataSourceTypeEnum.getCode(dataSourceType);
    List<User> users = new ArrayList<>(1);
    List<Department> departments = new ArrayList<>(1);
    List<Template> templates;
    long total = 0;
    User userInDb = userDao.findById(HttpUtils.getUserId(httpServletRequest));
    List<UserRole> userRoles = userRoleDao.findByUser(userInDb);
    Integer roleType = roleService.getRoleType(userRoles);
    if (roleType.equals(RoleDefaultTypeEnum.ADMIN.getCode())) {
        templates = ruleTemplateDao.findAllMultiTemplate(dataSourceTypeCode, page, size);
        total = ruleTemplateDao.countAllMultiTemplate(dataSourceTypeCode);
    } else if (roleType.equals(RoleDefaultTypeEnum.DEPARTMENT_ADMIN.getCode())) {
        for (UserRole userRole : userRoles) {
            Department department = userRole.getRole().getDepartment();
            if (department != null) {
                departments.add(department);
                List<User> userList = userDao.findByDepartment(department);
                users.addAll(userList);
            }
        }
        templates = ruleTemplateDao.findTemplates(RuleTemplateLevelEnum.DEFAULT_TEMPLATE.getCode(), RuleTemplateTypeEnum.MULTI_SOURCE_TEMPLATE.getCode(), departments.size() == 0 ? null : departments, users.size() == 0 ? null : users, dataSourceTypeCode, page, size);
        total = ruleTemplateDao.countTemplates(RuleTemplateLevelEnum.DEFAULT_TEMPLATE.getCode(), RuleTemplateTypeEnum.MULTI_SOURCE_TEMPLATE.getCode(), departments.size() == 0 ? null : departments, users.size() == 0 ? null : users, dataSourceTypeCode);
    } else {
        Department department = userInDb.getDepartment();
        if (department != null) {
            departments.add(department);
        }
        users.add(userInDb);
        templates = ruleTemplateDao.findTemplates(RuleTemplateLevelEnum.DEFAULT_TEMPLATE.getCode(), RuleTemplateTypeEnum.MULTI_SOURCE_TEMPLATE.getCode(), departments.size() == 0 ? null : departments, users.size() == 0 ? null : users, dataSourceTypeCode, page, size);
        total = ruleTemplateDao.countTemplates(RuleTemplateLevelEnum.DEFAULT_TEMPLATE.getCode(), RuleTemplateTypeEnum.MULTI_SOURCE_TEMPLATE.getCode(), departments.size() == 0 ? null : departments, users.size() == 0 ? null : users, dataSourceTypeCode);
    }
    GetAllResponse<RuleTemplateResponse> response = new GetAllResponse<>();
    response.setTotal(total);
    List<RuleTemplateResponse> responseList = new ArrayList<>();
    for (Template template : templates) {
        RuleTemplateResponse ruleTemplateResponse = new RuleTemplateResponse(template);
        List<Integer> types = templateDataSourceTypeDao.findByTemplate(template).stream().map(TemplateDataSourceType::getDataSourceTypeId).collect(Collectors.toList());
        ruleTemplateResponse.setDatasourceType(types);
        responseList.add(ruleTemplateResponse);
    }
    response.setData(responseList);
    LOGGER.info("Succeed to find multi rule_template. response: {}", response);
    return new GeneralResponse<>("200", "{&GET_MULTI_RULE_TEMPLATE_SUCCESSFULLY}", response);
}
Also used : TemplateUser(com.webank.wedatasphere.qualitis.rule.entity.TemplateUser) User(com.webank.wedatasphere.qualitis.entity.User) ArrayList(java.util.ArrayList) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) RuleTemplateResponse(com.webank.wedatasphere.qualitis.rule.response.RuleTemplateResponse) Department(com.webank.wedatasphere.qualitis.entity.Department) TemplateDepartment(com.webank.wedatasphere.qualitis.rule.entity.TemplateDepartment) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole) List(java.util.List) ArrayList(java.util.ArrayList) GetAllResponse(com.webank.wedatasphere.qualitis.response.GetAllResponse)

Example 22 with Template

use of com.webank.wedatasphere.qualitis.rule.entity.Template in project Qualitis by WeBankFinTech.

the class RuleTemplateServiceImpl method addRuleTemplate.

@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class }, propagation = Propagation.REQUIRED)
public RuleTemplateResponse addRuleTemplate(AddRuleTemplateRequest request) throws UnExpectedRequestException {
    AddRuleTemplateRequest.checkRequest(request, false);
    LOGGER.info("Add default rule template request detail: {}", request.toString());
    // Save template.
    checkTemplateName(request.getTemplateName());
    Template newTemplate = new Template();
    newTemplate.setName(request.getTemplateName());
    newTemplate.setClusterNum(request.getClusterNum());
    newTemplate.setDbNum(request.getDbNum());
    newTemplate.setTableNum(request.getTableNum());
    newTemplate.setFieldNum(request.getFieldNum());
    newTemplate.setActionType(request.getActionType());
    newTemplate.setMidTableAction(request.getMidTableAction());
    newTemplate.setSaveMidTable(request.getSaveMidTable());
    newTemplate.setShowSql(request.getMidTableAction());
    newTemplate.setTemplateType(request.getTemplateType());
    newTemplate.setImportExportName(UuidGenerator.generate());
    // Save create user info.
    User userInDb = userDao.findById(HttpUtils.getUserId(httpServletRequest));
    newTemplate.setCreateUser(userInDb);
    Template savedTemplate = ruleTemplateDao.saveTemplate(newTemplate);
    for (Integer type : request.getDatasourceType()) {
        TemplateDataSourceType templateDataSourceType = new TemplateDataSourceType(type, savedTemplate);
        templateDataSourceTypeDao.save(templateDataSourceType);
    }
    LOGGER.info("Succeed to save rule template, template_id: {}", savedTemplate.getId());
    // Determine the template level and save association table.
    List<UserRole> userRoles = userRoleDao.findByUser(userInDb);
    Integer roleType = roleService.getRoleType(userRoles);
    // Save department template
    if (roleType.equals(RoleDefaultTypeEnum.ADMIN.getCode())) {
        savedTemplate.setLevel(RuleTemplateLevelEnum.DEFAULT_TEMPLATE.getCode());
    } else if (roleType.equals(RoleDefaultTypeEnum.DEPARTMENT_ADMIN.getCode())) {
        savedTemplate.setLevel(RuleTemplateLevelEnum.DEPARTMENT_TEMPLATE.getCode());
        saveTemplateDepartment(ruleTemplateDao.saveTemplate(savedTemplate), userRoles);
    } else {
        savedTemplate.setLevel(RuleTemplateLevelEnum.PERSONAL_TEMPLATE.getCode());
        saveTemplateUser(userInDb, ruleTemplateDao.saveTemplate(savedTemplate));
    }
    // Save template info.
    createAndSaveTemplateInfo(savedTemplate, request);
    return new RuleTemplateResponse(savedTemplate);
}
Also used : RuleTemplateResponse(com.webank.wedatasphere.qualitis.rule.response.RuleTemplateResponse) TemplateUser(com.webank.wedatasphere.qualitis.rule.entity.TemplateUser) User(com.webank.wedatasphere.qualitis.entity.User) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole) TemplateDataSourceType(com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) Transactional(org.springframework.transaction.annotation.Transactional)

Example 23 with Template

use of com.webank.wedatasphere.qualitis.rule.entity.Template in project Qualitis by WeBankFinTech.

the class RuleTemplateServiceImpl method getRuleTemplateInputMeta.

@Override
public GeneralResponse<TemplateInputDemandResponse> getRuleTemplateInputMeta(Long ruleTemplateId) throws UnExpectedRequestException {
    // Check existence of rule template
    Template templateInDb = ruleTemplateDao.findById(ruleTemplateId);
    if (null == templateInDb) {
        throw new UnExpectedRequestException("rule_template_id {&DOES_NOT_EXIST}");
    }
    TemplateInputDemandResponse templateInputDemandResponse = new TemplateInputDemandResponse(templateInDb, regexpExprMapperRepository);
    LOGGER.info("Succeed to get the input of rule_template. rule_template_id: {}", ruleTemplateId);
    return new GeneralResponse<>("200", "{&GET_TEMPLATE_RULE_DEMAND_SUCCESSFULLY}", templateInputDemandResponse);
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) TemplateInputDemandResponse(com.webank.wedatasphere.qualitis.rule.response.TemplateInputDemandResponse) Template(com.webank.wedatasphere.qualitis.rule.entity.Template)

Example 24 with Template

use of com.webank.wedatasphere.qualitis.rule.entity.Template in project Qualitis by WeBankFinTech.

the class AlarmConfigServiceImpl method isChildOrParentOutput.

private Boolean isChildOrParentOutput(Template template, TemplateOutputMeta templateOutputMeta) {
    Template childTemplate = template.getChildTemplate();
    Template parentTemplate = template.getParentTemplate();
    if (childTemplate != null) {
        if (childTemplate.getTemplateOutputMetas().contains(templateOutputMeta)) {
            return true;
        }
    }
    if (parentTemplate != null) {
        if (parentTemplate.getTemplateOutputMetas().contains(templateOutputMeta)) {
            return true;
        }
    }
    return false;
}
Also used : Template(com.webank.wedatasphere.qualitis.rule.entity.Template)

Example 25 with Template

use of com.webank.wedatasphere.qualitis.rule.entity.Template in project Qualitis by WeBankFinTech.

the class CustomRuleServiceImpl method addCustomRuleReal.

private GeneralResponse<RuleResponse> addCustomRuleReal(AddCustomRuleRequest request, String loginUser) throws UnExpectedRequestException, PermissionDeniedRequestException {
    // Check Arguments
    AddCustomRuleRequest.checkRequest(request);
    String nowDate = ExecutionManagerImpl.PRINT_TIME_FORMAT.format(new Date());
    // Generate Template, TemplateStatisticsInputMeta and save
    Template template = ruleTemplateService.addCustomTemplate(request);
    TemplateDataSourceType templateDataSourceType = new TemplateDataSourceType();
    if (request.getLinkisDataSourceType() == null) {
        templateDataSourceType.setDataSourceTypeId(TemplateDataSourceTypeEnum.HIVE.getCode());
    } else {
        templateDataSourceType.setDataSourceTypeId(TemplateDataSourceTypeEnum.MYSQL.getCode());
    }
    templateDataSourceType.setTemplate(template);
    templateDataSourceTypeDao.save(templateDataSourceType);
    // Save rule, rule_alarm_config and ruleDataSource
    // Check existence of project
    Project projectInDb = projectService.checkProjectExistence(request.getProjectId(), loginUser);
    // Check permissions of project
    List<Integer> permissions = new ArrayList<>();
    permissions.add(ProjectUserPermissionEnum.DEVELOPER.getCode());
    projectService.checkProjectPermission(projectInDb, loginUser, permissions);
    // Check unique of rule name
    ruleService.checkRuleName(request.getRuleName(), projectInDb, null);
    // Check if cluster name is supported
    ruleDataSourceService.checkDataSourceClusterSupport(request.getClusterName());
    RuleGroup ruleGroup;
    if (request.getRuleGroupId() != null) {
        ruleGroup = ruleGroupDao.findById(request.getRuleGroupId());
        if (ruleGroup == null) {
            throw new UnExpectedRequestException(String.format("Rule Group: %s {&CAN_NOT_BE_NULL_OR_EMPTY}", request.getRuleGroupId()));
        }
    } else {
        ruleGroup = ruleGroupDao.saveRuleGroup(new RuleGroup("Group_" + UUID.randomUUID().toString().replace("-", ""), projectInDb.getId()));
    }
    Rule newRule = new Rule();
    // Set basic info.
    setBasicInfo(newRule, projectInDb, ruleGroup, template, loginUser, nowDate, request);
    String csId = request.getCsId();
    // For context service.
    boolean cs = false;
    if (StringUtils.isNotBlank(csId)) {
        newRule.setCsId(csId);
        cs = true;
    }
    String fileId = request.getFileId();
    // For fps file check.
    boolean fps = false;
    if (StringUtils.isNotBlank(fileId)) {
        fps = true;
    }
    String sqlCheckArea = request.getSqlCheckArea();
    // For fps file check.
    boolean sqlCheck = false;
    if (StringUtils.isNotBlank(sqlCheckArea)) {
        sqlCheck = true;
    }
    Rule savedRule = ruleDao.saveRule(newRule);
    LOGGER.info("Succeed to save custom rule, rule_id: {}", savedRule.getId());
    List<AlarmConfig> savedAlarmConfigs = new ArrayList<>();
    if (request.getAlarm()) {
        savedAlarmConfigs = alarmConfigService.checkAndSaveCustomAlarmVariable(request.getAlarmVariable(), savedRule);
        LOGGER.info("Succeed to save alarm_configs, alarm_configs: {}", savedAlarmConfigs);
    }
    List<RuleDataSource> ruleDataSources = ruleDataSourceService.checkAndSaveCustomRuleDataSource(request.getClusterName(), request.getProxyUser(), loginUser, savedRule, cs, sqlCheck, request.getLinkisDataSourceId(), request.getLinkisDataSourceVersionId(), request.getLinkisDataSourceName(), request.getLinkisDataSourceType());
    savedRule.setAlarmConfigs(new HashSet<>(savedAlarmConfigs));
    if (CollectionUtils.isNotEmpty(ruleDataSources)) {
        savedRule.setRuleDataSources(new HashSet<>(ruleDataSources));
    }
    // Update rule count of datasource
    ruleDataSourceService.updateRuleDataSourceCount(savedRule, 1);
    RuleResponse response = new RuleResponse(savedRule);
    LOGGER.info("Succeed to add custom rule, rule_id: {}", savedRule.getId());
    return new GeneralResponse<>("200", "{&SUCCEED_TO_ADD_CUSTOM_RULE}", response);
}
Also used : UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) RuleDataSource(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource) RuleResponse(com.webank.wedatasphere.qualitis.rule.response.RuleResponse) ArrayList(java.util.ArrayList) Date(java.util.Date) RuleGroup(com.webank.wedatasphere.qualitis.rule.entity.RuleGroup) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) Project(com.webank.wedatasphere.qualitis.project.entity.Project) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) TemplateDataSourceType(com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType) AlarmConfig(com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)

Aggregations

Template (com.webank.wedatasphere.qualitis.rule.entity.Template)29 ArrayList (java.util.ArrayList)17 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)13 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)11 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)10 AlarmConfig (com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)9 RuleDataSource (com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource)9 TemplateDataSourceType (com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType)9 HashSet (java.util.HashSet)8 TemplateOutputMeta (com.webank.wedatasphere.qualitis.rule.entity.TemplateOutputMeta)7 TemplateStatisticsInputMeta (com.webank.wedatasphere.qualitis.rule.entity.TemplateStatisticsInputMeta)6 RuleTemplateResponse (com.webank.wedatasphere.qualitis.rule.response.RuleTemplateResponse)6 List (java.util.List)6 User (com.webank.wedatasphere.qualitis.entity.User)5 UserRole (com.webank.wedatasphere.qualitis.entity.UserRole)5 TemplateUser (com.webank.wedatasphere.qualitis.rule.entity.TemplateUser)5 Date (java.util.Date)5 Set (java.util.Set)5 Transactional (org.springframework.transaction.annotation.Transactional)5 Department (com.webank.wedatasphere.qualitis.entity.Department)4