Search in sources :

Example 1 with AddFileRuleRequest

use of com.webank.wedatasphere.qualitis.rule.request.AddFileRuleRequest in project Qualitis by WeBankFinTech.

the class RuleNodeServiceImpl method copyRuleByRuleGroupId.

@Override
public GeneralResponse<RuleResponse> copyRuleByRuleGroupId(CopyRuleRequest request) throws UnExpectedRequestException, PermissionDeniedRequestException {
    CopyRuleRequest.checkRequest(request);
    Long ruleGroupId = request.getSourceRuleGroupId();
    RuleGroup ruleGroupInDb = ruleGroupDao.findById(request.getSourceRuleGroupId());
    int totalFinish = 0;
    RuleGroup targetRuleGroup;
    if (request.getTargetRuleGroupId() != null) {
        RuleGroup targetRuleGroupInDb = ruleGroupDao.findById(request.getTargetRuleGroupId());
        if (targetRuleGroupInDb != null) {
            targetRuleGroup = targetRuleGroupInDb;
        } else {
            throw new UnExpectedRequestException("Rule group id is illegal.");
        }
    } else {
        RuleGroup currentRuleGroup = new RuleGroup("Group_" + UUID.randomUUID().toString().replace("-", ""), request.getTargetProjectId() != null ? request.getTargetProjectId() : ruleGroupInDb.getProjectId());
        currentRuleGroup.setVersion(request.getVersion());
        targetRuleGroup = ruleGroupDao.saveRuleGroup(currentRuleGroup);
    }
    if (ruleGroupId != null) {
        LOGGER.info("Start to copy rules of rule group[ID=" + ruleGroupId + "].");
        if (ruleGroupInDb == null) {
            throw new UnExpectedRequestException("Rule group {&DOES_NOT_EXIST}");
        }
        List<Rule> rules = ruleDao.findByRuleGroup(ruleGroupInDb);
        for (Rule rule : rules) {
            switch(rule.getRuleType().intValue()) {
                case 1:
                    AddRuleRequest addRuleRequest = constructSingleRequest(rule, targetRuleGroup);
                    ruleService.addRuleForOuter(addRuleRequest, request.getCreateUser());
                    totalFinish++;
                    break;
                case 2:
                    AddCustomRuleRequest addCustomRuleRequest = constructCustomRequest(rule, targetRuleGroup);
                    customRuleService.addRuleForOuter(addCustomRuleRequest, request.getCreateUser());
                    totalFinish++;
                    break;
                case 3:
                    AddMultiSourceRuleRequest addMultiSourceRuleRequest = constructMultiRequest(rule, targetRuleGroup);
                    addMultiSourceRuleRequest.setLoginUser(request.getCreateUser());
                    multiSourceRuleService.addRuleForOuter(addMultiSourceRuleRequest, false);
                    totalFinish++;
                    break;
                case 4:
                    AddFileRuleRequest addFileRuleRequest = constructFileRequest(rule, targetRuleGroup);
                    fileRuleService.addRuleForOuter(addFileRuleRequest, request.getCreateUser());
                    totalFinish++;
                    break;
                default:
            }
        }
        if (totalFinish != rules.size()) {
            return new GeneralResponse<>("200", "{&COPY_RULE_FAILED}", new RuleResponse(targetRuleGroup.getId()));
        }
    } else if (CollectionUtils.isNotEmpty(request.getSourceRuleIdList())) {
    }
    return new GeneralResponse<>("200", "{&COPY_RULE_SUCCESSFULLY}", new RuleResponse(targetRuleGroup.getId()));
}
Also used : AddRuleRequest(com.webank.wedatasphere.qualitis.rule.request.AddRuleRequest) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) AddCustomRuleRequest(com.webank.wedatasphere.qualitis.rule.request.AddCustomRuleRequest) AddFileRuleRequest(com.webank.wedatasphere.qualitis.rule.request.AddFileRuleRequest) RuleResponse(com.webank.wedatasphere.qualitis.rule.response.RuleResponse) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) RuleGroup(com.webank.wedatasphere.qualitis.rule.entity.RuleGroup) AddMultiSourceRuleRequest(com.webank.wedatasphere.qualitis.rule.request.multi.AddMultiSourceRuleRequest)

