Search in sources :

Example 1 with RuleDataSource

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

the class RuleBatchServiceImpl method getFileRule.

@Override
public List<ExcelTemplateFileRuleByProject> getFileRule(Iterable<Rule> rules, String localeStr) {
    List<ExcelTemplateFileRuleByProject> lines = new ArrayList<>();
    for (Rule rule : rules) {
        if (!rule.getRuleType().equals(RuleTypeEnum.FILE_TEMPLATE_RULE.getCode())) {
            continue;
        }
        String ruleName = rule.getName();
        String ruleCnName = rule.getCnName();
        String ruleDetail = rule.getDetail();
        String templateName = rule.getTemplate().getName();
        ExcelTemplateFileRuleByProject ruleLinePrefix = new ExcelTemplateFileRuleByProject();
        ruleLinePrefix.setRuleName(ruleName);
        ruleLinePrefix.setRuleCnName(ruleCnName);
        ruleLinePrefix.setRuleDetail(ruleDetail);
        ruleLinePrefix.setTemplateName(templateName);
        ruleLinePrefix.setProjectName(rule.getProject().getName());
        ruleLinePrefix.setAbortOnFailure(rule.getAbortOnFailure());
        ruleLinePrefix.setRuleGroupName(rule.getRuleGroup().getRuleGroupName());
        ruleLinePrefix.setCreateUser(rule.getCreateUser());
        ruleLinePrefix.setCreateTime(rule.getCreateTime());
        ruleLinePrefix.setModifyUser(rule.getModifyUser());
        ruleLinePrefix.setModifyTime(rule.getModifyTime());
        ruleLinePrefix.setDeleteFailCheckResult(rule.getDeleteFailCheckResult());
        lines.add(ruleLinePrefix);
        for (RuleDataSource ruleDataSource : rule.getRuleDataSources()) {
            String clusterName = ruleDataSource.getClusterName();
            String databaseName = ruleDataSource.getDbName();
            String tableName = ruleDataSource.getTableName();
            String filter = ruleDataSource.getFilter();
            ExcelTemplateFileRuleByProject tmp = new ExcelTemplateFileRuleByProject(rule.getName());
            tmp.setClusterName(clusterName);
            tmp.setDatabaseName(databaseName);
            tmp.setTableName(tableName);
            tmp.setFilter(filter);
            LOGGER.info("Collect excel line: {}", tmp);
            lines.add(tmp);
        }
        for (AlarmConfig alarmConfig : rule.getAlarmConfigs()) {
            Double threshold = alarmConfig.getThreshold();
            String alarmCompareType = CompareTypeEnum.getCompareTypeName(alarmConfig.getCompareType());
            String alarmOutputName = FileOutputNameEnum.getFileOutputName(alarmConfig.getFileOutputName(), localeStr);
            String checkTemplateName = CheckTemplateEnum.getCheckTemplateName(alarmConfig.getCheckTemplate(), localeStr);
            String unit = alarmConfig.getFileOutputUnit() == null ? "" : FileOutputUnitEnum.fileOutputUnit(alarmConfig.getFileOutputUnit());
            ExcelTemplateFileRuleByProject tmp = new ExcelTemplateFileRuleByProject(rule.getName());
            tmp.setUnit(unit);
            tmp.setCompareType(alarmCompareType);
            tmp.setAlarmCheckName(alarmOutputName);
            tmp.setCheckTemplateName(checkTemplateName);
            tmp.setThreshold(String.valueOf(threshold));
            RuleMetric ruleMetric = alarmConfig.getRuleMetric();
            // Recod rule metric info (unique code).
            if (ruleMetric != null) {
                String enCode = ruleMetric.getEnCode();
                tmp.setRuleMetricEnCode(enCode);
                tmp.setRuleMetricName(ruleMetric.getName());
            }
            LOGGER.info("Collect excel line: {}", tmp);
            lines.add(tmp);
        }
    }
    return lines;
}
Also used : RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) RuleDataSource(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource) ArrayList(java.util.ArrayList) ExcelTemplateFileRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateFileRuleByProject) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) AlarmConfig(com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)

Example 2 with RuleDataSource

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

the class RuleBatchServiceImpl method basicInfoToExcel.

