use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class ConfigDataStoreTest method updateFile.
protected ConfigFile updateFile(String configRoot, ConfigDataStore store) {
Supplier<String> contentProvider = () -> "{ \n" + " tables: [{ \n" + " name: Test2\n" + " table: test\n" + " schema: test\n" + " measures : [\n" + " {\n" + " name : measure\n" + " type : INTEGER\n" + " definition: 'MAX({{$measure}})'\n" + " }\n" + " ] \n" + " dimensions : [\n" + " {\n" + " name : dimension\n" + " type : TEXT\n" + " definition : '{{$dimension}}'\n" + " }\n" + " ]\n" + " }]\n" + "}";
ConfigFile updatedFile = ConfigFile.builder().type(TABLE).contentProvider(contentProvider).path("models/tables/test.hjson").build();
ConfigDataStoreTransaction tx = store.beginTransaction();
RequestScope scope = mock(RequestScope.class);
tx.save(updatedFile, scope);
tx.flush(scope);
tx.commit(scope);
return updatedFile;
}
use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class ConfigDataStoreTest method testDelete.
@Test
public void testDelete(@TempDir Path configPath) {
String configRoot = configPath.toFile().getPath();
Validator validator = new DynamicConfigValidator(DefaultClassScanner.getInstance(), configRoot);
ConfigDataStore store = new ConfigDataStore(configRoot, validator);
ConfigFile newFile = createFile("test", store, false);
ConfigDataStoreTransaction tx = store.beginTransaction();
RequestScope scope = mock(RequestScope.class);
tx.delete(newFile, scope);
tx.flush(scope);
tx.commit(scope);
ConfigDataStoreTransaction readTx = store.beginReadTransaction();
ConfigFile loaded = readTx.loadObject(EntityProjection.builder().type(ClassType.of(ConfigFile.class)).build(), toId("models/tables/test.hjson", NO_VERSION), scope);
assertNull(loaded);
}
use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class ConfigDataStoreTest method createFile.
protected ConfigFile createFile(String tableName, ConfigDataStore store, boolean validateOnly) {
Supplier<String> contentProvider = () -> String.format("{ \n" + " tables: [{ \n" + " name: %s\n" + " table: test\n" + " schema: test\n" + " measures : [\n" + " {\n" + " name : measure\n" + " type : INTEGER\n" + " definition: 'MAX({{$measure}})'\n" + " }\n" + " ] \n" + " dimensions : [\n" + " {\n" + " name : dimension\n" + " type : TEXT\n" + " definition : '{{$dimension}}'\n" + " }\n" + " ]\n" + " }]\n" + "}", tableName);
ConfigFile newFile = ConfigFile.builder().type(TABLE).contentProvider(contentProvider).path(String.format("models/tables/%s.hjson", tableName)).build();
ConfigDataStoreTransaction tx = store.beginTransaction();
RequestScope scope = mock(RequestScope.class);
if (validateOnly) {
when(scope.getRequestHeaderByName(eq(VALIDATE_ONLY_HEADER))).thenReturn("true");
}
tx.createObject(newFile, scope);
tx.save(newFile, scope);
tx.flush(scope);
tx.commit(scope);
return newFile;
}
use of com.yahoo.elide.core.RequestScope in project elide by yahoo.
the class JSONAPITableExportOperation method getRequestScope.
@Override
public RequestScope getRequestScope(TableExport export, RequestScope scope, DataStoreTransaction tx, Map<String, List<String>> additionalRequestHeaders) {
UUID requestId = UUID.fromString(export.getRequestId());
User user = scope.getUser();
String apiVersion = scope.getApiVersion();
URIBuilder uri;
try {
uri = new URIBuilder(export.getQuery());
} catch (URISyntaxException e) {
throw new BadRequestException(e.getMessage());
}
MultivaluedMap<String, String> queryParams = JSONAPIAsyncQueryOperation.getQueryParams(uri);
// Call with additionalHeader alone
if (scope.getRequestHeaders().isEmpty()) {
return new RequestScope("", JSONAPIAsyncQueryOperation.getPath(uri), apiVersion, null, tx, user, queryParams, additionalRequestHeaders, requestId, getService().getElide().getElideSettings());
}
// Combine additionalRequestHeaders and existing scope's request headers
Map<String, List<String>> finalRequestHeaders = new HashMap<String, List<String>>();
scope.getRequestHeaders().forEach((entry, value) -> finalRequestHeaders.put(entry, value));
// additionalRequestHeaders will override any headers in scope.getRequestHeaders()
additionalRequestHeaders.forEach((entry, value) -> finalRequestHeaders.put(entry, value));
return new RequestScope("", JSONAPIAsyncQueryOperation.getPath(uri), apiVersion, null, tx, user, queryParams, scope.getRequestHeaders(), requestId, getService().getElide().getElideSettings());
}
use of com.yahoo.elide.core.RequestScope 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;
}
Aggregations