Search in sources :

Example 1 with TaskResult

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

the class ExecutionManagerImpl method modifyTaskStatus.

private int modifyTaskStatus(List<TaskRuleAlarmConfig> alarmConfigs, Task taskInDb, List<TaskResult> taskResults, int successRule) {
    boolean rulePass = true;
    for (TaskRuleAlarmConfig alarmConfig : alarmConfigs) {
        TaskResult taskResult = taskResults.stream().filter(taskResultInDb -> taskResultInDb.getRuleMetricId().equals(alarmConfig.getRuleMetric().getId())).iterator().next();
        if (!FilePassUtil.pass(alarmConfig, taskResult, taskResultDao)) {
            rulePass = false;
            alarmConfig.setStatus(AlarmConfigStatusEnum.NOT_PASS.getCode());
            if (alarmConfig.getDeleteFailCheckResult() != null && alarmConfig.getDeleteFailCheckResult()) {
                taskResult.setSaveResult(false);
                taskResultDao.saveTaskResult(taskResult);
            }
        } else {
            alarmConfig.setStatus(AlarmConfigStatusEnum.PASS.getCode());
        }
    }
    if (rulePass) {
        successRule++;
    } else if (taskInDb.getAbortOnFailure() != null && taskInDb.getAbortOnFailure()) {
        taskInDb.setStatus(TaskStatusEnum.FAILED.getCode());
    } else {
        taskInDb.setStatus(TaskStatusEnum.FAIL_CHECKOUT.getCode());
        taskInDb.setProgress(Double.parseDouble("1"));
    }
    return successRule;
}
Also used : TaskRuleAlarmConfig(com.webank.wedatasphere.qualitis.entity.TaskRuleAlarmConfig) TaskResult(com.webank.wedatasphere.qualitis.entity.TaskResult)

Example 2 with TaskResult

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

the class ExecutionManagerImpl method executeFileRule.