private void basicInfoToExcel(ExcelCustomRuleByProject ruleLinePrefix, Rule rule) {
    String ruleName = rule.getName();
    String ruleDetail = rule.getDetail();
    String ruleCnName = rule.getCnName();
    String outputName = rule.getOutputName();
    Boolean saveMidTable = rule.getTemplate().getSaveMidTable();
    RuleDataSource ruleDataSource = rule.getRuleDataSources().iterator().next();
    ruleLinePrefix.setProxyUser(ruleDataSource.getProxyUser());
    String clusterName = ruleDataSource.getClusterName();
    // Original custom rule, not sql check type.
    if (rule.getFunctionType() != null && StringUtils.isNotBlank(rule.getFunctionContent()) && StringUtils.isNotBlank(rule.getFromContent()) && StringUtils.isNotBlank(rule.getWhereContent())) {
        String functionName = FunctionTypeEnum.getFunctionByCode(rule.getFunctionType());
        String functionContent = rule.getFunctionContent();
        String whereContent = rule.getWhereContent();
        String fromContent = rule.getFromContent();
        ruleLinePrefix.setFunctionContent(functionContent);
        ruleLinePrefix.setWhereContent(whereContent);
        ruleLinePrefix.setFunctionName(functionName);
        ruleLinePrefix.setFromContent(fromContent);
    } else {
        ruleLinePrefix.setSqlCheckArea(rule.getTemplate().getMidTableAction());
    }
    if (ruleDataSource.getLinkisDataSourceId() != null) {
        String linkisDataSourceName = ruleDataSource.getLinkisDataSourceName();
        String linkisDataSourceId = String.valueOf(ruleDataSource.getLinkisDataSourceId());
        String linkisDataSourceType = TemplateDataSourceTypeEnum.getMessage(ruleDataSource.getDatasourceType());
        ruleLinePrefix.setLinkisDataSourceName(linkisDataSourceName);
        ruleLinePrefix.setLinkisDataSourceType(linkisDataSourceType);
        ruleLinePrefix.setLinkisDataSourceId(linkisDataSourceId);
    }
    ruleLinePrefix.setRuleGroupName(rule.getRuleGroup().getRuleGroupName());
    ruleLinePrefix.setProjectName(rule.getProject().getName());
    ruleLinePrefix.setAbortOnFailure(rule.getAbortOnFailure());
    ruleLinePrefix.setRuleDetail(ruleDetail);
    ruleLinePrefix.setRuleCnName(ruleCnName);
    ruleLinePrefix.setRuleName(ruleName);
    ruleLinePrefix.setOutputName(outputName);
    ruleLinePrefix.setClusterName(clusterName);
    ruleLinePrefix.setSaveMidTable(saveMidTable);
    ruleLinePrefix.setCreateUser(rule.getCreateUser());
    ruleLinePrefix.setCreateTime(rule.getCreateTime());
    ruleLinePrefix.setModifyUser(rule.getModifyUser());
    ruleLinePrefix.setModifyTime(rule.getModifyTime());
    ruleLinePrefix.setDeleteFailCheckResult(rule.getDeleteFailCheckResult());
    ruleLinePrefix.setSpecifyStaticStartupParam(rule.getSpecifyStaticStartupParam());
    if (rule.getSpecifyStaticStartupParam() != null && rule.getSpecifyStaticStartupParam()) {
        ruleLinePrefix.setStaticStartupParam(rule.getStaticStartupParam());
    }
}
Also used : RuleDataSource(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource)

Example 3 with RuleDataSource

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

the class RuleDataSourceServiceImpl method checkAndSaveCustomRuleDataSource.

@Override
@Transactional(rollbackFor = { RuntimeException.class })
public List<RuleDataSource> checkAndSaveCustomRuleDataSource(String clusterName, String proxyUser, String loginUser, Rule savedRule, boolean cs, boolean sqlCheck, Long linkisDataSourceId, Long linkisDataSourceVersionId, String linkisDataSourceName, String linkisDataSourceType) {
    List<RuleDataSource> ruleDataSources = new ArrayList<>();
    RuleDataSource ruleDataSource = new RuleDataSource();
    ruleDataSource.setDatasourceIndex(ORIGINAL_INDEX);
    ruleDataSource.setClusterName(clusterName);
    ruleDataSource.setProxyUser(proxyUser);
    ruleDataSource.setRule(savedRule);
    if (StringUtils.isNotBlank(linkisDataSourceType)) {
        ruleDataSource.setLinkisDataSourceId(linkisDataSourceId);
        ruleDataSource.setLinkisDataSourceName(linkisDataSourceName);
        ruleDataSource.setLinkisDataSourceVersionId(linkisDataSourceVersionId);
        ruleDataSource.setDatasourceType(TemplateDataSourceTypeEnum.getCode(linkisDataSourceType));
    }
    ruleDataSources.add(ruleDataSource);
    LOGGER.info("Start to save custom rule datasource with cluster name and proxy user.");
    if (CollectionUtils.isEmpty(ruleDataSources)) {
        return ruleDataSources;
    }
    return ruleDatasourceDao.saveAllRuleDataSource(ruleDataSources);
}
Also used : RuleDataSource(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource) ArrayList(java.util.ArrayList) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with RuleDataSource

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

