Search in sources :

Example 1 with TemplateStatisticsInputMeta

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

the class RuleNodeServiceImpl method saveRuleInfo.

public void saveRuleInfo(Rule savedRule, Template template, Project projectInDb, Set<AlarmConfig> alarmConfigs, Set<RuleVariable> ruleVariables, Set<RuleDataSource> ruleDataSources, Set<RuleDataSourceMapping> ruleDataSourceMappings) {
    LOGGER.info("Start to save rule Info.");
    List<AlarmConfig> alarmConfigList = new ArrayList<>();
    for (AlarmConfig alarmConfig : alarmConfigs) {
        alarmConfig.setRule(savedRule);
        ruleMetricSycn(alarmConfig);
        alarmConfig.setTemplateOutputMeta(savedRule.getTemplate().getTemplateOutputMetas().iterator().next());
        alarmConfigList.add(alarmConfig);
    }
    List<RuleVariable> ruleVariablesList = new ArrayList<>();
    TemplateStatisticsInputMeta templateStatisticsInputMeta = savedRule.getTemplate().getStatisticAction().iterator().next();
    for (RuleVariable ruleVariable : ruleVariables) {
        ruleVariable.setRule(savedRule);
        ruleVariable.setTemplateStatisticsInputMeta(templateStatisticsInputMeta);
        ruleVariablesList.add(ruleVariable);
    }
    if (template.getId().intValue() != savedRule.getTemplate().getId().intValue()) {
        ruleVariablesList.sort(Comparator.comparing(RuleVariable::getId));
        List<TemplateMidTableInputMeta> templateMidTableInputMetaList = new ArrayList<>();
        if (savedRule.getTemplate().getTemplateMidTableInputMetas() != null) {
            templateMidTableInputMetaList.addAll(savedRule.getTemplate().getTemplateMidTableInputMetas());
            templateMidTableInputMetaList.sort(Comparator.comparing(TemplateMidTableInputMeta::getId));
            Iterator iterator = templateMidTableInputMetaList.iterator();
            for (RuleVariable ruleVariable : ruleVariablesList) {
                ruleVariable.setTemplateMidTableInputMeta((TemplateMidTableInputMeta) iterator.next());
            }
        }
    }
    List<RuleDataSource> ruleDataSourceList = new ArrayList<>();
    for (RuleDataSource ruleDataSource : ruleDataSources) {
        ruleDataSource.setProjectId(projectInDb.getId());
        ruleDataSource.setRule(savedRule);
        ruleDataSourceList.add(ruleDataSource);
    }
    if (RuleTypeEnum.CUSTOM_RULE.getCode().equals(savedRule.getRuleType())) {
        List<AlarmConfig> customAlarmConfigs = new ArrayList<>();
        for (AlarmConfig alarmConfig : alarmConfigList) {
            TemplateOutputMeta templateOutputMetaInDb = savedRule.getTemplate().getTemplateOutputMetas().iterator().next();
            AlarmConfig customAlarmConfig = new AlarmConfig();
            customAlarmConfig.setRule(savedRule);
            customAlarmConfig.setRuleMetric(alarmConfig.getRuleMetric());
            customAlarmConfig.setThreshold(alarmConfig.getThreshold());
            customAlarmConfig.setTemplateOutputMeta(templateOutputMetaInDb);
            customAlarmConfig.setCheckTemplate(alarmConfig.getCheckTemplate());
            Integer checkTemplateCode = alarmConfig.getCheckTemplate();
            if (alarmConfig.getCheckTemplate().equals(CheckTemplateEnum.FIXED_VALUE.getCode()) || checkTemplateCode.equals(CheckTemplateEnum.FULL_YEAR_RING_GROWTH.getCode()) || checkTemplateCode.equals(CheckTemplateEnum.HALF_YEAR_GROWTH.getCode()) || checkTemplateCode.equals(CheckTemplateEnum.SEASON_RING_GROWTH.getCode()) || checkTemplateCode.equals(CheckTemplateEnum.MONTH_RING_GROWTH.getCode()) || checkTemplateCode.equals(CheckTemplateEnum.WEEK_RING_GROWTH.getCode()) || checkTemplateCode.equals(CheckTemplateEnum.DAY_RING_GROWTH.getCode()) || checkTemplateCode.equals(CheckTemplateEnum.HOUR_RING_GROWTH.getCode()) || checkTemplateCode.equals(CheckTemplateEnum.YEAR_ON_YEAR.getCode())) {
                customAlarmConfig.setCompareType(alarmConfig.getCompareType());
            }
            customAlarmConfigs.add(customAlarmConfig);
        }
        savedRule.setAlarmConfigs(new HashSet<>(alarmConfigDao.saveAllAlarmConfig(customAlarmConfigs)));
        savedRule.setRuleDataSources(new HashSet<>(ruleDataSourceDao.saveAllRuleDataSource(ruleDataSourceList)));
    } else {
        savedRule.setAlarmConfigs(new HashSet<>(alarmConfigDao.saveAllAlarmConfig(alarmConfigList)));
        savedRule.setRuleVariables(new HashSet<>(ruleVariableDao.saveAllRuleVariable(ruleVariablesList)));
        savedRule.setRuleDataSources(new HashSet<>(ruleDataSourceDao.saveAllRuleDataSource(ruleDataSourceList)));
        for (RuleDataSourceMapping ruleDataSourceMapping : ruleDataSourceMappings) {
            ruleDataSourceMapping.setRule(savedRule);
            ruleDataSourceMappingDao.saveRuleDataSourceMapping(ruleDataSourceMapping);
        }
    }
}
Also used : RuleDataSourceMapping(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSourceMapping) RuleDataSource(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource) ArrayList(java.util.ArrayList) TemplateMidTableInputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateMidTableInputMeta) TemplateOutputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateOutputMeta) TemplateStatisticsInputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateStatisticsInputMeta) Iterator(java.util.Iterator) AlarmConfig(com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig) RuleVariable(com.webank.wedatasphere.qualitis.rule.entity.RuleVariable)

