Search in sources :

Example 11 with Query

use of com.bakdata.conquery.apiv1.query.Query 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 12 with Query

use of com.bakdata.conquery.apiv1.query.Query in project conquery by bakdata.

the class QueryCleanupTask method execute.

@Override
public void execute(Map<String, List<String>> parameters, PrintWriter output) throws Exception {
    Duration queryExpiration = this.queryExpiration;
    if (parameters.containsKey(EXPIRATION_PARAM)) {
        if (parameters.get(EXPIRATION_PARAM).size() > 1) {
            log.warn("Will not respect more than one expiration time. Have `{}`", parameters.get(EXPIRATION_PARAM));
        }
        queryExpiration = Duration.parse(parameters.get(EXPIRATION_PARAM).get(0));
    }
    if (queryExpiration == null) {
        throw new IllegalArgumentException("Query Expiration may not be null");
    }
    log.info("Starting deletion of queries older than {} of {}", queryExpiration, storage.getAllExecutions().size());
    // Iterate for as long as no changes are needed (this is because queries can be referenced by other queries)
    while (true) {
        final QueryUtils.AllReusedFinder reusedChecker = new QueryUtils.AllReusedFinder();
        Set<ManagedExecution<?>> toDelete = new HashSet<>();
        for (ManagedExecution<?> execution : storage.getAllExecutions()) {
            // Gather all referenced queries via reused checker.
            if (execution instanceof ManagedQuery) {
                ((ManagedQuery) execution).getQuery().visit(reusedChecker);
            } else if (execution instanceof ManagedForm) {
                ((ManagedForm) execution).getFlatSubQueries().values().forEach(q -> q.getQuery().visit(reusedChecker));
            }
            if (execution.isShared()) {
                continue;
            }
            log.trace("{} is not shared", execution.getId());
            if (ArrayUtils.isNotEmpty(execution.getTags())) {
                continue;
            }
            log.trace("{} has no tags", execution.getId());
            if (execution.getLabel() != null && !isDefaultLabel(execution.getLabel())) {
                continue;
            }
            log.trace("{} has no label", execution.getId());
            if (LocalDateTime.now().minus(queryExpiration).isBefore(execution.getCreationTime())) {
                continue;
            }
            log.trace("{} is not older than {}.", execution.getId(), queryExpiration);
            toDelete.add(execution);
        }
        // remove all queries referenced in reused queries.
        final Collection<ManagedExecution<?>> referenced = reusedChecker.getReusedElements().stream().map(CQReusedQuery::getQueryId).map(storage::getExecution).collect(Collectors.toSet());
        toDelete.removeAll(referenced);
        if (toDelete.isEmpty()) {
            log.info("No queries to delete");
            break;
        }
        log.info("Deleting {} Executions", toDelete.size());
        for (ManagedExecution<?> execution : toDelete) {
            log.trace("Deleting Execution[{}]", execution.getId());
            storage.removeExecution(execution.getId());
        }
    }
}
Also used : PrintWriter(java.io.PrintWriter) ManagedQuery(com.bakdata.conquery.models.query.ManagedQuery) ManagedForm(com.bakdata.conquery.models.forms.managed.ManagedForm) Predicate(java.util.function.Predicate) Collection(java.util.Collection) LocalDateTime(java.time.LocalDateTime) Set(java.util.Set) ArrayUtils(org.apache.commons.lang3.ArrayUtils) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) ManagedExecution(com.bakdata.conquery.models.execution.ManagedExecution) Task(io.dropwizard.servlets.tasks.Task) Duration(java.time.Duration) Map(java.util.Map) QueryUtils(com.bakdata.conquery.util.QueryUtils) CQReusedQuery(com.bakdata.conquery.apiv1.query.concept.specific.CQReusedQuery) Pattern(java.util.regex.Pattern) MetaStorage(com.bakdata.conquery.io.storage.MetaStorage) CQReusedQuery(com.bakdata.conquery.apiv1.query.concept.specific.CQReusedQuery) Duration(java.time.Duration) QueryUtils(com.bakdata.conquery.util.QueryUtils) ManagedForm(com.bakdata.conquery.models.forms.managed.ManagedForm) ManagedQuery(com.bakdata.conquery.models.query.ManagedQuery) ManagedExecution(com.bakdata.conquery.models.execution.ManagedExecution) HashSet(java.util.HashSet)

Example 13 with Query

use of com.bakdata.conquery.apiv1.query.Query 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 14 with Query

use of com.bakdata.conquery.apiv1.query.Query in project conquery by bakdata.

the class QueryProcessor method postQuery.

/**
 * Creates a query for all datasets, then submits it for execution on the
 * intended dataset.
 */
