Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/date context alignment fallback #2060

Merged
merged 10 commits into from
Aug 31, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import com.bakdata.conquery.models.common.Range;
import com.bakdata.conquery.models.datasets.Dataset;
import com.bakdata.conquery.models.forms.export.AbsExportGenerator;
import com.bakdata.conquery.models.forms.util.DateContext;
import com.bakdata.conquery.models.forms.util.Alignment;
import com.bakdata.conquery.models.query.DateAggregationMode;
import com.bakdata.conquery.models.query.QueryResolveContext;
import com.bakdata.conquery.models.query.Visitable;
Expand All @@ -39,7 +39,7 @@ public void visit(Consumer<Visitable> visitor) {
}

@NotNull
private DateContext.Alignment alignmentHint = DateContext.Alignment.QUARTER;
private Alignment alignmentHint = Alignment.QUARTER;

@Override
public Query createSpecializedQuery(DatasetRegistry datasets, User user, Dataset submittedDataset) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.bakdata.conquery.models.common.daterange.CDateRange;
import com.bakdata.conquery.models.datasets.Dataset;
import com.bakdata.conquery.models.forms.managed.EntityDateQuery;
import com.bakdata.conquery.models.forms.util.DateContext;
import com.bakdata.conquery.models.forms.util.Alignment;
import com.bakdata.conquery.models.query.DateAggregationMode;
import com.bakdata.conquery.models.query.QueryResolveContext;
import com.bakdata.conquery.models.query.Visitable;
Expand Down Expand Up @@ -51,7 +51,7 @@ public void visit(Consumer<Visitable> visitor) {
}

@NotNull
private DateContext.Alignment alignmentHint = DateContext.Alignment.QUARTER;
private Alignment alignmentHint = Alignment.QUARTER;

