Skip to content

Commit

Permalink
Merge pull request #2514 from bakdata/fix/undo-usage-of-shared-interface
Browse files Browse the repository at this point in the history
Undo some unnecessary interface methods from ResultProviders.
  • Loading branch information
awildturtok authored May 25, 2022
2 parents b198269 + e9553de commit 84b6e3b
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public Stream<ExecutionStatus> getQueriesFiltered(Dataset datasetId, UriBuilder
subject
);
if (mq.isReadyToDownload(datasetAbilities)) {
setDownloadUrls(status, config.getResultProviders(), mq, uriBuilder, allProviders);
status.setResultUrls(getDownloadUrls(config.getResultProviders(), mq, uriBuilder, allProviders));
}
return status;
});
Expand All @@ -223,23 +223,18 @@ public Stream<ExecutionStatus> getQueriesFiltered(Dataset datasetId, UriBuilder
* Sets the result urls for the given result renderer. Result urls are only rendered for providers that match the
* result type of the execution.
*
* @param status The status that is edited.
* @param renderer The renderer that are requested for a result url generation.
* @param exec The execution that is used for generating the url
* @param uriBuilder The Uribuilder with the base configuration to generate the urls
* @param allProviders If true, forces {@link ResultRendererProvider} to return an URL if possible.
* @param <S> The type of the provided and returned status
* @return The modified status
*/
public static <S extends ExecutionStatus> S setDownloadUrls(S status, List<ResultRendererProvider> renderer, ManagedExecution<?> exec, UriBuilder uriBuilder, boolean allProviders) {
public static List<URL> getDownloadUrls(List<ResultRendererProvider> renderer, ManagedExecution<?> exec, UriBuilder uriBuilder, boolean allProviders) {

List<URL> resultUrls = renderer.stream()
.map(r -> r.generateResultURLs(exec, uriBuilder.clone(), allProviders))
.flatMap(Collection::stream).collect(Collectors.toList());
return renderer.stream()
.map(r -> r.generateResultURLs(exec, uriBuilder.clone(), allProviders))
.flatMap(Collection::stream).collect(Collectors.toList());

status.setResultUrls(resultUrls);

return status;
}


Expand Down Expand Up @@ -346,7 +341,7 @@ public FullExecutionStatus getQueryFullStatus(ManagedExecution<?> query, Subject
final FullExecutionStatus status = query.buildStatusFull(storage, subject, datasetRegistry, config);

if (query.isReadyToDownload(datasetAbilities)) {
setDownloadUrls(status, config.getResultProviders(), query, url, allProviders);
status.setResultUrls(getDownloadUrls(config.getResultProviders(), query, url, allProviders));
}
return status;
}
Expand Down Expand Up @@ -425,11 +420,9 @@ public List<URL> getSingleEntityExport(Subject subject, UriBuilder uriBuilder, S
throw ConqueryError.ContextError.fromErrorInfo(execution.getError());
}


// Use the provided format name to find the respective provider.
return config.getResultProviders().stream()
.map(resultRendererProvider -> resultRendererProvider.generateResultURLs(execution, uriBuilder.clone(), true))
.flatMap(Collection::stream)
.collect(Collectors.toList());
return getDownloadUrls(config.getResultProviders(), execution, uriBuilder, true);

}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package com.bakdata.conquery.io.result.ResultRender;

import java.net.URL;
import java.nio.charset.Charset;
import java.util.Collection;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import com.bakdata.conquery.commands.ManagerNode;
import com.bakdata.conquery.io.cps.CPSBase;
import com.bakdata.conquery.models.auth.entities.Subject;
import com.bakdata.conquery.models.datasets.Dataset;
import com.bakdata.conquery.models.execution.ManagedExecution;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import io.dropwizard.jersey.setup.JerseyEnvironment;
Expand All @@ -30,17 +26,5 @@ public interface ResultRendererProvider {
*/
Collection<URL> generateResultURLs(ManagedExecution<?> exec, UriBuilder uriBuilder, boolean allProviders);

/**
* TODO write a bit more to this
* @param subject Subject trying to download the result
* @param exec execution for which the result should be provided
* @param dataset dataset of the execution
* @param pretty if true, use pretty pringting/human readable formats
* @param charset charset to use for encoding strings
* @param onClose Hook to run right after the result is finished downloading
* @return Response object containing data of the executions result.
*/
Response createResult(Subject subject, ManagedExecution<?> exec, Dataset dataset, boolean pretty, Charset charset, Runnable onClose);

void registerResultResource(JerseyEnvironment environment, ManagerNode manager);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static <E extends ManagedExecution<?> & SingleTableResult> Response getAr
boolean pretty,
String fileExtension,
MediaType mediaType,
ConqueryConfig config, Runnable onClose) {
ConqueryConfig config) {

final Namespace namespace = datasetRegistry.get(dataset.getId());

Expand Down Expand Up @@ -99,7 +99,6 @@ public static <E extends ManagedExecution<?> & SingleTableResult> Response getAr
}
finally {
log.trace("DONE downloading data for `{}`", exec.getId());
onClose.run();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -58,8 +57,7 @@ public Collection<URL> generateResultURLs(ManagedExecution<?> exec, UriBuilder u
return List.of(ResultArrowFileResource.getDownloadURL(uriBuilder, (ManagedExecution<?> & SingleTableResult) exec));
}

@Override
public Response createResult(Subject subject, ManagedExecution<?> exec, Dataset dataset, boolean pretty, Charset charset, Runnable onClose) {
public Response createResult(Subject subject, ManagedExecution<?> exec, Dataset dataset, boolean pretty) {
return getArrowResult(
(output) -> (root) -> new ArrowFileWriter(root, new DictionaryProvider.MapDictionaryProvider(), Channels.newChannel(output)),
subject,
Expand All @@ -69,7 +67,7 @@ public Response createResult(Subject subject, ManagedExecution<?> exec, Dataset
pretty,
FILE_EXTENTION_ARROW_FILE,
MEDIA_TYPE,
config, onClose
config
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -56,8 +55,7 @@ public Collection<URL> generateResultURLs(ManagedExecution<?> exec, UriBuilder u
return List.of(ResultArrowStreamResource.getDownloadURL(uriBuilder, exec));
}

@Override
public Response createResult(Subject subject, ManagedExecution<?> exec, Dataset dataset, boolean pretty, Charset charset, Runnable onClose) {
public Response createResult(Subject subject, ManagedExecution<?> exec, Dataset dataset, boolean pretty) {
return getArrowResult(
(output) -> (root) -> new ArrowStreamWriter(root, new DictionaryProvider.MapDictionaryProvider(), output),
subject,
Expand All @@ -67,8 +65,7 @@ public Response createResult(Subject subject, ManagedExecution<?> exec, Dataset
pretty,
FILE_EXTENTION_ARROW_STREAM,
MEDIA_TYPE,
config,
onClose
config
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ public Collection<URL> generateResultURLs(ManagedExecution<?> exec, UriBuilder u
return List.of(ResultCsvResource.getDownloadURL(uriBuilder, (ManagedExecution<?> & SingleTableResult) exec));
}

@Override
public Response createResult(Subject subject, ManagedExecution<?> execRaw, Dataset dataset, boolean pretty, Charset charset, Runnable onClose) {
public Response createResult(Subject subject, ManagedExecution<?> execRaw, Dataset dataset, boolean pretty, Charset charset) {

final ManagedQuery exec = (ManagedQuery) execRaw;

Expand Down Expand Up @@ -114,7 +113,7 @@ public Response createResult(Subject subject, ManagedExecution<?> execRaw, Datas
throw new WebApplicationException("Failed to load result", e);
}
finally {
onClose.run();
log.trace("FINISHED downloading {}", exec.getId());
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -36,9 +35,11 @@
import io.dropwizard.jersey.setup.JerseyEnvironment;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

@Data
@CPSType(base = ResultRendererProvider.class, id = "XLSX")
@Slf4j
public class ExcelResultProvider implements ResultRendererProvider {

// Media type according to https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
Expand Down Expand Up @@ -77,8 +78,7 @@ public void registerResultResource(JerseyEnvironment environment, ManagerNode ma
environment.register(ResultExcelResource.class);
}

@Override
public Response createResult(Subject subject, ManagedExecution<?> exec, Dataset dataset, boolean pretty, Charset charset, Runnable onClose) {
public Response createResult(Subject subject, ManagedExecution<?> exec, Dataset dataset, boolean pretty) {
ConqueryMDC.setLocation(subject.getName());
final Namespace namespace = datasetRegistry.get(dataset.getId());

Expand All @@ -100,16 +100,12 @@ public Response createResult(Subject subject, ManagedExecution<?> exec, Dataset
ExcelRenderer excelRenderer = new ExcelRenderer(config.getExcel(), settings);

StreamingOutput out = output -> {
try {
excelRenderer.renderToStream(
config.getFrontend().getQueryUpload().getIdResultInfos(),
(ManagedExecution<?> & SingleTableResult) exec,
output
);
}
finally {
onClose.run();
}
excelRenderer.renderToStream(
config.getFrontend().getQueryUpload().getIdResultInfos(),
(ManagedExecution<?> & SingleTableResult) exec,
output
);
log.trace("FINISHED downloading {}", exec.getId());
};

return makeResponseWithFileName(out, exec.getLabelWithoutAutoLabelSuffix(), "xlsx", MEDIA_TYPE, ResultUtil.ContentDispositionOption.ATTACHMENT);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bakdata.conquery.resources.api;

import static com.bakdata.conquery.io.result.ResultUtil.checkSingleTableResult;
import static com.bakdata.conquery.io.result.ResultUtil.determineCharset;
import static com.bakdata.conquery.resources.ResourceConstants.*;

import java.net.MalformedURLException;
Expand Down Expand Up @@ -43,12 +42,11 @@ public Response get(
@PathParam(DATASET) Dataset dataset,
@PathParam(QUERY) ManagedExecution<?> query,
@HeaderParam("subject-agent") String userAgent,
@QueryParam("charset") String queryCharset,
@QueryParam("pretty") Optional<Boolean> pretty) {

checkSingleTableResult(query);
log.info("Result for {} download on dataset {} by subject {} ({}).", query.getId(), dataset.getId(), subject.getId(), subject.getName());
return processor.createResult(subject, query, dataset, pretty.orElse(false), determineCharset(userAgent, queryCharset), () -> {});
return processor.createResult(subject, query, dataset, pretty.orElse(false));
}

public static <E extends ManagedExecution<?> & SingleTableResult> URL getDownloadURL(UriBuilder uriBuilder, E exec) throws MalformedURLException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bakdata.conquery.resources.api;

import static com.bakdata.conquery.io.result.ResultUtil.checkSingleTableResult;
import static com.bakdata.conquery.io.result.ResultUtil.determineCharset;
import static com.bakdata.conquery.resources.ResourceConstants.*;

import java.net.MalformedURLException;
Expand Down Expand Up @@ -50,15 +49,14 @@ public static URL getDownloadURL(UriBuilder uriBuilder, ManagedExecution<?> exec
@Path("{" + QUERY + "}." + FILE_EXTENTION_ARROW_STREAM)
@Produces(AdditionalMediaTypes.ARROW_STREAM)
public Response get(
@Auth Subject subject,
@PathParam(DATASET) Dataset dataset,
@PathParam(QUERY) ManagedExecution<?> execution,
@HeaderParam("subject-agent") String userAgent,
@QueryParam("charset") String queryCharset,
@QueryParam("pretty") Optional<Boolean> pretty)
@Auth Subject subject,
@PathParam(DATASET) Dataset dataset,
@PathParam(QUERY) ManagedExecution<?> execution,
@HeaderParam("subject-agent") String userAgent,
@QueryParam("pretty") Optional<Boolean> pretty)
{
checkSingleTableResult(execution);
log.info("Result for {} download on dataset {} by subject {} ({}).", execution, dataset, subject.getId(), subject.getName());
return processor.createResult(subject, execution, dataset, pretty.orElse(false), determineCharset(userAgent, queryCharset), () -> {});
return processor.createResult(subject, execution, dataset, pretty.orElse(false));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public Response getAsCsv(
@QueryParam("pretty") Optional<Boolean> pretty) {
checkSingleTableResult(execution);
log.info("Result for {} download on dataset {} by subject {} ({}).", execution, dataset.getId(), subject.getId(), subject.getName());
return processor.createResult(subject, execution, dataset, pretty.orElse(Boolean.TRUE), determineCharset(userAgent, queryCharset), () -> {
});
return processor.createResult(subject, execution, dataset, pretty.orElse(Boolean.TRUE), determineCharset(userAgent, queryCharset));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bakdata.conquery.resources.api;

import static com.bakdata.conquery.io.result.ResultUtil.checkSingleTableResult;
import static com.bakdata.conquery.io.result.ResultUtil.determineCharset;
import static com.bakdata.conquery.resources.ResourceConstants.DATASET;
import static com.bakdata.conquery.resources.ResourceConstants.QUERY;

Expand Down Expand Up @@ -37,21 +36,19 @@ public class ResultExcelResource {

@Inject
private ExcelResultProvider processor;

@GET
@Path("{" + QUERY + "}.xlsx")
@Produces(AdditionalMediaTypes.EXCEL)
public Response get(
@Auth Subject subject,
@PathParam(DATASET) Dataset dataset,
@PathParam(QUERY) ManagedExecution<?> execution,
@HeaderParam("subject-agent") String userAgent,
@QueryParam("charset") String queryCharset,
@QueryParam("pretty") Optional<Boolean> pretty) {
@Auth Subject subject,
@PathParam(DATASET) Dataset dataset,
@PathParam(QUERY) ManagedExecution<?> execution,
@HeaderParam("subject-agent") String userAgent,
@QueryParam("pretty") Optional<Boolean> pretty) {
checkSingleTableResult(execution);
log.info("Result for {} download on dataset {} by subject {} ({}).", execution.getId(), dataset, subject.getId(), subject.getName());
return processor.createResult(subject, execution, dataset, pretty.orElse(true), determineCharset(userAgent, queryCharset), () -> {
});
return processor.createResult(subject, execution, dataset, pretty.orElse(true));
}

public static <E extends ManagedExecution<?> & SingleTableResult> URL getDownloadURL(UriBuilder uriBuilder, E exec) throws MalformedURLException {
Expand Down

0 comments on commit 84b6e3b

Please sign in to comment.