Search in sources :

Example 1 with PrintSettings

use of com.bakdata.conquery.models.query.PrintSettings in project conquery by bakdata.

the class ResultExcelProcessor method getExcelResult.

public <E extends ManagedExecution<?> & SingleTableResult> Response getExcelResult(Subject subject, E exec, DatasetId datasetId, boolean pretty) {
    ConqueryMDC.setLocation(subject.getName());
    final Namespace namespace = datasetRegistry.get(datasetId);
    Dataset dataset = namespace.getDataset();
    subject.authorize(dataset, Ability.READ);
    subject.authorize(dataset, Ability.DOWNLOAD);
    subject.authorize(exec, Ability.READ);
    IdPrinter idPrinter = config.getFrontend().getQueryUpload().getIdPrinter(subject, exec, namespace);
    final Locale locale = I18n.LOCALE.get();
    PrintSettings settings = new PrintSettings(pretty, locale, datasetRegistry, config, idPrinter::createId);
    ExcelRenderer excelRenderer = new ExcelRenderer(config.getExcel(), settings);
    StreamingOutput out = output -> excelRenderer.renderToStream(config.getFrontend().getQueryUpload().getIdResultInfos(), (ManagedExecution<?> & SingleTableResult) exec, output);
    return makeResponseWithFileName(out, exec.getLabelWithoutAutoLabelSuffix(), "xlsx", MEDIA_TYPE, ResultUtil.ContentDispositionOption.ATTACHMENT);
}
Also used : IdPrinter(com.bakdata.conquery.models.identifiable.mapping.IdPrinter) Locale(java.util.Locale) ResultUtil(com.bakdata.conquery.io.result.ResultUtil) ConqueryMDC(com.bakdata.conquery.util.io.ConqueryMDC) ConqueryConfig(com.bakdata.conquery.models.config.ConqueryConfig) ResultUtil.makeResponseWithFileName(com.bakdata.conquery.io.result.ResultUtil.makeResponseWithFileName) Subject(com.bakdata.conquery.models.auth.entities.Subject) RequiredArgsConstructor(lombok.RequiredArgsConstructor) StreamingOutput(javax.ws.rs.core.StreamingOutput) SingleTableResult(com.bakdata.conquery.models.query.SingleTableResult) ArrayList(java.util.ArrayList) Dataset(com.bakdata.conquery.models.datasets.Dataset) PrintSettings(com.bakdata.conquery.models.query.PrintSettings) List(java.util.List) MediaType(javax.ws.rs.core.MediaType) Response(javax.ws.rs.core.Response) ManagedExecution(com.bakdata.conquery.models.execution.ManagedExecution) Ability(com.bakdata.conquery.models.auth.permissions.Ability) Locale(java.util.Locale) DatasetId(com.bakdata.conquery.models.identifiable.ids.specific.DatasetId) I18n(com.bakdata.conquery.models.i18n.I18n) DatasetRegistry(com.bakdata.conquery.models.worker.DatasetRegistry) IdPrinter(com.bakdata.conquery.models.identifiable.mapping.IdPrinter) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) Namespace(com.bakdata.conquery.models.worker.Namespace) Dataset(com.bakdata.conquery.models.datasets.Dataset) PrintSettings(com.bakdata.conquery.models.query.PrintSettings) StreamingOutput(javax.ws.rs.core.StreamingOutput) SingleTableResult(com.bakdata.conquery.models.query.SingleTableResult) ManagedExecution(com.bakdata.conquery.models.execution.ManagedExecution) Namespace(com.bakdata.conquery.models.worker.Namespace)

Example 2 with PrintSettings

use of com.bakdata.conquery.models.query.PrintSettings in project conquery by bakdata.

the class CsvResultGenerationTest method generateExpectedCSV.

