Search in sources :

Example 1 with CQExternal

use of com.bakdata.conquery.apiv1.query.concept.specific.external.CQExternal in project conquery by bakdata.

the class LoadingUtil method importPreviousQueries.

public static void importPreviousQueries(StandaloneSupport support, RequiredData content, User user) throws IOException {
    // Load previous query results if available
    int id = 1;
    for (ResourceFile queryResults : content.getPreviousQueryResults()) {
        UUID queryId = new UUID(0L, id++);
        final CsvParser parser = support.getConfig().getCsv().withParseHeaders(false).withSkipHeader(false).createParser();
        String[][] data = parser.parseAll(queryResults.stream()).toArray(new String[0][]);
        ConceptQuery q = new ConceptQuery(new CQExternal(Arrays.asList("ID", "DATE_SET"), data));
        ManagedExecution<?> managed = support.getNamespace().getExecutionManager().createQuery(support.getNamespace().getNamespaces(), q, queryId, user, support.getNamespace().getDataset());
        user.addPermission(managed.createPermission(AbilitySets.QUERY_CREATOR));
        if (managed.getState() == ExecutionState.FAILED) {
            fail("Query failed");
        }
    }
    for (JsonNode queryNode : content.getPreviousQueries()) {
        ObjectMapper mapper = new SingletonNamespaceCollection(support.getNamespaceStorage().getCentralRegistry()).injectIntoNew(Jackson.MAPPER);
        mapper = support.getDataset().injectIntoNew(mapper);
        Query query = mapper.readerFor(Query.class).readValue(queryNode);
        UUID queryId = new UUID(0L, id++);
        ManagedExecution<?> managed = support.getNamespace().getExecutionManager().createQuery(support.getNamespace().getNamespaces(), query, queryId, user, support.getNamespace().getDataset());
        user.addPermission(ExecutionPermission.onInstance(AbilitySets.QUERY_CREATOR, managed.getId()));
        if (managed.getState() == ExecutionState.FAILED) {
            fail("Query failed");
        }
    }
    // wait only if we actually did anything
    if (!content.getPreviousQueryResults().isEmpty()) {
        support.waitUntilWorkDone();
    }
}
Also used : ConceptQuery(com.bakdata.conquery.apiv1.query.ConceptQuery) Query(com.bakdata.conquery.apiv1.query.Query) SingletonNamespaceCollection(com.bakdata.conquery.models.worker.SingletonNamespaceCollection) CsvParser(com.univocity.parsers.csv.CsvParser) CQExternal(com.bakdata.conquery.apiv1.query.concept.specific.external.CQExternal) JsonNode(com.fasterxml.jackson.databind.JsonNode) UUID(java.util.UUID) ConceptQuery(com.bakdata.conquery.apiv1.query.ConceptQuery) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with CQExternal

use of com.bakdata.conquery.apiv1.query.concept.specific.external.CQExternal in project conquery by bakdata.

the class ManagedQuery method makeDefaultLabel.

/**
 * Creates a default label based on the submitted {@link QueryDescription}.
 * The Label is customized by mentioning that a description contained a
 * {@link CQExternal}, {@link CQReusedQuery} or {@link CQConcept}, in this order.
 * In case of one ore more {@link CQConcept} the distinct labels of the concepts are chosen
 * and concatinated until a length of {@value #MAX_CONCEPT_LABEL_CONCAT_LENGTH} is reached.
 * All further labels are dropped.
 */