Example 2 with TemplateStatisticsInputMeta

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

the class RuleTemplateServiceImpl method addCustomTemplate.

@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public Template addCustomTemplate(AddCustomRuleRequest request) {
    Template newTemplate = new Template();
    newTemplate.setName(request.getProjectId() + "_" + request.getRuleName() + "_template");
    newTemplate.setSaveMidTable(request.getSaveMidTable());
    String sqlCheckArea = request.getSqlCheckArea();
    if (StringUtils.isNotBlank(sqlCheckArea)) {
        newTemplate.setMidTableAction(sqlCheckArea);
        newTemplate.setTemplateType(RuleTemplateTypeEnum.CUSTOM.getCode());
        newTemplate.setActionType(TemplateActionTypeEnum.SQL.getCode());
        Template savedTemplate = ruleTemplateDao.saveTemplate(newTemplate);
        LOGGER.info("Succeed to save custom template, template_id: {}", savedTemplate.getId());
        // Generate statistics input meta by rule metric
        Set<String> ruleMetricCodeSet = request.getAlarmVariable().stream().map(CustomAlarmConfigRequest::getRuleMetricEnCode).collect(Collectors.toSet());
        Set<TemplateStatisticsInputMeta> templateStatisticsInputMetas = new HashSet<>(ruleMetricCodeSet.size());
        Set<TemplateOutputMeta> templateOutputMetas = new HashSet<>(ruleMetricCodeSet.size());
        for (String enCode : ruleMetricCodeSet) {
            // xx_xx_xx_encode, index is 3.
            RuleMetric ruleMetricInDb = ruleMetricDao.findByEnCode(enCode);
            templateStatisticsInputMetas.addAll(templateStatisticsInputMetaService.getAndSaveTemplateStatisticsInputMeta(ruleMetricInDb.getName(), FunctionTypeEnum.MAX_FUNCTION.getCode(), ruleMetricInDb.getName(), true, savedTemplate));
            templateOutputMetas.addAll(templateOutputMetaService.getAndSaveTemplateOutputMeta(ruleMetricInDb.getName(), FunctionTypeEnum.MAX_FUNCTION.getCode(), true, savedTemplate));
        }
        savedTemplate.setStatisticAction(templateStatisticsInputMetas);
        savedTemplate.setTemplateOutputMetas(templateOutputMetas);
        savedTemplate.setTemplateOutputMetas(templateOutputMetas);
        return savedTemplate;
    } else {
        newTemplate.setMidTableAction(getMidTableAction(request));
        newTemplate.setTemplateType(RuleTemplateTypeEnum.CUSTOM.getCode());
        newTemplate.setActionType(TemplateActionTypeEnum.SQL.getCode());
        Template savedTemplate = ruleTemplateDao.saveTemplate(newTemplate);
        LOGGER.info("Succeed to save custom template, template_id: {}", savedTemplate.getId());
        Set<TemplateStatisticsInputMeta> templateStatisticsInputMetas = templateStatisticsInputMetaService.getAndSaveTemplateStatisticsInputMeta(request.getOutputName(), request.getFunctionType(), request.getFunctionContent(), request.getSaveMidTable(), savedTemplate);
        savedTemplate.setStatisticAction(templateStatisticsInputMetas);
        Set<TemplateOutputMeta> templateOutputMetas = templateOutputMetaService.getAndSaveTemplateOutputMeta(request.getOutputName(), request.getFunctionType(), request.getSaveMidTable(), savedTemplate);
        savedTemplate.setTemplateOutputMetas(templateOutputMetas);
        return savedTemplate;
    }
}
Also used : RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) 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) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with TemplateStatisticsInputMeta

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