@Override
public TaskSubmitResult executeFileRule(List<Rule> fileRules, String submitTime, Application application, String user, String clusterName, StringBuffer runDate) throws UnExpectedRequestException, MetaDataAcquireFailedException {
    LOGGER.info("Start to execute file rule task and save check result.");
    Task taskInDb = taskDao.save(new Task(application, submitTime, TaskStatusEnum.SUBMITTED.getCode()));
    Set<TaskDataSource> taskDataSources = new HashSet<>(fileRules.size());
    Set<TaskRuleSimple> taskRuleSimples = new HashSet<>(fileRules.size());
    int totalRules = fileRules.size();
    int successRule = 0;
    for (Rule rule : fileRules) {
        if (rule.getAbortOnFailure() != null) {
            taskInDb.setAbortOnFailure(rule.getAbortOnFailure());
        }
        TaskRuleSimple taskRuleSimple = new TaskRuleSimple(rule, taskInDb, httpServletRequest.getHeader("Content-Language"));
        taskRuleSimples.add(taskRuleSimpleRepository.save(taskRuleSimple));
        RuleDataSource ruleDataSource = rule.getRuleDataSources().iterator().next();
        taskDataSources.add(taskDataSourceRepository.save(new TaskDataSource(ruleDataSource, taskInDb)));
        // Check rule datasource: 1) table 2) partition.
        if (StringUtils.isEmpty(ruleDataSource.getFilter())) {
            TableStatisticsInfo result;
            try {
                String proxyUser = ruleDataSource.getProxyUser();
                result = metaDataClient.getTableStatisticsInfo(StringUtils.isNotBlank(clusterName) ? clusterName : ruleDataSource.getClusterName(), ruleDataSource.getDbName(), ruleDataSource.getTableName(), StringUtils.isNotBlank(proxyUser) ? proxyUser : user);
            } catch (RestClientException e) {
                LOGGER.error("Failed to get table statistics with linkis api.", e);
                throw new UnExpectedRequestException("{&FAILED_TO_GET_DATASOURCE_INFO}");
            }
            if (result == null) {
                throw new UnExpectedRequestException("{&FAILED_TO_GET_DATASOURCE_INFO}");
            }
            String fullSize = result.getTableSize();
            List<TaskResult> taskResultInDbs = saveTaskRusult(fullSize, Double.parseDouble(result.getTableFileCount() + ""), application, submitTime, rule, rule.getAlarmConfigs(), runDate.toString());
            successRule = modifyTaskStatus(taskRuleSimple.getTaskRuleAlarmConfigList(), taskInDb, taskResultInDbs, successRule);
        } else {
            PartitionStatisticsInfo result;
            try {
                String proxyUser = ruleDataSource.getProxyUser();
                result = metaDataClient.getPartitionStatisticsInfo(StringUtils.isNotBlank(clusterName) ? clusterName : ruleDataSource.getClusterName(), ruleDataSource.getDbName(), ruleDataSource.getTableName(), filterToPartitionPath(DateExprReplaceUtil.replaceFilter(new Date(), ruleDataSource.getFilter())), StringUtils.isNotBlank(proxyUser) ? proxyUser : user);
            } catch (RestClientException e) {
                LOGGER.error("Failed to get table statistics with linkis api.", e);
                throw new UnExpectedRequestException("{&FAILED_TO_GET_DATASOURCE_INFO}");
            }
            if (result == null) {
                throw new UnExpectedRequestException("{&FAILED_TO_GET_DATASOURCE_INFO}");
            }
            String fullSize = result.getPartitionSize();
            List<TaskResult> taskResultInDbs = saveTaskRusult(fullSize, Double.parseDouble(result.getPartitionChildCount() + ""), application, submitTime, rule, rule.getAlarmConfigs(), runDate.toString());
            successRule = modifyTaskStatus(taskRuleSimple.getTaskRuleAlarmConfigList(), taskInDb, taskResultInDbs, successRule);
        }
        if (taskInDb.getStatus().equals(TaskStatusEnum.FAILED.getCode())) {
            break;
        }
    }
    taskInDb.setTaskDataSources(taskDataSources);
    taskInDb.setTaskRuleSimples(taskRuleSimples);
    // Update task status
    taskInDb.setEndTime(ExecutionManagerImpl.PRINT_TIME_FORMAT.format(new Date()));
    if (totalRules == successRule) {
        taskInDb.setStatus(TaskStatusEnum.PASS_CHECKOUT.getCode());
        taskInDb.setProgress(Double.parseDouble("1"));
    }
    taskDao.save(taskInDb);
    LOGGER.info("Finished to execute file rule task and save check result.");
    TaskSubmitResult taskSubmitResult = new TaskSubmitResult();
    taskSubmitResult.setApplicationId(application.getId());
    taskSubmitResult.setClusterName(clusterName);
    return taskSubmitResult;
}
Also used : UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) Task(com.webank.wedatasphere.qualitis.entity.Task) DataQualityTask(com.webank.wedatasphere.qualitis.bean.DataQualityTask) TaskRuleDataSource(com.webank.wedatasphere.qualitis.bean.TaskRuleDataSource) RuleDataSource(com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource) Date(java.util.Date) TaskDataSource(com.webank.wedatasphere.qualitis.entity.TaskDataSource) RestClientException(org.springframework.web.client.RestClientException) TaskResult(com.webank.wedatasphere.qualitis.entity.TaskResult) TaskRule(com.webank.wedatasphere.qualitis.bean.TaskRule) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) PartitionStatisticsInfo(com.webank.wedatasphere.qualitis.metadata.response.table.PartitionStatisticsInfo) TableStatisticsInfo(com.webank.wedatasphere.qualitis.metadata.response.table.TableStatisticsInfo) TaskRuleSimple(com.webank.wedatasphere.qualitis.entity.TaskRuleSimple) HashSet(java.util.HashSet) TaskSubmitResult(com.webank.wedatasphere.qualitis.bean.TaskSubmitResult)

