Search in sources :

Example 1 with Partitions

use of org.hisp.dhis.analytics.Partitions in project dhis2-core by dhis2.

the class PartitionUtilsTest method testGetTablePeriodMapPrune.

@Test
public void testGetTablePeriodMapPrune() {
    Set<String> validPartitions = new HashSet<>();
    validPartitions.add(TBL + "_2000");
    validPartitions.add(TBL + "_2002");
    validPartitions.add(TBL + "_2003");
    ListMap<Partitions, DimensionalItemObject> map = PartitionUtils.getPartitionPeriodMap(getList(createPeriod("2000April"), createPeriod("2000"), createPeriod("2001"), createPeriod("2001Oct"), createPeriod("2002Oct")), TBL, null, validPartitions);
    assertEquals(4, map.size());
    assertTrue(map.keySet().contains(new Partitions().add(TBL + "_2000")));
    assertTrue(map.keySet().contains(new Partitions().add(TBL + "_2000")));
    assertTrue(map.keySet().contains(new Partitions().add(TBL + "_2002")));
    assertTrue(map.keySet().contains(new Partitions().add(TBL + "_2002").add(TBL + "_2003")));
}
Also used : Partitions(org.hisp.dhis.analytics.Partitions) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with Partitions

use of org.hisp.dhis.analytics.Partitions in project dhis2-core by dhis2.

the class PartitionUtils method getPartitions.

public static Partitions getPartitions(Period period, String tablePrefix, String tableSuffix, Set<String> validPartitions) {
    tablePrefix = StringUtils.trimToEmpty(tablePrefix);
    tableSuffix = StringUtils.trimToEmpty(tableSuffix);
    Partitions partitions = new Partitions();
    int startYear = PeriodType.getCalendar().fromIso(period.getStartDate()).getYear();
    int endYear = PeriodType.getCalendar().fromIso(period.getEndDate()).getYear();
    while (startYear <= endYear) {
        String name = tablePrefix + SEP + startYear + tableSuffix;
        partitions.add(name.toLowerCase());
        startYear++;
    }
    return partitions.prunePartitions(validPartitions);
}
Also used : Partitions(org.hisp.dhis.analytics.Partitions)

Example 3 with Partitions

use of org.hisp.dhis.analytics.Partitions in project dhis2-core by dhis2.

the class DefaultQueryPlanner method groupByPartition.

// -------------------------------------------------------------------------
// Supportive - group by methods
// -------------------------------------------------------------------------
@Override
public List<DataQueryParams> groupByPartition(DataQueryParams params, QueryPlannerParams plannerParams) {
    Set<String> validPartitions = partitionManager.getDataValueAnalyticsPartitions();
    String tableName = plannerParams.getTableName();
    String tableSuffix = plannerParams.getTableSuffix();
    List<DataQueryParams> queries = new ArrayList<>();
    if (params.isSkipPartitioning()) {
        DataQueryParams query = DataQueryParams.newBuilder(params).withPartitions(new Partitions().add(tableName)).build();
        queries.add(query);
    } else if (!params.getPeriods().isEmpty()) {
        ListMap<Partitions, DimensionalItemObject> partitionPeriodMap = PartitionUtils.getPartitionPeriodMap(params.getPeriods(), tableName, tableSuffix, validPartitions);
        for (Partitions partitions : partitionPeriodMap.keySet()) {
            if (partitions.hasAny()) {
                DataQueryParams query = DataQueryParams.newBuilder(params).withPeriods(partitionPeriodMap.get(partitions)).withPartitions(partitions).build();
                queries.add(query);
            }
        }
    } else if (!params.getFilterPeriods().isEmpty()) {
        Partitions partitions = PartitionUtils.getPartitions(params.getFilterPeriods(), tableName, tableSuffix, validPartitions);
        if (partitions.hasAny()) {
            DataQueryParams query = DataQueryParams.newBuilder(params).withPartitions(partitions).build();
            queries.add(query);
        }
    } else if (params.hasStartEndDate()) {
        Partitions partitions = PartitionUtils.getPartitions(params.getStartDate(), params.getEndDate(), tableName, tableSuffix, validPartitions);
        if (partitions.hasAny()) {
            DataQueryParams query = DataQueryParams.newBuilder(params).withPartitions(partitions).build();
            queries.add(query);
        }
    } else {
        throw new IllegalQueryException("Query does not contain any period dimension items");
    }
    if (queries.size() > 1) {
        log.debug(String.format("Split on partition: %d", queries.size()));
    }
    return queries;
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) Partitions(org.hisp.dhis.analytics.Partitions) ArrayList(java.util.ArrayList) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException) ListMap(org.hisp.dhis.common.ListMap)

Example 4 with Partitions

use of org.hisp.dhis.analytics.Partitions in project dhis2-core by dhis2.

the class PartitionUtils method getPartitions.

/**
 * Returns partitions for the given list of periods.
 *
 * @param period the period.
 * @return partitions for the given list of periods.
 */
public static Partitions getPartitions(List<DimensionalItemObject> periods) {
    final Set<Integer> years = new HashSet<>();
    periods.forEach(p -> {
        Period period = (Period) p;
        years.addAll(getYears(period));
    });
    return new Partitions(years);
}
Also used : Partitions(org.hisp.dhis.analytics.Partitions) Period(org.hisp.dhis.period.Period) HashSet(java.util.HashSet)

Example 5 with Partitions

use of org.hisp.dhis.analytics.Partitions in project dhis2-core by dhis2.

the class DefaultEventQueryPlanner method withTableNameAndPartitions.

// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
/**
 * Sets table name and partitions on the given query.
 *
 * @param params the event query parameters.
 * @return a {@link EventQueryParams}.
 */
private EventQueryParams withTableNameAndPartitions(EventQueryParams params) {
    Partitions partitions = params.hasStartEndDate() ? PartitionUtils.getPartitions(params.getStartDate(), params.getEndDate()) : PartitionUtils.getPartitions(params.getAllPeriods());
    String baseName = params.hasEnrollmentProgramIndicatorDimension() ? AnalyticsTableType.ENROLLMENT.getTableName() : AnalyticsTableType.EVENT.getTableName();
    String tableName = PartitionUtils.getTableName(baseName, params.getProgram());
    if (params.getCurrentUser() != null) {
        partitionManager.filterNonExistingPartitions(partitions, tableName);
    }
    return new EventQueryParams.Builder(params).withTableName(tableName).withPartitions(partitions).build();
}
Also used : EventQueryParams(org.hisp.dhis.analytics.event.EventQueryParams) Partitions(org.hisp.dhis.analytics.Partitions)

Aggregations

Partitions (org.hisp.dhis.analytics.Partitions)20 Test (org.junit.jupiter.api.Test)9 DateTime (org.joda.time.DateTime)7 DhisSpringTest (org.hisp.dhis.DhisSpringTest)6 EventQueryParams (org.hisp.dhis.analytics.event.EventQueryParams)6 Test (org.junit.Test)5 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)4 HashSet (java.util.HashSet)3 DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)3 Period (org.hisp.dhis.period.Period)3 ArrayList (java.util.ArrayList)2 DhisConvenienceTest.createPeriod (org.hisp.dhis.DhisConvenienceTest.createPeriod)2 QueryPlannerParams (org.hisp.dhis.analytics.QueryPlannerParams)1 IllegalQueryException (org.hisp.dhis.common.IllegalQueryException)1 ListMap (org.hisp.dhis.common.ListMap)1