private String generateExpectedCSV(List<EntityResult> results, List<ResultInfo> resultInfos, PrintSettings settings) {
    List<String> expected = new ArrayList<>();
    expected.add(ResultTestUtil.ID_FIELDS.stream().map(info -> info.defaultColumnName(settings)).collect(Collectors.joining(",")) + "," + getResultTypes().stream().map(ResultType::typeInfo).collect(Collectors.joining(",")) + "\n");
    results.stream().map(EntityResult.class::cast).forEach(res -> {
        for (Object[] line : res.listResultLines()) {
            StringJoiner valueJoiner = new StringJoiner(",");
            valueJoiner.add(String.valueOf(res.getEntityId()));
            valueJoiner.add(String.valueOf(res.getEntityId()));
            for (int lIdx = 0; lIdx < line.length; lIdx++) {
                Object val = line[lIdx];
                if (val == null) {
                    valueJoiner.add("");
                    continue;
                }
                ResultInfo info = resultInfos.get(lIdx);
                final String printVal = info.getType().printNullable(settings, val);
                valueJoiner.add(printVal.contains(String.valueOf(CONFIG.getCsv().getDelimeter())) ? "\"" + printVal + "\"" : printVal);
            }
            expected.add(valueJoiner + "\n");
        }
    });
    return expected.stream().collect(Collectors.joining());
}
Also used : ManagedQuery(com.bakdata.conquery.models.query.ManagedQuery) ConqueryConfig(com.bakdata.conquery.models.config.ConqueryConfig) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) EntityResult(com.bakdata.conquery.models.query.results.EntityResult) ArrayList(java.util.ArrayList) PrintSettings(com.bakdata.conquery.models.query.PrintSettings) EntityPrintId(com.bakdata.conquery.models.identifiable.mapping.EntityPrintId) Locale(java.util.Locale) I18n(com.bakdata.conquery.models.i18n.I18n) ResultType(com.bakdata.conquery.models.externalservice.ResultType) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) ResultTestUtil.getTestEntityResults(com.bakdata.conquery.io.result.ResultTestUtil.getTestEntityResults) SelectResultInfo(com.bakdata.conquery.models.query.resultinfo.SelectResultInfo) StringWriter(java.io.StringWriter) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) ResultTestUtil(com.bakdata.conquery.io.result.ResultTestUtil) StringJoiner(java.util.StringJoiner) CQConcept(com.bakdata.conquery.apiv1.query.concept.specific.CQConcept) NotNull(org.jetbrains.annotations.NotNull) NonPersistentStoreFactory(com.bakdata.conquery.util.NonPersistentStoreFactory) ArrayList(java.util.ArrayList) ResultType(com.bakdata.conquery.models.externalservice.ResultType) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) SelectResultInfo(com.bakdata.conquery.models.query.resultinfo.SelectResultInfo) StringJoiner(java.util.StringJoiner)

Example 3 with PrintSettings

use of com.bakdata.conquery.models.query.PrintSettings in project conquery by bakdata.

the class CsvResultGenerationTest method writeAndRead.

@Test
void writeAndRead() throws IOException {
    // Prepare every input data
    PrintSettings printSettings = new PrintSettings(true, Locale.GERMAN, null, CONFIG, (cer) -> EntityPrintId.from(Integer.toString(cer.getEntityId()), Integer.toString(cer.getEntityId())), (selectInfo) -> selectInfo.getSelect().getLabel());
    // The Shard nodes send Object[] but since Jackson is used for deserialization, nested collections are always a list because they are not further specialized
    List<EntityResult> results = getTestEntityResults();
    ManagedQuery mquery = getTestQuery();
    // First we write to the buffer, than we read from it and parse it as TSV
    StringWriter writer = new StringWriter();
    CsvRenderer renderer = new CsvRenderer(CONFIG.getCsv().createWriter(writer), printSettings);
    renderer.toCSV(ResultTestUtil.ID_FIELDS, mquery.getResultInfos(), mquery.streamResults());
    String computed = writer.toString();
    String expected = generateExpectedCSV(results, mquery.getResultInfos(), printSettings);
    log.info("Wrote and than read this csv data: {}", computed);
    assertThat(computed).isNotEmpty();
    assertThat(computed).isEqualTo(expected);
}
Also used : StringWriter(java.io.StringWriter) PrintSettings(com.bakdata.conquery.models.query.PrintSettings) ManagedQuery(com.bakdata.conquery.models.query.ManagedQuery) EntityResult(com.bakdata.conquery.models.query.results.EntityResult) Test(org.junit.jupiter.api.Test)