Example 3 with TaskResult

use of com.webank.wedatasphere.qualitis.entity.TaskResult 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 4 with TaskResult

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

the class ApplicationServiceImpl method joinAlarmConfig.

private void joinAlarmConfig(List<ExcelResult> results, TaskRuleSimple taskRuleSimple, ExcelResult excelResult, UploadResultRequest request, StringBuffer checkTemplateStr, StringBuffer resultStr) {
    for (TaskRuleAlarmConfig taskRuleAlarmConfig : taskRuleSimple.getTaskRuleAlarmConfigList()) {
        Integer checkTemplate = taskRuleAlarmConfig.getCheckTemplate();
        String checkTemplateName = CheckTemplateEnum.getCheckTemplateName(checkTemplate);
        String outputName = taskRuleAlarmConfig.getOutputName();
        String compare = CompareTypeEnum.getCompareTypeName(taskRuleAlarmConfig.getCompareType());
        Double threshold = taskRuleAlarmConfig.getThreshold();
        Integer statusCode = taskRuleAlarmConfig.getStatus();
        String status = AlarmConfigStatusEnum.getMessage(statusCode);
        RuleMetric ruleMetric = taskRuleAlarmConfig.getRuleMetric();
        TaskResult taskResult = taskResultDao.find(taskRuleSimple.getApplicationId(), taskRuleSimple.getRuleId(), ruleMetric != null ? ruleMetric.getId() : -1);
        String resultValue = "";
        Long runDate = null;
        if (taskResult != null) {
            resultValue = StringUtils.isNotBlank(taskResult.getValue()) ? taskResult.getValue() : "0";
            runDate = taskResult.getRunDate();
        }
        if (request.getStatus().contains(statusCode)) {
            checkTemplateStr.append(checkTemplateName).append(StringUtils.isNotBlank(compare) ? compare : "").append(" ").append(threshold).append(" ").append(status);
            ExcelResult excelAlarmConfig = new ExcelResult();
            BeanUtils.copyProperties(excelResult, excelAlarmConfig);
            excelAlarmConfig.setRuleCheckTemplates(checkTemplateStr.toString());
            resultStr.append(outputName).append(": ").append(resultValue);
            excelAlarmConfig.setHistoryResult(resultStr.toString());
            excelAlarmConfig.setCreateTime(runDate != null ? runDate.toString() : "");
            excelAlarmConfig.setExecutionUser(taskRuleSimple.getExecuteUser());
            results.add(excelAlarmConfig);
            checkTemplateStr.delete(0, checkTemplateStr.length());
            resultStr.delete(0, checkTemplateStr.length());
        }
    }
}
Also used : RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) TaskRuleAlarmConfig(com.webank.wedatasphere.qualitis.entity.TaskRuleAlarmConfig) ExcelResult(com.webank.wedatasphere.qualitis.excel.ExcelResult) TaskResult(com.webank.wedatasphere.qualitis.entity.TaskResult)

Example 5 with TaskResult

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

the class TaskServiceImpl method getTaskDetail.