Example 2 with AddFileRuleRequest

use of com.webank.wedatasphere.qualitis.rule.request.AddFileRuleRequest in project Qualitis by WeBankFinTech.

the class RuleNodeServiceImpl method constructFileRequest.

private AddFileRuleRequest constructFileRequest(Rule rule, RuleGroup ruleGroup) {
    AddFileRuleRequest addFileRuleRequest = new AddFileRuleRequest();
    String newVersion = ruleGroup.getVersion();
    if (StringUtils.isEmpty(newVersion)) {
        String newRuleName = rule.getName() + "_copy_" + ruleGroup.getId();
        LOGGER.info("File rule start to be copied. Copied rule name: " + newRuleName);
        addFileRuleRequest.setRuleName(newRuleName);
        if (StringUtils.isNotBlank(rule.getCnName())) {
            addFileRuleRequest.setRuleCnName(rule.getCnName() + "_副本");
        }
    } else {
        String oldVersion = rule.getRuleGroup().getVersion();
        LOGGER.info("File rule start to be copied. Copied rule name: " + rule.getName() + "_" + ruleGroup.getVersion());
        if (StringUtils.isNotBlank(oldVersion) && rule.getName().endsWith(oldVersion)) {
            addFileRuleRequest.setRuleName(rule.getName().replace(oldVersion, newVersion));
            if (StringUtils.isNotBlank(rule.getCnName())) {
                addFileRuleRequest.setRuleCnName(rule.getCnName().replace(oldVersion, newVersion));
            }
        } else {
            addFileRuleRequest.setRuleName(rule.getName() + "_" + newVersion);
            if (StringUtils.isNotBlank(rule.getCnName())) {
                addFileRuleRequest.setRuleCnName(rule.getCnName() + "_" + newVersion);
            }
        }
    }
    String ruleDetail = rule.getDetail();
    addFileRuleRequest.setRuleDetail(ruleDetail);
    addFileRuleRequest.setAbortOnFailure(rule.getAbortOnFailure());
    addFileRuleRequest.setCsId(rule.getCsId());
    addFileRuleRequest.setRuleGroupId(ruleGroup.getId());
    addFileRuleRequest.setProjectId(ruleGroup.getProjectId());
    addFileRuleRequest.setAbortOnFailure(rule.getAbortOnFailure());
    for (RuleDataSource ruleDataSource : rule.getRuleDataSources()) {
        DataSourceRequest dataSourceRequest = new DataSourceRequest();
        String clusterName = ruleDataSource.getClusterName();
        String databaseName = ruleDataSource.getDbName();
        String tableName = ruleDataSource.getTableName();
        String filter = ruleDataSource.getFilter();
        dataSourceRequest.setClusterName(clusterName);
        dataSourceRequest.setDbName(databaseName);
        dataSourceRequest.setTableName(tableName);
        dataSourceRequest.setFilter(filter);
        addFileRuleRequest.setDatasource(dataSourceRequest);
        break;
    }
    List<FileAlarmConfigRequest> alarmVariable = new ArrayList<>();
    for (AlarmConfig alarmConfig : rule.getAlarmConfigs()) {
        FileAlarmConfigRequest fileAlarmConfigRequest = new FileAlarmConfigRequest();
        Double threshold = alarmConfig.getThreshold();
        Integer unit = alarmConfig.getFileOutputUnit();
        Integer alarmCompareType = alarmConfig.getCompareType();
        Integer alarmOutputName = alarmConfig.getFileOutputName();
        Integer checkTemplateName = alarmConfig.getCheckTemplate();
        fileAlarmConfigRequest.setFileOutputUnit(unit);
        fileAlarmConfigRequest.setCompareType(alarmCompareType);
        fileAlarmConfigRequest.setFileOutputName(alarmOutputName);
        fileAlarmConfigRequest.setCheckTemplate(checkTemplateName);
        fileAlarmConfigRequest.setThreshold(threshold);
        RuleMetric ruleMetric = alarmConfig.getRuleMetric();
        // Recod rule metric info (unique code).
        if (ruleMetric != null) {
            String enCode = ruleMetric.getEnCode();
            fileAlarmConfigRequest.setRuleMetricEnCode(enCode);
            fileAlarmConfigRequest.setUploadAbnormalValue(alarmConfig.getUploadAbnormalValue());
            fileAlarmConfigRequest.setUploadRuleMetricValue(alarmConfig.getUploadRuleMetricValue());
        }
        fileAlarmConfigRequest.setDeleteFailCheckResult(alarmConfig.getDeleteFailCheckResult());
        alarmVariable.add(fileAlarmConfigRequest);
    }
    addFileRuleRequest.setAlarm(true);
    addFileRuleRequest.setAlarmVariable(alarmVariable);
    return addFileRuleRequest;
}
Also used : DataSourceRequest(com.webank.wedatasphere.qualitis.rule.request.DataSourceRequest) RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) RuleDataSource(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource) AddFileRuleRequest(com.webank.wedatasphere.qualitis.rule.request.AddFileRuleRequest) ArrayList(java.util.ArrayList) FileAlarmConfigRequest(com.webank.wedatasphere.qualitis.rule.request.FileAlarmConfigRequest) AlarmConfig(com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)

