Search in sources :

Example 36 with DataQueryParams

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

the class DefaultAnalyticsService method getAggregatedDataValueSet.

@Override
public DataValueSet getAggregatedDataValueSet(DataQueryParams params) {
    DataQueryParams query = DataQueryParams.newBuilder(params).withSkipMeta(false).withSkipData(false).withIncludeNumDen(false).withOutputFormat(OutputFormat.DATA_VALUE_SET).build();
    Grid grid = getAggregatedDataValueGridInternal(query);
    return AnalyticsUtils.getDataValueSetFromGrid(params, grid);
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) ListGrid(org.hisp.dhis.system.grid.ListGrid) Grid(org.hisp.dhis.common.Grid)

Example 37 with DataQueryParams

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

the class DefaultDataQueryService method getFromAnalyticalObject.

@Override
public DataQueryParams getFromAnalyticalObject(AnalyticalObject object) {
    Assert.notNull(object, "Analytical object cannot be null");
    DataQueryParams.Builder params = DataQueryParams.newBuilder();
    I18nFormat format = i18nManager.getI18nFormat();
    IdScheme idScheme = IdScheme.UID;
    Date date = object.getRelativePeriodDate();
    String userOrgUnit = object.getRelativeOrganisationUnit() != null ? object.getRelativeOrganisationUnit().getUid() : null;
    List<OrganisationUnit> userOrgUnits = getUserOrgUnits(null, userOrgUnit);
    object.populateAnalyticalProperties();
    for (DimensionalObject column : object.getColumns()) {
        params.addDimension(getDimension(column.getDimension(), getDimensionalItemIds(column.getItems()), date, userOrgUnits, format, false, false, idScheme));
    }
    for (DimensionalObject row : object.getRows()) {
        params.addDimension(getDimension(row.getDimension(), getDimensionalItemIds(row.getItems()), date, userOrgUnits, format, false, false, idScheme));
    }
    for (DimensionalObject filter : object.getFilters()) {
        params.addFilter(getDimension(filter.getDimension(), getDimensionalItemIds(filter.getItems()), date, userOrgUnits, format, false, false, idScheme));
    }
    return params.build();
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) I18nFormat(org.hisp.dhis.i18n.I18nFormat) DimensionalObject(org.hisp.dhis.common.DimensionalObject)

Example 38 with DataQueryParams

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

the class JdbcRawAnalyticsManager method getStatement.

private String getStatement(DataQueryParams params, String partition) {
    List<String> dimensionColumns = params.getDimensions().stream().map(d -> statementBuilder.columnQuote(d.getDimensionName())).collect(Collectors.toList());
    setOrgUnitSelect(params, dimensionColumns);
    SqlHelper sqlHelper = new SqlHelper();
    String sql = "select " + StringUtils.join(dimensionColumns, ", ") + ", " + DIM_NAME_OU + ", value " + "from " + partition + " ax " + "inner join organisationunit ou on ax.ou = ou.uid " + "inner join _periodstructure ps on ax.pe = ps.iso ";
    for (DimensionalObject dim : params.getDimensions()) {
        if (!dim.getItems().isEmpty() && !dim.isFixed()) {
            String col = statementBuilder.columnQuote(dim.getDimensionName());
            if (DimensionalObject.ORGUNIT_DIM_ID.equals(dim.getDimension())) {
                sql += sqlHelper.whereAnd() + " (";
                for (DimensionalItemObject item : dim.getItems()) {
                    OrganisationUnit unit = (OrganisationUnit) item;
                    sql += DIM_NAME_OU + " like '" + unit.getPath() + "%' or ";
                }
                sql = TextUtils.removeLastOr(sql) + ") ";
            } else {
                sql += sqlHelper.whereAnd() + " " + col + " in (" + getQuotedCommaDelimitedString(getUids(dim.getItems())) + ") ";
            }
        }
    }
    if (params.hasStartEndDate()) {
        sql += sqlHelper.whereAnd() + " " + "ps.startdate >= '" + DateUtils.getMediumDateString(params.getStartDate()) + "' and " + "ps.enddate <= '" + DateUtils.getMediumDateString(params.getEndDate()) + "' ";
    }
    return sql;
}
Also used : ORGUNIT_DIM_ID(org.hisp.dhis.common.DimensionalObject.ORGUNIT_DIM_ID) StringUtils(org.apache.commons.lang.StringUtils) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) Resource(javax.annotation.Resource) Autowired(org.springframework.beans.factory.annotation.Autowired) StatementBuilder(org.hisp.dhis.jdbc.StatementBuilder) DateUtils(org.hisp.dhis.system.util.DateUtils) RawAnalyticsManager(org.hisp.dhis.analytics.RawAnalyticsManager) Grid(org.hisp.dhis.common.Grid) Collectors(java.util.stream.Collectors) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) List(java.util.List) IdentifiableObjectUtils.getUids(org.hisp.dhis.common.IdentifiableObjectUtils.getUids) DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalObject(org.hisp.dhis.common.DimensionalObject) Log(org.apache.commons.logging.Log) SqlRowSet(org.springframework.jdbc.support.rowset.SqlRowSet) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) LogFactory(org.apache.commons.logging.LogFactory) Collections(java.util.Collections) SqlHelper(org.hisp.dhis.commons.util.SqlHelper) TextUtils(org.hisp.dhis.commons.util.TextUtils) OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) SqlHelper(org.hisp.dhis.commons.util.SqlHelper) TextUtils.getQuotedCommaDelimitedString(org.hisp.dhis.commons.util.TextUtils.getQuotedCommaDelimitedString) DimensionalObject(org.hisp.dhis.common.DimensionalObject)

