Search in sources :

Example 6 with TableExportResult

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

the class JsonAPITableExportOperationTest method testProcessQuery.

@Test
public void testProcessQuery() throws IOException {
    dataPrep();
    TableExport queryObj = new TableExport();
    String query = "/tableExport?sort=principalName&fields=principalName";
    String id = "edc4a871-dff2-4054-804e-d80075cf827d";
    queryObj.setId(id);
    queryObj.setQuery(query);
    queryObj.setQueryType(QueryType.JSONAPI_V1_0);
    queryObj.setResultType(ResultType.CSV);
    JSONAPITableExportOperation jsonAPIOperation = new JSONAPITableExportOperation(new JSONExportFormatter(elide), asyncExecutorService, queryObj, requestScope, engine);
    TableExportResult queryResultObj = (TableExportResult) jsonAPIOperation.call();
    assertEquals(200, queryResultObj.getHttpStatus());
    assertEquals("https://elide.io/export/edc4a871-dff2-4054-804e-d80075cf827d.csv", queryResultObj.getUrl().toString());
    assertEquals(1, queryResultObj.getRecordCount());
    assertNull(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)

Example 7 with TableExportResult

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

the class JsonAPITableExportOperationTest method testProcessBadEntityQuery.

@Test
public void testProcessBadEntityQuery() throws IOException {
    dataPrep();
    TableExport queryObj = new TableExport();
    String query = "/tableExportInvalid?sort=principalName&fields=principalName";
    String id = "edc4a871-dff2-4054-804e-d80075cf827d";
    queryObj.setId(id);
    queryObj.setQuery(query);
    queryObj.setQueryType(QueryType.JSONAPI_V1_0);
    queryObj.setResultType(ResultType.CSV);
    JSONAPITableExportOperation jsonAPIOperation = new JSONAPITableExportOperation(new JSONExportFormatter(elide), asyncExecutorService, queryObj, requestScope, engine);
    TableExportResult queryResultObj = (TableExportResult) jsonAPIOperation.call();
    assertEquals(200, queryResultObj.getHttpStatus());
    assertEquals("Unknown collection tableExportInvalid", 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)

Example 8 with TableExportResult

use of com.yahoo.elide.async.models.TableExportResult 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 9 with TableExportResult

use of com.yahoo.elide.async.models.TableExportResult 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 10 with TableExportResult

use of com.yahoo.elide.async.models.TableExportResult 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)

Aggregations

TableExport (com.yahoo.elide.async.models.TableExport)13 TableExportResult (com.yahoo.elide.async.models.TableExportResult)13 JSONExportFormatter (com.yahoo.elide.async.export.formatter.JSONExportFormatter)10 Test (org.junit.jupiter.api.Test)10 FileExtensionType (com.yahoo.elide.async.models.FileExtensionType)3 Observable (io.reactivex.Observable)3 Getter (lombok.Getter)3 Slf4j (lombok.extern.slf4j.Slf4j)3 IOException (java.io.IOException)2 Iterator (java.util.Iterator)2 Singleton (javax.inject.Singleton)2 Setter (lombok.Setter)2 Elide (com.yahoo.elide.Elide)1 TableExportFormatter (com.yahoo.elide.async.export.formatter.TableExportFormatter)1 SingleRootProjectionValidator (com.yahoo.elide.async.export.validator.SingleRootProjectionValidator)1 Validator (com.yahoo.elide.async.export.validator.Validator)1 AsyncAPI (com.yahoo.elide.async.models.AsyncAPI)1 AsyncAPIResult (com.yahoo.elide.async.models.AsyncAPIResult)1 AsyncExecutorService (com.yahoo.elide.async.service.AsyncExecutorService)1 ResultStorageEngine (com.yahoo.elide.async.service.storageengine.ResultStorageEngine)1