Search in sources :

Example 6 with TemplateStatisticsInputMeta

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

the class JdbcTranslator method sentenceWithRunDate.

private void sentenceWithRunDate(Set<TemplateStatisticsInputMeta> templateStatisticsInputMetas, List<RuleVariable> ruleVariables, List<String> list, String applicationId, String createTime, Integer count, Long ruleId, Map<String, Long> ruleMetricMap, String runDate) throws RuleVariableNotSupportException, RuleVariableNotFoundException {
    Date runRealDate = null;
    try {
        runRealDate = new SimpleDateFormat("yyyyMMdd").parse(runDate);
    } catch (ParseException e) {
        LOGGER.error(e.getMessage(), e);
        throw new RuleVariableNotSupportException("{&FAILED_TO_PARSE_RUN_DATE}");
    }
    list.add("var connection: Connection = null");
    list.add("classOf[com.mysql.jdbc.Driver]");
    list.add("try {");
    list.add("\tconnection = DriverManager.getConnection(\"" + mysqlAddress + "\", " + PROP_VARIABLE_NAME + ")");
    for (TemplateStatisticsInputMeta s : templateStatisticsInputMetas) {
        String funcName = s.getFuncName();
        String value = getValue(ruleVariables, s);
        String persistSentence = statisticsAndSaveResultTemplate.replace(STATISTICS_VALUE_PLACEHOLDER, funcName + "(" + value + ")").replace(STATISTICS_APPLICATION_ID_PLACEHOLDER, applicationId).replace(STATISTICS_RESULT_TYPE_PLACEHOLDER, s.getResultType()).replace(STATISTICS_CREATE_TIME_PLACEHOLDER, createTime).replace(SqlTemplateConverter.VARIABLE_NAME_PLACEHOLDER, getVariable(count)).replace(STATISTICS_RULE_ID_PLACEHOLDER, ruleId + "");
        persistSentence = persistSentence.replace(STATISTICS_RUN_DATE_PLACEHOLDER, runRealDate.getTime() + "");
        StringBuffer selectSql = new StringBuffer();
        StringBuffer deleteSql = new StringBuffer();
        String varName = s.getName().replace("{", "").replace("}", "").replace("&", "");
        if (ruleMetricMap.get(value) != null) {
            persistSentence = persistSentence.replace(STATISTICS_RULE_METRIC_ID_PLACEHOLDER, ruleMetricMap.get(value) + "");
            selectSql.append("val selectSql").append("_").append(varName).append(" = \"(select * from ").append(resultTableName).append(" where rule_id = ").append(ruleId).append(" and rule_metric_id = ").append(ruleMetricMap.get(value)).append(" and (run_date = ").append(runRealDate.getTime()).append(")) qualitis_tmp_table\"");
            deleteSql.append("val deleteSql").append("_").append(varName).append(" = \"delete from ").append(resultTableName).append(" where rule_id = ").append(ruleId).append(" and rule_metric_id = ").append(ruleMetricMap.get(value)).append(" and (run_date = ").append(runRealDate.getTime()).append(")\"");
        } else {
            if (CollectionUtils.isNotEmpty(ruleMetricMap.values())) {
                persistSentence = persistSentence.replace(STATISTICS_RULE_METRIC_ID_PLACEHOLDER, ruleMetricMap.values().iterator().next() + "");
                selectSql.append("val selectSql").append("_").append(varName).append(" = \"(select * from ").append(resultTableName).append(" where rule_id = ").append(ruleId).append(" and rule_metric_id = ").append(ruleMetricMap.values().iterator().next()).append(" and (run_date = ").append(runRealDate.getTime()).append(")) qualitis_tmp_table\"");
                deleteSql.append("val deleteSql").append("_").append(varName).append(" = \"delete from ").append(resultTableName).append(" where rule_id = ").append(ruleId).append(" and rule_metric_id = ").append(ruleMetricMap.values().iterator().next()).append(" and (run_date = ").append(runRealDate.getTime()).append(")\"");
            } else {
                persistSentence = persistSentence.replace(STATISTICS_RULE_METRIC_ID_PLACEHOLDER, "-1");
                selectSql.append("val selectSql").append("_").append(varName).append(" = \"(select * from ").append(resultTableName).append(" where rule_id = ").append(ruleId).append(" and rule_metric_id = ").append("-1").append(" and (run_date = ").append(runRealDate.getTime()).append(")) qualitis_tmp_table\"");
                deleteSql.append("val deleteSql").append("_").append(varName).append(" = \"delete from ").append(resultTableName).append(" where rule_id = ").append(ruleId).append(" and rule_metric_id = ").append("-1").append(" and (run_date = ").append(runRealDate.getTime()).append(")\"");
            }
        }
        list.add(selectSql.toString());
        // Judge the existence of task result with rule ID, rule metric ID, run date.
        list.add("val resultDF" + "_" + varName + " = spark.read.jdbc(\"" + mysqlAddress + "\", selectSql" + "_" + varName + ", prop)");
        list.add("val lines" + "_" + varName + " = resultDF" + "_" + varName + ".count()");
        list.add("if (lines" + "_" + varName + " >= 1) {");
        // Delete the exist task result before insert.
        list.add(deleteSql.toString());
        list.add("connection.createStatement().executeUpdate(deleteSql" + "_" + varName + ")");
        list.add("}");
        list.add(persistSentence);
        LOGGER.info("Succeed to get persist sentence. sentence: {}", persistSentence);
    }
    list.add("} catch {");
    list.add("case e: Exception => println(\"JDBC operations failed because of \", e.getMessage())");
    list.add("} finally {");
    list.add("\tconnection.close()");
    list.add("}");
}
Also used : ParseException(java.text.ParseException) RuleVariableNotSupportException(com.webank.wedatasphere.qualitis.exception.RuleVariableNotSupportException) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) TemplateStatisticsInputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateStatisticsInputMeta)

