Search in sources :

Example 26 with UnExpectedRequestException

use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException 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 27 with UnExpectedRequestException

use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.

the class RuleNodeServiceImpl method updateImportedChildRule.

public void updateImportedChildRule(Rule parentRule, RuleNodeRequest ruleNodeRequest) throws IOException, UnExpectedRequestException {
    if (parentRule.getChildRule() == null) {
        return;
    }
    LOGGER.info("Start to update imported child rule.");
    ObjectMapper objectMapper = new ObjectMapper();
    Rule childRule = parentRule.getChildRule();
    Rule childRuleObject = objectMapper.readValue(ruleNodeRequest.getChildRuleObject(), Rule.class);
    Template childTemplateObject = objectMapper.readValue(ruleNodeRequest.getChildTemplateObject(), Template.class);
    Set<RuleDataSource> childRuleDataSources = objectMapper.readValue(ruleNodeRequest.getChildRuleDataSourcesObject(), new TypeReference<Set<RuleDataSource>>() {
    });
    Set<RuleDataSourceMapping> childRuleDataSourceMappings = objectMapper.readValue(ruleNodeRequest.getChildRuleDataSourceMappingsObject(), new TypeReference<Set<RuleDataSourceMapping>>() {
    });
    Set<AlarmConfig> childAlarmConfigs = objectMapper.readValue(ruleNodeRequest.getChildAlarmConfigsObject(), new TypeReference<Set<AlarmConfig>>() {
    });
    Set<RuleVariable> childRuleVariables = objectMapper.readValue(ruleNodeRequest.getChildRuleVariablesObject(), new TypeReference<Set<RuleVariable>>() {
    });
    childRule.setAlarm(childRuleObject.getAlarm());
    childRule.setAbortOnFailure(childRuleObject.getAbortOnFailure());
    childRule.setFromContent(childRuleObject.getFromContent());
    childRule.setWhereContent(childRuleObject.getWhereContent());
    childRule.setRuleType(childRuleObject.getRuleType());
    childRule.setRuleTemplateName(childTemplateObject.getName());
    childRule.setOutputName(childRuleObject.getOutputName());
    childRule.setFunctionType(childRuleObject.getFunctionType());
    childRule.setFunctionContent(childRuleObject.getFunctionContent());
    Template childTemplateInDb = ruleTemplateService.checkRuleTemplate(childTemplateObject.getId());
    if (childTemplateInDb == null) {
        throw new UnExpectedRequestException("Child template [id = " + childTemplateObject.getId() + "] does not exist.");
    }
    alarmConfigService.deleteByRule(childRule);
    LOGGER.info("Succeed to delete all alarm_config of child rule. rule_id: {}", childRule.getId());
    ruleVariableService.deleteByRule(childRule);
    LOGGER.info("Succeed to delete all rule_variable of child rule. rule_id: {}", childRule.getId());
    ruleDataSourceService.deleteByRule(childRule);
    LOGGER.info("Succeed to delete all rule_dataSources of child rule. rule_id: {}", childRule.getId());
    ruleDataSourceMappingService.deleteByRule(childRule);
    LOGGER.info("Succeed to delete all rule_dataSource_mapping. rule_id: {}", childRule.getId());
    Rule updateRule = ruleDao.saveRule(childRule);
    List<RuleDataSource> ruleDataSourceList = new ArrayList<>();
    for (RuleDataSource ruleDataSource : childRuleDataSources) {
        ruleDataSource.setProjectId(parentRule.getProject().getId());
        ruleDataSource.setRule(updateRule);
        ruleDataSourceList.add(ruleDataSource);
    }
    List<AlarmConfig> alarmConfigList = new ArrayList<>();
    for (AlarmConfig alarmConfig : childAlarmConfigs) {
        alarmConfig.setRule(updateRule);
        alarmConfigList.add(alarmConfig);
    }
    List<RuleVariable> ruleVariablesList = new ArrayList<>();
    for (RuleVariable ruleVariable : childRuleVariables) {
        ruleVariable.setRule(updateRule);
        ruleVariablesList.add(ruleVariable);
    }
    updateRule.setAlarmConfigs(new HashSet<>(alarmConfigDao.saveAllAlarmConfig(alarmConfigList)));
    updateRule.setRuleVariables(new HashSet<>(ruleVariableDao.saveAllRuleVariable(ruleVariablesList)));
    updateRule.setRuleDataSources(new HashSet<>(ruleDataSourceDao.saveAllRuleDataSource(ruleDataSourceList)));
    for (RuleDataSourceMapping ruleDataSourceMapping : childRuleDataSourceMappings) {
        ruleDataSourceMapping.setRule(updateRule);
        ruleDataSourceMappingDao.saveRuleDataSourceMapping(ruleDataSourceMapping);
    }
    updateRule.setParentRule(parentRule);
    parentRule.setChildRule(updateRule);
}
Also used : RuleDataSourceMapping(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSourceMapping) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) RuleDataSource(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource) Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) AlarmConfig(com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig) RuleVariable(com.webank.wedatasphere.qualitis.rule.entity.RuleVariable)

