Search in sources :

Example 1 with ErrorType

use of io.prestosql.spi.ErrorType in project hetu-core by openlookeng.

the class QueryResource method getAllQueryInfo.

@GET
public Response getAllQueryInfo(@QueryParam("state") String stateFilter, @QueryParam("failed") String failedFilter, @QueryParam("sort") String sortFilter, @QueryParam("sortOrder") String sortOrder, @QueryParam("search") String searchFilter, @QueryParam("pageNum") Integer pageNum, @QueryParam("pageSize") Integer pageSize, @Context HttpServletRequest servletRequest) {
    // if the user is admin, don't filter results by user.
    Optional<String> filterUser = AccessControlUtil.getUserForFilter(accessControl, serverConfig, servletRequest, groupProvider);
    if (pageNum != null && pageNum <= 0) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    if (pageSize != null && pageSize <= 0) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    String[] states = (stateFilter == null || stateFilter.equals("")) ? new String[0] : stateFilter.split(",");
    String[] failed = (failedFilter == null || failedFilter.equals("")) ? new String[0] : failedFilter.split(",");
    HashMap<QueryState, Boolean> statesMap = new HashMap<>();
    for (String state : states) {
        try {
            QueryState expectedState = QueryState.valueOf(state.toUpperCase(Locale.ENGLISH));
            statesMap.put(expectedState, true);
        } catch (Exception ex) {
            return Response.status(Status.BAD_REQUEST).build();
        }
    }
    HashMap<ErrorType, Boolean> failedMap = new HashMap<>();
    for (String failedValue : failed) {
        try {
            ErrorType failedState = ErrorType.valueOf(failedValue.toUpperCase(Locale.ENGLISH));
            failedMap.put(failedState, true);
        } catch (Exception ex) {
            return Response.status(Status.BAD_REQUEST).build();
        }
    }
    List<BasicQueryInfo> allQueries = dispatchManager.getQueries();
    List<BasicQueryInfo> filterQueries = new ArrayList<>();
    for (BasicQueryInfo queryInfo : allQueries) {
        if (filterUser(queryInfo, filterUser) && filterState(queryInfo, stateFilter, statesMap, failedFilter, failedMap) && filterSearch(queryInfo, searchFilter)) {
            filterQueries.add(queryInfo);
        }
    }
    // default sort order ascending
    SortOrder sortOrderValue;
    if (sortOrder == null || sortOrder.equals("")) {
        sortOrderValue = SortOrder.ASCENDING;
    } else {
        try {
            sortOrderValue = SortOrder.valueOf(sortOrder.toUpperCase(Locale.ENGLISH));
        } catch (Exception e) {
            return Response.status(Status.BAD_REQUEST).build();
        }
    }
    if (sortFilter != null && !sortFilter.equals("")) {
        try {
            QuerySortFilter sort = QuerySortFilter.valueOf(sortFilter.toUpperCase(Locale.ENGLISH));
            if (sortOrderValue == SortOrder.DESCENDING) {
                filterQueries.sort(Collections.reverseOrder(sort.getCompare()));
            } else {
                filterQueries.sort(sort.getCompare());
            }
        } catch (Exception ex) {
            return Response.status(Status.BAD_REQUEST).build();
        }
    }
    if (pageNum == null || pageSize == null) {
        ImmutableList.Builder<BasicQueryInfo> builder = new ImmutableList.Builder<>();
        for (BasicQueryInfo queryInfo : filterQueries) {
            builder.add(queryInfo);
        }
        return Response.ok(builder.build()).build();
    }
    // pagination
    int total = filterQueries.size();
    if (total == 0) {
        return Response.ok(new QueryResponse(total, filterQueries)).build();
    } else if (total - pageSize * (pageNum - 1) <= 0) {
        return Response.status(Status.BAD_REQUEST).build();
    }
    QueryResponse res;
    int start = (pageNum - 1) * pageSize;
    int end = Math.min(pageNum * pageSize, total);
    List<BasicQueryInfo> subList = filterQueries.subList(start, end);
    res = new QueryResponse(total, subList);
    return Response.ok(res).build();
}
Also used : HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) ClientBuilder(javax.ws.rs.client.ClientBuilder) ArrayList(java.util.ArrayList) QueryState(io.prestosql.execution.QueryState) KillQueryProcedure.createPreemptQueryException(io.prestosql.connector.system.KillQueryProcedure.createPreemptQueryException) KillQueryProcedure.createKillQueryException(io.prestosql.connector.system.KillQueryProcedure.createKillQueryException) NoSuchElementException(java.util.NoSuchElementException) PrestoException(io.prestosql.spi.PrestoException) ErrorType(io.prestosql.spi.ErrorType) GET(javax.ws.rs.GET)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 KillQueryProcedure.createKillQueryException (io.prestosql.connector.system.KillQueryProcedure.createKillQueryException)1 KillQueryProcedure.createPreemptQueryException (io.prestosql.connector.system.KillQueryProcedure.createPreemptQueryException)1 QueryState (io.prestosql.execution.QueryState)1 ErrorType (io.prestosql.spi.ErrorType)1 PrestoException (io.prestosql.spi.PrestoException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 NoSuchElementException (java.util.NoSuchElementException)1 GET (javax.ws.rs.GET)1 ClientBuilder (javax.ws.rs.client.ClientBuilder)1