@Override
protected String makeDefaultLabel(PrintSettings cfg) {
    final StringBuilder sb = new StringBuilder();
    final Map<Class<? extends Visitable>, List<Visitable>> sortedContents = Visitable.stream(query).collect(Collectors.groupingBy(Visitable::getClass));
    int sbStartSize = sb.length();
    // Check for CQExternal
    List<Visitable> externals = sortedContents.getOrDefault(CQExternal.class, Collections.emptyList());
    if (!externals.isEmpty()) {
        if (sb.length() > 0) {
            sb.append(" ");
        }
        sb.append(C10N.get(CQElementC10n.class, I18n.LOCALE.get()).external());
    }
    // Check for CQReused
    if (sortedContents.containsKey(CQReusedQuery.class)) {
        if (sb.length() > 0) {
            sb.append(" ");
        }
        sb.append(C10N.get(CQElementC10n.class, I18n.LOCALE.get()).reused());
    }
    // Check for CQConcept
    if (sortedContents.containsKey(CQConcept.class)) {
        if (sb.length() > 0) {
            sb.append(" ");
        }
        // Track length of text we are appending for concepts.
        final AtomicInteger length = new AtomicInteger();
        sortedContents.get(CQConcept.class).stream().map(CQConcept.class::cast).map(c -> makeLabelWithRootAndChild(c, cfg)).filter(Predicate.not(Strings::isNullOrEmpty)).distinct().takeWhile(elem -> length.addAndGet(elem.length()) < MAX_CONCEPT_LABEL_CONCAT_LENGTH).forEach(label -> sb.append(label).append(" "));
        // Last entry will output one Space that we don't want
        if (sb.length() > 0) {
            sb.deleteCharAt(sb.length() - 1);
        }
        // If not all Concept could be included in the name, point that out
        if (length.get() > MAX_CONCEPT_LABEL_CONCAT_LENGTH) {
            sb.append(" ").append(C10N.get(CQElementC10n.class, I18n.LOCALE.get()).furtherConcepts());
        }
    }
    // Fallback to id if nothing could be extracted from the query description
    if (sbStartSize == sb.length()) {
        sb.append(getId().getExecution());
    }
    return sb.toString();
}
Also used : ConqueryConfig(com.bakdata.conquery.models.config.ConqueryConfig) ExecutionStatus(com.bakdata.conquery.apiv1.ExecutionStatus) ManagedExecution(com.bakdata.conquery.models.execution.ManagedExecution) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) Map(java.util.Map) ToString(lombok.ToString) QueryDescription(com.bakdata.conquery.apiv1.query.QueryDescription) ResultType(com.bakdata.conquery.models.externalservice.ResultType) ShardResult(com.bakdata.conquery.models.query.results.ShardResult) WorkerMessage(com.bakdata.conquery.models.messages.namespaces.WorkerMessage) CPSType(com.bakdata.conquery.io.cps.CPSType) Predicate(java.util.function.Predicate) NonNull(lombok.NonNull) SecondaryIdQuery(com.bakdata.conquery.apiv1.query.SecondaryIdQuery) Set(java.util.Set) User(com.bakdata.conquery.models.auth.entities.User) Collectors(java.util.stream.Collectors) Dataset(com.bakdata.conquery.models.datasets.Dataset) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) C10N(c10n.C10N) CQReusedQuery(com.bakdata.conquery.apiv1.query.concept.specific.CQReusedQuery) MetaStorage(com.bakdata.conquery.io.storage.MetaStorage) Namespace(com.bakdata.conquery.models.worker.Namespace) Setter(lombok.Setter) Getter(lombok.Getter) ExecutionState(com.bakdata.conquery.models.execution.ExecutionState) Subject(com.bakdata.conquery.models.auth.entities.Subject) EntityResult(com.bakdata.conquery.models.query.results.EntityResult) NamespacedIdentifiable(com.bakdata.conquery.models.identifiable.ids.NamespacedIdentifiable) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) ExecuteQuery(com.bakdata.conquery.models.messages.namespaces.specific.ExecuteQuery) CQElementC10n(com.bakdata.conquery.internationalization.CQElementC10n) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore) I18n(com.bakdata.conquery.models.i18n.I18n) NamespacedIdentifiableCollector(com.bakdata.conquery.util.QueryUtils.NamespacedIdentifiableCollector) ResultInfo(com.bakdata.conquery.models.query.resultinfo.ResultInfo) UniqueNamer(com.bakdata.conquery.models.query.resultinfo.UniqueNamer) CQExternal(com.bakdata.conquery.apiv1.query.concept.specific.external.CQExternal) Consumer(java.util.function.Consumer) FullExecutionStatus(com.bakdata.conquery.apiv1.FullExecutionStatus) DatasetRegistry(com.bakdata.conquery.models.worker.DatasetRegistry) Query(com.bakdata.conquery.apiv1.query.Query) Preconditions(com.google.common.base.Preconditions) CQConcept(com.bakdata.conquery.apiv1.query.concept.specific.CQConcept) Collections(java.util.Collections) NoArgsConstructor(lombok.NoArgsConstructor) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) ArrayList(java.util.ArrayList) CQConcept(com.bakdata.conquery.apiv1.query.concept.specific.CQConcept) Strings(com.google.common.base.Strings)

