Search in sources :

Example 51 with Rule

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

the class RuleNodeServiceImpl method importChildRule.

private void importChildRule(Rule parentRule, RuleGroup ruleGroup, RuleNodeRequest ruleNodeRequest, Project projectInDb) throws IOException {
    LOGGER.info("Start to import child rule.");
    ObjectMapper objectMapper = new ObjectMapper();
    Rule childRule = objectMapper.readValue(ruleNodeRequest.getChildRuleObject(), Rule.class);
    Template childTemplate = 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.setProject(projectInDb);
    childRule.setTemplate(childTemplate);
    childRule.setRuleGroup(ruleGroup);
    Rule savedRule = ruleDao.saveRule(childRule);
    LOGGER.info("Succeed to save child rule. Rule info is {}", savedRule.toString());
    List<AlarmConfig> alarmConfigList = new ArrayList<>();
    for (AlarmConfig alarmConfig : childAlarmConfigs) {
        alarmConfig.setRule(savedRule);
        alarmConfigList.add(alarmConfig);
    }
    List<RuleVariable> ruleVariablesList = new ArrayList<>();
    for (RuleVariable ruleVariable : childRuleVariables) {
        ruleVariable.setRule(savedRule);
        ruleVariablesList.add(ruleVariable);
    }
    List<RuleDataSource> ruleDataSourceList = new ArrayList<>();
    for (RuleDataSource ruleDataSource : childRuleDataSources) {
        ruleDataSource.setProjectId(projectInDb.getId());
        ruleDataSource.setRule(savedRule);
        ruleDataSourceList.add(ruleDataSource);
    }
    savedRule.setAlarmConfigs(new HashSet<>(alarmConfigDao.saveAllAlarmConfig(alarmConfigList)));
    savedRule.setRuleVariables(new HashSet<>(ruleVariableDao.saveAllRuleVariable(ruleVariablesList)));
    savedRule.setRuleDataSources(new HashSet<>(ruleDataSourceDao.saveAllRuleDataSource(ruleDataSourceList)));
    for (RuleDataSourceMapping ruleDataSourceMapping : childRuleDataSourceMappings) {
        ruleDataSourceMapping.setRule(savedRule);
        ruleDataSourceMappingDao.saveRuleDataSourceMapping(ruleDataSourceMapping);
    }
    savedRule.setParentRule(parentRule);
    parentRule.setChildRule(savedRule);
    LOGGER.info("Succeed to import child rule.");
}
Also used : RuleDataSourceMapping(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSourceMapping) 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 52 with Rule

use of com.webank.wedatasphere.qualitis.rule.entity.Rule 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)

Example 53 with Rule

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

the class CustomRuleServiceImpl method modifyCustomRuleReal.

