Search in sources :

Example 6 with QueryModifiers

use of org.hisp.dhis.common.QueryModifiers in project dhis2-core by dhis2.

the class CommonExpressionVisitor method visitWithQueryMods.

/**
 * Visits a context with a configuration of query modifiers.
 *
 * @param ctx any context
 * @param mods the query modifiers
 * @return the value with the applied offset
 */
public Object visitWithQueryMods(ParserRuleContext ctx, QueryModifiers mods) {
    QueryModifiers savedQueryMods = state.getQueryMods();
    state.setQueryMods(mods);
    Object result = visit(ctx);
    state.setQueryMods(savedQueryMods);
    return result;
}
Also used : QueryModifiers(org.hisp.dhis.common.QueryModifiers)

Example 7 with QueryModifiers

use of org.hisp.dhis.common.QueryModifiers in project dhis2-core by dhis2.

the class ExpressionServiceTest method testIndicatorFunctionParsing.

@Test
void testIndicatorFunctionParsing() {
    DimensionalItemId id;
    id = new DimensionalItemId(DATA_ELEMENT, "dataElemenA", (QueryModifiers) null);
    assertEquals(id, parseItemId("#{dataElemenA}"));
    id = new DimensionalItemId(DATA_ELEMENT, "dataElemenA", QueryModifiers.builder().aggregationType(LAST).build());
    assertEquals(id, parseItemId("#{dataElemenA}.aggregationType(LAST)"));
    id = new DimensionalItemId(DATA_ELEMENT, "dataElemenA", QueryModifiers.builder().periodOffset(10).build());
    assertEquals(id, parseItemId("#{dataElemenA}.periodOffset(10)"));
    id = new DimensionalItemId(DATA_ELEMENT, "dataElemenA", QueryModifiers.builder().periodOffset(-5).build());
    assertEquals(id, parseItemId("#{dataElemenA}.periodOffset(-2).periodOffset(-3)"));
    id = new DimensionalItemId(DATA_ELEMENT, "dataElemenA", QueryModifiers.builder().minDate(parseDate("2020-01-01")).build());
    assertEquals(id, parseItemId("#{dataElemenA}.minDate(2020-1-1)"));
    id = new DimensionalItemId(DATA_ELEMENT, "dataElemenA", QueryModifiers.builder().maxDate(parseDate("2021-12-31")).build());
    assertEquals(id, parseItemId("#{dataElemenA}.maxDate(2021-12-31)"));
    id = new DimensionalItemId(DATA_ELEMENT, "dataElemenA", QueryModifiers.builder().periodOffset(-3).minDate(parseDate("2021-04-01")).maxDate(parseDate("2021-04-30")).build());
    assertEquals(id, parseItemId("#{dataElemenA}.periodOffset(-3).minDate(2021-04-1).maxDate(2021-4-30)"));
}
Also used : QueryModifiers(org.hisp.dhis.common.QueryModifiers) DimensionalItemId(org.hisp.dhis.common.DimensionalItemId) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 8 with QueryModifiers

use of org.hisp.dhis.common.QueryModifiers in project dhis2-core by dhis2.

the class QueryPlannerUtils method getMinMaxDateDateElementMap.

/**
 * Creates a mapping between minDate/maxDate query modifiers and data
 * elements for the given data elements.
 *
 * @param dataElements list of data elements.
 */
public static ListMap<QueryModifiers, DimensionalItemObject> getMinMaxDateDateElementMap(List<DimensionalItemObject> dataElements) {
    ListMap<QueryModifiers, DimensionalItemObject> map = new ListMap<>();
    for (DimensionalItemObject element : dataElements) {
        QueryModifiers queryMods = element.getQueryMods();
        // Get QueryModifiers but only with min and max date, nothing else
        QueryModifiers minMaxDateModifiers = (queryMods == null) ? QueryModifiers.builder().build() : QueryModifiers.builder().minDate(queryMods.getMinDate()).maxDate(queryMods.getMaxDate()).build();
        map.putValue(minMaxDateModifiers, element);
    }
    return map;
}
Also used : QueryModifiers(org.hisp.dhis.common.QueryModifiers) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) ListMap(org.hisp.dhis.common.ListMap)