the class RuleTemplateServiceImpl method getModifyRuleTemplateDetail.

@Override
public RuleTemplateResponse getModifyRuleTemplateDetail(Long templateId) throws UnExpectedRequestException {
    // Check template existence
    Template templateInDb = checkRuleTemplate(templateId);
    RuleTemplateResponse response = new RuleTemplateResponse(templateInDb);
    response.setClusterNum(templateInDb.getClusterNum());
    response.setDbNum(templateInDb.getDbNum());
    response.setTableNum(templateInDb.getTableNum());
    response.setFieldNum(templateInDb.getFieldNum());
    List<TemplateDataSourceType> templateDataSourceTypes = templateDataSourceTypeDao.findByTemplate(templateInDb);
    response.setDatasourceType(templateDataSourceTypes.stream().map(TemplateDataSourceType::getDataSourceTypeId).collect(Collectors.toList()));
    response.setActionType(templateInDb.getActionType());
    response.setMidTableAction(templateInDb.getMidTableAction());
    response.setSaveMidTable(templateInDb.getSaveMidTable());
    List<TemplateOutputMetaResponse> outputMetaResponses = new ArrayList<>(1);
    List<TemplateMidTableInputMetaResponse> midTableInputMetaResponses = new ArrayList<>(2);
    List<TemplateStatisticsInputMetaResponse> statisticsInputMetaResponses = new ArrayList<>(1);
    for (TemplateOutputMeta templateOutputMeta : templateInDb.getTemplateOutputMetas()) {
        TemplateOutputMetaResponse templateOutputMetaResponse = new TemplateOutputMetaResponse();
        templateOutputMetaResponse.setOutputName(templateOutputMeta.getOutputName());
        outputMetaResponses.add(templateOutputMetaResponse);
    }
    response.setTemplateOutputMetaResponses(outputMetaResponses);
    for (TemplateMidTableInputMeta templateMidTableInputMeta : templateInDb.getTemplateMidTableInputMetas()) {
        TemplateMidTableInputMetaResponse templateMidTableInputMetaResponse = new TemplateMidTableInputMetaResponse();
        templateMidTableInputMetaResponse.setName(templateMidTableInputMeta.getName());
        templateMidTableInputMetaResponse.setPlaceholder(templateMidTableInputMeta.getPlaceholder());
        templateMidTableInputMetaResponse.setPlaceholderDescription(templateMidTableInputMeta.getPlaceholderDescription());
        templateMidTableInputMetaResponse.setInputType(templateMidTableInputMeta.getInputType());
        midTableInputMetaResponses.add(templateMidTableInputMetaResponse);
    }
    response.setTemplateMidTableInputMetaResponses(midTableInputMetaResponses);
    for (TemplateStatisticsInputMeta templateStatisticsInputMeta : templateInDb.getStatisticAction()) {
        TemplateStatisticsInputMetaResponse templateStatisticsInputMetaResponse = new TemplateStatisticsInputMetaResponse();
        templateStatisticsInputMetaResponse.setName(templateStatisticsInputMeta.getName());
        templateStatisticsInputMetaResponse.setFuncName(templateStatisticsInputMeta.getFuncName());
        templateStatisticsInputMetaResponse.setValue(templateStatisticsInputMeta.getValue());
        templateStatisticsInputMetaResponse.setValueType(templateStatisticsInputMeta.getValueType());
        statisticsInputMetaResponses.add(templateStatisticsInputMetaResponse);
    }
    response.setTemplateStatisticsInputMetaResponses(statisticsInputMetaResponses);
    return response;
}
Also used : ArrayList(java.util.ArrayList) TemplateMidTableInputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateMidTableInputMeta) TemplateStatisticsInputMetaResponse(com.webank.wedatasphere.qualitis.rule.response.TemplateStatisticsInputMetaResponse) TemplateOutputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateOutputMeta) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) TemplateStatisticsInputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateStatisticsInputMeta) RuleTemplateResponse(com.webank.wedatasphere.qualitis.rule.response.RuleTemplateResponse) TemplateOutputMetaResponse(com.webank.wedatasphere.qualitis.rule.response.TemplateOutputMetaResponse) TemplateMidTableInputMetaResponse(com.webank.wedatasphere.qualitis.rule.response.TemplateMidTableInputMetaResponse) TemplateDataSourceType(com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType)

