Search in sources :

Example 21 with CDateRange

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);
}
Also used : CDateRange(com.bakdata.conquery.models.common.daterange.CDateRange) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) CDateRange(com.bakdata.conquery.models.common.daterange.CDateRange) LocalDate(java.time.LocalDate) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 22 with CDateRange

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);
}
Also used : ArrayList(java.util.ArrayList) CDateRange(com.bakdata.conquery.models.common.daterange.CDateRange) EntityResult(com.bakdata.conquery.models.query.results.EntityResult) MultilineEntityResult(com.bakdata.conquery.models.query.results.MultilineEntityResult) DateContext(com.bakdata.conquery.models.forms.util.DateContext) DateAggregator(com.bakdata.conquery.models.query.queryplan.DateAggregator) CDateSet(com.bakdata.conquery.models.common.CDateSet)

Example 23 with CDateRange

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;
}
Also used : CDateRange(com.bakdata.conquery.models.common.daterange.CDateRange) ExportForm(com.bakdata.conquery.apiv1.forms.export_form.ExportForm) LocalDate(java.time.LocalDate)

Example 24 with CDateRange

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);
}
Also used : CDateRange(com.bakdata.conquery.models.common.daterange.CDateRange) CDateSet(com.bakdata.conquery.models.common.CDateSet) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 25 with CDateRange

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);
}
Also used : ParsingException(com.bakdata.conquery.models.exceptions.ParsingException) CDateRange(com.bakdata.conquery.models.common.daterange.CDateRange)

Aggregations

CDateRange (com.bakdata.conquery.models.common.daterange.CDateRange)25 Test (org.junit.jupiter.api.Test)8 DateContext (com.bakdata.conquery.models.forms.util.DateContext)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 CDateSet (com.bakdata.conquery.models.common.CDateSet)3 ArrayList (java.util.ArrayList)3 ExportForm (com.bakdata.conquery.apiv1.forms.export_form.ExportForm)2 LocalDate (java.time.LocalDate)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 Column (com.bakdata.conquery.models.datasets.Column)1 Table (com.bakdata.conquery.models.datasets.Table)1 CBlock (com.bakdata.conquery.models.events.CBlock)1 ParsingException (com.bakdata.conquery.models.exceptions.ParsingException)1 DateAggregator (com.bakdata.conquery.models.query.queryplan.DateAggregator)1 EntityResult (com.bakdata.conquery.models.query.results.EntityResult)1 MultilineEntityResult (com.bakdata.conquery.models.query.results.MultilineEntityResult)1 ObjectReader (com.fasterxml.jackson.databind.ObjectReader)1