public ManagedExecution<?> postQuery(Dataset dataset, QueryDescription query, Subject subject) {
    log.info("Query posted on Dataset[{}] by User[{{}].", dataset.getId(), subject.getId());
    // This maps works as long as we have query visitors that are not configured in anyway.
    // So adding a visitor twice would replace the previous one but both would have yielded the same result.
    // For the future a better data structure might be desired that also regards similar QueryVisitors of different configuration
    ClassToInstanceMap<QueryVisitor> visitors = MutableClassToInstanceMap.create();
    query.addVisitors(visitors);
    // Initialize checks that need to traverse the query tree
    visitors.putInstance(QueryUtils.OnlyReusingChecker.class, new QueryUtils.OnlyReusingChecker());
    visitors.putInstance(NamespacedIdentifiableCollector.class, new NamespacedIdentifiableCollector());
    final String primaryGroupName = AuthorizationHelper.getPrimaryGroup(subject, storage).map(Group::getName).orElse("none");
    visitors.putInstance(ExecutionMetrics.QueryMetricsReporter.class, new ExecutionMetrics.QueryMetricsReporter(primaryGroupName));
    // Chain all Consumers
    Consumer<Visitable> consumerChain = QueryUtils.getNoOpEntryPoint();
    for (QueryVisitor visitor : visitors.values()) {
        consumerChain = consumerChain.andThen(visitor);
    }
    // Apply consumers to the query tree
    query.visit(consumerChain);
    query.authorize(subject, dataset, visitors);
    // After all authorization checks we can now use the actual subject to invoke the query and do not to bubble down the Userish in methods
    ExecutionMetrics.reportNamespacedIds(visitors.getInstance(NamespacedIdentifiableCollector.class).getIdentifiables(), primaryGroupName);
    ExecutionMetrics.reportQueryClassUsage(query.getClass(), primaryGroupName);
    final Namespace namespace = datasetRegistry.get(dataset.getId());
    final ExecutionManager executionManager = namespace.getExecutionManager();
    // If this is only a re-executing query, try to execute the underlying query instead.
    {
        final Optional<ManagedExecutionId> executionId = visitors.getInstance(QueryUtils.OnlyReusingChecker.class).getOnlyReused();
        final Optional<ManagedExecution<?>> execution = executionId.map(id -> tryReuse(query, id, datasetRegistry, config, executionManager, subject.getUser()));
        if (execution.isPresent()) {
            return execution.get();
        }
    }
    // Execute the query
    return executionManager.runQuery(datasetRegistry, query, subject.getUser(), dataset, config);
}
Also used : ManagedQuery(com.bakdata.conquery.models.query.ManagedQuery) ConqueryConfig(com.bakdata.conquery.models.config.ConqueryConfig) URL(java.net.URL) ExternalUpload(com.bakdata.conquery.apiv1.query.ExternalUpload) RequiredArgsConstructor(lombok.RequiredArgsConstructor) ManagedExecution(com.bakdata.conquery.models.execution.ManagedExecution) Map(java.util.Map) DatasetId(com.bakdata.conquery.models.identifiable.ids.specific.DatasetId) BadRequestException(javax.ws.rs.BadRequestException) UriBuilder(javax.ws.rs.core.UriBuilder) QueryDescription(com.bakdata.conquery.apiv1.query.QueryDescription) CQAnd(com.bakdata.conquery.apiv1.query.concept.specific.CQAnd) ExecutionMetrics(com.bakdata.conquery.metrics.ExecutionMetrics) Collection(java.util.Collection) SecondaryIdQuery(com.bakdata.conquery.apiv1.query.SecondaryIdQuery) Visitable(com.bakdata.conquery.models.query.Visitable) Set(java.util.Set) User(com.bakdata.conquery.models.auth.entities.User) Collectors(java.util.stream.Collectors) Dataset(com.bakdata.conquery.models.datasets.Dataset) ConceptQuery(com.bakdata.conquery.apiv1.query.ConceptQuery) ExternalUploadResult(com.bakdata.conquery.apiv1.query.ExternalUploadResult) SecondaryIdDescription(com.bakdata.conquery.models.datasets.SecondaryIdDescription) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) CancelQuery(com.bakdata.conquery.models.messages.namespaces.specific.CancelQuery) Ability(com.bakdata.conquery.models.auth.permissions.Ability) QueryUtils(com.bakdata.conquery.util.QueryUtils) Optional(java.util.Optional) MetaStorage(com.bakdata.conquery.io.storage.MetaStorage) Namespace(com.bakdata.conquery.models.worker.Namespace) ExecutionManager(com.bakdata.conquery.models.query.ExecutionManager) ResultRendererProvider(com.bakdata.conquery.io.result.ResultRender.ResultRendererProvider) Getter(lombok.Getter) ExecutionState(com.bakdata.conquery.models.execution.ExecutionState) Subject(com.bakdata.conquery.models.auth.entities.Subject) ClassToInstanceMap(com.google.common.collect.ClassToInstanceMap) HttpServletRequest(javax.servlet.http.HttpServletRequest) ManagedExecutionId(com.bakdata.conquery.models.identifiable.ids.specific.ManagedExecutionId) QueryVisitor(com.bakdata.conquery.models.query.visitor.QueryVisitor) NamespacedIdentifiableCollector(com.bakdata.conquery.util.QueryUtils.NamespacedIdentifiableCollector) Group(com.bakdata.conquery.models.auth.entities.Group) CQExternal(com.bakdata.conquery.apiv1.query.concept.specific.external.CQExternal) AuthorizationHelper(com.bakdata.conquery.models.auth.AuthorizationHelper) Consumer(java.util.function.Consumer) MutableClassToInstanceMap(com.google.common.collect.MutableClassToInstanceMap) AuthorizationHelper.buildDatasetAbilityMap(com.bakdata.conquery.models.auth.AuthorizationHelper.buildDatasetAbilityMap) DatasetRegistry(com.bakdata.conquery.models.worker.DatasetRegistry) CQElement(com.bakdata.conquery.apiv1.query.CQElement) Query(com.bakdata.conquery.apiv1.query.Query) QueryVisitor(com.bakdata.conquery.models.query.visitor.QueryVisitor) ExecutionManager(com.bakdata.conquery.models.query.ExecutionManager) NamespacedIdentifiableCollector(com.bakdata.conquery.util.QueryUtils.NamespacedIdentifiableCollector) Optional(java.util.Optional) Visitable(com.bakdata.conquery.models.query.Visitable) ExecutionMetrics(com.bakdata.conquery.metrics.ExecutionMetrics) Namespace(com.bakdata.conquery.models.worker.Namespace) QueryUtils(com.bakdata.conquery.util.QueryUtils)

