Search in sources :

Example 16 with TableExport

use of com.yahoo.elide.async.models.TableExport in project elide by yahoo.

the class JsonAPITableExportOperationTest method dataPrep.

/**
 * Prepping and Storing an TableExport entry to be queried later on.
 * @throws IOException  IOException
 */
private void dataPrep() throws IOException {
    TableExport temp = new TableExport();
    DataStoreTransaction tx = dataStore.beginTransaction();
    RequestScope scope = new RequestScope(null, null, NO_VERSION, null, tx, user, null, Collections.emptyMap(), UUID.randomUUID(), elide.getElideSettings());
    tx.save(temp, scope);
    tx.commit(scope);
    tx.close();
}
Also used : TableExport(com.yahoo.elide.async.models.TableExport) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) RequestScope(com.yahoo.elide.core.RequestScope)

Example 17 with TableExport

use of com.yahoo.elide.async.models.TableExport in project elide by yahoo.

the class TableExportOperation method call.

@Override
public AsyncAPIResult call() {
    log.debug("TableExport Object from request: {}", exportObj);
    Elide elide = service.getElide();
    TableExportResult exportResult = new TableExportResult();
    UUID requestId = UUID.fromString(exportObj.getRequestId());
    try (DataStoreTransaction tx = elide.getDataStore().beginTransaction()) {
        // Do Not Cache Export Results
        Map<String, List<String>> requestHeaders = new HashMap<String, List<String>>();
        requestHeaders.put("bypasscache", new ArrayList<String>(Arrays.asList("true")));
        RequestScope requestScope = getRequestScope(exportObj, scope, tx, requestHeaders);
        Collection<EntityProjection> projections = getProjections(exportObj, requestScope);
        validateProjections(projections);
        EntityProjection projection = projections.iterator().next();
        Observable<PersistentResource> observableResults = Observable.empty();
        elide.getTransactionRegistry().addRunningTransaction(requestId, tx);
        // TODO - we need to add the baseUrlEndpoint to the queryObject.
        // TODO - Can we have projectionInfo as null?
        requestScope.setEntityProjection(projection);
        if (projection != null) {
            projection.setPagination(null);
            observableResults = PersistentResource.loadRecords(projection, Collections.emptyList(), requestScope);
        }
        Observable<String> results = Observable.empty();
        String preResult = formatter.preFormat(projection, exportObj);
        results = observableResults.map(resource -> {
            this.recordNumber++;
            return formatter.format(resource, recordNumber);
        });
        String postResult = formatter.postFormat(projection, exportObj);
        // Stitch together Pre-Formatted, Formatted, Post-Formatted results of Formatter in single observable.
        Observable<String> interimResults = concatStringWithObservable(preResult, results, true);
        Observable<String> finalResults = concatStringWithObservable(postResult, interimResults, false);
        TableExportResult result = storeResults(exportObj, engine, finalResults);
        if (result != null && result.getMessage() != null) {
            throw new IllegalStateException(result.getMessage());
        }
        exportResult.setUrl(new URL(generateDownloadURL(exportObj, scope)));
        exportResult.setRecordCount(recordNumber);
        tx.flush(requestScope);
        elide.getAuditLogger().commit();
        tx.commit(requestScope);
    } catch (BadRequestException e) {
        exportResult.setMessage(e.getMessage());
    } catch (MalformedURLException e) {
        exportResult.setMessage("Download url generation failure.");
    } catch (IOException e) {
        log.error("IOException during TableExport", e);
        exportResult.setMessage(e.getMessage());
    } catch (Exception e) {
        exportResult.setMessage(e.getMessage());
    } finally {
        // Follows same flow as GraphQL. The query may result in failure but request was successfully processed.
        exportResult.setHttpStatus(200);
        exportResult.setCompletedOn(new Date());
        elide.getTransactionRegistry().removeRunningTransaction(requestId);
        elide.getAuditLogger().clear();
    }
    return exportResult;
}
Also used : Arrays(java.util.Arrays) Getter(lombok.Getter) ResultStorageEngine(com.yahoo.elide.async.service.storageengine.ResultStorageEngine) AsyncAPIResult(com.yahoo.elide.async.models.AsyncAPIResult) URL(java.net.URL) Date(java.util.Date) SingleRootProjectionValidator(com.yahoo.elide.async.export.validator.SingleRootProjectionValidator) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) ArrayList(java.util.ArrayList) Validator(com.yahoo.elide.async.export.validator.Validator) Map(java.util.Map) PersistentResource(com.yahoo.elide.core.PersistentResource) Observable(io.reactivex.Observable) AsyncAPI(com.yahoo.elide.async.models.AsyncAPI) TableExport(com.yahoo.elide.async.models.TableExport) RequestScope(com.yahoo.elide.core.RequestScope) Elide(com.yahoo.elide.Elide) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) FileExtensionType(com.yahoo.elide.async.models.FileExtensionType) MalformedURLException(java.net.MalformedURLException) Collection(java.util.Collection) EntityProjection(com.yahoo.elide.core.request.EntityProjection) IOException(java.io.IOException) TableExportFormatter(com.yahoo.elide.async.export.formatter.TableExportFormatter) UUID(java.util.UUID) TableExportResult(com.yahoo.elide.async.models.TableExportResult) AsyncExecutorService(com.yahoo.elide.async.service.AsyncExecutorService) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Collections(java.util.Collections) EntityProjection(com.yahoo.elide.core.request.EntityProjection) PersistentResource(com.yahoo.elide.core.PersistentResource) MalformedURLException(java.net.MalformedURLException) HashMap(java.util.HashMap) IOException(java.io.IOException) RequestScope(com.yahoo.elide.core.RequestScope) TableExportResult(com.yahoo.elide.async.models.TableExportResult) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Date(java.util.Date) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) ArrayList(java.util.ArrayList) List(java.util.List) Elide(com.yahoo.elide.Elide) UUID(java.util.UUID)