Example 3 with CQExternal

use of com.bakdata.conquery.apiv1.query.concept.specific.external.CQExternal in project conquery by bakdata.

the class QueryProcessor method uploadEntities.

/**
 * Try to resolve the external upload, if successful, create query for the subject and return id and statistics for that.
 */
public ExternalUploadResult uploadEntities(Subject subject, Dataset dataset, ExternalUpload upload) {
    final CQExternal.ResolveStatistic statistic = CQExternal.resolveEntities(upload.getValues(), upload.getFormat(), datasetRegistry.get(dataset.getId()).getStorage().getIdMapping(), config.getFrontend().getQueryUpload(), config.getLocale().getDateReader());
    // Resolving nothing is a problem thus we fail.
    if (statistic.getResolved().isEmpty()) {
        throw new BadRequestException(Response.status(Response.Status.BAD_REQUEST).entity(new ExternalUploadResult(null, 0, statistic.getUnresolvedId(), statistic.getUnreadableDate())).build());
    }
    final ConceptQuery query = new ConceptQuery(new CQExternal(upload.getFormat(), upload.getValues()));
    // We only create the Query, really no need to execute it as it's only useful for composition.
    final ManagedQuery execution = ((ManagedQuery) datasetRegistry.get(dataset.getId()).getExecutionManager().createExecution(datasetRegistry, query, subject.getUser(), dataset));
    execution.setLastResultCount((long) statistic.getResolved().size());
    if (upload.getLabel() != null) {
        execution.setLabel(upload.getLabel());
    }
    execution.initExecutable(datasetRegistry, config);
    return new ExternalUploadResult(execution.getId(), statistic.getResolved().size(), statistic.getUnresolvedId(), statistic.getUnreadableDate());
}
Also used : ExternalUploadResult(com.bakdata.conquery.apiv1.query.ExternalUploadResult) BadRequestException(javax.ws.rs.BadRequestException) CQExternal(com.bakdata.conquery.apiv1.query.concept.specific.external.CQExternal) ManagedQuery(com.bakdata.conquery.models.query.ManagedQuery) ConceptQuery(com.bakdata.conquery.apiv1.query.ConceptQuery)

Example 4 with CQExternal

use of com.bakdata.conquery.apiv1.query.concept.specific.external.CQExternal in project conquery by bakdata.

the class DefaultLabelTest method autoLabelUploadQuery.

@ParameterizedTest
@CsvSource({ "de,Hochgeladene-Liste", "en,Uploaded-List" })
void autoLabelUploadQuery(Locale locale, String autoLabel) {
    I18n.LOCALE.set(locale);
    CQExternal external = new CQExternal(List.of(), new String[0][0]);
    ConceptQuery cq = new ConceptQuery(external);
    ManagedQuery mQuery = cq.toManagedExecution(user, DATASET);
    mQuery.setLabel(mQuery.makeAutoLabel(getPrintSettings(locale)));
    assertThat(mQuery.getLabel()).isEqualTo(autoLabel + AUTO_LABEL_SUFFIX);
    assertThat(mQuery.getLabelWithoutAutoLabelSuffix()).isEqualTo(autoLabel);
}
Also used : CQExternal(com.bakdata.conquery.apiv1.query.concept.specific.external.CQExternal) ManagedQuery(com.bakdata.conquery.models.query.ManagedQuery) ConceptQuery(com.bakdata.conquery.apiv1.query.ConceptQuery) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with CQExternal