Example 9 with QueryModifiers

use of org.hisp.dhis.common.QueryModifiers in project dhis2-core by dhis2.

the class QueryPlannerTest method planQueryN.

/**
 * Query spans 5 QueryModifiers minDate/MaxDate combinations.
 */
@Test
void planQueryN() {
    QueryModifiers modsA = QueryModifiers.builder().minDate(parseDate("2022-01-01")).build();
    QueryModifiers modsB = QueryModifiers.builder().minDate(parseDate("2022-02-01")).build();
    QueryModifiers modsC = QueryModifiers.builder().maxDate(parseDate("2022-12-31")).build();
    QueryModifiers modsD = QueryModifiers.builder().minDate(parseDate("2022-01-01")).maxDate(parseDate("2022-12-31")).build();
    deC.setQueryMods(modsA);
    deD.setQueryMods(modsB);
    deG.setQueryMods(modsC);
    deH.setQueryMods(modsD);
    deI.setQueryMods(modsD);
    deC.setAggregationType(AggregationType.SUM);
    deD.setAggregationType(AggregationType.SUM);
    deI.setAggregationType(AggregationType.SUM);
    DataQueryParams params = DataQueryParams.newBuilder().withDataElements(getList(deA, deB, deC, deD, deG, deH, deI)).withPeriods(getList(createPeriod("2022"))).build();
    QueryPlannerParams plannerParams = QueryPlannerParams.newBuilder().withOptimalQueries(4).withTableType(ANALYTICS_TABLE_TYPE).build();
    DataQueryGroups queryGroups = queryPlanner.planQuery(params, plannerParams);
    assertEquals(5, queryGroups.getAllQueries().size());
    assertEquals(1, queryGroups.getSequentialQueries().size());
    assertEquals(5, queryGroups.getLargestGroupSize());
    List<DataQueryParams> group = queryGroups.getAllQueries();
    assertQueryMods(group, null, deA, deB);
    assertQueryMods(group, modsA, deC);
    assertQueryMods(group, modsB, deD);
    assertQueryMods(group, modsC, deG);
    assertQueryMods(group, modsD, deH, deI);
}
Also used : QueryModifiers(org.hisp.dhis.common.QueryModifiers) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DataQueryGroups(org.hisp.dhis.analytics.DataQueryGroups) QueryPlannerParams(org.hisp.dhis.analytics.QueryPlannerParams) Test(org.junit.jupiter.api.Test) DhisSpringTest(org.hisp.dhis.DhisSpringTest)

Example 10 with QueryModifiers

use of org.hisp.dhis.common.QueryModifiers in project dhis2-core by dhis2.

the class FunctionAggregationType method evaluate.

@Override
public Object evaluate(ExprContext ctx, CommonExpressionVisitor visitor) {
    AggregationType aggregationType = parseAggregationType(ctx.aggregationType.getText());
    QueryModifiers queryMods = visitor.getState().getQueryModsBuilder().aggregationType(aggregationType).build();
    return visitor.visitWithQueryMods(ctx.expr(0), queryMods);
}
Also used : QueryModifiers(org.hisp.dhis.common.QueryModifiers) AggregationType(org.hisp.dhis.analytics.AggregationType)

Aggregations

QueryModifiers (org.hisp.dhis.common.QueryModifiers)10 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)3 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)3 Date (java.util.Date)2 DhisSpringTest (org.hisp.dhis.DhisSpringTest)2 ParserUtils.parseExpressionDate (org.hisp.dhis.parser.expression.ParserUtils.parseExpressionDate)2 Test (org.junit.jupiter.api.Test)2 ArrayList (java.util.ArrayList)1 AggregationType (org.hisp.dhis.analytics.AggregationType)1 DataQueryGroups (org.hisp.dhis.analytics.DataQueryGroups)1 QueryPlannerParams (org.hisp.dhis.analytics.QueryPlannerParams)1 DimensionalItemId (org.hisp.dhis.common.DimensionalItemId)1 ListMap (org.hisp.dhis.common.ListMap)1 DataElement (org.hisp.dhis.dataelement.DataElement)1 ExpressionState (org.hisp.dhis.parser.expression.ExpressionState)1