@Override
public GeneralResponse<?> getTaskDetail(Long taskId) throws UnExpectedRequestException {
    Task taskInDb = taskDao.findById(taskId);
    if (taskInDb == null) {
        throw new UnExpectedRequestException("Job id [" + taskId + "] {&DOES_NOT_EXIST}");
    }
    // FIXME:
    List<TaskRuleAlarmConfig> distinct;
    Set<TaskRuleSimple> taskRuleSimples = new HashSet<>(taskInDb.getTaskRuleSimples().size());
    for (TaskRuleSimple taskRuleSimple : taskInDb.getTaskRuleSimples()) {
        distinct = taskRuleSimple.getTaskRuleAlarmConfigList().stream().distinct().collect(Collectors.toList());
        taskRuleSimple.setTaskRuleAlarmConfigList(distinct);
        taskRuleSimples.add(taskRuleSimple);
    }
    taskInDb.setTaskRuleSimples(taskRuleSimples);
    // Find single table verification rules
    List<TaskRuleSimple> singleRuleIds = taskInDb.getTaskRuleSimples().stream().filter(taskRuleSimple -> taskRuleSimple.getRuleType().equals(RuleTypeEnum.SINGLE_TEMPLATE_RULE.getCode())).collect(Collectors.toList());
    // Find all datasources of single table verification rules, and save it in the map
    Map<TaskRuleSimple, List<TaskDataSource>> singleRuleDataSourceMap = new HashMap<>(singleRuleIds.size());
    for (TaskRuleSimple taskRuleSimple : singleRuleIds) {
        singleRuleDataSourceMap.put(taskRuleSimple, taskDataSourceDao.findByTaskAndRuleId(taskInDb, taskRuleSimple.getRuleId()));
    }
    // Find custom table verification rules
    List<TaskRuleSimple> customRuleIds = taskInDb.getTaskRuleSimples().stream().filter(taskRuleSimple -> taskRuleSimple.getRuleType().equals(RuleTypeEnum.CUSTOM_RULE.getCode())).collect(Collectors.toList());
    // Find all datasources of custom table verification rules, and save it in the map
    Map<TaskRuleSimple, List<TaskDataSource>> customRuleDataSourceMap = new HashMap<>(customRuleIds.size());
    for (TaskRuleSimple taskRuleSimple : customRuleIds) {
        customRuleDataSourceMap.put(taskRuleSimple, taskDataSourceDao.findByTaskAndRuleId(taskInDb, taskRuleSimple.getRuleId()));
    }
    // Find multi-table verification rules
    List<TaskRuleSimple> multiRuleIds = taskInDb.getTaskRuleSimples().stream().filter(taskRuleSimple -> taskRuleSimple.getRuleType().equals(RuleTypeEnum.MULTI_TEMPLATE_RULE.getCode())).collect(Collectors.toList());
    // Find all datasources of multi-table verification rules, and save it in the map
    Map<TaskRuleSimple, List<TaskDataSource>> multiRuleDataSourceMap = new HashMap<>(multiRuleIds.size());
    for (TaskRuleSimple taskRuleSimple : multiRuleIds) {
        multiRuleDataSourceMap.put(taskRuleSimple, taskDataSourceDao.findByTaskAndRuleId(taskInDb, taskRuleSimple.getRuleId()));
    }
    // Find file table verification rules
    List<TaskRuleSimple> fileRuleIds = taskInDb.getTaskRuleSimples().stream().filter(taskRuleSimple -> taskRuleSimple.getRuleType().equals(RuleTypeEnum.FILE_TEMPLATE_RULE.getCode())).collect(Collectors.toList());
    // Find all datasources of file table verification rules, and save it in the map
    Map<TaskRuleSimple, List<TaskDataSource>> fileRuleDataSourceMap = new HashMap<>(fileRuleIds.size());
    for (TaskRuleSimple taskRuleSimple : fileRuleIds) {
        fileRuleDataSourceMap.put(taskRuleSimple, taskDataSourceDao.findByTaskAndRuleId(taskInDb, taskRuleSimple.getRuleId()));
    }
    List<Long> allRuleIds = new ArrayList<>();
    for (TaskRuleSimple taskRuleSimple : taskInDb.getTaskRuleSimples()) {
        allRuleIds.add(taskRuleSimple.getRuleId());
        if (taskRuleSimple.getChildRuleSimple() != null) {
            allRuleIds.add(taskRuleSimple.getChildRuleSimple().getRuleId());
        }
    }
    List<TaskResult> allTaskResult = taskResultDao.findByApplicationIdAndRuleIn(taskInDb.getApplication().getId(), allRuleIds);
    Map<Long, List<TaskResult>> allResultMap = new HashMap<>(allTaskResult.size());
    for (TaskResult taskResult : allTaskResult) {
        if (allResultMap.get(taskResult.getRuleId()) != null) {
            allResultMap.get(taskResult.getRuleId()).add(taskResult);
        } else {
            List<TaskResult> taskResults = new ArrayList<>(1);
            taskResults.add(taskResult);
            allResultMap.put(taskResult.getRuleId(), taskResults);
        }
    }
    TaskCheckResultResponse taskCheckResultResponse = new TaskCheckResultResponse(taskInDb, singleRuleDataSourceMap, customRuleDataSourceMap, multiRuleDataSourceMap, fileRuleDataSourceMap, allResultMap);
    return new GeneralResponse<>("200", "{&SUCCEED_TO_GET_TASK_DETAIL}", taskCheckResultResponse);
}
Also used : java.util(java.util) TaskService(com.webank.wedatasphere.qualitis.service.TaskService) TaskDataSourceDao(com.webank.wedatasphere.qualitis.dao.TaskDataSourceDao) Autowired(org.springframework.beans.factory.annotation.Autowired) Collectors(java.util.stream.Collectors) RuleTypeEnum(com.webank.wedatasphere.qualitis.rule.constant.RuleTypeEnum) TaskRuleSimple(com.webank.wedatasphere.qualitis.entity.TaskRuleSimple) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) TaskCheckResultResponse(com.webank.wedatasphere.qualitis.response.TaskCheckResultResponse) TaskDao(com.webank.wedatasphere.qualitis.dao.TaskDao) Task(com.webank.wedatasphere.qualitis.entity.Task) Service(org.springframework.stereotype.Service) TaskRuleAlarmConfig(com.webank.wedatasphere.qualitis.entity.TaskRuleAlarmConfig) TaskResultDao(com.webank.wedatasphere.qualitis.dao.TaskResultDao) TaskResult(com.webank.wedatasphere.qualitis.entity.TaskResult) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) TaskDataSource(com.webank.wedatasphere.qualitis.entity.TaskDataSource) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) Task(com.webank.wedatasphere.qualitis.entity.Task) TaskCheckResultResponse(com.webank.wedatasphere.qualitis.response.TaskCheckResultResponse) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) TaskRuleAlarmConfig(com.webank.wedatasphere.qualitis.entity.TaskRuleAlarmConfig) TaskResult(com.webank.wedatasphere.qualitis.entity.TaskResult) TaskRuleSimple(com.webank.wedatasphere.qualitis.entity.TaskRuleSimple)