Example 3 with AddFileRuleRequest

use of com.webank.wedatasphere.qualitis.rule.request.AddFileRuleRequest in project Qualitis by WeBankFinTech.

the class RuleBatchServiceImpl method constructAddFileRuleRequest.

private List<AddFileRuleRequest> constructAddFileRuleRequest(Map<String, List<ExcelTemplateFileRuleByProject>> fileRulePartitionedByRuleName, Project project, String localeStr) throws UnExpectedRequestException {
    List<AddFileRuleRequest> addFileRuleRequests = new ArrayList<>();
    for (String ruleName : fileRulePartitionedByRuleName.keySet()) {
        List<ExcelTemplateFileRuleByProject> ruleInfos = fileRulePartitionedByRuleName.get(ruleName);
        ExcelTemplateFileRuleByProject firstCommonInfo = ruleInfos.get(0);
        AddFileRuleRequest addFileRuleRequest = new AddFileRuleRequest();
        String ruleGroupName = firstCommonInfo.getRuleGroupName();
        if (StringUtils.isBlank(ruleGroupName)) {
            throw new UnExpectedRequestException("RuleGroupName {&CAN_NOT_BE_NULL_OR_EMPTY}");
        }
        boolean alarm = false;
        List<FileAlarmConfigRequest> alarmConfigRequests = new ArrayList<>();
        DataSourceRequest dataSourceRequest = new DataSourceRequest();
        Long projectId = project.getId();
        for (ExcelTemplateFileRuleByProject excelTemplateFileRule : ruleInfos) {
            getDataFileSourceRequest(dataSourceRequest, excelTemplateFileRule);
            getFileAlarmConfig(alarmConfigRequests, excelTemplateFileRule, localeStr);
        }
        RuleGroup ruleGroupInDb = ruleGroupDao.findByRuleGroupNameAndProjectId(ruleGroupName, projectId);
        if (ruleGroupInDb != null) {
            addFileRuleRequest.setRuleGroupId(ruleGroupInDb.getId());
        } else {
            RuleGroup ruleGroup = ruleGroupDao.saveRuleGroup(new RuleGroup(ruleGroupName, project.getId()));
            addFileRuleRequest.setRuleGroupId(ruleGroup.getId());
        }
        // Construct addRuleRequest
        addFileRuleRequest.setRuleName(ruleName);
        addFileRuleRequest.setRuleDetail(firstCommonInfo.getRuleDetail());
        addFileRuleRequest.setAbortOnFailure(firstCommonInfo.getAbortOnFailure());
        if (!alarmConfigRequests.isEmpty()) {
            alarm = true;
        }
        addFileRuleRequest.setAlarm(alarm);
        addFileRuleRequest.setAlarmVariable(alarmConfigRequests);
        addFileRuleRequest.setDatasource(dataSourceRequest);
        addFileRuleRequest.setProjectId(projectId);
        addFileRuleRequests.add(addFileRuleRequest);
    }
    return addFileRuleRequests;
}
Also used : DataSourceRequest(com.webank.wedatasphere.qualitis.rule.request.DataSourceRequest) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) ArrayList(java.util.ArrayList) FileAlarmConfigRequest(com.webank.wedatasphere.qualitis.rule.request.FileAlarmConfigRequest) RuleGroup(com.webank.wedatasphere.qualitis.rule.entity.RuleGroup) AddFileRuleRequest(com.webank.wedatasphere.qualitis.rule.request.AddFileRuleRequest) ExcelTemplateFileRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateFileRuleByProject)