Example 4 with PrintSettings

use of com.bakdata.conquery.models.query.PrintSettings in project conquery by bakdata.

the class ResultArrowProcessor method getArrowResult.

public static <E extends ManagedExecution<?> & SingleTableResult> Response getArrowResult(Function<OutputStream, Function<VectorSchemaRoot, ArrowWriter>> writerProducer, Subject subject, E exec, Dataset dataset, DatasetRegistry datasetRegistry, boolean pretty, String fileExtension, MediaType mediaType, ConqueryConfig config) {
    final Namespace namespace = datasetRegistry.get(dataset.getId());
    ConqueryMDC.setLocation(subject.getName());
    log.info("Downloading results for {} on dataset {}", exec, dataset);
    subject.authorize(dataset, Ability.READ);
    subject.authorize(dataset, Ability.DOWNLOAD);
    subject.authorize(exec, Ability.READ);
    // Check if subject is permitted to download on all datasets that were referenced by the query
    authorizeDownloadDatasets(subject, exec);
    if (!(exec instanceof ManagedQuery || (exec instanceof ManagedForm && ((ManagedForm) exec).getSubQueries().size() == 1))) {
        return Response.status(HttpStatus.SC_UNPROCESSABLE_ENTITY, "Execution result is not a single Table").build();
    }
    // Get the locale extracted by the LocaleFilter
    IdPrinter idPrinter = config.getFrontend().getQueryUpload().getIdPrinter(subject, exec, namespace);
    final Locale locale = I18n.LOCALE.get();
    PrintSettings settings = new PrintSettings(pretty, locale, datasetRegistry, config, idPrinter::createId);
    // Collect ResultInfos for id columns and result columns
    final List<ResultInfo> resultInfosId = config.getFrontend().getQueryUpload().getIdResultInfos();
    final List<ResultInfo> resultInfosExec = exec.getResultInfos();
    StreamingOutput out = output -> renderToStream(writerProducer.apply(output), settings, config.getArrow().getBatchSize(), resultInfosId, resultInfosExec, exec.streamResults());
    return makeResponseWithFileName(out, exec.getLabelWithoutAutoLabelSuffix(), fileExtension, mediaType, ResultUtil.ContentDispositionOption.ATTACHMENT);
}
Also used : IdPrinter(com.bakdata.conquery.models.identifiable.mapping.IdPrinter) Locale(java.util.Locale) ManagedQuery(com.bakdata.conquery.models.query.ManagedQuery) ConqueryConfig(com.bakdata.conquery.models.config.ConqueryConfig) ManagedForm(com.bakdata.conquery.models.forms.managed.ManagedForm) Subject(com.bakdata.conquery.models.auth.entities.Subject) AuthorizationHelper.authorizeDownloadDatasets(com.bakdata.conquery.models.auth.AuthorizationHelper.authorizeDownloadDatasets) HttpStatus(org.apache.http.HttpStatus) Function(java.util.function.Function) ArrayList(java.util.ArrayList) PrintSettings(com.bakdata.conquery.models.query.PrintSettings) UtilityClass(lombok.experimental.UtilityClass) MediaType(javax.ws.rs.core.MediaType) ManagedExecution(com.bakdata.conquery.models.execution.ManagedExecution) ArrowWriter(org.apache.arrow.vector.ipc.ArrowWriter) Locale(java.util.Locale) ArrowRenderer.renderToStream(com.bakdata.conquery.io.result.arrow.ArrowRenderer.renderToStream) I18n(com.bakdata.conquery.models.i18n.I18n) IdPrinter(com.bakdata.conquery.models.identifiable.mapping.IdPrinter) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) OutputStream(java.io.OutputStream) ResultUtil(com.bakdata.conquery.io.result.ResultUtil) ConqueryMDC(com.bakdata.conquery.util.io.ConqueryMDC) ResultUtil.makeResponseWithFileName(com.bakdata.conquery.io.result.ResultUtil.makeResponseWithFileName) VectorSchemaRoot(org.apache.arrow.vector.VectorSchemaRoot) StreamingOutput(javax.ws.rs.core.StreamingOutput) SingleTableResult(com.bakdata.conquery.models.query.SingleTableResult) Dataset(com.bakdata.conquery.models.datasets.Dataset) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Response(javax.ws.rs.core.Response) Ability(com.bakdata.conquery.models.auth.permissions.Ability) DatasetRegistry(com.bakdata.conquery.models.worker.DatasetRegistry) Namespace(com.bakdata.conquery.models.worker.Namespace) ManagedForm(com.bakdata.conquery.models.forms.managed.ManagedForm) PrintSettings(com.bakdata.conquery.models.query.PrintSettings) StreamingOutput(javax.ws.rs.core.StreamingOutput) ManagedQuery(com.bakdata.conquery.models.query.ManagedQuery) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) Namespace(com.bakdata.conquery.models.worker.Namespace)