Example 4 with TemplateStatisticsInputMeta

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

the class RuleTemplateServiceImpl method createAndSaveTemplateInfo.

private void createAndSaveTemplateInfo(Template savedTemplate, AddRuleTemplateRequest request) {
    // Save template output meta.
    Set<TemplateOutputMeta> templateOutputMetas = new HashSet<>();
    for (TemplateOutputMetaRequest templateOutputMetaRequest : request.getTemplateOutputMetaRequests()) {
        templateOutputMetas.addAll(templateOutputMetaService.getAndSaveTemplateOutputMeta(templateOutputMetaRequest.getOutputName(), FunctionTypeEnum.getFunctionTypeByName(templateOutputMetaRequest.getFieldName()), request.getSaveMidTable(), savedTemplate));
    }
    savedTemplate.setTemplateOutputMetas(templateOutputMetas);
    LOGGER.info("Success to save template output meta. TemplateOutputMetas: {}", savedTemplate.getTemplateOutputMetas());
    // Save template mid_table input meta
    List<TemplateMidTableInputMeta> templateMidTableInputMetas = new ArrayList<>();
    for (TemplateMidTableInputMetaRequest templateMidTableInputMetaRequest : request.getTemplateMidTableInputMetaRequests()) {
        TemplateMidTableInputMeta templateMidTableInputMeta = new TemplateMidTableInputMeta();
        templateMidTableInputMeta.setName(templateMidTableInputMetaRequest.getName());
        templateMidTableInputMeta.setFieldType(templateMidTableInputMetaRequest.getFieldType());
        templateMidTableInputMeta.setInputType(templateMidTableInputMetaRequest.getInputType());
        templateMidTableInputMeta.setPlaceholder(templateMidTableInputMetaRequest.getPlaceholder());
        templateMidTableInputMeta.setPlaceholderDescription(templateMidTableInputMetaRequest.getPlaceholderDescription());
        templateMidTableInputMeta.setRegexpType(templateMidTableInputMetaRequest.getRegexpType());
        templateMidTableInputMeta.setReplaceByRequest(templateMidTableInputMetaRequest.getReplaceByRequest());
        templateMidTableInputMeta.setTemplate(savedTemplate);
        templateMidTableInputMetas.add(templateMidTableInputMeta);
    }
    savedTemplate.setTemplateMidTableInputMetas(templateMidTableInputMetaService.saveAll(templateMidTableInputMetas));
    LOGGER.info("Success to save template mid_table input meta. TemplateMidTableInputMetas: {}", savedTemplate.getTemplateMidTableInputMetas());
    // Save template statistics input meta
    List<TemplateStatisticsInputMeta> templateStatisticsInputMetas = new ArrayList<>();
    for (TemplateStatisticsInputMetaRequest templateStatisticsInputMetaRequest : request.getTemplateStatisticsInputMetaRequests()) {
        TemplateStatisticsInputMeta templateStatisticsInputMeta = new TemplateStatisticsInputMeta();
        templateStatisticsInputMeta.setName(templateStatisticsInputMetaRequest.getName());
        templateStatisticsInputMeta.setFuncName(templateStatisticsInputMetaRequest.getFuncName());
        templateStatisticsInputMeta.setResultType(templateStatisticsInputMetaRequest.getResultType());
        templateStatisticsInputMeta.setValue(templateStatisticsInputMetaRequest.getValue());
        templateStatisticsInputMeta.setValueType(templateStatisticsInputMetaRequest.getValueType());
        templateStatisticsInputMeta.setTemplate(savedTemplate);
        templateStatisticsInputMetas.add(templateStatisticsInputMeta);
    }
    savedTemplate.setStatisticAction(templateStatisticsInputMetaService.saveAll(templateStatisticsInputMetas));
    LOGGER.info("Success to save template statistics input meta. templateStatisticsInputMetas: {}", savedTemplate.getStatisticAction());
}
Also used : TemplateOutputMetaRequest(com.webank.wedatasphere.qualitis.rule.request.TemplateOutputMetaRequest) TemplateStatisticsInputMetaRequest(com.webank.wedatasphere.qualitis.rule.request.TemplateStatisticsInputMetaRequest) TemplateMidTableInputMetaRequest(com.webank.wedatasphere.qualitis.rule.request.TemplateMidTableInputMetaRequest) ArrayList(java.util.ArrayList) TemplateMidTableInputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateMidTableInputMeta) TemplateOutputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateOutputMeta) HashSet(java.util.HashSet) TemplateStatisticsInputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateStatisticsInputMeta)