Example 39 with DataQueryParams

use of org.hisp.dhis.analytics.DataQueryParams 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 40 with DataQueryParams

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

the class DefaultQueryPlanner method groupByStartEndDate.

@Override
public List<DataQueryParams> groupByStartEndDate(DataQueryParams params) {
    List<DataQueryParams> queries = new ArrayList<>();
    if (!params.getPeriods().isEmpty()) {
        for (DimensionalItemObject item : params.getPeriods()) {
            Period period = (Period) item;
            DataQueryParams query = DataQueryParams.newBuilder(params).withStartDate(period.getStartDate()).withEndDate(period.getEndDate()).build();
            BaseDimensionalObject staticPeriod = (BaseDimensionalObject) query.getDimension(PERIOD_DIM_ID);
            staticPeriod.setDimensionName(period.getIsoDate());
            staticPeriod.setFixed(true);
            queries.add(query);
        }
    } else if (!params.getFilterPeriods().isEmpty()) {
        Period period = (Period) params.getFilterPeriods().get(0);
        DataQueryParams query = DataQueryParams.newBuilder(params).withStartDate(period.getStartDate()).withEndDate(period.getEndDate()).removeFilter(PERIOD_DIM_ID).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 period: %d", queries.size()));
    }
    return queries;
}
Also used : DataQueryParams(org.hisp.dhis.analytics.DataQueryParams) DimensionalItemObject(org.hisp.dhis.common.DimensionalItemObject) BaseDimensionalObject(org.hisp.dhis.common.BaseDimensionalObject) ArrayList(java.util.ArrayList) Period(org.hisp.dhis.period.Period) IllegalQueryException(org.hisp.dhis.common.IllegalQueryException)

Aggregations

DataQueryParams (org.hisp.dhis.analytics.DataQueryParams)81 Test (org.junit.Test)52 DhisSpringTest (org.hisp.dhis.DhisSpringTest)51 BaseDimensionalObject (org.hisp.dhis.common.BaseDimensionalObject)24 QueryPlannerParams (org.hisp.dhis.analytics.QueryPlannerParams)22 DimensionalItemObject (org.hisp.dhis.common.DimensionalItemObject)19 DataQueryGroups (org.hisp.dhis.analytics.DataQueryGroups)17 ArrayList (java.util.ArrayList)16 DimensionalObject (org.hisp.dhis.common.DimensionalObject)15 HashSet (java.util.HashSet)14 LinkedHashSet (java.util.LinkedHashSet)13 HashMap (java.util.HashMap)7 PeriodType.getPeriodTypeFromIsoString (org.hisp.dhis.period.PeriodType.getPeriodTypeFromIsoString)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 List (java.util.List)6 Map (java.util.Map)6 OrganisationUnit.getParentGraphMap (org.hisp.dhis.organisationunit.OrganisationUnit.getParentGraphMap)6 OrganisationUnit.getParentNameGraphMap (org.hisp.dhis.organisationunit.OrganisationUnit.getParentNameGraphMap)6 EventQueryParams (org.hisp.dhis.analytics.event.EventQueryParams)5 Grid (org.hisp.dhis.common.Grid)5