Example 7 with TemplateStatisticsInputMeta

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

the class RuleNodeServiceImpl method importRuleReal.

private void importRuleReal(RuleNodeRequest ruleNodeRequest, Rule ruleInDb, Rule rule, Project projectInDb, Template template, RuleGroup ruleGroup, Set<AlarmConfig> alarmConfigs, Set<RuleVariable> ruleVariables, Set<RuleDataSource> ruleDataSources, Set<RuleDataSourceMapping> ruleDataSourceMappings) throws IOException, UnExpectedRequestException {
    if (ruleInDb == null) {
        LOGGER.info("Import in first time. That means adding.");
        rule.setProject(projectInDb);
        ruleGroup.setProjectId(projectInDb.getId());
        if (RuleTypeEnum.CUSTOM_RULE.getCode().equals(rule.getRuleType())) {
            Template saveTemplate = ruleTemplateDao.saveTemplate(template);
            Set<Integer> templateDateTypes = ruleDataSources.stream().filter(currRuleDataSource -> currRuleDataSource.getDatasourceType() != null).map(RuleDataSource::getDatasourceType).collect(Collectors.toSet());
            for (Integer templateDataType : templateDateTypes) {
                TemplateDataSourceType templateDataSourceType = new TemplateDataSourceType(templateDataType, saveTemplate);
                templateDataSourceTypeDao.save(templateDataSourceType);
            }
            Set<TemplateStatisticsInputMeta> templateStatisticsInputMetas = new HashSet<>();
            Set<TemplateOutputMeta> templateOutputMetaSet = new HashSet<>();
            if (rule.getOutputName() != null && rule.getFunctionType() != null && rule.getFunctionContent() != null) {
                templateStatisticsInputMetas = templateStatisticsInputMetaService.getAndSaveTemplateStatisticsInputMeta(rule.getOutputName(), rule.getFunctionType(), rule.getFunctionContent(), saveTemplate.getSaveMidTable(), saveTemplate);
                templateOutputMetaSet = templateOutputMetaService.getAndSaveTemplateOutputMeta(rule.getOutputName(), rule.getFunctionType(), saveTemplate.getSaveMidTable(), saveTemplate);
            } else {
                List<RuleMetric> ruleMetrics = alarmConfigs.stream().map(AlarmConfig::getRuleMetric).collect(Collectors.toList());
                for (RuleMetric ruleMetric : ruleMetrics) {
                    templateStatisticsInputMetas.addAll(templateStatisticsInputMetaService.getAndSaveTemplateStatisticsInputMeta(ruleMetric.getName(), FunctionTypeEnum.MAX_FUNCTION.getCode(), ruleMetric.getName(), saveTemplate.getSaveMidTable(), saveTemplate));
                    templateOutputMetaSet.addAll(templateOutputMetaService.getAndSaveTemplateOutputMeta(ruleMetric.getName(), FunctionTypeEnum.MAX_FUNCTION.getCode(), saveTemplate.getSaveMidTable(), saveTemplate));
                }
            }
            saveTemplate.setStatisticAction(templateStatisticsInputMetas);
            saveTemplate.setTemplateOutputMetas(templateOutputMetaSet);
            rule.setTemplate(saveTemplate);
        } else if (RuleTypeEnum.FILE_TEMPLATE_RULE.getCode().equals(rule.getRuleType())) {
            LOGGER.info("Start to import file rule. {}", rule.getName());
            // Save file rule template.
            rule.setTemplate(ruleTemplateDao.saveTemplate(template));
            // Save file rule group
            rule.setRuleGroup(ruleGroup);
            Rule savedRule = ruleDao.saveRule(rule);
            // Save file alarmconfig
            List<AlarmConfig> alarmConfigList = new ArrayList<>();
            for (AlarmConfig alarmConfig : alarmConfigs) {
                ruleMetricSycn(alarmConfig);
                alarmConfig.setRule(savedRule);
                alarmConfigList.add(alarmConfig);
            }
            List<RuleDataSource> ruleDataSourceList = new ArrayList<>();
            for (RuleDataSource ruleDataSource : ruleDataSources) {
                ruleDataSource.setProjectId(projectInDb.getId());
                ruleDataSource.setRule(savedRule);
                ruleDataSourceList.add(ruleDataSource);
            }
            savedRule.setAlarmConfigs(new HashSet<>(alarmConfigDao.saveAllAlarmConfig(alarmConfigList)));
            savedRule.setRuleDataSources(new HashSet<>(ruleDataSourceDao.saveAllRuleDataSource(ruleDataSourceList)));
            LOGGER.info("Finish to import file rule. {}", rule.getName());
            return;
        } else {
            // When trying out the newly created rule template in the development center, the production center needs to synchronize the rule template.
            synchroRuleTemplate(ruleNodeRequest, rule, template, ruleDataSources);
        }
        rule.setRuleGroup(ruleGroup);
        Rule savedRule = ruleDao.saveRule(rule);
        saveRuleInfo(savedRule, template, projectInDb, alarmConfigs, ruleVariables, ruleDataSources, ruleDataSourceMappings);
        if (ruleNodeRequest.getChildRuleObject() != null) {
            importChildRule(savedRule, ruleGroup, ruleNodeRequest, projectInDb);
        }
    } else {
        LOGGER.info("Import multiple times. That is to update.");
        if (RuleTypeEnum.FILE_TEMPLATE_RULE.getCode().equals(rule.getRuleType())) {
            LOGGER.info("Start to update import file rule. {}", rule.getName());
            Set<AlarmConfig> alarmConfigList = new HashSet<>();
            for (AlarmConfig alarmConfig : alarmConfigs) {
                ruleMetricSycn(alarmConfig);
                alarmConfig.setRule(ruleInDb);
                alarmConfigList.add(alarmConfig);
            }
            updateImportedFileRule(rule, ruleInDb, ruleGroup, alarmConfigList, ruleDataSources);
            LOGGER.info("Finish to update import file rule. {}", rule.getName());
            return;
        }
        updateImportedRule(rule, ruleInDb, ruleGroup, ruleNodeRequest, template, alarmConfigs, ruleVariables, ruleDataSources, ruleDataSourceMappings);
    }
}
Also used : RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) RuleDataSource(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource) TemplateOutputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateOutputMeta) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) TemplateStatisticsInputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateStatisticsInputMeta) ArrayList(java.util.ArrayList) List(java.util.List) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) TemplateDataSourceType(com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType) HashSet(java.util.HashSet) AlarmConfig(com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)