Example 5 with TemplateStatisticsInputMeta

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

the class TemplateStatisticsInputMetaServiceImpl method getAndSaveTemplateStatisticsInputMeta.

@Override
@Transactional(rollbackFor = RuntimeException.class)
public Set<TemplateStatisticsInputMeta> getAndSaveTemplateStatisticsInputMeta(String outputName, Integer functionType, String functionContent, Boolean saveMidTable, Template template) {
    Set<TemplateStatisticsInputMeta> templateStatisticsInputMetas = new HashSet<>();
    TemplateStatisticsInputMeta templateStatisticsInputMeta = new TemplateStatisticsInputMeta();
    templateStatisticsInputMeta.setFuncName(FunctionTypeEnum.getByCode(functionType).getFunction());
    templateStatisticsInputMeta.setValue(functionContent.replace("-", "_"));
    templateStatisticsInputMeta.setValueType(StatisticsValueTypeEnum.FIXED_VALUE.getCode());
    templateStatisticsInputMeta.setName(outputName.replace("-", "_"));
    templateStatisticsInputMeta.setResultType("Long");
    templateStatisticsInputMeta.setTemplate(template);
    try {
        TemplateStatisticsInputMeta savedTemplateStatisticsInputMeta = repository.save(templateStatisticsInputMeta);
        templateStatisticsInputMetas.add(savedTemplateStatisticsInputMeta);
        LOGGER.info("Succeed to save template statistics input_meta, input_meta_id: {}, name: {}", savedTemplateStatisticsInputMeta.getId(), savedTemplateStatisticsInputMeta.getName());
    } catch (Exception e) {
        LOGGER.error("Save template statistics input meta exception.", e);
    }
    return templateStatisticsInputMetas;
}
Also used : UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) TemplateStatisticsInputMeta(com.webank.wedatasphere.qualitis.rule.entity.TemplateStatisticsInputMeta) HashSet(java.util.HashSet) Transactional(org.springframework.transaction.annotation.Transactional)

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