Search in sources :

Example 1 with Template

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

Example 2 with Template

use of com.webank.wedatasphere.qualitis.rule.entity.Template 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 3 with Template

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

the class RuleNodeServiceImpl method importRule.

@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class }, propagation = Propagation.REQUIRED)
public void importRule(RuleNodeRequest ruleNodeRequest, Project projectInDb, RuleGroup ruleGroup, ObjectMapper objectMapper) throws UnExpectedRequestException, IOException {
    // Rule, Template, RuleGroup.
    Rule rule = objectMapper.readValue(ruleNodeRequest.getRuleObject(), Rule.class);
    Template template = objectMapper.readValue(ruleNodeRequest.getTemplateObject(), Template.class);
    // RuleDataSource.
    Set<RuleDataSource> ruleDataSources = objectMapper.readValue(ruleNodeRequest.getRuleDataSourcesObject(), new TypeReference<Set<RuleDataSource>>() {
    });
    Set<RuleDataSourceMapping> ruleDataSourceMappings = objectMapper.readValue(ruleNodeRequest.getRuleDataSourceMappingsObject(), new TypeReference<Set<RuleDataSourceMapping>>() {
    });
    // Rule check meta info.
    Set<AlarmConfig> alarmConfigs = objectMapper.readValue(ruleNodeRequest.getAlarmConfigsObject(), new TypeReference<Set<AlarmConfig>>() {
    });
    Set<RuleVariable> ruleVariables = objectMapper.readValue(ruleNodeRequest.getRuleVariablesObject(), new TypeReference<Set<RuleVariable>>() {
    });
    LOGGER.info("Import basic information: {}", new StringBuilder().append("\n").append(rule.getName()).append("\n").append(template.getName()).append("\n").toString());
    LOGGER.info(objectMapper.writeValueAsString(ruleNodeRequest));
    Rule ruleInDb = ruleDao.findByProjectAndRuleName(projectInDb, rule.getName());
    try {
        importRuleReal(ruleNodeRequest, ruleInDb, rule, projectInDb, template, ruleGroup, alarmConfigs, ruleVariables, ruleDataSources, ruleDataSourceMappings);
    } catch (NullPointerException e) {
        LOGGER.error("Rule object attributes must not be null.", e);
    }
}
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) Template(com.webank.wedatasphere.qualitis.rule.entity.Template) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) AlarmConfig(com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig) RuleVariable(com.webank.wedatasphere.qualitis.rule.entity.RuleVariable) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with Template

use of com.webank.wedatasphere.qualitis.rule.entity.Template 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 5 with Template

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

Aggregations

Template (com.webank.wedatasphere.qualitis.rule.entity.Template)29 ArrayList (java.util.ArrayList)17 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)13 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)11 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)10 AlarmConfig (com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)9 RuleDataSource (com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource)9 TemplateDataSourceType (com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType)9 HashSet (java.util.HashSet)8 TemplateOutputMeta (com.webank.wedatasphere.qualitis.rule.entity.TemplateOutputMeta)7 TemplateStatisticsInputMeta (com.webank.wedatasphere.qualitis.rule.entity.TemplateStatisticsInputMeta)6 RuleTemplateResponse (com.webank.wedatasphere.qualitis.rule.response.RuleTemplateResponse)6 List (java.util.List)6 User (com.webank.wedatasphere.qualitis.entity.User)5 UserRole (com.webank.wedatasphere.qualitis.entity.UserRole)5 TemplateUser (com.webank.wedatasphere.qualitis.rule.entity.TemplateUser)5 Date (java.util.Date)5 Set (java.util.Set)5 Transactional (org.springframework.transaction.annotation.Transactional)5 Department (com.webank.wedatasphere.qualitis.entity.Department)4