Example 8 with TemplateStatisticsInputMeta

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

the class RuleNodeServiceImpl method updateImportedRule.

public void updateImportedRule(Rule rule, Rule ruleInDb, RuleGroup ruleGroup, RuleNodeRequest ruleNodeRequest, Template template, Set<AlarmConfig> alarmConfigs, Set<RuleVariable> ruleVariables, Set<RuleDataSource> ruleDataSources, Set<RuleDataSourceMapping> ruleDataSourceMappings) throws UnExpectedRequestException, IOException {
    ruleInDb.setRuleGroup(ruleGroup);
    ruleInDb.setAlarm(rule.getAlarm());
    ruleInDb.setAbortOnFailure(rule.getAbortOnFailure());
    ruleInDb.setFromContent(rule.getFromContent());
    ruleInDb.setWhereContent(rule.getWhereContent());
    ruleInDb.setRuleType(rule.getRuleType());
    ruleInDb.setRuleTemplateName(template.getName());
    ruleInDb.setOutputName(rule.getOutputName());
    ruleInDb.setFunctionType(rule.getFunctionType());
    ruleInDb.setFunctionContent(rule.getFunctionContent());
    ruleInDb.setDeleteFailCheckResult(rule.getDeleteFailCheckResult());
    if (RuleTypeEnum.CUSTOM_RULE.getCode().equals(rule.getRuleType())) {
        ruleTemplateService.deleteCustomTemplate(ruleInDb.getTemplate());
        Template savedTemplate = ruleTemplateDao.saveTemplate(template);
        Set<TemplateStatisticsInputMeta> templateStatisticsInputMetas = new HashSet<>();
        Set<TemplateOutputMeta> templateOutputMetaSet = new HashSet<>();
        if (rule.getOutputName() != null && rule.getFunctionType() != null && rule.getFunctionContent() != null) {
            templateStatisticsInputMetas = templateStatisticsInputMetaService.getAndSaveTemplateStatisticsInputMeta(rule.getOutputName(), rule.getFunctionType(), rule.getFunctionContent(), savedTemplate.getSaveMidTable(), savedTemplate);
            templateOutputMetaSet = templateOutputMetaService.getAndSaveTemplateOutputMeta(rule.getOutputName(), rule.getFunctionType(), savedTemplate.getSaveMidTable(), savedTemplate);
        } else {
            List<RuleMetric> ruleMetrics = alarmConfigs.stream().map(AlarmConfig::getRuleMetric).collect(Collectors.toList());
            for (RuleMetric ruleMetric : ruleMetrics) {
                templateStatisticsInputMetas.addAll(templateStatisticsInputMetaService.getAndSaveTemplateStatisticsInputMeta(ruleMetric.getName(), FunctionTypeEnum.MAX_FUNCTION.getCode(), ruleMetric.getName(), savedTemplate.getSaveMidTable(), savedTemplate));
                templateOutputMetaSet.addAll(templateOutputMetaService.getAndSaveTemplateOutputMeta(ruleMetric.getName(), FunctionTypeEnum.MAX_FUNCTION.getCode(), savedTemplate.getSaveMidTable(), savedTemplate));
            }
        }
        savedTemplate.setStatisticAction(templateStatisticsInputMetas);
        savedTemplate.setTemplateOutputMetas(templateOutputMetaSet);
        ruleInDb.setTemplate(savedTemplate);
    } else {
        synchroRuleTemplate(ruleNodeRequest, ruleInDb, template, ruleDataSources);
    }
    alarmConfigService.deleteByRule(ruleInDb);
    LOGGER.info("Succeed to delete all alarm_config. rule_id: {}", ruleInDb.getId());
    Integer ruleType = ruleInDb.getRuleType();
    if (RuleTypeEnum.SINGLE_TEMPLATE_RULE.getCode().equals(ruleType) || RuleTypeEnum.MULTI_TEMPLATE_RULE.getCode().equals(ruleType)) {
        ruleVariableService.deleteByRule(ruleInDb);
        LOGGER.info("Succeed to delete all rule_variable. rule_id: {}", ruleInDb.getId());
        if (RuleTypeEnum.MULTI_TEMPLATE_RULE.getCode().equals(ruleType)) {
            ruleDataSourceMappingService.deleteByRule(ruleInDb);
            LOGGER.info("Succeed to delete all rule_dataSource_mapping. rule_id: {}", ruleInDb.getId());
        }
    }
    ruleDataSourceService.deleteByRule(ruleInDb);
    LOGGER.info("Succeed to delete all rule_dataSources. rule_id: {}", ruleInDb.getId());
    Rule updateRule = ruleDao.saveRule(ruleInDb);
    saveRuleInfo(updateRule, template, updateRule.getProject(), alarmConfigs, ruleVariables, ruleDataSources, ruleDataSourceMappings);
    updateImportedChildRule(updateRule, ruleNodeRequest);
}
Also used : RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) TemplateOutputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateOutputMeta) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) TemplateStatisticsInputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateStatisticsInputMeta) HashSet(java.util.HashSet)

