Search in sources :

Example 1 with ExcelRuleMetric

use of com.webank.wedatasphere.qualitis.project.excel.ExcelRuleMetric in project Qualitis by WeBankFinTech.

the class RuleMetricServiceImpl method upload.

@Override
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<?> upload(InputStream fileInputStream, FormDataContentDisposition fileDisposition) throws UnExpectedRequestException, IOException {
    // Check Arguments
    if (fileInputStream == null || fileDisposition == null) {
        throw new UnExpectedRequestException("{&FILE_CAN_NOT_BE_NULL_OR_EMPTY}");
    }
    // Check suffix name of file
    String fileName = fileDisposition.getFileName();
    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 + "]");
    }
    String userName = HttpUtils.getUserName(httpServletRequest);
    LOGGER.info(userName + " start to upload rule metrics.");
    ExcelRuleMetricListener listener = new ExcelRuleMetricListener();
    List<ExcelRuleMetric> excelRuleMetrics = listener.getRuleMetricContent();
    ExcelReader excelReader = new ExcelReader(fileInputStream, null, listener);
    List<Sheet> sheets = excelReader.getSheets();
    for (Sheet sheet : sheets) {
        if (sheet.getSheetName().equals(ExcelSheetName.RULE_METRIC_NAME)) {
            sheet.setClazz(ExcelRuleMetric.class);
            sheet.setHeadLineMun(1);
            excelReader.read(sheet);
        }
    }
    if (CollectionUtils.isEmpty(excelRuleMetrics)) {
        throw new UnExpectedRequestException("{&FILE_CAN_NOT_BE_EMPTY_OR_FILE_CAN_NOT_BE_RECOGNIZED}");
    }
    try {
        for (ExcelRuleMetric excelRuleMetric : excelRuleMetrics) {
            AddRuleMetricRequest addRuleMetricRequest = new AddRuleMetricRequest();
            addRuleMetric(addRuleMetricRequest);
        }
        LOGGER.info("Succeed to add all rule metrics");
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
    } finally {
        fileInputStream.close();
    }
    return new GeneralResponse<>("200", "{&FAILED_TO_UPLOAD_RULE_METRIC}", null);
}
Also used : ExcelRuleMetric(com.webank.wedatasphere.qualitis.project.excel.ExcelRuleMetric) ExcelReader(com.alibaba.excel.ExcelReader) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) ExcelRuleMetricListener(com.webank.wedatasphere.qualitis.excel.ExcelRuleMetricListener) AddRuleMetricRequest(com.webank.wedatasphere.qualitis.request.AddRuleMetricRequest) Sheet(com.alibaba.excel.metadata.Sheet) PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) IOException(java.io.IOException) WriteExcelException(com.webank.wedatasphere.qualitis.rule.exception.WriteExcelException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with ExcelRuleMetric

use of com.webank.wedatasphere.qualitis.project.excel.ExcelRuleMetric in project Qualitis by WeBankFinTech.

the class RuleMetricServiceImpl method download.

@Override
public GeneralResponse<?> download(DownloadRuleMetricRequest request, HttpServletResponse response) throws UnExpectedRequestException, IOException, WriteExcelException, PermissionDeniedRequestException {
    // Check rule metric IDs permission.
    PageRequest pageRequest = new PageRequest(0, Integer.MAX_VALUE);
    List<RuleMetricResponse> ownRuleMetric = getAllRuleMetric(pageRequest).getData().getData().stream().collect(Collectors.toList());
    if (ownRuleMetric.size() <= 0 || ownRuleMetric.size() >= MAX_RULE_METRIC_COUNT) {
        throw new UnExpectedRequestException("The number of metrics is illegal");
    }
    List<Long> downloadIds = request.getRuleMetricIds();
    List<Long> ownIds = ownRuleMetric.stream().map(RuleMetricResponse::getId).collect(Collectors.toList());
    if (ownIds.containsAll(downloadIds)) {
        List<RuleMetricResponse> downloadRuleMetric = ownRuleMetric.stream().filter(ruleMetricResponse -> downloadIds.contains(ruleMetricResponse.getId())).collect(Collectors.toList());
        List<ExcelRuleMetric> excelRuleMetrics = new ArrayList<>(downloadRuleMetric.size());
        for (RuleMetricResponse ruleMetricResponse : downloadRuleMetric) {
            ExcelRuleMetric excelRuleMetric = new ExcelRuleMetric();
            excelRuleMetric.setName(ruleMetricResponse.getName());
            excelRuleMetric.setChName(ruleMetricResponse.getCnName());
            excelRuleMetric.setMetricDesc(ruleMetricResponse.getMetricDesc());
            int bussCode = ruleMetricResponse.getBussCode();
            excelRuleMetric.setDimension(bussCode + "");
            if (RuleMetricBussCodeEnum.SUBSYSTEM.getCode().equals(bussCode)) {
                excelRuleMetric.setFullCnName(ruleMetricResponse.getFullCnName());
                excelRuleMetric.setSubSystemName(ruleMetricResponse.getSubSystemName());
                excelRuleMetric.setSubSystemId(String.valueOf(ruleMetricResponse.getSubSystemId()));
            } else if (RuleMetricBussCodeEnum.PRODUCT.getCode().equals(bussCode)) {
                excelRuleMetric.setProductId(ruleMetricResponse.getProductId());
                excelRuleMetric.setProductName(ruleMetricResponse.getProductName());
            } else if (RuleMetricBussCodeEnum.CUSTOM.getCode().equals(bussCode)) {
                excelRuleMetric.setBussCustom(ruleMetricResponse.getBussCustom());
            }
            excelRuleMetric.setFrequency(String.valueOf(ruleMetricResponse.getFrequency()));
            excelRuleMetric.setDevDepartmentName(ruleMetricResponse.getDevDepartmentName());
            excelRuleMetric.setOpsDepartmentName(ruleMetricResponse.getOpsDepartmentName());
            excelRuleMetric.setDepartmentCode(ruleMetricResponse.getDepartmentCode());
            excelRuleMetric.setDepartmentName(ruleMetricResponse.getDepartmentName());
            excelRuleMetric.setAvailable(ruleMetricResponse.getAvailable());
            excelRuleMetric.setEnCode(ruleMetricResponse.getEnCode());
            excelRuleMetric.setType(ruleMetricResponse.getType());
            excelRuleMetrics.add(excelRuleMetric);
        }
        String fileName = "batch_metrics_export_" + FILE_DATE_FORMATTER.format(new Date());
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.setContentType("application/octet-stream");
        response.addHeader("Content-Disposition", "attachment;filename*=UTF-8''" + fileName);
        response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
        OutputStream outputStream = response.getOutputStream();
        writeExcelToOutput(excelRuleMetrics, outputStream);
        outputStream.flush();
    } else {
        throw new PermissionDeniedRequestException("HAS_NO_PERMISSION_TO_ACCESS", 403);
    }
    LOGGER.info("Succeed to download all rule metrics in type of excel");
    return null;
}
Also used : StringUtils(org.apache.commons.lang.StringUtils) FormDataContentDisposition(org.glassfish.jersey.media.multipart.FormDataContentDisposition) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) HiveRuleDetail(com.webank.wedatasphere.qualitis.project.response.HiveRuleDetail) RuleMetricValueResponse(com.webank.wedatasphere.qualitis.response.RuleMetricValueResponse) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) AlarmConfig(com.webank.wedatasphere.qualitis.rule.entity.AlarmConfig) RuleMetricDao(com.webank.wedatasphere.qualitis.dao.RuleMetricDao) HttpUtils(com.webank.wedatasphere.qualitis.util.HttpUtils) GetAllResponse(com.webank.wedatasphere.qualitis.response.GetAllResponse) RuleMetricBussCodeEnum(com.webank.wedatasphere.qualitis.constant.RuleMetricBussCodeEnum) Context(javax.ws.rs.core.Context) ExcelReader(com.alibaba.excel.ExcelReader) PageRequest(com.webank.wedatasphere.qualitis.request.PageRequest) ModifyRuleMetricRequest(com.webank.wedatasphere.qualitis.request.ModifyRuleMetricRequest) Department(com.webank.wedatasphere.qualitis.entity.Department) Set(java.util.Set) RuleMetricDepartmentUser(com.webank.wedatasphere.qualitis.entity.RuleMetricDepartmentUser) Collectors(java.util.stream.Collectors) ExecutionManagerImpl(com.webank.wedatasphere.qualitis.submitter.impl.ExecutionManagerImpl) ExcelTypeEnum(com.alibaba.excel.support.ExcelTypeEnum) List(java.util.List) RuleMetricResponse(com.webank.wedatasphere.qualitis.response.RuleMetricResponse) Sheet(com.alibaba.excel.metadata.Sheet) CommonChecker(com.webank.wedatasphere.qualitis.project.request.CommonChecker) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) RoleDefaultTypeEnum(com.webank.wedatasphere.qualitis.rule.constant.RoleDefaultTypeEnum) UserRole(com.webank.wedatasphere.qualitis.entity.UserRole) FastDateFormat(org.apache.commons.lang.time.FastDateFormat) RuleMetricTypeConfigDao(com.webank.wedatasphere.qualitis.dao.RuleMetricTypeConfigDao) RuleMetricQueryRequest(com.webank.wedatasphere.qualitis.request.RuleMetricQueryRequest) UserRoleDao(com.webank.wedatasphere.qualitis.dao.UserRoleDao) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule) ArrayList(java.util.ArrayList) ExcelWriter(com.alibaba.excel.ExcelWriter) RuleMetricConditionResponse(com.webank.wedatasphere.qualitis.response.RuleMetricConditionResponse) RuleMetricLevelEnum(com.webank.wedatasphere.qualitis.constant.RuleMetricLevelEnum) HttpServletRequest(javax.servlet.http.HttpServletRequest) User(com.webank.wedatasphere.qualitis.entity.User) ExcelRuleMetricListener(com.webank.wedatasphere.qualitis.excel.ExcelRuleMetricListener) ExcelRuleMetric(com.webank.wedatasphere.qualitis.project.excel.ExcelRuleMetric) CollectionUtils(org.apache.commons.collections.CollectionUtils) Propagation(org.springframework.transaction.annotation.Propagation) Service(org.springframework.stereotype.Service) RuleMetricService(com.webank.wedatasphere.qualitis.service.RuleMetricService) TaskResult(com.webank.wedatasphere.qualitis.entity.TaskResult) DownloadRuleMetricRequest(com.webank.wedatasphere.qualitis.request.DownloadRuleMetricRequest) RoleService(com.webank.wedatasphere.qualitis.service.RoleService) OutputStream(java.io.OutputStream) Logger(org.slf4j.Logger) RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) HttpServletResponse(javax.servlet.http.HttpServletResponse) RuleMetricTypeConfig(com.webank.wedatasphere.qualitis.entity.RuleMetricTypeConfig) IOException(java.io.IOException) AddRuleMetricRequest(com.webank.wedatasphere.qualitis.request.AddRuleMetricRequest) DataInfo(com.webank.wedatasphere.qualitis.metadata.response.DataInfo) URLEncoder(java.net.URLEncoder) ExcelSheetName(com.webank.wedatasphere.qualitis.project.constant.ExcelSheetName) RuleDao(com.webank.wedatasphere.qualitis.rule.dao.RuleDao) RuleMetricDepartmentUserDao(com.webank.wedatasphere.qualitis.dao.RuleMetricDepartmentUserDao) UserDao(com.webank.wedatasphere.qualitis.dao.UserDao) TaskResultDao(com.webank.wedatasphere.qualitis.dao.TaskResultDao) WriteExcelException(com.webank.wedatasphere.qualitis.rule.exception.WriteExcelException) InputStream(java.io.InputStream) Transactional(org.springframework.transaction.annotation.Transactional) ExcelRuleMetric(com.webank.wedatasphere.qualitis.project.excel.ExcelRuleMetric) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) PermissionDeniedRequestException(com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) Date(java.util.Date) RuleMetricResponse(com.webank.wedatasphere.qualitis.response.RuleMetricResponse) PageRequest(com.webank.wedatasphere.qualitis.request.PageRequest)