Example 4 with AddFileRuleRequest

use of com.webank.wedatasphere.qualitis.rule.request.AddFileRuleRequest in project Qualitis by WeBankFinTech.

the class RuleBatchServiceImpl method addTemplateFileRule.

private void addTemplateFileRule(Map<String, List<ExcelTemplateFileRuleByProject>> fileRulePartitionedByRuleName, Project project, String userName, boolean aomp) throws UnExpectedRequestException, PermissionDeniedRequestException {
    Map<String, List<ExcelTemplateFileRuleByProject>> modifyRulePartitionedByRuleName = new HashMap<>(1);
    Map<String, List<ExcelTemplateFileRuleByProject>> addRulePartitionedByRuleName = new HashMap<>(1);
    String localeStr = httpServletRequest.getHeader("Content-Language");
    for (Iterator<String> iterator = fileRulePartitionedByRuleName.keySet().iterator(); iterator.hasNext(); ) {
        String currentRuleName = iterator.next();
        Rule currentRule = ruleDao.findByProjectAndRuleName(project, currentRuleName);
        if (currentRule != null) {
            modifyRulePartitionedByRuleName.put(currentRuleName, fileRulePartitionedByRuleName.get(currentRuleName));
            List<AddFileRuleRequest> addRuleRequestList = constructAddFileRuleRequest(modifyRulePartitionedByRuleName, project, localeStr);
            ModifyFileRuleRequest modifyRuleRequest = new ModifyFileRuleRequest();
            modifyRuleRequest.setRuleId(currentRule.getId());
            AddFileRuleRequest addRuleRequest = addRuleRequestList.iterator().next();
            BeanUtils.copyProperties(addRuleRequest, modifyRuleRequest);
            LOGGER.info("Start to modify template file rule. request: {}", modifyRuleRequest);
            if (aomp) {
                fileRuleService.modifyRuleDetailForOuter(modifyRuleRequest, userName);
            } else {
                fileRuleService.modifyRuleDetail(modifyRuleRequest);
            }
            LOGGER.info("Success to modify template file rule");
            modifyRulePartitionedByRuleName.clear();
        } else {
            addRulePartitionedByRuleName.put(currentRuleName, fileRulePartitionedByRuleName.get(currentRuleName));
            List<AddFileRuleRequest> addRuleRequestList = constructAddFileRuleRequest(addRulePartitionedByRuleName, project, localeStr);
            AddFileRuleRequest currentAddFileRuleRequest = addRuleRequestList.iterator().next();
            LOGGER.info("Start to add template file rule. request: {}", currentAddFileRuleRequest);
            if (aomp) {
                fileRuleService.addRuleForOuter(currentAddFileRuleRequest, userName);
            } else {
                fileRuleService.addRuleForUpload(currentAddFileRuleRequest);
            }
            LOGGER.info("Success to add template file rule.");
            addRulePartitionedByRuleName.clear();
        }
    }
}
Also used : AddFileRuleRequest(com.webank.wedatasphere.qualitis.rule.request.AddFileRuleRequest) HashMap(java.util.HashMap) ModifyFileRuleRequest(com.webank.wedatasphere.qualitis.rule.request.ModifyFileRuleRequest) ArrayList(java.util.ArrayList) List(java.util.List) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule)