private GeneralResponse<RuleResponse> modifyCustomRuleReal(ModifyCustomRuleRequest request, String loginUser) throws UnExpectedRequestException, MetaDataAcquireFailedException, PermissionDeniedRequestException {
    ModifyCustomRuleRequest.checkRequest(request);
    Rule ruleInDb = ruleDao.findById(request.getRuleId());
    if (ruleInDb == null) {
        throw new UnExpectedRequestException("rule_id [" + request.getRuleId() + "] {&DOES_NOT_EXIST}");
    }
    Project projectInDb = projectService.checkProjectExistence(ruleInDb.getProject().getId(), loginUser);
    if (!ruleInDb.getRuleType().equals(RuleTypeEnum.CUSTOM_RULE.getCode())) {
        throw new UnExpectedRequestException("rule_id: [" + request.getRuleId() + "]) {&IS_NOT_A_CUSTOM_RULE}");
    }
    // Check permissions of project
    List<Integer> permissions = new ArrayList<>();
    permissions.add(ProjectUserPermissionEnum.DEVELOPER.getCode());
    projectService.checkProjectPermission(projectInDb, loginUser, permissions);
    LOGGER.info("Succeed to find custom rule. rule_id: {}", ruleInDb.getId());
    // Check existence of project name
    ruleService.checkRuleName(request.getRuleName(), ruleInDb.getProject(), ruleInDb.getId());
    // Check if cluster name supported
    ruleDataSourceService.checkDataSourceClusterSupport(request.getClusterName());
    String nowDate = ExecutionManagerImpl.PRINT_TIME_FORMAT.format(new Date());
    // Delete alarm config by custom rule
    alarmConfigService.deleteByRule(ruleInDb);
    LOGGER.info("Succeed to delete all alarm_config. rule_id: {}", ruleInDb.getId());
    // Delete template of custom rule
    ruleTemplateService.deleteCustomTemplate(ruleInDb.getTemplate());
    LOGGER.info("Succeed to delete custom rule template. rule_id: {}", request.getRuleId());
    // Delete rule datasource of custom 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);
    // Save template, alarm config, rule datasource of custom rule
    AddCustomRuleRequest addCustomRuleRequest = new AddCustomRuleRequest();
    BeanUtils.copyProperties(request, addCustomRuleRequest);
    Template template = ruleTemplateService.addCustomTemplate(addCustomRuleRequest);
    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);
    // Modify custom rule and save
    setBasicInfo(ruleInDb, template, loginUser, nowDate, request);
    String csId = request.getCsId();
    ruleInDb.setCsId(csId);
    boolean cs = false;
    if (StringUtils.isNotBlank(csId)) {
        cs = true;
    }
    String fileId = request.getFileId();
    boolean fps = false;
    if (StringUtils.isNotBlank(fileId)) {
        fps = true;
    }
    String sqlCheckArea = request.getSqlCheckArea();
    boolean sqlCheck = false;
    if (StringUtils.isNotBlank(sqlCheckArea)) {
        sqlCheck = true;
    }
    Rule savedRule = ruleDao.saveRule(ruleInDb);
    // Save alarm config and rule datasource
    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(ruleInDb, 1);
    RuleResponse response = new RuleResponse(savedRule);
    LOGGER.info("Succeed to modify custom rule, rule_id: {}", savedRule.getId());
    // projectEventService.record(savedRule.getProject().getId(), loginUser, "modify", "custom rule[name= " + savedRule.getName() + "].", EventTypeEnum.MODIFY_PROJECT.getCode());
    return new GeneralResponse<>("200", "{&SUCCEED_TO_MODIFY_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) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) Project(com.webank.wedatasphere.qualitis.project.entity.Project) AddCustomRuleRequest(com.webank.wedatasphere.qualitis.rule.request.AddCustomRuleRequest) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) TemplateDataSourceType(com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType) AlarmConfig(com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)

Example 54 with Rule

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

the class CustomRuleServiceImpl method deleteCustomRule.

/**
 * Delete custom rules, including template of custom rules
 * @param request
 * @return
 * @throws UnExpectedRequestException
 */
@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<?> deleteCustomRule(DeleteCustomRuleRequest request) throws UnExpectedRequestException, PermissionDeniedRequestException {
    // Check Arguments
    DeleteCustomRuleRequest.checkRequest(request);
    String loginUser = HttpUtils.getUserName(httpServletRequest);
    // Check existence of rule by ruleId
    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.CUSTOM_RULE.getCode())) {
        throw new UnExpectedRequestException("rule_id: [" + request.getRuleId() + "]) {&IS_NOT_A_CUSTOM_RULE}");
    }
    // Update rule count of datasource
    ruleDataSourceService.updateRuleDataSourceCount(ruleInDb, -1);
    return deleteCustomRuleReal(ruleInDb);
}
Also used : UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) Project(com.webank.wedatasphere.qualitis.project.entity.Project) ArrayList(java.util.ArrayList) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) Transactional(org.springframework.transaction.annotation.Transactional)

Example 55 with Rule

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

the class FileRuleServiceImpl 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.FILE_TEMPLATE_RULE.getCode())) {
        throw new UnExpectedRequestException("rule(id: [" + ruleId + "]) {&IS_NOT_A_FILE_RULE}");
    }
    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", "{&SUCCEED_TO_GET_FILE_RULE_DETAIL}", 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) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule)

Aggregations

Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)61 ArrayList (java.util.ArrayList)41 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)35 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)25 RuleDataSource (com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource)25 Project (com.webank.wedatasphere.qualitis.project.entity.Project)23 AlarmConfig (com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)21 Date (java.util.Date)20 List (java.util.List)20 Transactional (org.springframework.transaction.annotation.Transactional)19 HashMap (java.util.HashMap)18 RuleMetric (com.webank.wedatasphere.qualitis.entity.RuleMetric)15 IOException (java.io.IOException)15 Set (java.util.Set)14 RuleGroup (com.webank.wedatasphere.qualitis.rule.entity.RuleGroup)13 Template (com.webank.wedatasphere.qualitis.rule.entity.Template)13 HashSet (java.util.HashSet)13 Collectors (java.util.stream.Collectors)13 Logger (org.slf4j.Logger)13 MetaDataAcquireFailedException (com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException)12