use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.
the class RuleBatchServiceImpl method downloadRules.
@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<?> downloadRules(DownloadRuleRequest request, HttpServletResponse response) throws UnExpectedRequestException, IOException, WriteExcelException, PermissionDeniedRequestException {
// Check Arguments
DownloadRuleRequest.checkRequest(request);
String loginUser = HttpUtils.getUserName(httpServletRequest);
Boolean projectAllRules = request.getProjectId() != null;
List<Rule> ruleLists;
if (projectAllRules) {
LOGGER.info("Downloading all rules of project. project id: {}", request.getProjectId());
Project project = projectDao.findById(request.getProjectId());
if (project == null) {
throw new UnExpectedRequestException("{&PROJECT_ID} {&DOES_NOT_EXIST}");
}
ruleLists = ruleDao.findByProject(project);
} else {
LOGGER.info("Downloading all rules. rule ids: {}", request.getRuleIds());
ruleLists = ruleDao.findByIds(request.getRuleIds());
List<Long> ruleIds = ruleLists.stream().map(Rule::getId).distinct().collect(Collectors.toList());
List<Long> notExistRules = request.getRuleIds().stream().filter(l -> !ruleIds.contains(l)).collect(Collectors.toList());
if (!notExistRules.isEmpty()) {
throw new UnExpectedRequestException("{&THE_IDS_OF_RULE}: " + notExistRules.toString() + " {&DOES_NOT_EXIST}");
}
}
if (ruleLists == null || ruleLists.isEmpty()) {
throw new UnExpectedRequestException("{&NO_RULE_CAN_DOWNLOAD}");
}
// Check permissions of project
List<Integer> permissions = new ArrayList<>();
permissions.add(ProjectUserPermissionEnum.DEVELOPER.getCode());
projectService.checkProjectPermission(ruleLists.iterator().next().getProject(), loginUser, permissions);
LOGGER.info("Succeed to find rules that will be downloaded. rule_ids: {}", ruleLists.stream().map(Rule::getId));
return downloadRulesReal(ruleLists, response, loginUser);
}
use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.
the class RuleBatchServiceImpl method getAlarmConfig.
private void getAlarmConfig(List<AlarmConfigRequest> alarmConfigRequests, ExcelTemplateRuleByProject excelTemplateRule, Template template, String localeStr) throws UnExpectedRequestException {
String templateOutputName = excelTemplateRule.getAlarmCheckName();
if (!StringUtils.isBlank(templateOutputName)) {
String checkTemplateName = excelTemplateRule.getCheckTemplateName();
String compareTypeName = excelTemplateRule.getCompareType();
String threshold = excelTemplateRule.getThreshold();
TemplateOutputMeta templateOutputMeta = findTemplateOutputMetaByTemplateAndOutputName(template, templateOutputName);
if (templateOutputMeta == null) {
throw new UnExpectedRequestException("{&TEMPLATE_OUTPUT_NAME} {&DOES_NOT_EXIST}");
}
AlarmConfigRequest alarmConfigRequest = new AlarmConfigRequest();
alarmConfigRequest.setCheckTemplate(CheckTemplateEnum.getCheckTemplateCode(checkTemplateName, localeStr));
alarmConfigRequest.setCompareType(CompareTypeEnum.getCompareTypeCode(compareTypeName));
alarmConfigRequest.setThreshold(Double.valueOf(threshold));
alarmConfigRequest.setOutputMetaId(templateOutputMeta.getId());
// Rule Metric.
String ruleMetricEnCode = excelTemplateRule.getRuleMetricEnCode();
if (StringUtils.isNotBlank(ruleMetricEnCode)) {
// xx_xx_xx_encode, index is 3.
RuleMetric ruleMetricInDb = ruleMetricDao.findByEnCode(ruleMetricEnCode);
if (ruleMetricInDb == null) {
throw new UnExpectedRequestException("Rule metric[Code=" + ruleMetricEnCode + "] " + "{&DOES_NOT_EXIST}");
}
String code = ruleMetricInDb.getEnCode();
alarmConfigRequest.setRuleMetricEnCode(code);
}
alarmConfigRequest.setUploadRuleMetricValue(excelTemplateRule.getUploadRuleMetricValue());
alarmConfigRequest.setUploadAbnormalValue(excelTemplateRule.getUploadAbnormalValue());
alarmConfigRequests.add(alarmConfigRequest);
}
}
use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.
the class RuleBatchServiceImpl method getTemplateArgument.
private void getTemplateArgument(List<TemplateArgumentRequest> templateArgumentRequests, ExcelTemplateRuleByProject excelTemplateRule, Template template) throws UnExpectedRequestException {
String argumentKey = excelTemplateRule.getArgumentKey();
if (!StringUtils.isBlank(argumentKey)) {
TemplateArgumentRequest templateArgumentRequest = new TemplateArgumentRequest();
templateArgumentRequest.setArgumentStep(InputActionStepEnum.TEMPLATE_INPUT_META.getCode());
String argumentValue = excelTemplateRule.getArgumentValue();
templateArgumentRequest.setArgumentValue(argumentValue);
TemplateMidTableInputMeta templateMidTableInputMeta = findByTemplateAndName(template, argumentKey);
if (templateMidTableInputMeta == null) {
throw new UnExpectedRequestException("{&TEMPLATE_ARGUMENT}: [" + argumentKey + "] {&DOES_NOT_EXIST}");
}
templateArgumentRequest.setArgumentId(templateMidTableInputMeta.getId());
templateArgumentRequests.add(templateArgumentRequest);
}
}
use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.
the class RuleDataSourceServiceImpl method checkDataSourceClusterSupport.
/**
* Check if cluster name supported
* return if there is no cluster config
* @param submittedClusterNames
* @throws UnExpectedRequestException
*/
@Override
public void checkDataSourceClusterSupport(Set<String> submittedClusterNames) throws UnExpectedRequestException {
if (submittedClusterNames == null || submittedClusterNames.isEmpty()) {
return;
}
List<ClusterInfo> clusters = clusterInfoDao.findAllClusterInfo(0, Integer.MAX_VALUE);
if (clusters == null || clusters.isEmpty()) {
LOGGER.info("Failed to find cluster info config. End to check the limitation of cluster info.");
return;
}
Set<String> supportClusterNames = new HashSet<>();
for (ClusterInfo info : clusters) {
supportClusterNames.add(info.getClusterName());
}
Set<String> unSupportClusterNameSet = new HashSet<>();
for (String clusterName : submittedClusterNames) {
if (!supportClusterNames.contains(clusterName)) {
unSupportClusterNameSet.add(clusterName);
}
}
if (unSupportClusterNameSet.size() > 0) {
throw new UnExpectedRequestException(String.format("{&NOT_SUPPORT_CLUSTER_NAME}:%s,{&ONLY_SUPPORT_CLUSTER_NAME_ARE}:%s", unSupportClusterNameSet, submittedClusterNames.toString()));
}
}
use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.
the class ProjectBatchServiceImpl method uploadProjectsReal.
private GeneralResponse<?> uploadProjectsReal(InputStream fileInputStream, String fileName, String userName, boolean aomp) throws IOException, UnExpectedRequestException, PermissionDeniedRequestException, RoleNotFoundException {
String suffixName = fileName.substring(fileName.lastIndexOf('.'));
if (!suffixName.equals(SUPPORT_EXCEL_SUFFIX_NAME)) {
throw new UnExpectedRequestException("{&DO_NOT_SUPPORT_SUFFIX_NAME}: [" + suffixName + "]. {&ONLY_SUPPORT} [" + SUPPORT_EXCEL_SUFFIX_NAME + "]", 422);
}
if (userName == null) {
return new GeneralResponse<>("401", "{&PLEASE_LOGIN}", null);
}
User user = userDao.findByUsername(userName);
Long userId = user.getId();
// Read file and create project
ExcelProjectListener listener = readExcel(fileInputStream);
// Check if excel file is empty
if (listener.getExcelProjectContent().isEmpty() && listener.getExcelRuleContent().isEmpty() && listener.getExcelCustomRuleContent().isEmpty() && listener.getExcelMultiRuleContent().isEmpty() && listener.getTemplateFileExcelContent().isEmpty() && listener.getExcelMetricContent().isEmpty()) {
throw new UnExpectedRequestException("{&FILE_CAN_NOT_BE_EMPTY_OR_FILE_CAN_NOT_BE_RECOGNIZED}", 422);
}
for (ExcelProject excelProject : listener.getExcelProjectContent()) {
Project project = projectDao.findByNameAndCreateUser(excelProject.getProjectName(), userName);
if (project != null) {
if (!aomp) {
LOGGER.info("hint for user to decide to override or not.");
}
// Means update project.
LOGGER.info("Start to update project[name={}] with upload project file.", project.getName());
ModifyProjectDetailRequest request = convertExcelProjectToModifyProjectRequest(excelProject, project, userName);
projectService.modifyProjectDetail(request, false);
} else {
// Check excel project arguments is valid or not
AddProjectRequest request = convertExcelProjectToAddProjectRequest(excelProject);
projectService.addProject(request, userId);
}
}
for (ExcelRuleMetric excelRuleMetric : listener.getExcelMetricContent()) {
RuleMetric ruleMetric = ruleMetricDao.findByName(excelRuleMetric.getName());
if (ruleMetric != null) {
if (!aomp) {
LOGGER.info("hint for user to decide to override or not.");
}
LOGGER.info("Start to update rule metric[name={}] with upload rule metric file.", ruleMetric.getName());
modifyRuleMetric(excelRuleMetric, ruleMetric, userName);
} else {
addRuleMetric(excelRuleMetric, userName);
}
}
// Create rules according to excel sheet
Map<String, Map<String, List<ExcelTemplateRuleByProject>>> excelTemplateRulePartitionedByProject = listener.getExcelRuleContent();
Map<String, Map<String, List<ExcelCustomRuleByProject>>> excelCustomRulePartitionedByProject = listener.getExcelCustomRuleContent();
Map<String, Map<String, List<ExcelMultiTemplateRuleByProject>>> excelMultiTemplateRulePartitionedByProject = listener.getExcelMultiRuleContent();
Map<String, Map<String, List<ExcelTemplateFileRuleByProject>>> excelTemplateFileRulePartitionedByProject = listener.getTemplateFileExcelContent();
Set<String> allProjects = new HashSet<>();
allProjects.addAll(excelTemplateRulePartitionedByProject.keySet());
allProjects.addAll(excelCustomRulePartitionedByProject.keySet());
allProjects.addAll(excelMultiTemplateRulePartitionedByProject.keySet());
allProjects.addAll(excelTemplateFileRulePartitionedByProject.keySet());
for (String projectName : allProjects) {
try {
Project projectInDb = projectDao.findByNameAndCreateUser(projectName, userName);
if (projectInDb == null) {
throw new UnExpectedRequestException("{&PROJECT}: [" + projectName + "] {&DOES_NOT_EXIST}");
}
ruleBatchService.getAndSaveRule(excelTemplateRulePartitionedByProject.get(projectName), excelCustomRulePartitionedByProject.get(projectName), excelMultiTemplateRulePartitionedByProject.get(projectName), excelTemplateFileRulePartitionedByProject.get(projectName), projectInDb, userName, aomp);
} catch (Exception e) {
throw new UnExpectedRequestException(e.getMessage());
}
}
fileInputStream.close();
return new GeneralResponse<>("200", "{&SUCCEED_TO_UPLOAD_FILE}", null);
}
Aggregations