Search in sources :

Example 51 with UnExpectedRequestException

use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException 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 52 with UnExpectedRequestException

use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.

the class RuleMetricServiceImpl method getRulesByRuleMetric.

@Override
public DataInfo<HiveRuleDetail> getRulesByRuleMetric(long id, int page, int size) throws UnExpectedRequestException {
    DataInfo<HiveRuleDetail> dataInfo = new DataInfo<>();
    if (id <= 0) {
        throw new UnExpectedRequestException("{&REQUEST_CAN_NOT_BE_NULL}");
    }
    // Check rule metric existence.
    RuleMetric ruleMetricInDb = ruleMetricDao.findById(id);
    if (ruleMetricInDb == null) {
        throw new UnExpectedRequestException("Rule Metric ID [" + id + "] {&DOES_NOT_EXIST}");
    }
    List<Long> ruleIds = taskResultDao.findRuleByRuleMetric(ruleMetricInDb.getId(), page, size).stream().distinct().collect(Collectors.toList());
    int total = taskResultDao.countRuleByRuleMetric(ruleMetricInDb.getId());
    List<HiveRuleDetail> result = new ArrayList<>(ruleIds.size());
    for (Long ruleId : ruleIds) {
        Rule ruleInDb = ruleDao.findById(ruleId);
        if (ruleInDb == null) {
            total--;
            continue;
        }
        List<Long> ruleMetricIds = ruleInDb.getAlarmConfigs().stream().map(AlarmConfig::getRuleMetric).filter(ruleMetric -> ruleMetric != null).map(RuleMetric::getId).collect(Collectors.toList());
        if (ruleInDb != null && ruleMetricIds.contains(id)) {
            HiveRuleDetail hiveRuleDetail = new HiveRuleDetail(ruleInDb);
            result.add(hiveRuleDetail);
        }
    }
    dataInfo.setContent(result);
    dataInfo.setTotalCount(total);
    return dataInfo;
}
Also used : DataInfo(com.webank.wedatasphere.qualitis.metadata.response.DataInfo) ExcelRuleMetric(com.webank.wedatasphere.qualitis.project.excel.ExcelRuleMetric) RuleMetric(com.webank.wedatasphere.qualitis.entity.RuleMetric) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) HiveRuleDetail(com.webank.wedatasphere.qualitis.project.response.HiveRuleDetail) ArrayList(java.util.ArrayList) Rule(com.webank.wedatasphere.qualitis.rule.entity.Rule)

Example 53 with UnExpectedRequestException

use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.

the class MetaDataController method getTablesByDataSource.

@GET
@Path("data_source/tables")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public GeneralResponse<GetAllResponse<TableInfoDetail>> getTablesByDataSource(@QueryParam("clusterName") String clusterName, @QueryParam("proxyUser") String proxyUser, @QueryParam("dataSourceId") Long dataSourceId, @QueryParam("dbName") String dbName) {
    try {
        if (dataSourceId == null || StringUtils.isBlank(dbName) || StringUtils.isBlank(clusterName)) {
            throw new UnExpectedRequestException("Request " + "{&CAN_NOT_BE_NULL_OR_EMPTY}");
        }
        Map response = metaDataService.getTablesByDataSource(clusterName, proxyUser, dataSourceId, dbName);
        GetAllResponse allResponse = new GetAllResponse();
        List<String> tables = (List<String>) response.get("tables");
        List<TableInfoDetail> tableInfoDetails = new ArrayList<>(CollectionUtils.isEmpty(tables) ? 0 : tables.size());
        for (String table : tables) {
            TableInfoDetail tableInfoDetail = new TableInfoDetail(table);
            tableInfoDetails.add(tableInfoDetail);
        }
        allResponse.setTotal(CollectionUtils.isEmpty(tableInfoDetails) ? 0 : tableInfoDetails.size());
        allResponse.setData(tableInfoDetails);
        return new GeneralResponse<>("200", "{&GET_TABLE_SUCCESSFULLY}", allResponse);
    } catch (MetaDataAcquireFailedException e) {
        LOGGER.error(e.getMessage(), e);
        return new GeneralResponse<>("500", e.getMessage(), null);
    } catch (UnExpectedRequestException e) {
        LOGGER.error(e.getMessage(), e);
        return new GeneralResponse<>("500", e.getMessage(), null);
    } catch (Exception e) {
        LOGGER.error(e.getMessage(), e);
        return new GeneralResponse<>("500", "{&FAILED_TO_GET_TABLE_BY_DATABASE}", null);
    }
}
Also used : UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) ArrayList(java.util.ArrayList) ResourceAccessException(org.springframework.web.client.ResourceAccessException) MetaDataAcquireFailedException(com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) MetaDataAcquireFailedException(com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException) ArrayList(java.util.ArrayList) List(java.util.List) GetAllResponse(com.webank.wedatasphere.qualitis.response.GetAllResponse) Map(java.util.Map) CsTableInfoDetail(com.webank.wedatasphere.qualitis.metadata.response.table.CsTableInfoDetail) TableInfoDetail(com.webank.wedatasphere.qualitis.metadata.response.table.TableInfoDetail) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) GET(javax.ws.rs.GET)

Example 54 with UnExpectedRequestException

use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.

the class PermissionServiceImpl method addPermission.