the class RuleDataSourceServiceImpl method checkAndSaveFileRuleDataSource.

@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public RuleDataSource checkAndSaveFileRuleDataSource(DataSourceRequest request, Rule rule, boolean cs) throws UnExpectedRequestException {
    DataSourceRequest.checkRequest(request, cs, "File rule datasource request");
    RuleDataSource newRuleDataSource = new RuleDataSource();
    newRuleDataSource.setClusterName(request.getClusterName());
    newRuleDataSource.setDbName(request.getDbName());
    newRuleDataSource.setTableName(request.getTableName());
    if (request.getFilter() != null) {
        newRuleDataSource.setFilter(request.getFilter());
    } else {
        newRuleDataSource.setFilter("");
    }
    newRuleDataSource.setColName("");
    newRuleDataSource.setProxyUser(request.getProxyUser());
    newRuleDataSource.setRule(rule);
    newRuleDataSource.setProjectId(rule.getProject().getId());
    return ruleDatasourceDao.saveRuleDataSource(newRuleDataSource);
}
Also used : RuleDataSource(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with RuleDataSource

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

the class FileRuleServiceImpl method addRuleReal.

private GeneralResponse<RuleResponse> addRuleReal(AddFileRuleRequest request, String loginUser) throws UnExpectedRequestException, PermissionDeniedRequestException {
    // Check Arguments
    AddFileRuleRequest.checkRequest(request, false);
    // Generate Template, TemplateOutputMeta and save
    Template template = ruleTemplateService.addFileTemplate(request);
    // Check existence of project
    Project projectInDb = projectService.checkProjectExistence(request.getProjectId(), loginUser);
    String nowDate = ExecutionManagerImpl.PRINT_TIME_FORMAT.format(new Date());
    // 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 cluster support
    ruleDataSourceService.checkDataSourceClusterSupport(request.getDatasource().getClusterName());
    RuleGroup ruleGroup;
    if (request.getRuleGroupId() != null) {
        ruleGroup = ruleGroupDao.findById(request.getRuleGroupId());
        if (ruleGroup == null) {
            throw new UnExpectedRequestException(String.format("Rule Group: %s {&DOES_NOT_EXIST}", request.getRuleGroupId()));
        }
    } else {
        ruleGroup = ruleGroupDao.saveRuleGroup(new RuleGroup("Group_" + UUID.randomUUID().toString().replace("-", ""), projectInDb.getId()));
    }
    // Save rule.
    Rule newRule = new Rule();
    newRule.setRuleType(RuleTypeEnum.FILE_TEMPLATE_RULE.getCode());
    newRule.setTemplate(template);
    newRule.setName(request.getRuleName());
    newRule.setCnName(request.getRuleCnName());
    newRule.setDetail(request.getRuleDetail());
    newRule.setAlarm(request.getAlarm());
    newRule.setProject(projectInDb);
    newRule.setRuleTemplateName(template.getName());
    newRule.setRuleGroup(ruleGroup);
    newRule.setAbortOnFailure(request.getAbortOnFailure());
    newRule.setCreateUser(loginUser);
    newRule.setCreateTime(nowDate);
    String csId = request.getCsId();
    boolean cs = false;
    if (StringUtils.isNotBlank(csId)) {
        newRule.setCsId(csId);
        cs = true;
    }
    Rule savedRule = ruleDao.saveRule(newRule);
    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(savedRule, 1);
    RuleResponse response = new RuleResponse(savedRule);
    LOGGER.info("Succeed to add file rule, rule_id: {}", savedRule.getId());
    return new GeneralResponse<>("200", "{&SUCCEED_TO_ADD_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) 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) AlarmConfig(com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)

Aggregations

RuleDataSource (com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource)37 ArrayList (java.util.ArrayList)29 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)21 AlarmConfig (com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)18 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)14 Template (com.webank.wedatasphere.qualitis.rule.entity.Template)11 HashSet (java.util.HashSet)11 List (java.util.List)10 Set (java.util.Set)10 Transactional (org.springframework.transaction.annotation.Transactional)10 RuleMetric (com.webank.wedatasphere.qualitis.entity.RuleMetric)9 Project (com.webank.wedatasphere.qualitis.project.entity.Project)9 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)9 RuleDataSourceMapping (com.webank.wedatasphere.qualitis.rule.entity.RuleDataSourceMapping)9 Date (java.util.Date)9 HashMap (java.util.HashMap)9 RuleVariable (com.webank.wedatasphere.qualitis.rule.entity.RuleVariable)8 Map (java.util.Map)8 SpecCharEnum (com.webank.wedatasphere.qualitis.constant.SpecCharEnum)6 MetaDataAcquireFailedException (com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException)6