Example 15 with Query

use of com.bakdata.conquery.apiv1.query.Query in project conquery by bakdata.

the class ExecuteForm method react.

@Override
public void react(Worker worker) throws Exception {
    log.info("Started Form {}", formId);
    // Execution might have been cancelled before so we uncancel it here.
    final QueryExecutor queryExecutor = worker.getQueryExecutor();
    queryExecutor.unsetQueryCancelled(formId);
    // Execute all plans.
    for (Entry<ManagedExecutionId, Query> entry : queries.entrySet()) {
        final Query query = entry.getValue();
        ShardResult result = createResult(worker, entry.getKey());
        // Before we start the query, we create it once to test if it will succeed before creating it multiple times for evaluation per core.
        try {
            query.createQueryPlan(new QueryPlanContext(worker));
        } catch (Exception e) {
            ConqueryError err = asConqueryError(e);
            log.warn("Failed to create query plans for {}.", formId, err);
            queryExecutor.sendFailureToManagerNode(result, err);
            return;
        }
        final QueryExecutionContext subQueryContext = new QueryExecutionContext(formId, queryExecutor, worker.getStorage(), worker.getBucketManager());
        if (!queryExecutor.execute(query, subQueryContext, result)) {
            return;
        }
    }
}
Also used : ConqueryError.asConqueryError(com.bakdata.conquery.models.error.ConqueryError.asConqueryError) ConqueryError(com.bakdata.conquery.models.error.ConqueryError) Query(com.bakdata.conquery.apiv1.query.Query) QueryExecutionContext(com.bakdata.conquery.models.query.QueryExecutionContext) QueryExecutor(com.bakdata.conquery.models.query.QueryExecutor) ManagedExecutionId(com.bakdata.conquery.models.identifiable.ids.specific.ManagedExecutionId) FormShardResult(com.bakdata.conquery.models.query.results.FormShardResult) ShardResult(com.bakdata.conquery.models.query.results.ShardResult) QueryPlanContext(com.bakdata.conquery.models.query.QueryPlanContext)

Aggregations

ConceptQuery (com.bakdata.conquery.apiv1.query.ConceptQuery)12 Dataset (com.bakdata.conquery.models.datasets.Dataset)12 Query (com.bakdata.conquery.apiv1.query.Query)11 ManagedQuery (com.bakdata.conquery.models.query.ManagedQuery)11 MetaStorage (com.bakdata.conquery.io.storage.MetaStorage)10 QueryTest (com.bakdata.conquery.integration.json.QueryTest)8 ExecutionState (com.bakdata.conquery.models.execution.ExecutionState)8 Slf4j (lombok.extern.slf4j.Slf4j)8 Namespace (com.bakdata.conquery.models.worker.Namespace)7 CQAnd (com.bakdata.conquery.apiv1.query.concept.specific.CQAnd)6 CQConcept (com.bakdata.conquery.apiv1.query.concept.specific.CQConcept)6 CQReusedQuery (com.bakdata.conquery.apiv1.query.concept.specific.CQReusedQuery)6 User (com.bakdata.conquery.models.auth.entities.User)6 Response (javax.ws.rs.core.Response)6 CQExternal (com.bakdata.conquery.apiv1.query.concept.specific.external.CQExternal)5 ShardNode (com.bakdata.conquery.commands.ShardNode)5 IntegrationUtils (com.bakdata.conquery.integration.common.IntegrationUtils)5 LoadingUtil (com.bakdata.conquery.integration.common.LoadingUtil)5 LoadingUtil.importSecondaryIds (com.bakdata.conquery.integration.common.LoadingUtil.importSecondaryIds)5 JsonIntegrationTest (com.bakdata.conquery.integration.json.JsonIntegrationTest)5