@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<PermissionResponse> addPermission(AddPermissionRequest request) throws UnExpectedRequestException {
    // Check Arguments
    AddPermissionRequest.checkRequest(request);
    String method = request.getMethod().trim();
    String url = request.getUrl().trim();
    // Find permission by method and url
    Permission permissionInDb = permissionDao.findByMethodAndUrl(method, url);
    if (permissionInDb != null) {
        throw new UnExpectedRequestException("{&METHOD_AND_URL_ALREADY_EXIST}, request: " + request);
    }
    Permission newPermission = new Permission();
    newPermission.setMethod(method);
    newPermission.setUrl(url);
    Permission savedPermission = permissionDao.savePermission(newPermission);
    LOGGER.info("Succeed to add permission, id: {}, method: {}, url: {}, current_user: {}", savedPermission.getId(), method, url, HttpUtils.getUserName(httpServletRequest));
    return new GeneralResponse<>("200", "{&ADD_PERMISSION_SUCCESSFULLY}", new PermissionResponse(savedPermission));
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) UserSpecPermission(com.webank.wedatasphere.qualitis.entity.UserSpecPermission) Permission(com.webank.wedatasphere.qualitis.entity.Permission) RolePermission(com.webank.wedatasphere.qualitis.entity.RolePermission) PermissionResponse(com.webank.wedatasphere.qualitis.response.PermissionResponse) Transactional(org.springframework.transaction.annotation.Transactional)

Example 55 with UnExpectedRequestException

use of com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException in project Qualitis by WeBankFinTech.

the class PermissionServiceImpl method deletePermission.

@Override
@Transactional(rollbackFor = { RuntimeException.class, UnExpectedRequestException.class })
public GeneralResponse<?> deletePermission(DeletePermissionRequest request) throws UnExpectedRequestException {
    // Check Arguments
    checkRequest(request);
    Long permissionId = request.getPermissionId();
    // Find permissions by permission id
    Permission permissionInDb = permissionDao.findById(permissionId);
    if (permissionInDb == null) {
        throw new UnExpectedRequestException("{&PERMISSION_ID_NOT_EXIST}, request: " + request);
    }
    List<RolePermission> rolePermissionsInDb = rolePermissionDao.findByPermission(permissionInDb);
    if (null != rolePermissionsInDb && !rolePermissionsInDb.isEmpty()) {
        throw new UnExpectedRequestException("{&DELETE_ERROR_ROLE_PERMISSION_HAS_FOREIGN_KEY}");
    }
    List<UserSpecPermission> userSpecPermissionsInDb = userSpecPermissionDao.findByPermission(permissionInDb);
    if (null != userSpecPermissionsInDb && !userSpecPermissionsInDb.isEmpty()) {
        throw new UnExpectedRequestException("{&DELETE_ERROR_USER_SPEC_PERMISSION_HAS_FOREIGN_KEY}");
    }
    permissionDao.deletePermission(permissionInDb);
    LOGGER.info("Succeed to delete permission, permissionId: {}, method: {}, url: {}, current_user: {}", permissionId, permissionInDb.getMethod(), permissionInDb.getUrl(), HttpUtils.getUserName(httpServletRequest));
    return new GeneralResponse<>("200", "{&DELETE_PERMISSION_SUCCESSFULLY}", null);
}
Also used : GeneralResponse(com.webank.wedatasphere.qualitis.response.GeneralResponse) UnExpectedRequestException(com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException) UserSpecPermission(com.webank.wedatasphere.qualitis.entity.UserSpecPermission) Permission(com.webank.wedatasphere.qualitis.entity.Permission) RolePermission(com.webank.wedatasphere.qualitis.entity.RolePermission) RolePermission(com.webank.wedatasphere.qualitis.entity.RolePermission) UserSpecPermission(com.webank.wedatasphere.qualitis.entity.UserSpecPermission) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

UnExpectedRequestException (com.webank.wedatasphere.qualitis.exception.UnExpectedRequestException)150 GeneralResponse (com.webank.wedatasphere.qualitis.response.GeneralResponse)97 ArrayList (java.util.ArrayList)56 Transactional (org.springframework.transaction.annotation.Transactional)54 Project (com.webank.wedatasphere.qualitis.project.entity.Project)33 User (com.webank.wedatasphere.qualitis.entity.User)32 Rule (com.webank.wedatasphere.qualitis.rule.entity.Rule)32 RuleMetric (com.webank.wedatasphere.qualitis.entity.RuleMetric)25 Map (java.util.Map)24 ClusterInfo (com.webank.wedatasphere.qualitis.entity.ClusterInfo)22 List (java.util.List)22 PermissionDeniedRequestException (com.webank.wedatasphere.qualitis.exception.PermissionDeniedRequestException)19 IOException (java.io.IOException)19 MetaDataAcquireFailedException (com.webank.wedatasphere.qualitis.metadata.exception.MetaDataAcquireFailedException)17 Date (java.util.Date)17 HashMap (java.util.HashMap)17 RuleDataSource (com.webank.wedatasphere.qualitis.rule.entity.RuleDataSource)15 RuleGroup (com.webank.wedatasphere.qualitis.rule.entity.RuleGroup)15 Collectors (java.util.stream.Collectors)15 Template (com.webank.wedatasphere.qualitis.rule.entity.Template)14