Search in sources :

Example 56 with ColumnView

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);
}
Also used : ColumnView(org.activityinfo.model.query.ColumnView) DoubleArrayColumnView(org.activityinfo.model.query.DoubleArrayColumnView) DoubleArrayColumnView(org.activityinfo.model.query.DoubleArrayColumnView)

Example 57 with ColumnView

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);
    };
}
Also used : SymbolNode(org.activityinfo.model.formula.SymbolNode) ColumnView(org.activityinfo.model.query.ColumnView) Functions(com.google.common.base.Functions) CompoundExpr(org.activityinfo.model.formula.CompoundExpr) java.util(java.util) Function(com.google.common.base.Function) MonthFunction(org.activityinfo.model.formula.functions.date.MonthFunction) Cardinality(org.activityinfo.model.type.Cardinality) ParsedFormula(org.activityinfo.analysis.ParsedFormula) DimensionMapping(org.activityinfo.ui.client.analysis.model.DimensionMapping) EnumType(org.activityinfo.model.type.enumerated.EnumType) ColumnType(org.activityinfo.model.query.ColumnType) LocalDateType(org.activityinfo.model.type.time.LocalDateType) I18N(org.activityinfo.i18n.shared.I18N) Ordering(com.google.common.collect.Ordering) ColumnModel(org.activityinfo.model.query.ColumnModel) ColumnSet(org.activityinfo.model.query.ColumnSet) DimensionModel(org.activityinfo.ui.client.analysis.model.DimensionModel) QuarterFunction(org.activityinfo.model.formula.functions.date.QuarterFunction) FormTree(org.activityinfo.model.formTree.FormTree) EnumItem(org.activityinfo.model.type.enumerated.EnumItem) DateLevel(org.activityinfo.ui.client.analysis.model.DateLevel) ColumnView(org.activityinfo.model.query.ColumnView)

Example 58 with ColumnView

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);
}
Also used : EnumType(org.activityinfo.model.type.enumerated.EnumType) ColumnView(org.activityinfo.model.query.ColumnView) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 59 with ColumnView

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;
}
Also used : ColumnView(org.activityinfo.model.query.ColumnView)

Example 60 with ColumnView

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;
        }
    });
}
Also used : DimensionCategory(org.activityinfo.legacy.shared.reports.content.DimensionCategory) ColumnView(org.activityinfo.model.query.ColumnView) Activity(org.activityinfo.store.mysql.metadata.Activity) LinkedActivity(org.activityinfo.store.mysql.metadata.LinkedActivity) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) QueryFilter(org.activityinfo.server.command.QueryFilter) ResourceId(org.activityinfo.model.resource.ResourceId) ColumnModel(org.activityinfo.model.query.ColumnModel) ActivityField(org.activityinfo.store.mysql.metadata.ActivityField) Nullable(javax.annotation.Nullable)

Aggregations

ColumnView (org.activityinfo.model.query.ColumnView)67 ColumnSet (org.activityinfo.model.query.ColumnSet)22 Test (org.junit.Test)22 QueryModel (org.activityinfo.model.query.QueryModel)21 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)8 ResourceId (org.activityinfo.model.resource.ResourceId)7 NullFormSupervisor (org.activityinfo.store.query.shared.NullFormSupervisor)7 DoubleArrayColumnView (org.activityinfo.model.query.DoubleArrayColumnView)6 NullFormScanCache (org.activityinfo.store.query.shared.NullFormScanCache)6 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)5 FormClass (org.activityinfo.model.form.FormClass)5 FormTree (org.activityinfo.model.formTree.FormTree)5 FormulaNode (org.activityinfo.model.formula.FormulaNode)4 Nullable (javax.annotation.Nullable)3 FormulaSyntaxException (org.activityinfo.model.formula.diagnostic.FormulaSyntaxException)3 ColumnModel (org.activityinfo.model.query.ColumnModel)3 RecordRef (org.activityinfo.model.type.RecordRef)3 PrintWriter (java.io.PrintWriter)2 HashSet (java.util.HashSet)2 EntityCategory (org.activityinfo.legacy.shared.reports.content.EntityCategory)2