Example 5 with AddFileRuleRequest

use of com.webank.wedatasphere.qualitis.rule.request.AddFileRuleRequest in project Qualitis by WeBankFinTech.

the class FileRuleServiceImpl method modifyRuleDetailReal.

private GeneralResponse<RuleResponse> modifyRuleDetailReal(ModifyFileRuleRequest request, String loginUser) throws UnExpectedRequestException, PermissionDeniedRequestException {
    // Check Arguments
    ModifyFileRuleRequest.checkRequest(request);
    // Check existence of rule
    Rule ruleInDb = ruleDao.findById(request.getRuleId());
    if (ruleInDb == null) {
        throw new UnExpectedRequestException("rule_id [" + request.getRuleId() + "] {&DOES_NOT_EXIST}");
    }
    // Check existence of project
    Project projectInDb = projectService.checkProjectExistence(ruleInDb.getProject().getId(), loginUser);
    // Check permissions of project
    List<Integer> permissions = new ArrayList<>();
    permissions.add(ProjectUserPermissionEnum.DEVELOPER.getCode());
    projectService.checkProjectPermission(projectInDb, loginUser, permissions);
    if (!ruleInDb.getRuleType().equals(RuleTypeEnum.FILE_TEMPLATE_RULE.getCode())) {
        throw new UnExpectedRequestException("rule(id: [" + request.getRuleId() + "]) {&IS_NOT_A_FILE_RULE}");
    }
    LOGGER.info("Succeed to find rule. rule_id: {}", ruleInDb.getId());
    String nowDate = ExecutionManagerImpl.PRINT_TIME_FORMAT.format(new Date());
    String csId = request.getCsId();
    boolean cs = false;
    if (StringUtils.isNotBlank(csId)) {
        cs = true;
        ruleInDb.setCsId(csId);
    } else {
        LOGGER.info("Delete context service ID. rule_id: {}, cs_id: {}", ruleInDb.getId(), ruleInDb.getCsId());
        ruleInDb.setCsId(csId);
        LOGGER.info("After delete context service ID. rule_id: {}, cs_id: {}", ruleInDb.getId(), ruleInDb.getCsId());
    }
    // Check cluster support
    ruleDataSourceService.checkDataSourceClusterSupport(request.getDatasource().getClusterName());
    // Delete alarm config by file rule
    alarmConfigService.deleteByRule(ruleInDb);
    LOGGER.info("Succeed to delete all alarm_config. rule_id: {}", ruleInDb.getId());
    // Delete template of file rule
    ruleTemplateService.deleteFileRuleTemplate(ruleInDb.getTemplate().getId());
    LOGGER.info("Succeed to delete file rule template. rule_id: {}", request.getRuleId());
    // Delete rule datasource of file rule
    ruleDataSourceService.deleteByRule(ruleInDb);
    LOGGER.info("Succeed to delete all rule_dataSources. rule_id: {}", ruleInDb.getId());
    // Update rule count of datasource
    ruleDataSourceService.updateRuleDataSourceCount(ruleInDb, -1);
    // Check existence of rule
    AddFileRuleRequest addFileRuleRequest = new AddFileRuleRequest();
    BeanUtils.copyProperties(request, addFileRuleRequest);
    Template template = ruleTemplateService.addFileTemplate(addFileRuleRequest);
    // Save rule
    ruleInDb.setRuleType(RuleTypeEnum.FILE_TEMPLATE_RULE.getCode());
    ruleInDb.setTemplate(template);
    ruleInDb.setName(request.getRuleName());
    ruleInDb.setCnName(request.getRuleCnName());
    ruleInDb.setDetail(request.getRuleDetail());
    ruleInDb.setAlarm(request.getAlarm());
    ruleInDb.setRuleTemplateName(template.getName());
    ruleInDb.setAbortOnFailure(request.getAbortOnFailure());
    ruleInDb.setModifyUser(loginUser);
    ruleInDb.setModifyTime(nowDate);
    Rule savedRule = ruleDao.saveRule(ruleInDb);
    LOGGER.info("Succeed to save file rule, rule_id: {}", savedRule.getId());
    List<AlarmConfig> savedAlarmConfigs = new ArrayList<>();
    if (request.getAlarm()) {
        savedAlarmConfigs = alarmConfigService.checkAndSaveFileAlarmVariable(request.getAlarmVariable(), savedRule);
        LOGGER.info("Succeed to save alarm_configs, alarm_configs: {}", savedAlarmConfigs);
    }
    List<RuleDataSource> ruleDataSources = new ArrayList<>();
    ruleDataSources.add(ruleDataSourceService.checkAndSaveFileRuleDataSource(request.getDatasource(), savedRule, cs));
    savedRule.setAlarmConfigs(new HashSet<>(savedAlarmConfigs));
    savedRule.setRuleDataSources(new HashSet<>(ruleDataSources));
    // Update rule count of datasource
    ruleDataSourceService.updateRuleDataSourceCount(ruleInDb, 1);
    RuleResponse response = new RuleResponse(savedRule);
    LOGGER.info("Succeed to modify file rule, rule_id: {}", savedRule.getId());
    // projectEventService.record(savedRule.getProject().getId(), loginUser, "modify", "file rule[name= " + savedRule.getName() + "].", EventTypeEnum.MODIFY_PROJECT.getCode());
    return new GeneralResponse<>("200", "{&SUCCEED_TO_MODIFY_FILE_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) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) Project(com.webank.wedatasphere.qualitis.project.entity.Project) AddFileRuleRequest(com.webank.wedatasphere.qualitis.rule.request.AddFileRuleRequest) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) AlarmConfig(com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)

Aggregations

AddFileRuleRequest (com.webank.wedatasphere.qualitis.rule.request.AddFileRuleRequest)5 ArrayList (java.util.ArrayList)4 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)3 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)3 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)2 AlarmConfig (com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)2 RuleDataSource (com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource)2 RuleGroup (com.webank.wedatasphere.qualitis.rule.entity.RuleGroup)2 DataSourceRequest (com.webank.wedatasphere.qualitis.rule.request.DataSourceRequest)2 FileAlarmConfigRequest (com.webank.wedatasphere.qualitis.rule.request.FileAlarmConfigRequest)2 RuleResponse (com.webank.wedatasphere.qualitis.rule.response.RuleResponse)2 RuleMetric (com.webank.wedatasphere.qualitis.entity.RuleMetric)1 Project (com.webank.wedatasphere.qualitis.project.entity.Project)1 ExcelTemplateFileRuleByProject (com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateFileRuleByProject)1 Template (com.webank.wedatasphere.qualitis.rule.entity.Template)1 AddCustomRuleRequest (com.webank.wedatasphere.qualitis.rule.request.AddCustomRuleRequest)1 AddRuleRequest (com.webank.wedatasphere.qualitis.rule.request.AddRuleRequest)1 ModifyFileRuleRequest (com.webank.wedatasphere.qualitis.rule.request.ModifyFileRuleRequest)1 AddMultiSourceRuleRequest (com.webank.wedatasphere.qualitis.rule.request.multi.AddMultiSourceRuleRequest)1 Date (java.util.Date)1