use of com.bakdata.conquery.models.common.daterange.CDateRange in project conquery by bakdata.
the class RangeTest method deserialize.
@ParameterizedTest(name = "{0}")
@MethodSource
public void deserialize(String json, Range<LocalDate> expected, CDateRange expectedCDateRange) throws IOException {
ObjectReader reader = Jackson.MAPPER.readerFor(Jackson.MAPPER.getTypeFactory().constructParametricType(Range.class, LocalDate.class));
Range<LocalDate> range = reader.readValue(json);
assertThat(range).isEqualTo(expected);
assertThat(range).isEqualToComparingFieldByFieldRecursively(expected);
CDateRange cDateRange = CDateRange.of(range);
assertThat(cDateRange).isEqualTo(expectedCDateRange);
assertThat(cDateRange).isEqualToComparingFieldByFieldRecursively(expectedCDateRange);
}
use of com.bakdata.conquery.models.common.daterange.CDateRange in project conquery by bakdata.
the class EntityDateQueryPlan method execute.
@Override
public Optional<MultilineEntityResult> execute(QueryExecutionContext ctx, Entity entity) {
// Don't set the query date aggregator here because the subqueries should set their aggregator independently
// Execute the prerequisite query
Optional<EntityResult> preResult = query.execute(ctx, entity);
if (preResult.isEmpty()) {
return Optional.empty();
}
Optional<DateAggregator> validityDateAggregator = query.getValidityDateAggregator();
if (validityDateAggregator.isEmpty()) {
return Optional.empty();
}
final CDateSet aggregationResult = validityDateAggregator.get().createAggregationResult();
aggregationResult.retainAll(dateRestriction);
// Generate DateContexts in the provided resolutions
List<DateContext> contexts = new ArrayList<>();
for (CDateRange range : aggregationResult.asRanges()) {
contexts.addAll(DateContext.generateAbsoluteContexts(range, resolutionsAndAlignments));
}
FormQueryPlan resolutionQuery = new FormQueryPlan(contexts, features);
return resolutionQuery.execute(ctx, entity);
}
use of com.bakdata.conquery.models.common.daterange.CDateRange in project conquery by bakdata.
the class DateContext method generateRelativeContexts.
/**
* Generates a list of date contexts around an index date which belong either to the feature group (before the index)
* or the outcome group (after the index). The computed feature and outcome date ranges cover a date range
* specified by featureTime*timeUnit for the the feature range (respective outcomeTime for the outcome range).
* These ranges are sub divided in to the coarseness of the given resolutions.
* The event (a certain day) itself is expanded to a date range according to the desired alignment and the indexPlacement
* determines to which group it belongs.
*/
public static List<DateContext> generateRelativeContexts(int event, IndexPlacement indexPlacement, int featureTime, int outcomeTime, CalendarUnit timeUnit, List<ExportForm.ResolutionAndAlignment> resolutionAndAlignment) {
if (featureTime < 1 && outcomeTime < 1) {
throw new IllegalArgumentException("Both relative times were smaller than 1 (featureTime: " + featureTime + "; outcomeTime: " + outcomeTime + ")");
}
List<DateContext> dcList = new ArrayList<>();
LocalDate eventdate = CDate.toLocalDate(event);
CDateRange featureRange = generateFeatureRange(event, indexPlacement, featureTime, timeUnit);
CDateRange outcomeRange = generateOutcomeRange(event, indexPlacement, outcomeTime, timeUnit);
for (ExportForm.ResolutionAndAlignment mode : resolutionAndAlignment) {
Function<CDateRange, List<CDateRange>> featureRangeDivider = getDateRangeSubdivider(AlignmentReference.END, mode.getResolution(), mode.getAlignment());
Function<CDateRange, List<CDateRange>> outcomeRangeDivider = getDateRangeSubdivider(AlignmentReference.START, mode.getResolution(), mode.getAlignment());
if (featureRange != null) {
List<CDateRange> featureRanges = featureRangeDivider.apply(featureRange);
/*
* Depending on the index placement the event date belong to the feature range , outcome range or neither. This is represented in the index.
* If the index placement is BEFORE, the event date is included in the most recent feature date range, which is marked by an index of 0.
* If the index placement is NEUTRAL, the event date is not included in any date range and not range index is marked with 0.
* If the index placement is AFTER, the event date is included in the earliest outcome date range, which is marked by 0.
*/
int index = indexPlacement.equals(IndexPlacement.BEFORE) ? featureRanges.size() - 1 : featureRanges.size();
for (CDateRange subRange : featureRanges) {
DateContext dc = new DateContext(subRange, FeatureGroup.FEATURE, // For now there is no index for complete
mode.getResolution().equals(Resolution.COMPLETE) ? null : -index, eventdate, mode.getResolution());
index--;
dcList.add(dc);
}
}
if (outcomeRange != null) {
int index = indexPlacement.equals(IndexPlacement.AFTER) ? 0 : 1;
for (CDateRange subRange : outcomeRangeDivider.apply(outcomeRange)) {
DateContext dc = new DateContext(subRange, FeatureGroup.OUTCOME, // For now there is no index for complete
mode.getResolution().equals(Resolution.COMPLETE) ? null : index, eventdate, mode.getResolution());
index++;
dcList.add(dc);
}
}
}
return dcList;
}
use of com.bakdata.conquery.models.common.daterange.CDateRange in project conquery by bakdata.
the class CDateSetTest method testAddMerging.
@ParameterizedTest(name = "{0}")
@MethodSource("arguments")
public void testAddMerging(String expected, CDateRange[] ranges) {
CDateSet set = CDateSet.create();
for (CDateRange range : ranges) {
set.add(range);
}
assertThat(set).hasToString(expected);
}
use of com.bakdata.conquery.models.common.daterange.CDateRange in project conquery by bakdata.
the class DateReader method parseToCDateRange.
/**
* Try and parse value to {@link CDateRange} using all available rangeFormats, starting at the last known successful one.
*/
public CDateRange parseToCDateRange(String value) {
if (Strings.isNullOrEmpty(value)) {
return null;
}
final int root = lastRangeFormatIndex.get();
for (int offset = 0; offset < rangeStartEndSeperators.size(); offset++) {
final int index = (root + offset) % rangeStartEndSeperators.size();
String sep = rangeStartEndSeperators.get(index);
try {
CDateRange result = parseToCDateRange(value, sep);
lastRangeFormatIndex.set(index);
return result;
} catch (ParsingException e) {
log.trace("Parsing failed for date range `{}` using `{}`", value, sep, e);
}
}
throw new ParsingException("None of the configured formats allowed to parse the date range: " + value);
}
Aggregations