Example 18 with TableExport

use of com.yahoo.elide.async.models.TableExport in project elide by yahoo.

the class GraphQLTableExportOperationTest method testProcessQueryWithMultipleProjection.

@Test
public void testProcessQueryWithMultipleProjection() {
    TableExport queryObj = new TableExport();
    String query = "{\"query\":\"{ tableExport { edges { node { principalName } } } asyncQuery { edges { node { principalName } } } }\",\"variables\":null}";
    String id = "edc4a871-dff2-4094-804e-d80075cf827d";
    queryObj.setId(id);
    queryObj.setQuery(query);
    queryObj.setQueryType(QueryType.GRAPHQL_V1_0);
    queryObj.setResultType(ResultType.CSV);
    GraphQLTableExportOperation graphQLOperation = new GraphQLTableExportOperation(new JSONExportFormatter(elide), asyncExecutorService, queryObj, requestScope, engine);
    TableExportResult queryResultObj = (TableExportResult) graphQLOperation.call();
    assertEquals(200, queryResultObj.getHttpStatus());
    assertEquals("Export is only supported for single Query with one root projection.", queryResultObj.getMessage());
    assertNull(queryResultObj.getRecordCount());
    assertNull(queryResultObj.getUrl());
}
Also used : TableExport(com.yahoo.elide.async.models.TableExport) JSONExportFormatter(com.yahoo.elide.async.export.formatter.JSONExportFormatter) TableExportResult(com.yahoo.elide.async.models.TableExportResult) Test(org.junit.jupiter.api.Test)

Example 19 with TableExport

use of com.yahoo.elide.async.models.TableExport in project elide by yahoo.

the class GraphQLTableExportOperationTest method testProcessQueryWithRelationship.