use of com.bakdata.conquery.apiv1.query.concept.specific.external.CQExternal in project conquery by bakdata.

the class DefaultLabelTest method autoLabelComplexQuery.

@ParameterizedTest
@CsvSource({ "de,Hochgeladene-Liste Anfrage Concept1 Concept2 und weitere", "en,Uploaded-List Query Concept1 Concept2 and further" })
void autoLabelComplexQuery(Locale locale, String autoLabel) {
    I18n.LOCALE.set(locale);
    final ManagedQuery managedQuery = new ManagedQuery(null, null, DATASET);
    managedQuery.setQueryId(UUID.randomUUID());
    CQAnd and = new CQAnd();
    CQConcept concept1 = makeCQConcept("Concept1");
    CQConcept concept2 = makeCQConcept("Concept2");
    CQConcept concept3 = makeCQConcept("Concept3veryveryveryveryveryveryveryverylooooooooooooooooooooonglabel");
    and.setChildren(List.of(new CQExternal(List.of(), new String[0][0]), new CQReusedQuery(managedQuery.getId()), concept1, concept2, concept3));
    ConceptQuery cq = new ConceptQuery(and);
    ManagedQuery mQuery = cq.toManagedExecution(user, DATASET);
    mQuery.setLabel(mQuery.makeAutoLabel(getPrintSettings(locale)));
    assertThat(mQuery.getLabel()).isEqualTo(autoLabel + AUTO_LABEL_SUFFIX);
    assertThat(mQuery.getLabelWithoutAutoLabelSuffix()).isEqualTo(autoLabel);
}
Also used : CQReusedQuery(com.bakdata.conquery.apiv1.query.concept.specific.CQReusedQuery) CQConcept(com.bakdata.conquery.apiv1.query.concept.specific.CQConcept) CQExternal(com.bakdata.conquery.apiv1.query.concept.specific.external.CQExternal) ManagedQuery(com.bakdata.conquery.models.query.ManagedQuery) CQAnd(com.bakdata.conquery.apiv1.query.concept.specific.CQAnd) ConceptQuery(com.bakdata.conquery.apiv1.query.ConceptQuery) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

CQExternal (com.bakdata.conquery.apiv1.query.concept.specific.external.CQExternal)6 ConceptQuery (com.bakdata.conquery.apiv1.query.ConceptQuery)5 ManagedQuery (com.bakdata.conquery.models.query.ManagedQuery)4 CQConcept (com.bakdata.conquery.apiv1.query.concept.specific.CQConcept)3 CQReusedQuery (com.bakdata.conquery.apiv1.query.concept.specific.CQReusedQuery)3 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)3 CsvSource (org.junit.jupiter.params.provider.CsvSource)3 Query (com.bakdata.conquery.apiv1.query.Query)2 CQAnd (com.bakdata.conquery.apiv1.query.concept.specific.CQAnd)2 C10N (c10n.C10N)1 ExecutionStatus (com.bakdata.conquery.apiv1.ExecutionStatus)1 FullExecutionStatus (com.bakdata.conquery.apiv1.FullExecutionStatus)1 ExternalUploadResult (com.bakdata.conquery.apiv1.query.ExternalUploadResult)1 QueryDescription (com.bakdata.conquery.apiv1.query.QueryDescription)1 SecondaryIdQuery (com.bakdata.conquery.apiv1.query.SecondaryIdQuery)1 CQElementC10n (com.bakdata.conquery.internationalization.CQElementC10n)1 CPSType (com.bakdata.conquery.io.cps.CPSType)1 MetaStorage (com.bakdata.conquery.io.storage.MetaStorage)1 Subject (com.bakdata.conquery.models.auth.entities.Subject)1 User (com.bakdata.conquery.models.auth.entities.User)1