Example 28 with UnExpectedRequestException

use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.

the class RuleNodeServiceImpl method exportRuleByGroupId.

@Override
public GeneralResponse<RuleNodeResponses> exportRuleByGroupId(Long ruleGroupId) throws UnExpectedRequestException {
    // Check existence of ruleGroup
    RuleGroup ruleGroup = ruleGroupDao.findById(ruleGroupId);
    if (ruleGroup == null) {
        throw new UnExpectedRequestException("Rule group [Id = " + ruleGroupId + "] does not exist.");
    }
    LOGGER.info("Succeed to find rule group. Rule group id: {}.", ruleGroup.getId());
    List<RuleNodeResponse> responses = new ArrayList<>();
    try {
        List<Rule> rules = ruleDao.findByRuleGroup(ruleGroup);
        if (CollectionUtils.isEmpty(rules)) {
            throw new UnExpectedRequestException("Rule group [id = " + ruleGroup.getId() + "] does not have rules.");
        }
        for (Rule rule : rules) {
            responses.add(ruleNodeResponse(rule));
        }
    } catch (IOException e) {
        LOGGER.error("Failed to export rule because of JSON serialization opeartions.", e);
        return new GeneralResponse<>("500", "{&FAILED_TO_EXPORT_RULE}", null);
    }
    LOGGER.info("Succeed to export rule. Rule info: {}", Arrays.toString(responses.toArray()));
    RuleNodeResponses ruleNodeResponses = new RuleNodeResponses(responses);
    return new GeneralResponse<>("200", "{&EXPORT_RULE_SUCCESSFULLY}", ruleNodeResponses);
}
Also used : RuleNodeResponses(com.webank.wedatasphere.qualitis.rule.response.RuleNodeResponses) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) RuleNodeResponse(com.webank.wedatasphere.qualitis.rule.response.RuleNodeResponse) ArrayList(java.util.ArrayList) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) IOException(java.io.IOException) RuleGroup(com.webank.wedatasphere.qualitis.rule.entity.RuleGroup)

Example 29 with UnExpectedRequestException

use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.

the class RuleServiceImpl method modifyRuleDetailReal.

private GeneralResponse<RuleResponse> modifyRuleDetailReal(ModifyRuleRequest request, String loginUser) throws UnExpectedRequestException, PermissionDeniedRequestException {
    // Check Arguments
    ModifyRuleRequest.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);
    if (!ruleInDb.getRuleType().equals(RuleTypeEnum.SINGLE_TEMPLATE_RULE.getCode())) {
        throw new UnExpectedRequestException("rule(id: [" + request.getRuleId() + "]) {&IS_NOT_A_RULE_WITH_TEMPLATE}");
    }
    // Check permissions of project
    List<Integer> permissions = new ArrayList<>();
    permissions.add(ProjectUserPermissionEnum.DEVELOPER.getCode());
    projectService.checkProjectPermission(projectInDb, loginUser, permissions);
    LOGGER.info("Succeed to find rule. rule_id: {}", ruleInDb.getId());
    // For workflow node context.
    String csId = request.getCsId();
    boolean cs = false;
    if (StringUtils.isNotBlank(csId)) {
        cs = true;
    }
    ruleInDb.setCsId(csId);
    // Check existence of rule template
    Template templateInDb = ruleTemplateService.checkRuleTemplate(request.getRuleTemplateId());
    // Check datasource size
    checkDataSourceNumber(templateInDb, request.getDatasource());
    // Check cluster name supported
    checkDataSourceClusterLimit(request.getDatasource());
    // Check arguments size
    checkRuleArgumentSize(request.getTemplateArgumentRequests(), templateInDb);
    // Check existence of rule
    checkRuleName(request.getRuleName(), ruleInDb.getProject(), ruleInDb.getId());
    // Basic rule info.
    setBasicInfo(ruleInDb, templateInDb, request, loginUser);
    // Delete all alarm_config,rule_variable,rule_datasource
    alarmConfigService.deleteByRule(ruleInDb);
    LOGGER.info("Succeed to delete all alarm_config. rule_id: {}", ruleInDb.getId());
    ruleVariableService.deleteByRule(ruleInDb);
    LOGGER.info("Succeed to delete all rule_variable. rule_id: {}", ruleInDb.getId());
    ruleDataSourceService.deleteByRule(ruleInDb);
    LOGGER.info("Succeed to delete all rule_dataSources. rule_id: {}", ruleInDb.getId());
    // Update rule count of datasource
    ruleDataSourceService.updateRuleDataSourceCount(ruleInDb, -1);
    Rule savedRule = ruleDao.saveRule(ruleInDb);
    LOGGER.info("Succeed to save rule. rule_id: {}", savedRule.getId());
    // For repeat fps table name when concurrent task is running.
    addUuid(request.getDatasource());
    // Save alarm_config,ruleVariableĺ’ŚruleDataSource
    List<RuleVariable> ruleVariables = autoAdaptArgumentAndGetRuleVariable(templateInDb, request.getDatasource(), request.getTemplateArgumentRequests(), savedRule);
    List<RuleVariable> savedRuleVariables = ruleVariableService.saveRuleVariable(ruleVariables);
    LOGGER.info("Succeed to save rule_variable, rule_variable: {}", savedRuleVariables);
    List<AlarmConfig> savedAlarmConfigs = new ArrayList<>();
    if (request.getAlarm()) {
        savedAlarmConfigs = alarmConfigService.checkAndSaveAlarmVariable(request.getAlarmVariable(), savedRule);
        LOGGER.info("Succeed to save alarm_configs, alarm_configs: {}", savedAlarmConfigs);
    }
    List<RuleDataSource> savedRuleDataSource = ruleDataSourceService.checkAndSaveRuleDataSource(request.getDatasource(), savedRule, cs, loginUser);
    LOGGER.info("Succeed to save rule_dataSources, rule_dataSources: {}", savedRuleDataSource);
    savedRule.setRuleVariables(new HashSet<>(savedRuleVariables));
    savedRule.setAlarmConfigs(new HashSet<>(savedAlarmConfigs));
    savedRule.setRuleDataSources(new HashSet<>(savedRuleDataSource));
    // Update rule count of datasource
    ruleDataSourceService.updateRuleDataSourceCount(ruleInDb, 1);
    RuleResponse response = new RuleResponse(savedRule);
    LOGGER.info("Succeed to modify rule. response: {}", response);
    // projectEventService.record(request.getProjectId(), loginUser, "modify", "rule[name= " + savedRule.getName() + "].", EventTypeEnum.MODIFY_PROJECT.getCode());
    return new GeneralResponse<>("200", "{&MODIFY_RULE_SUCCESSFULLY}", response);
}
Also used : UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) RuleResponse(com.webank.wedatasphere.qualitis.rule.response.RuleResponse) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) Project(com.webank.wedatasphere.qualitis.project.entity.Project)