Example 9 with TemplateStatisticsInputMeta

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

the class RuleNodeServiceImpl method synchroRuleTemplate.

public void synchroRuleTemplate(RuleNodeRequest ruleNodeRequest, Rule rule, Template template, Set<RuleDataSource> ruleDataSources) throws IOException {
    Template templateInDb = ruleTemplateDao.findById(template.getId());
    Template templateNameInDb = null;
    if (StringUtils.isNotBlank(template.getImportExportName())) {
        templateNameInDb = ruleTemplateDao.findByImportExportName(template.getImportExportName());
    }
    if (templateInDb == null && templateNameInDb == null) {
        LOGGER.info("Start to synchronize the rule template. Template: {}", template);
        ObjectMapper objectMapper = new ObjectMapper();
        // Template meta info.
        Set<TemplateOutputMeta> templateOutputMetaSet = objectMapper.readValue(ruleNodeRequest.getTemplateTemplateOutputMetaObject(), new TypeReference<Set<TemplateOutputMeta>>() {
        });
        Set<TemplateMidTableInputMeta> templateMidTableInputMetaSet = objectMapper.readValue(ruleNodeRequest.getTemplateTemplateMidTableInputMetaObject(), new TypeReference<Set<TemplateMidTableInputMeta>>() {
        });
        Set<TemplateStatisticsInputMeta> templateStatisticsInputMetaSet = objectMapper.readValue(ruleNodeRequest.getTemplateTemplateStatisticsInputMetaObject(), new TypeReference<Set<TemplateStatisticsInputMeta>>() {
        });
        Template savedTemplate = ruleTemplateDao.saveTemplate(template);
        Set<TemplateOutputMeta> templateOutputMetas = new HashSet<>();
        for (TemplateOutputMeta outputMeta : templateOutputMetaSet) {
            outputMeta.setTemplate(savedTemplate);
            templateOutputMetas.add(templateOutputMetaDao.saveTemplateOutputMeta(outputMeta));
        }
        savedTemplate.setTemplateOutputMetas(templateOutputMetas);
        LOGGER.info("Success to save template output meta. TemplateOutputMetas: {}", savedTemplate.getTemplateOutputMetas());
        List<TemplateMidTableInputMeta> templateMidTableInputMetas = new ArrayList<>();
        for (TemplateMidTableInputMeta templateMidTableInputMeta : templateMidTableInputMetaSet) {
            templateMidTableInputMeta.setTemplate(savedTemplate);
            templateMidTableInputMetas.add(templateMidTableInputMeta);
        }
        templateMidTableInputMetas.sort(Comparator.comparing(TemplateMidTableInputMeta::getId));
        savedTemplate.setTemplateMidTableInputMetas(templateMidTableInputMetaService.saveAll(templateMidTableInputMetas));
        LOGGER.info("Success to save template mid_table input meta. TemplateMidTableInputMetas: {}", savedTemplate.getTemplateMidTableInputMetas());
        List<TemplateStatisticsInputMeta> templateStatisticsInputMetas = new ArrayList<>();
        for (TemplateStatisticsInputMeta templateStatisticsInputMeta : templateStatisticsInputMetaSet) {
            templateStatisticsInputMeta.setTemplate(savedTemplate);
            templateStatisticsInputMetas.add(templateStatisticsInputMeta);
        }
        savedTemplate.setStatisticAction(templateStatisticsInputMetaService.saveAll(templateStatisticsInputMetas));
        LOGGER.info("Success to save template statistics input meta. templateStatisticsInputMetas: {}", savedTemplate.getStatisticAction());
        Set<Integer> templateDateTypes = ruleDataSources.stream().filter(currRuleDataSource -> currRuleDataSource.getDatasourceType() != null).map(RuleDataSource::getDatasourceType).collect(Collectors.toSet());
        for (Integer templateDataType : templateDateTypes) {
            TemplateDataSourceType templateDataSourceType = new TemplateDataSourceType(templateDataType, savedTemplate);
            templateDataSourceTypeDao.save(templateDataSourceType);
        }
        LOGGER.info("Success to save template data types. Template data types: {}", Arrays.toString(templateDateTypes.toArray()));
        LOGGER.info("Finished to synchronize the rule template. Template: {}", savedTemplate);
        rule.setTemplate(savedTemplate);
    } else if (templateInDb != null) {
        rule.setTemplate(templateInDb);
    } else if (templateNameInDb != null) {
        rule.setTemplate(templateNameInDb);
    } else {
        rule.setTemplate(template);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ArrayList(java.util.ArrayList) TemplateMidTableInputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateMidTableInputMeta) TemplateOutputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateOutputMeta) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) TemplateStatisticsInputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateStatisticsInputMeta) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) TemplateDataSourceType(com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType) HashSet(java.util.HashSet)

