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();
}
}
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();
}
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());
}
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);
}
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);
}
Aggregations