@Test
public void testProcessQueryWithRelationship() {
    TableExport queryObj = new TableExport();
    String query = "{\"query\":\"{ group { edges { node { name products {edges { node { name } } } } } } }\", \"variables\":null}";
    String id = "edc4a871-dff2-4194-804e-d80075cf827d";
    queryObj.setId(id);
    queryObj.setQuery(query);
    queryObj.setQueryType(QueryType.GRAPHQL_V1_0);
    queryObj.setResultType(ResultType.CSV);
    GraphQLTableExportOperation graphQLOperation = new GraphQLTableExportOperation(new JSONExportFormatter(elide), asyncExecutorService, queryObj, requestScope, engine);
    TableExportResult queryResultObj = (TableExportResult) graphQLOperation.call();
    assertEquals(200, queryResultObj.getHttpStatus());
    assertEquals("Export is not supported for Query that requires traversing Relationships.", queryResultObj.getMessage());
    assertNull(queryResultObj.getRecordCount());
    assertNull(queryResultObj.getUrl());
}
Also used : TableExport(com.yahoo.elide.async.models.TableExport) JSONExportFormatter(com.yahoo.elide.async.export.formatter.JSONExportFormatter) TableExportResult(com.yahoo.elide.async.models.TableExportResult) Test(org.junit.jupiter.api.Test)

Example 20 with TableExport

use of com.yahoo.elide.async.models.TableExport in project elide by yahoo.

the class GraphQLTableExportOperationTest method testProcessBadQuery.

@Test
public void testProcessBadQuery() throws IOException {
    dataPrep();
    TableExport queryObj = new TableExport();
    String query = "{\"query\":\"{ tableExport { edges { node { id principalName}  } }\",\"variables\":null}";
    String id = "edc4a871-dff2-4054-804e-d80075cf827d";
    queryObj.setId(id);
    queryObj.setQuery(query);
    queryObj.setQueryType(QueryType.GRAPHQL_V1_0);
    queryObj.setResultType(ResultType.CSV);
    GraphQLTableExportOperation graphQLOperation = new GraphQLTableExportOperation(new JSONExportFormatter(elide), asyncExecutorService, queryObj, requestScope, engine);
    TableExportResult queryResultObj = (TableExportResult) graphQLOperation.call();
    assertEquals(200, queryResultObj.getHttpStatus());
    assertEquals("Bad Request Body'Can't parse query: { tableExport { edges { node { id principalName}  } }'", queryResultObj.getMessage());
}
Also used : TableExport(com.yahoo.elide.async.models.TableExport) JSONExportFormatter(com.yahoo.elide.async.export.formatter.JSONExportFormatter) TableExportResult(com.yahoo.elide.async.models.TableExportResult) Test(org.junit.jupiter.api.Test)

Aggregations

TableExport (com.yahoo.elide.async.models.TableExport)28 Test (org.junit.jupiter.api.Test)20 TableExportResult (com.yahoo.elide.async.models.TableExportResult)13 EntityProjection (com.yahoo.elide.core.request.EntityProjection)11 JSONExportFormatter (com.yahoo.elide.async.export.formatter.JSONExportFormatter)10 Attribute (com.yahoo.elide.core.request.Attribute)9 LinkedHashSet (java.util.LinkedHashSet)8 PersistentResource (com.yahoo.elide.core.PersistentResource)4 FileExtensionType (com.yahoo.elide.async.models.FileExtensionType)3 RequestScope (com.yahoo.elide.core.RequestScope)3 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)3 Resource (com.yahoo.elide.jsonapi.models.Resource)3 Observable (io.reactivex.Observable)3 LinkedHashMap (java.util.LinkedHashMap)3 TableExportFormatter (com.yahoo.elide.async.export.formatter.TableExportFormatter)2 AsyncAPIResult (com.yahoo.elide.async.models.AsyncAPIResult)2 IOException (java.io.IOException)2 Iterator (java.util.Iterator)2 Singleton (javax.inject.Singleton)2 Getter (lombok.Getter)2