Example 30 with UnExpectedRequestException

use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.

the class RuleServiceImpl method getRuleDetail.

@Override
public GeneralResponse<RuleDetailResponse> getRuleDetail(Long ruleId) throws UnExpectedRequestException {
    // Check existence of rule
    Rule ruleInDb = ruleDao.findById(ruleId);
    if (ruleInDb == null) {
        throw new UnExpectedRequestException("rule_id [" + ruleId + "] {&DOES_NOT_EXIST}");
    }
    if (!ruleInDb.getRuleType().equals(RuleTypeEnum.SINGLE_TEMPLATE_RULE.getCode())) {
        throw new UnExpectedRequestException("rule(id: [" + ruleId + "]) {&IS_NOT_A_RULE_WITH_TEMPLATE}");
    }
    Long projectInDbId = ruleInDb.getProject().getId();
    String loginUser = HttpUtils.getUserName(httpServletRequest);
    projectService.checkProjectExistence(projectInDbId, loginUser);
    LOGGER.info("Succeed to find rule. rule_id: {}", ruleInDb.getId());
    RuleDetailResponse response = new RuleDetailResponse(ruleInDb);
    LOGGER.info("Succeed to get rule detail. rule_id: {}", ruleId);
    return new GeneralResponse<>("200", "{&GET_RULE_DETAIL_SUCCESSFULLY}", response);
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) RuleDetailResponse(com.webank.wedatasphere.qualitis.rule.response.RuleDetailResponse)

Aggregations

UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)150 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)97 ArrayList (java.util.ArrayList)56 Transactional (org.springframework.transaction.annotation.Transactional)54 Project (com.webank.wedatasphere.qualitis.project.entity.Project)33 User (com.webank.wedatasphere.qualitis.entity.User)32 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)32 RuleMetric (com.webank.wedatasphere.qualitis.entity.RuleMetric)25 Map (java.util.Map)24 ClusterInfo (com.webank.wedatasphere.qualitis.entity.ClusterInfo)22 List (java.util.List)22 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)19 IOException (java.io.IOException)19 MetaDataAcquireFailedException (com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException)17 Date (java.util.Date)17 HashMap (java.util.HashMap)17 RuleDataSource (com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource)15 RuleGroup (com.webank.wedatasphere.qualitis.rule.entity.RuleGroup)15 Collectors (java.util.stream.Collectors)15 Template (com.webank.wedatasphere.qualitis.rule.entity.Template)14