use of org.activityinfo.model.query.ColumnView in project activityinfo by bedatadriven.
the class JoinedSubFormColumnViewSlot method join.
private ColumnView join() {
// The nested column may contain multiple rows
// for each row on the left
ColumnView subColumn = nestedColumn.get();
int numSubRows = subColumn.numRows();
// In order to produce a column with one summarized entry per
// output row, we need to assign "groupIds" going from
// parentId -> master row index via the primary key
int[] masterRowId = new int[numSubRows];
double[] subColumnValues = new double[numSubRows];
SubFormJoin join = Iterables.getOnlyElement(links);
PrimaryKeyMap parentLookup = join.getMasterPrimaryKey().get();
ColumnView parentColumn = join.getParentColumn().get();
for (int i = 0; i < numSubRows; ++i) {
// Get the parent id of this row
String parentId = parentColumn.getString(i);
masterRowId[i] = parentLookup.getRowIndex(parentId);
// Store the value
subColumnValues[i] = subColumn.getDouble(i);
}
int numMasterRows = parentLookup.numRows();
// Sort the data values by master row index
double[] result = Aggregation.sortAndAggregate(statistic, masterRowId, subColumnValues, numSubRows, numMasterRows);
return new DoubleArrayColumnView(result);
}
use of org.activityinfo.model.query.ColumnView in project activityinfo by bedatadriven.
the class EffectiveMapping method createReader.
public DimensionReader createReader(ColumnSet columnSet) {
String missingCategory = computeMissingCategory();
ColumnView columnView = columnSet.getColumnView(getColumnId());
if (columnView == null) {
return row -> missingCategory;
}
Function<String, String> map = createMap();
return row -> {
String category = columnView.getString(row);
if (category == null) {
return missingCategory;
}
return map.apply(category);
};
}
use of org.activityinfo.model.query.ColumnView in project activityinfo by bedatadriven.
the class EffectiveMapping method createMultiDimSet.
public MultiDim createMultiDimSet(ColumnSet columnSet) {
EnumType enumType = (EnumType) this.formula.getResultType();
int numItems = enumType.getValues().size();
int numCategories;
if (model.getMissingIncluded()) {
// If we are included cases with no values set, then consider it
// a category on its own
numCategories = numItems + 1;
} else {
numCategories = numItems;
}
String[] labels = new String[numCategories];
BitSet[] bitSets = new BitSet[numCategories];
List<EnumItem> values = enumType.getValues();
for (int j = 0; j < numItems; j++) {
BitSet bitSet = new BitSet();
EnumItem enumItem = values.get(j);
ColumnView columnView = columnSet.getColumnView(getColumnId(enumItem));
assert columnView != null;
assert columnView.getType() == ColumnType.BOOLEAN;
for (int i = 0; i < columnView.numRows(); i++) {
if (columnView.getBoolean(i) == ColumnView.TRUE) {
bitSet.set(i, true);
}
}
labels[j] = enumItem.getLabel();
bitSets[j] = bitSet;
}
if (model.getMissingIncluded()) {
labels[numCategories - 1] = getMissingLabel();
bitSets[numCategories - 1] = computeMissingBitSet(columnSet.getNumRows(), numItems, bitSets);
}
return new MultiDim(this.index, labels, bitSets);
}
use of org.activityinfo.model.query.ColumnView in project activityinfo by bedatadriven.
the class PivotAdapter method extractSiteIds.
private int[] extractSiteIds(ColumnSet columnSet) {
ColumnView columnView = columnSet.getColumnView(SITE_ID_KEY);
int[] ids = new int[columnView.numRows()];
for (int i = 0; i < columnView.numRows(); i++) {
String resourceId = columnView.getString(i);
if (resourceId != null) {
ids[i] = CuidAdapter.getLegacyIdFromCuid(resourceId);
}
}
return ids;
}
use of org.activityinfo.model.query.ColumnView in project activityinfo by bedatadriven.
the class PivotAdapter method executeTargetValuesQuery.
private void executeTargetValuesQuery(Integer databaseId) {
if (filter.isRestricted(DimensionType.AttributeGroup) || filter.isRestricted(DimensionType.AdminLevel) || filter.isRestricted(DimensionType.Site) || filter.isRestricted(DimensionType.Location)) {
// No results, exit now
return;
}
ResourceId targetFormClassId = CuidAdapter.cuid(CuidAdapter.TARGET_FORM_CLASS_DOMAIN, databaseId);
QueryModel queryModel = new QueryModel(targetFormClassId);
QueryFilter queryFilter = new QueryFilter(filter, attributeFilters, LOGGER);
queryModel.setFilter(queryFilter.composeTargetFilter());
final Collection<Activity> activities = databases.get(databaseId);
// Add all indicators we're querying for
for (Activity activity : activities) {
for (ActivityField indicator : selectedIndicators(activity)) {
queryModel.selectField(targetFieldExpr(indicator)).as(alias(indicator));
}
}
for (DimBinding dimension : groupBy) {
for (ColumnModel columnModel : dimension.getTargetColumnQuery(targetFormClassId)) {
queryModel.addColumn(columnModel);
}
}
enqueueQuery(queryModel, new Function<ColumnSet, Void>() {
@Nullable
@Override
public Void apply(@Nullable ColumnSet columnSet) {
for (Activity activity : activities) {
for (ActivityField indicator : selectedIndicators(activity)) {
ColumnView measureView = columnSet.getColumnView(alias(indicator));
DimensionCategory indicatorCategory = null;
if (indicatorDimension.isPresent()) {
indicatorCategory = indicatorDimension.get().category(indicator);
}
for (int i = 0; i < columnSet.getNumRows(); i++) {
double value = measureView.getDouble(i);
if (!Double.isNaN(value) && !Double.isInfinite(value)) {
Map<Dimension, DimensionCategory> key = new HashMap<>();
for (DimBinding dim : groupBy) {
DimensionCategory category = dim.extractTargetCategory(activity, columnSet, i);
if (category != null) {
key.put(dim.getModel(), category);
}
}
if (indicatorCategory != null) {
key.put(indicatorDimension.get().getModel(), indicatorCategory);
}
Accumulator bucket = bucketForKey(key, indicator.getAggregation());
bucket.addValue(value);
}
}
}
}
return null;
}
});
}
Aggregations