Search in sources :

Example 1 with AlarmConfig

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

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

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

the class RuleBatchServiceImpl method alarmConfigsToExcel.

private void alarmConfigsToExcel(Rule rule, List<ExcelTemplateRuleByProject> lines, String localeStr) {
    for (AlarmConfig alarmConfig : rule.getAlarmConfigs()) {
        Double threshold = alarmConfig.getThreshold();
        String alarmOutputName = alarmConfig.getTemplateOutputMeta().getOutputName();
        String alarmCompareType = CompareTypeEnum.getCompareTypeName(alarmConfig.getCompareType());
        String checkTemplateName = CheckTemplateEnum.getCheckTemplateName(alarmConfig.getCheckTemplate(), localeStr);
        ExcelTemplateRuleByProject tmp = new ExcelTemplateRuleByProject(rule.getName());
        tmp.setCheckTemplateName(checkTemplateName);
        tmp.setThreshold(String.valueOf(threshold));
        tmp.setAlarmCheckName(alarmOutputName);
        tmp.setCompareType(alarmCompareType);
        RuleMetric ruleMetric = alarmConfig.getRuleMetric();
        if (ruleMetric != null) {
            String enCode = ruleMetric.getEnCode();
            tmp.setRuleMetricEnCode(enCode);
            tmp.setRuleMetricName(ruleMetric.getName());
        }
        tmp.setUploadRuleMetricValue(alarmConfig.getUploadRuleMetricValue());
        tmp.setUploadAbnormalValue(alarmConfig.getUploadAbnormalValue());
        LOGGER.info("Collect excel line: {}", tmp);
        lines.add(tmp);
    }
}
Also used : RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) ExcelTemplateRuleByProject(com.webank.wedatasphere.qualitis.project.excel.ExcelTemplateRuleByProject) AlarmConfig(com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)

Example 4 with AlarmConfig

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

the class ExecutionManagerImpl method saveTaskRusult.

private List<TaskResult> saveTaskRusult(String fullSize, double fileCount, Application application, String submitTime, Rule rule, Set<AlarmConfig> alarmConfig, String runDate) throws UnExpectedRequestException {
    double number = Double.parseDouble(fullSize.split(" ")[0]);
    String unit = fullSize.split(" ")[1];
    // Save task result.
    List<TaskResult> taskResults = new ArrayList<>();
    List<Integer> fileOutputNames = alarmConfig.stream().map(AlarmConfig::getFileOutputName).distinct().collect(Collectors.toList());
    for (Integer fileOutputName : fileOutputNames) {
        AlarmConfig currentAlarmConfig = alarmConfig.stream().filter(alarmConfigSetting -> alarmConfigSetting.getFileOutputName().equals(fileOutputName)).iterator().next();
        RuleMetric ruleMetric = currentAlarmConfig.getRuleMetric();
        if (ruleMetric == null) {
            throw new UnExpectedRequestException("File rule metric {&CAN_NOT_BE_NULL_OR_EMPTY}");
        }
        TaskResult taskResult;
        if (StringUtils.isNotBlank(runDate)) {
            TaskResult existTaskResult = taskResultDao.find(runDate, rule.getId(), ruleMetric.getId());
            if (existTaskResult != null) {
                taskResult = existTaskResult;
            } else {
                taskResult = new TaskResult();
                Date runRealDate = null;
                try {
                    runRealDate = new SimpleDateFormat("yyyyMMdd").parse(runDate);
                } catch (ParseException e) {
                    String errorMsg = "Parse date string with run date failed. Exception message: " + e.getMessage();
                    LOGGER.error(errorMsg);
                    throw new UnExpectedRequestException(errorMsg);
                }
                taskResult.setRunDate(runRealDate.getTime());
            }
        } else {
            taskResult = new TaskResult();
        }
        taskResult.setApplicationId(application.getId());
        taskResult.setCreateTime(submitTime);
        taskResult.setRuleId(rule.getId());
        taskResult.setRuleMetricId(ruleMetric.getId());
        if (fileOutputName.equals(FileOutputNameEnum.FILE_COUNT.getCode())) {
            taskResult.setResultType("int");
            taskResult.setValue(fileCount + "");
        } else if (fileOutputName.equals(FileOutputNameEnum.DIR_SIZE.getCode())) {
            String alarmConfigUnit = FileOutputUnitEnum.fileOutputUnit(currentAlarmConfig.getFileOutputUnit());
            taskResult.setResultType(alarmConfigUnit);
            taskResult.setValue(UnitTransfer.alarmconfigToTaskResult(number, alarmConfigUnit, unit.toUpperCase()) + "");
        } else {
            throw new UnExpectedRequestException("Unknown file output name.");
        }
        taskResults.add(taskResultDao.saveTaskResult(taskResult));
    }
    return taskResults;
}
Also used : RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) ArrayList(java.util.ArrayList) Date(java.util.Date) TaskResult(com.webank.wedatasphere.qualitis.entity.TaskResult) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat) AlarmConfig(com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig) TaskRuleAlarmConfig(com.webank.wedatasphere.qualitis.entity.TaskRuleAlarmConfig)

Example 5 with AlarmConfig

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

Aggregations

AlarmConfig (com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig)24 ArrayList (java.util.ArrayList)21 RuleMetric (com.webank.wedatasphere.qualitis.entity.RuleMetric)14 RuleDataSource (com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource)14 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)12 Template (com.webank.wedatasphere.qualitis.rule.entity.Template)9 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)8 RuleDataSourceMapping (com.webank.wedatasphere.qualitis.rule.entity.RuleDataSourceMapping)6 Date (java.util.Date)6 RuleVariable (com.webank.wedatasphere.qualitis.rule.entity.RuleVariable)5 HashSet (java.util.HashSet)5 Project (com.webank.wedatasphere.qualitis.project.entity.Project)4 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)4 TemplateOutputMeta (com.webank.wedatasphere.qualitis.rule.entity.TemplateOutputMeta)4 CustomAlarmConfigRequest (com.webank.wedatasphere.qualitis.rule.request.CustomAlarmConfigRequest)4 FileAlarmConfigRequest (com.webank.wedatasphere.qualitis.rule.request.FileAlarmConfigRequest)4 RuleResponse (com.webank.wedatasphere.qualitis.rule.response.RuleResponse)4 Set (java.util.Set)4 TaskRuleAlarmConfig (com.webank.wedatasphere.qualitis.entity.TaskRuleAlarmConfig)3 TemplateDataSourceType (com.webank.wedatasphere.qualitis.rule.entity.TemplateDataSourceType)3