Aggregations

TaskResult (com.webank.wedatasphere.qualitis.entity.TaskResult)8 TaskRuleAlarmConfig (com.webank.wedatasphere.qualitis.entity.TaskRuleAlarmConfig)5 Task (com.webank.wedatasphere.qualitis.entity.Task)3 TaskDataSource (com.webank.wedatasphere.qualitis.entity.TaskDataSource)3 TaskRuleSimple (com.webank.wedatasphere.qualitis.entity.TaskRuleSimple)3 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)3 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)3 ArrayList (java.util.ArrayList)3 Date (java.util.Date)3 TaskSubmitResult (com.webank.wedatasphere.qualitis.bean.TaskSubmitResult)2 RuleMetric (com.webank.wedatasphere.qualitis.entity.RuleMetric)2 RuleDataSource (com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource)2 HashSet (java.util.HashSet)2 DataQualityTask (com.webank.wedatasphere.qualitis.bean.DataQualityTask)1 TaskRule (com.webank.wedatasphere.qualitis.bean.TaskRule)1 TaskRuleDataSource (com.webank.wedatasphere.qualitis.bean.TaskRuleDataSource)1 TaskDao (com.webank.wedatasphere.qualitis.dao.TaskDao)1 TaskDataSourceDao (com.webank.wedatasphere.qualitis.dao.TaskDataSourceDao)1 TaskResultDao (com.webank.wedatasphere.qualitis.dao.TaskResultDao)1 Application (com.webank.wedatasphere.qualitis.entity.Application)1