use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.
the class DefaultQueryPlanner method splitByDimension.
// -------------------------------------------------------------------------
// Supportive methods
// -------------------------------------------------------------------------
/**
* Splits the given list of queries in sub queries on the given dimension.
*/
private DataQueryGroups splitByDimension(DataQueryGroups queryGroups, String dimension, int optimalQueries) {
int optimalForSubQuery = MathUtils.divideToFloor(optimalQueries, queryGroups.getLargestGroupSize());
List<DataQueryParams> subQueries = new ArrayList<>();
for (DataQueryParams query : queryGroups.getAllQueries()) {
DimensionalObject dim = query.getDimension(dimension);
List<DimensionalItemObject> values = null;
if (dim == null || (values = dim.getItems()) == null || values.isEmpty()) {
subQueries.add(DataQueryParams.newBuilder(query).build());
continue;
}
List<List<DimensionalItemObject>> valuePages = new PaginatedList<>(values).setNumberOfPages(optimalForSubQuery).getPages();
for (List<DimensionalItemObject> valuePage : valuePages) {
DataQueryParams subQuery = DataQueryParams.newBuilder(query).withDimensionOptions(dim.getDimension(), valuePage).build();
subQueries.add(subQuery);
}
}
if (subQueries.size() > queryGroups.getAllQueries().size()) {
log.debug(String.format("Split on dimension %s: %d", dimension, (subQueries.size() / queryGroups.getAllQueries().size())));
}
return DataQueryGroups.newBuilder().withQueries(subQueries).build();
}
use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.
the class DefaultQueryPlanner method groupByAggregationType.
/**
* Groups the given query in sub queries based on the aggregation type of its
* data elements. The aggregation type can be sum, average aggregation or
* average disaggregation. Sum means that the data elements have sum aggregation
* operator. Average aggregation means that the data elements have the average
* aggregation operator and that the period type of the data elements have
* higher or equal frequency than the aggregation period type. Average disaggregation
* means that the data elements have the average aggregation operator and
* that the period type of the data elements have lower frequency than the
* aggregation period type. Average bool means that the data elements have the
* average aggregation operator and the bool value type.
* <p>
* If no data elements are present, the aggregation type will be determined
* based on the first data element in the first data element group in the
* first data element group set in the query.
* <p>
* If the aggregation type is already set/overridden in the request, the
* query will be returned unchanged. If there are no data elements or data
* element group sets specified the aggregation type will fall back to sum.
*
* @param params the data query parameters.
* @return a list of {@link DataQueryParams}.
*/
private List<DataQueryParams> groupByAggregationType(DataQueryParams params) {
List<DataQueryParams> queries = new ArrayList<>();
if (!params.getDataElements().isEmpty()) {
ListMap<AggregationType, DimensionalItemObject> aggregationTypeDataElementMap = QueryPlannerUtils.getAggregationTypeDataElementMap(params);
for (AggregationType aggregationType : aggregationTypeDataElementMap.keySet()) {
DataQueryParams query = DataQueryParams.newBuilder(params).withDataElements(aggregationTypeDataElementMap.get(aggregationType)).withAggregationType(aggregationType).build();
queries.add(query);
}
} else if (!params.getDataElementGroupSets().isEmpty()) {
DimensionalObject degs = params.getDataElementGroupSets().get(0);
DataElementGroup deg = (DataElementGroup) (degs.hasItems() ? degs.getItems().get(0) : null);
AggregationType aggregationType = ObjectUtils.firstNonNull(params.getAggregationType(), SUM);
if (deg != null && !deg.getMembers().isEmpty()) {
PeriodType periodType = PeriodType.getPeriodTypeByName(params.getPeriodType());
aggregationType = ObjectUtils.firstNonNull(params.getAggregationType(), deg.getAggregationType());
aggregationType = QueryPlannerUtils.getAggregationType(deg.getValueType(), aggregationType, periodType, deg.getPeriodType());
}
DataQueryParams query = DataQueryParams.newBuilder(params).withAggregationType(aggregationType).build();
queries.add(query);
} else {
DataQueryParams query = DataQueryParams.newBuilder(params).withAggregationType(ObjectUtils.firstNonNull(params.getAggregationType(), SUM)).build();
queries.add(query);
}
if (queries.size() > 1) {
log.debug(String.format("Split on aggregation type: %d", queries.size()));
}
return queries;
}
use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.
the class DataValidationTask method getEventMapForSlidingWindow.
private MapMap<String, DimensionalItemObject, Double> getEventMapForSlidingWindow(Set<DimensionalItemObject> dimensionItems, Period period, OrganisationUnit organisationUnit) {
MapMap<String, DimensionalItemObject, Double> map = new MapMap<>();
if (dimensionItems.isEmpty() || period == null || organisationUnit == null) {
return map;
}
// We want to position the sliding window over the most recent data. To achieve this, we need to satisfy the
// following criteria:
//
// 1. Window end should not be later than the current date
// 2. Window end should not be later than the period.endDate
// Criteria 1
Calendar endDate = Calendar.getInstance();
Calendar startDate = Calendar.getInstance();
// Criteria 2
if (endDate.getTime().after(period.getEndDate())) {
endDate.setTime(period.getEndDate());
}
// The window size is based on the frequencyOrder of the period's periodType:
startDate.setTime(endDate.getTime());
startDate.add(Calendar.DATE, (-1 * period.frequencyOrder()));
DataQueryParams params = DataQueryParams.newBuilder().withDataDimensionItems(Lists.newArrayList(dimensionItems)).withAttributeOptionCombos(Lists.newArrayList()).withStartDate(startDate.getTime()).withEndDate(endDate.getTime()).withFilterOrganisationUnits(Lists.newArrayList(organisationUnit)).build();
return getEventData(params);
}
use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.
the class DataValidationTask method getEventMap.
/**
* Returns aggregated event data for the given parameters.
*
* @param dimensionItems the data dimension items.
* @param period the period.
* @param organisationUnit the organisation unit.
* @return a map mapping of attribute option combo identifier to data element operand
* and value.
*/
private MapMap<String, DimensionalItemObject, Double> getEventMap(Set<DimensionalItemObject> dimensionItems, Period period, OrganisationUnit organisationUnit) {
MapMap<String, DimensionalItemObject, Double> map = new MapMap<>();
if (dimensionItems.isEmpty() || period == null || organisationUnit == null) {
return map;
}
DataQueryParams params = DataQueryParams.newBuilder().withDataDimensionItems(Lists.newArrayList(dimensionItems)).withAttributeOptionCombos(Lists.newArrayList()).withFilterPeriods(Lists.newArrayList(period)).withFilterOrganisationUnits(Lists.newArrayList(organisationUnit)).build();
return getEventData(params);
}
use of org.hisp.dhis.analytics.DataQueryParams in project dhis2-core by dhis2.
the class DataQueryServiceTest method testGetFromUrlD.
@Test
public void testGetFromUrlD() {
Set<String> dimensionParams = new HashSet<>();
dimensionParams.add("dx:" + deA.getDimensionItem() + ";" + deB.getDimensionItem() + ";" + patA.getDimensionItem() + ";" + patB.getDimensionItem());
Set<String> filterParams = new HashSet<>();
filterParams.add("ou:" + ouA.getDimensionItem());
DataQueryParams params = dataQueryService.getFromUrl(dimensionParams, filterParams, null, null, null, null, null, false, false, false, false, false, false, false, false, false, false, null, null, null, false, null, null, null, false, null);
assertEquals(2, params.getDataElements().size());
assertEquals(2, params.getProgramAttributes().size());
assertEquals(1, params.getFilterOrganisationUnits().size());
}
Aggregations