Example 10 with TemplateStatisticsInputMeta

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

the class SqlTemplateConverter method generateSparkSqlByTask.

/**
 * Convert task into scala code
 * @param rule
 * @param date
 * @param applicationId
 * @param midTableName
 * @param createTime
 * @param partition
 * @param execParams
 * @param count
 * @param runDate
 * @param dataSourceMysqlConnect
 * @return
 * @throws ConvertException
 * @throws RuleVariableNotSupportException
 * @throws RuleVariableNotFoundException
 */
private List<String> generateSparkSqlByTask(Rule rule, Date date, String applicationId, String midTableName, String createTime, StringBuffer partition, Map<String, String> execParams, int count, String runDate, Map<Long, Map> dataSourceMysqlConnect) throws ConvertException, RuleVariableNotSupportException, RuleVariableNotFoundException, UnExpectedRequestException {
    List<String> sqlList = new ArrayList<>();
    // Collect rule metric and build in save sentence sql.
    List<RuleMetric> ruleMetrics = rule.getAlarmConfigs().stream().map(AlarmConfig::getRuleMetric).distinct().collect(Collectors.toList());
    Map<String, Long> ruleMetricMap = new HashMap<>(ruleMetrics.size());
    if (CollectionUtils.isNotEmpty(ruleMetrics)) {
        LOGGER.info("Start to get rule metric for task result save. Rule metrics: {}", Arrays.toString(ruleMetrics.toArray()));
        for (RuleMetric ruleMetric : ruleMetrics) {
            if (ruleMetric != null) {
                ruleMetricMap.put(ruleMetric.getName(), ruleMetric.getId());
            }
        }
        LOGGER.info("Finish to get rule metric for task result save.");
    }
    // Get SQL from template after remove '\n'
    String templateMidTableAction = rule.getTemplate().getMidTableAction().replace("\n", " ");
    Map<String, String> filters = new HashMap<>(2);
    if (CUSTOM_RULE.intValue() == rule.getRuleType()) {
        templateMidTableAction = customMidTableActionUpdate(rule, templateMidTableAction, date, execParams, partition, ruleMetricMap);
    } else if (MUL_SOURCE_RULE.intValue() == rule.getRuleType()) {
        templateMidTableAction = multiMidTableActionUpdate(rule, templateMidTableAction, date, filters);
    }
    // Get input meta from template
    List<RuleVariable> inputMetaRuleVariables = rule.getRuleVariables().stream().filter(ruleVariable -> ruleVariable.getInputActionStep().equals(InputActionStepEnum.TEMPLATE_INPUT_META.getCode())).collect(Collectors.toList());
    // If partition is not specified, replace with filter in rule configuration.
    if (StringUtils.isBlank(partition.toString())) {
        templateMidTableAction = fillPartitionWithRuleConfiguration(partition, rule, templateMidTableAction, inputMetaRuleVariables);
    }
    // Get dbs and tables
    Map<String, String> dbTableMap = new HashMap<>(4);
    // Get mappings
    StringBuffer mappings = new StringBuffer();
    StringBuffer realFilter = new StringBuffer();
    // Get SQL From template and replace all replaceholders
    String midTableAction = replaceVariable(templateMidTableAction, inputMetaRuleVariables, partition.toString(), realFilter, dbTableMap, mappings, date);
    Set<TemplateStatisticsInputMeta> templateStatisticsAction = rule.getTemplate().getStatisticAction();
    Map sourceConnect = new HashMap(8);
    Map targetConnect = new HashMap(8);
    if (dataSourceMysqlConnect != null && dataSourceMysqlConnect.size() > 0) {
        for (RuleDataSource ruleDataSource : rule.getRuleDataSources()) {
            Map connectParams = dataSourceMysqlConnect.get(ruleDataSource.getId());
            if (connectParams == null) {
                continue;
            }
            if (ruleDataSource.getDatasourceIndex() != null && ruleDataSource.getDatasourceIndex().equals(0)) {
                // If mysql sec, decrypt password and user name.
                sourceConnect = dataSourceMysqlConnect.get(ruleDataSource.getId());
            }
            if (ruleDataSource.getDatasourceIndex() != null && ruleDataSource.getDatasourceIndex().equals(1)) {
                // If mysql sec, decrypt password and user name.
                targetConnect = dataSourceMysqlConnect.get(ruleDataSource.getId());
            }
        }
    }
    sqlList.add("val UUID = java.util.UUID.randomUUID.toString");
    // 跨表规则
    if (RuleTemplateTypeEnum.MULTI_SOURCE_TEMPLATE.getCode().equals(rule.getTemplate().getTemplateType()) && dbTableMap.size() > 0) {
        // Import sql function.
        sqlList.addAll(getImportSql());
        // Generate UUID.
        // Transform original table.
        Set<String> columns = new HashSet<>();
        if (rule.getTemplate().getId().longValue() == MUL_SOURCE_ACCURACY_TEMPLATE_ID.longValue()) {
            // Get accuracy columns.
            columns = rule.getRuleDataSourceMappings().stream().map(RuleDataSourceMapping::getLeftColumnNames).map(column -> column.replace("tmp1.", "").replace("tmp2.", "")).collect(Collectors.toSet());
        }
        if (rule.getTemplate().getId().longValue() == MUL_SOURCE_COMMON_TEMPLATE_ID.longValue()) {
            sqlList.addAll(getCommonTransformSql(dbTableMap, mappings, count, partition.toString(), filters, sourceConnect, targetConnect));
        } else {
            sqlList.addAll(getSpecialTransformSql(dbTableMap, count, partition.toString(), filters, Strings.join(columns, ','), sourceConnect, targetConnect));
            if (optimizationConfig.getLightweightQuery()) {
                count += 3;
            }
        }
        sqlList.addAll(getSaveMidTableSentenceSettings());
        sqlList.addAll(getSaveMidTableSentence(midTableName, count, runDate));
    } else {
        // Generate select statement and save into hive database
        RuleDataSource ruleDataSource = rule.getRuleDataSources().stream().filter(dataSource -> dataSource.getDatasourceIndex() == null).iterator().next();
        Map connParams = dataSourceMysqlConnect.get(ruleDataSource.getId());
        if (connParams != null) {
            connParams = dataSourceMysqlConnect.get(ruleDataSource.getId());
        }
        sqlList.addAll(generateSparkSqlAndSaveSentence(midTableAction, midTableName, rule.getTemplate(), count, connParams, runDate));
        count++;
    }
    // Generate statistics statement, and save into mysql
    List<RuleVariable> statisticsRuleVariables = rule.getRuleVariables().stream().filter(ruleVariable -> ruleVariable.getInputActionStep().equals(InputActionStepEnum.STATISTICS_ARG.getCode())).collect(Collectors.toList());
    sqlList.addAll(saveStatisticAndSaveMySqlSentence(rule.getId(), ruleMetricMap, templateStatisticsAction, applicationId, statisticsRuleVariables, createTime, count, runDate));
    return sqlList;
}
Also used : RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) Arrays(java.util.Arrays) RuleTaskDetail(com.webank.wedatasphere.qualitis.bean.RuleTaskDetail) SpecCharEnum(com.webank.wedatasphere.qualitis.constant.SpecCharEnum) Date(java.util.Date) RuleVariable(com.webank.wedatasphere.qualitis.rule.entity.RuleVariable) LoggerFactory(org.slf4j.LoggerFactory) SimpleDateFormat(java.text.SimpleDateFormat) Autowired(org.springframework.beans.factory.annotation.Autowired) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) RuleDataSource(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource) DateExprReplaceUtil(com.webank.wedatasphere.qualitis.util.DateExprReplaceUtil) HashSet(java.util.HashSet) AlarmConfig(com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig) Matcher(java.util.regex.Matcher) CollectionUtils(org.apache.commons.collections.CollectionUtils) DataQualityTaskException(com.webank.wedatasphere.qualitis.exception.DataQualityTaskException) Map(java.util.Map) ConvertException(com.webank.wedatasphere.qualitis.exception.ConvertException) RuleVariableNotSupportException(com.webank.wedatasphere.qualitis.exception.RuleVariableNotSupportException) RuleVariableNotFoundException(com.webank.wedatasphere.qualitis.exception.RuleVariableNotFoundException) TemplateMidTableInputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateMidTableInputMeta) Logger(org.slf4j.Logger) InputActionStepEnum(com.webank.wedatasphere.qualitis.rule.constant.InputActionStepEnum) AbstractTranslator(com.webank.wedatasphere.qualitis.translator.AbstractTranslator) RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) RuleDataSourceMapping(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSourceMapping) RuleConstraintEnum(com.webank.wedatasphere.qualitis.metadata.constant.RuleConstraintEnum) DataQualityJob(com.webank.wedatasphere.qualitis.bean.DataQualityJob) TemplateInputTypeEnum(com.webank.wedatasphere.qualitis.rule.constant.TemplateInputTypeEnum) List(java.util.List) Component(org.springframework.stereotype.Component) RuleTemplateTypeEnum(com.webank.wedatasphere.qualitis.rule.constant.RuleTemplateTypeEnum) Strings(org.apache.logging.log4j.util.Strings) DataQualityTask(com.webank.wedatasphere.qualitis.bean.DataQualityTask) TemplateStatisticsInputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateStatisticsInputMeta) Pattern(java.util.regex.Pattern) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) OptimizationConfig(com.webank.wedatasphere.qualitis.config.OptimizationConfig) RuleDataSource(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TemplateStatisticsInputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateStatisticsInputMeta) HashMap(java.util.HashMap) Map(java.util.Map) AlarmConfig(com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig) RuleVariable(com.webank.wedatasphere.qualitis.rule.entity.RuleVariable) HashSet(java.util.HashSet)

Aggregations

TemplateStatisticsInputMeta (com.webank.wedatasphere.qualitis.rule.entity.TemplateStatisticsInputMeta)11 TemplateOutputMeta (com.webank.wedatasphere.qualitis.rule.entity.TemplateOutputMeta)7 HashSet (java.util.HashSet)7 Template (com.webank.wedatasphere.qualitis.rule.entity.Template)6 ArrayList (java.util.ArrayList)6 TemplateMidTableInputMeta (com.webank.wedatasphere.qualitis.rule.entity.TemplateMidTableInputMeta)5 RuleMetric (com.webank.wedatasphere.qualitis.entity.RuleMetric)4 AlarmConfig (com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)3 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)3 RuleDataSource (com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource)3 TemplateDataSourceType (com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType)3 RuleVariableNotSupportException (com.webank.wedatasphere.qualitis.exception.RuleVariableNotSupportException)2 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)2 RuleDataSourceMapping (com.webank.wedatasphere.qualitis.rule.entity.RuleDataSourceMapping)2 RuleVariable (com.webank.wedatasphere.qualitis.rule.entity.RuleVariable)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 List (java.util.List)2 Set (java.util.Set)2 Transactional (org.springframework.transaction.annotation.Transactional)2