@Override
public void resolve(QueryResolveContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
import com.bakdata.conquery.models.execution.ManagedExecution;
import com.bakdata.conquery.models.forms.managed.ManagedForm;
import com.bakdata.conquery.models.forms.managed.ManagedInternalForm;
import com.bakdata.conquery.models.forms.util.DateContext;
import com.bakdata.conquery.models.forms.util.Alignment;
import com.bakdata.conquery.models.forms.util.Resolution;
import com.bakdata.conquery.models.i18n.I18n;
import com.bakdata.conquery.models.identifiable.ids.specific.ManagedExecutionId;
import com.bakdata.conquery.models.query.ManagedQuery;
Expand Down Expand Up @@ -56,14 +57,14 @@ public class ExportForm extends Form {
private Mode timeMode;

@NotNull @NotEmpty
private List<DateContext.Resolution> resolution = List.of(DateContext.Resolution.COMPLETE);
private List<Resolution> resolution = List.of(Resolution.COMPLETE);

private boolean alsoCreateCoarserSubdivisions = true;

@JsonIgnore
private Query prerequisite;
@JsonIgnore
private List<DateContext.Resolution> resolvedResolutions;
private List<Resolution> resolvedResolutions;

@Override
public void visit(Consumer<Visitable> visitor) {
Expand Down Expand Up @@ -118,21 +119,24 @@ public String getLocalizedTypeLabel() {
/**
* Maps the given resolution to a fitting alignment. It tries to use the alignment which was given as a hint.
* If the alignment does not fit to a resolution (resolution is finer than the alignment), the first alignment that
* this resolution supports is chosen (see the alignment order in {@link DateContext.Resolution})
* this resolution supports is chosen (see the alignment order in {@link Resolution})
* @param resolutions The temporal resolutions for which sub queries should be generated per entity
* @param alignmentHint The preferred calendar alignment on which the sub queries of each resolution should be aligned.
* Note that this alignment is chosen when a resolution is equal or coarser.
* @return The given resolutions mapped to a fitting calendar alignment.
*/
public static List<ExportForm.ResolutionAndAlignment> getResolutionAlignmentMap(List<DateContext.Resolution> resolutions, DateContext.Alignment alignmentHint) {
public static List<ExportForm.ResolutionAndAlignment> getResolutionAlignmentMap(List<Resolution> resolutions, Alignment alignmentHint) {

return resolutions.stream()
.map(r -> ResolutionAndAlignment.of(r, getFittingAlignment(alignmentHint, r)))
.collect(Collectors.toList());
}

private static DateContext.Alignment getFittingAlignment(DateContext.Alignment alignmentHint, DateContext.Resolution resolution) {
return resolution.getSupportedAlignments().contains(alignmentHint)? alignmentHint : resolution.getSupportedAlignments().iterator().next();
private static Alignment getFittingAlignment(Alignment alignmentHint, Resolution resolution) {
if(resolution.isAlignmentSupported(alignmentHint) ) {
return alignmentHint;
}
return resolution.getDefaultAlignment();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warum nimmst du hier den Default?

Copy link
Collaborator Author

@thoniTUB thoniTUB Aug 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Njimefo
Im ExportFormular kann der Nutzer verschiedene zeitliche Stratifizierungen auswählen, also ob Events über Tage, Quartale oder Jahre zusammen gefasst werden sollen (Monate, Wochen wären theoretisch auch möglich). Das ist die Resolution, also die Auflösung der Stratifizierung.

Dann gibt es noch das kalendarische Alignment. Das gibt an, woran sich die, durch die Resolution festgelegten, Datumsbereiche ausrichten. So kannst du zum Beispiel eine Auflösung von Jahren am heutigen
Tag (31.08.2021 - 31.08.2022),
Quartal (01.07.2021 - 30.06.2022) oder
Jahr (01.01.2021 - 31.12.2021) ausrichten.

Jetzt zum Default: Die API lässt es zu, dass du ein alignmentHint gibts. Es passt aber ein gröberes Aligment nicht zu einer feineren Auflösung: Es ist nicht möglich Quartale am Kalenderjahr auszurichten. An dieser Stelle gibt es einen Fallback zum Default.

}

/**
Expand All @@ -141,12 +145,12 @@ private static DateContext.Alignment getFittingAlignment(DateContext.Alignment a
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public static class ResolutionAndAlignment {
private final DateContext.Resolution resolution;
private final DateContext.Alignment alignment;
private final Resolution resolution;
private final Alignment alignment;

@JsonCreator
public static ResolutionAndAlignment of(DateContext.Resolution resolution, DateContext.Alignment alignment){
if (!resolution.getSupportedAlignments().contains(alignment)) {
public static ResolutionAndAlignment of(Resolution resolution, Alignment alignment){
if (!resolution.isAlignmentSupported(alignment)) {
throw new ValidationException(String.format("The alignment %s is not supported by the resolution %s", alignment, resolution));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.bakdata.conquery.models.datasets.Dataset;
import com.bakdata.conquery.models.forms.export.RelExportGenerator;
import com.bakdata.conquery.models.forms.managed.RelativeFormQuery;
import com.bakdata.conquery.models.forms.util.DateContext;
import com.bakdata.conquery.models.forms.util.CalendarUnit;
import com.bakdata.conquery.models.query.DateAggregationMode;
import com.bakdata.conquery.models.query.QueryResolveContext;
import com.bakdata.conquery.models.query.Visitable;
Expand All @@ -30,7 +30,7 @@
@CPSType(id="RELATIVE", base=Mode.class)
public class RelativeMode extends Mode {
@NotNull
private DateContext.CalendarUnit timeUnit;
private CalendarUnit timeUnit;
@Min(0)
private int timeCountBefore;
@Min(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import com.bakdata.conquery.io.cps.CPSBase;
import com.bakdata.conquery.io.cps.CPSType;
import com.bakdata.conquery.models.forms.util.DateContext;
import com.bakdata.conquery.models.forms.util.Alignment;
import com.bakdata.conquery.models.forms.util.Resolution;
import com.bakdata.conquery.models.identifiable.ids.IId;
import com.bakdata.conquery.models.query.entity.Entity;
import com.bakdata.conquery.models.query.queryplan.QueryPlan;
Expand Down Expand Up @@ -231,8 +232,7 @@ public static class ExecutionCreationPlanDateContextError extends ContextError {

private final static String ALIGNMENT = "alignment";
private final static String RESOLUTION = "resolution";
private final static String ALIGNMENT_SUPPORTED = "alignmentsSupported";
private final static String TEMPLATE = "Alignment ${" + ALIGNMENT + "} and resolution ${" + RESOLUTION + "} don't fit together. The resolution only supports these alignments: ${" + ALIGNMENT_SUPPORTED + "}";
private final static String TEMPLATE = "Alignment ${" + ALIGNMENT + "} and resolution ${" + RESOLUTION + "} are not compatible.";

/**
* Constructor for deserialization.
Expand All @@ -242,11 +242,10 @@ private ExecutionCreationPlanDateContextError() {
super(TEMPLATE);
}

public ExecutionCreationPlanDateContextError(DateContext.Alignment alignment, DateContext.Resolution resolution) {
public ExecutionCreationPlanDateContextError(Alignment alignment, Resolution resolution) {
this();
getContext().put(ALIGNMENT, Objects.toString(alignment));
getContext().put(RESOLUTION, Objects.toString(resolution));
getContext().put(ALIGNMENT_SUPPORTED, Objects.toString(resolution.getSupportedAlignments()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.bakdata.conquery.io.cps.CPSBase;
import com.bakdata.conquery.io.cps.CPSType;
import com.bakdata.conquery.models.events.MajorTypeId;
import com.bakdata.conquery.models.forms.util.DateContext;
import com.bakdata.conquery.models.forms.util.Resolution;
import com.bakdata.conquery.models.query.PrintSettings;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
Expand Down Expand Up @@ -139,13 +139,13 @@ public static class ResolutionT extends PrimitiveResultType {

@Override
public String print(PrintSettings cfg, Object f) {
if (f instanceof DateContext.Resolution) {
return ((DateContext.Resolution) f).toString(cfg.getLocale());
if (f instanceof Resolution) {
return ((Resolution) f).toString(cfg.getLocale());
}
try {
// If the object was parsed as a simple string, try to convert it to a
// DateContextMode to get Internationalization
return DateContext.Resolution.valueOf(f.toString()).toString(cfg.getLocale());
return Resolution.valueOf(f.toString()).toString(cfg.getLocale());
} catch (Exception e) {
throw new IllegalArgumentException(f + " is not a valid resolution.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.bakdata.conquery.apiv1.query.concept.specific.ResultInfoDecorator;
import com.bakdata.conquery.apiv1.query.concept.specific.temporal.TemporalSampler;
import com.bakdata.conquery.models.forms.managed.RelativeFormQuery;
import com.bakdata.conquery.models.forms.util.DateContext;
import com.bakdata.conquery.models.forms.util.CalendarUnit;
import com.google.common.collect.ImmutableClassToInstanceMap;
import lombok.AllArgsConstructor;

Expand All @@ -27,7 +27,7 @@ public static RelativeFormQuery generate(RelativeMode mode) {
return generate(mode.getForm().getPrerequisite(), mode.getResolvedFeatures(), mode.getResolvedOutcomes(), mode.getIndexSelector(), mode.getIndexPlacement(), mode.getTimeCountBefore(), mode.getTimeCountAfter(), mode.getTimeUnit(), resolutionsAndAlignments);
}

public static RelativeFormQuery generate(Query query, ArrayConceptQuery features, ArrayConceptQuery outcomes, TemporalSampler indexSelector, IndexPlacement indexPlacement, int timeCountBefore, int timeCountAfter, DateContext.CalendarUnit timeUnit, List<ExportForm.ResolutionAndAlignment> resolutionsAndAlignments) {
public static RelativeFormQuery generate(Query query, ArrayConceptQuery features, ArrayConceptQuery outcomes, TemporalSampler indexSelector, IndexPlacement indexPlacement, int timeCountBefore, int timeCountAfter, CalendarUnit timeUnit, List<ExportForm.ResolutionAndAlignment> resolutionsAndAlignments) {

return new RelativeFormQuery(
query,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import com.bakdata.conquery.apiv1.query.concept.specific.temporal.TemporalSampler;
import com.bakdata.conquery.io.cps.CPSType;
import com.bakdata.conquery.models.execution.ManagedExecution;
import com.bakdata.conquery.models.forms.util.DateContext;
import com.bakdata.conquery.models.forms.util.CalendarUnit;
import com.bakdata.conquery.models.query.DateAggregationMode;
import com.bakdata.conquery.models.query.QueryPlanContext;
import com.bakdata.conquery.models.query.QueryResolveContext;
Expand Down Expand Up @@ -47,7 +47,7 @@ public class RelativeFormQuery extends Query {
@Min(0)
private final int timeCountAfter;
@NotNull
private final DateContext.CalendarUnit timeUnit;
private final CalendarUnit timeUnit;
@NotNull
private final List<ExportForm.ResolutionAndAlignment> resolutionsAndAlignmentMap;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import com.bakdata.conquery.apiv1.forms.export_form.ExportForm;
import com.bakdata.conquery.models.common.CDateSet;
import com.bakdata.conquery.models.error.ConqueryError;
import com.bakdata.conquery.models.forms.util.CalendarUnit;
import com.bakdata.conquery.models.forms.util.DateContext;
import com.bakdata.conquery.models.forms.util.Resolution;
import com.bakdata.conquery.models.forms.util.ResultModifier;
import com.bakdata.conquery.models.query.QueryExecutionContext;
import com.bakdata.conquery.apiv1.query.concept.specific.temporal.TemporalSampler;
Expand Down Expand Up @@ -41,7 +43,7 @@ public class RelativeFormQueryPlan implements QueryPlan<MultilineEntityResult> {
private final IndexPlacement indexPlacement;
private final int timeCountBefore;
private final int timeCountAfter;
private final DateContext.CalendarUnit timeUnit;
private final CalendarUnit timeUnit;
private final List<ExportForm.ResolutionAndAlignment> resolutionsAndAlignmentMap;

private final transient List<FormQueryPlan> featureSubqueries = new ArrayList<>();
Expand Down Expand Up @@ -197,13 +199,13 @@ private boolean hasCompleteDateContexts(List<DateContext> contexts) {
if (featurePlan.getAggregatorSize() > 0 && outcomePlan.getAggregatorSize() > 0) {
// We have features and outcomes check if both have complete date ranges (they should be at the beginning of the list)
return contexts.size()>=2
&& contexts.get(0).getSubdivisionMode().equals(DateContext.Resolution.COMPLETE)
&& contexts.get(1).getSubdivisionMode().equals(DateContext.Resolution.COMPLETE)
&& contexts.get(0).getSubdivisionMode().equals(Resolution.COMPLETE)
&& contexts.get(1).getSubdivisionMode().equals(Resolution.COMPLETE)
&& !contexts.get(0).getFeatureGroup().equals(contexts.get(1).getFeatureGroup());
}
// Otherwise, if only features or outcomes are given check the first date context. The empty feature/outcome query
// will still return an empty result which will be merged with to a complete result.
return contexts.get(0).getSubdivisionMode().equals(DateContext.Resolution.COMPLETE);
return contexts.get(0).getSubdivisionMode().equals(Resolution.COMPLETE);
}

private FormQueryPlan createSubQuery(ArrayConceptQueryPlan subPlan, List<DateContext> contexts, FeatureGroup featureGroup) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.bakdata.conquery.models.forms.util;

import com.bakdata.conquery.models.common.daterange.CDateRange;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.List;
import java.util.Map;
import java.util.OptionalInt;
import java.util.function.Function;

/**
* Specifier for the alignment of {@link DateContext}s of a certain resolution.
* The alignment provides a method to sub divide a dateRangeMask into ranges aligned to the calendar equivalent.
* These sub divisions can then be merged to form the equally grain or coarser desired resolution for the
* {@link DateContext}s.
*/
@RequiredArgsConstructor
public enum Alignment {
NO_ALIGN(List::of){
@Override
protected Map<Resolution, Integer> getAmountPerResolution() {
return Map.of(Resolution.COMPLETE, 1);
}
}, // Special case for resolution == COMPLETE
DAY(CDateRange::getCoveredDays) {
@Override
protected Map<Resolution, Integer> getAmountPerResolution() {
return Map.of(
Resolution.YEARS, 365,
Resolution.QUARTERS, 90,
Resolution.DAYS, 1);
}
},
QUARTER(CDateRange::getCoveredQuarters) {
@Override
protected Map<Resolution, Integer> getAmountPerResolution() {
return Map.of(
Resolution.YEARS, 4,
Resolution.QUARTERS, 1);
}
},
YEAR(CDateRange::getCoveredYears) {
@Override
protected Map<Resolution, Integer> getAmountPerResolution() {
return Map.of(Resolution.YEARS, 1);
}
};

@Getter
@JsonIgnore
private final Function<CDateRange, List<CDateRange>> subdivider;

@JsonIgnore
protected abstract Map<Resolution, Integer> getAmountPerResolution();

/**
* Returns the amount of calendar alignment sub date ranges that would fit in to this resolution.
*/
@JsonIgnore
public OptionalInt getAmountForResolution(Resolution resolution) {
Integer amount = getAmountPerResolution().get(resolution);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Würde ich über contains machen aber unwichtig

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Habs angepasst

if (amount == null) {
return OptionalInt.empty();
}
return OptionalInt.of(amount);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.bakdata.conquery.models.forms.util;

import com.bakdata.conquery.models.common.daterange.CDateRange;
import com.google.common.collect.Lists;

import java.util.List;

/**
* Specifies whether the sub date ranges (which have the maximum length specified by the {@link Resolution})
* are aligned with regard to beginning or the end of a date range mask. This affects the generated sub date ranges
* only if the {@link Alignment} is finer than the {@link Resolution}.
*/
public enum AlignmentReference {
START() {
@Override
public List<CDateRange> getAlignedIterationDirection(List<CDateRange> alignedSubDivisions) {
return alignedSubDivisions;
}

@Override
public int getInterestingBorder(CDateRange daterange) {
return daterange.getMinValue();
}

@Override
public CDateRange makeMergedRange(CDateRange lastDaterange, int prioInteressingBorder) {
return CDateRange.of(prioInteressingBorder, lastDaterange.getMaxValue());
}
},
END() {
@Override
public List<CDateRange> getAlignedIterationDirection(List<CDateRange> alignedSubDivisions) {
return Lists.reverse(alignedSubDivisions);
}

@Override
public int getInterestingBorder(CDateRange daterange) {
return daterange.getMaxValue();
}

@Override
public CDateRange makeMergedRange(CDateRange lastDaterange, int prioInteressingBorder) {
return CDateRange.of(lastDaterange.getMinValue(), prioInteressingBorder);
}
};

public abstract List<CDateRange> getAlignedIterationDirection(List<CDateRange> alignedSubDivisions);

public abstract int getInterestingBorder(CDateRange daterange);

public abstract CDateRange makeMergedRange(CDateRange lastDaterange, int prioInteressingBorder);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.bakdata.conquery.models.forms.util;

import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public enum CalendarUnit {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doku

DAYS(Alignment.DAY),
QUARTERS(Alignment.QUARTER),
YEARS(Alignment.YEAR);

@Getter
private final Alignment alignment;
}
Loading