Aggregations

ExcelReader (com.alibaba.excel.ExcelReader)2 Sheet (com.alibaba.excel.metadata.Sheet)2 ExcelRuleMetricListener (com.webank.wedatasphere.qualitis.excel.ExcelRuleMetricListener)2 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)2 UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)2 ExcelRuleMetric (com.webank.wedatasphere.qualitis.project.excel.ExcelRuleMetric)2 AddRuleMetricRequest (com.webank.wedatasphere.qualitis.request.AddRuleMetricRequest)2 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)2 ExcelWriter (com.alibaba.excel.ExcelWriter)1 ExcelTypeEnum (com.alibaba.excel.support.ExcelTypeEnum)1 RuleMetricBussCodeEnum (com.webank.wedatasphere.qualitis.constant.RuleMetricBussCodeEnum)1 RuleMetricLevelEnum (com.webank.wedatasphere.qualitis.constant.RuleMetricLevelEnum)1 RuleMetricDao (com.webank.wedatasphere.qualitis.dao.RuleMetricDao)1 RuleMetricDepartmentUserDao (com.webank.wedatasphere.qualitis.dao.RuleMetricDepartmentUserDao)1 RuleMetricTypeConfigDao (com.webank.wedatasphere.qualitis.dao.RuleMetricTypeConfigDao)1 TaskResultDao (com.webank.wedatasphere.qualitis.dao.TaskResultDao)1 UserDao (com.webank.wedatasphere.qualitis.dao.UserDao)1 UserRoleDao (com.webank.wedatasphere.qualitis.dao.UserRoleDao)1 Department (com.webank.wedatasphere.qualitis.entity.Department)1 RuleMetric (com.webank.wedatasphere.qualitis.entity.RuleMetric)1