Example 5 with PrintSettings

use of com.bakdata.conquery.models.query.PrintSettings in project conquery by bakdata.

the class ResultCsvProcessor method getResult.

public <E extends ManagedExecution<?> & SingleTableResult> Response getResult(Subject subject, Dataset dataset, E exec, String userAgent, String queryCharset, boolean pretty) {
    final Namespace namespace = datasetRegistry.get(dataset.getId());
    ConqueryMDC.setLocation(subject.getName());
    log.info("Downloading results for {} on dataset {}", exec, dataset);
    subject.authorize(namespace.getDataset(), Ability.READ);
    subject.authorize(namespace.getDataset(), Ability.DOWNLOAD);
    subject.authorize(exec, Ability.READ);
    // Check if subject is permitted to download on all datasets that were referenced by the query
    authorizeDownloadDatasets(subject, exec);
    IdPrinter idPrinter = config.getFrontend().getQueryUpload().getIdPrinter(subject, exec, namespace);
    // Get the locale extracted by the LocaleFilter
    final Locale locale = I18n.LOCALE.get();
    PrintSettings settings = new PrintSettings(pretty, locale, datasetRegistry, config, idPrinter::createId);
    Charset charset = determineCharset(userAgent, queryCharset);
    StreamingOutput out = os -> {
        try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, charset))) {
            CsvRenderer renderer = new CsvRenderer(config.getCsv().createWriter(writer), settings);
            renderer.toCSV(config.getFrontend().getQueryUpload().getIdResultInfos(), exec.getResultInfos(), exec.streamResults());
        } catch (EofException e) {
            log.info("User canceled download");
        } catch (Exception e) {
            throw new WebApplicationException("Failed to load result", e);
        }
    };
    return makeResponseWithFileName(out, exec.getLabelWithoutAutoLabelSuffix(), "csv", new MediaType("text", "csv", charset.toString()), ResultUtil.ContentDispositionOption.ATTACHMENT);
}
Also used : Locale(java.util.Locale) ConqueryConfig(com.bakdata.conquery.models.config.ConqueryConfig) Subject(com.bakdata.conquery.models.auth.entities.Subject) RequiredArgsConstructor(lombok.RequiredArgsConstructor) AuthorizationHelper.authorizeDownloadDatasets(com.bakdata.conquery.models.auth.AuthorizationHelper.authorizeDownloadDatasets) EofException(org.eclipse.jetty.io.EofException) PrintSettings(com.bakdata.conquery.models.query.PrintSettings) MediaType(javax.ws.rs.core.MediaType) Charset(java.nio.charset.Charset) ManagedExecution(com.bakdata.conquery.models.execution.ManagedExecution) Locale(java.util.Locale) OutputStreamWriter(java.io.OutputStreamWriter) I18n(com.bakdata.conquery.models.i18n.I18n) IdPrinter(com.bakdata.conquery.models.identifiable.mapping.IdPrinter) ResultUtil(com.bakdata.conquery.io.result.ResultUtil) ConqueryMDC(com.bakdata.conquery.util.io.ConqueryMDC) ResultUtil.makeResponseWithFileName(com.bakdata.conquery.io.result.ResultUtil.makeResponseWithFileName) BufferedWriter(java.io.BufferedWriter) ResultUtil.determineCharset(com.bakdata.conquery.io.result.ResultUtil.determineCharset) StreamingOutput(javax.ws.rs.core.StreamingOutput) SingleTableResult(com.bakdata.conquery.models.query.SingleTableResult) Dataset(com.bakdata.conquery.models.datasets.Dataset) Slf4j(lombok.extern.slf4j.Slf4j) Response(javax.ws.rs.core.Response) Ability(com.bakdata.conquery.models.auth.permissions.Ability) WebApplicationException(javax.ws.rs.WebApplicationException) DatasetRegistry(com.bakdata.conquery.models.worker.DatasetRegistry) Namespace(com.bakdata.conquery.models.worker.Namespace) EofException(org.eclipse.jetty.io.EofException) WebApplicationException(javax.ws.rs.WebApplicationException) Charset(java.nio.charset.Charset) ResultUtil.determineCharset(com.bakdata.conquery.io.result.ResultUtil.determineCharset) StreamingOutput(javax.ws.rs.core.StreamingOutput) Namespace(com.bakdata.conquery.models.worker.Namespace) EofException(org.eclipse.jetty.io.EofException) WebApplicationException(javax.ws.rs.WebApplicationException) BufferedWriter(java.io.BufferedWriter) IdPrinter(com.bakdata.conquery.models.identifiable.mapping.IdPrinter) PrintSettings(com.bakdata.conquery.models.query.PrintSettings) MediaType(javax.ws.rs.core.MediaType) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

PrintSettings (com.bakdata.conquery.models.query.PrintSettings)9 ConqueryConfig (com.bakdata.conquery.models.config.ConqueryConfig)7 ManagedQuery (com.bakdata.conquery.models.query.ManagedQuery)7 I18n (com.bakdata.conquery.models.i18n.I18n)6 ResultInfo (com.bakdata.conquery.models.query.resultinfo.ResultInfo)6 List (java.util.List)6 Locale (java.util.Locale)6 EntityResult (com.bakdata.conquery.models.query.results.EntityResult)5 ArrayList (java.util.ArrayList)5 Slf4j (lombok.extern.slf4j.Slf4j)5 Test (org.junit.jupiter.api.Test)5 IdPrinter (com.bakdata.conquery.models.identifiable.mapping.IdPrinter)4 CQConcept (com.bakdata.conquery.apiv1.query.concept.specific.CQConcept)3 ResultTestUtil (com.bakdata.conquery.io.result.ResultTestUtil)3 ResultUtil (com.bakdata.conquery.io.result.ResultUtil)3 ResultUtil.makeResponseWithFileName (com.bakdata.conquery.io.result.ResultUtil.makeResponseWithFileName)3 Subject (com.bakdata.conquery.models.auth.entities.Subject)3 Ability (com.bakdata.conquery.models.auth.permissions.Ability)3 Dataset (com.bakdata.conquery.models.datasets.Dataset)3 ManagedExecution (com.bakdata.conquery.models.execution.ManagedExecution)3