Search in sources :

Example 11 with ColumnModel

use of org.activityinfo.model.query.ColumnModel in project activityinfo by bedatadriven.

the class AttributeDimBinding method getColumnQuery.

@Override
public List<ColumnModel> getColumnQuery(FormTree formTree) {
    List<FormTree.Node> fields = formTree.getLeaves();
    // Does this form tree have the specific attribute group identified by the id?
    FieldPath fieldId = findFieldById(fields);
    if (fieldId == null) {
        fieldId = findFieldByName(fields);
    }
    if (fieldId != null) {
        return Collections.singletonList(new ColumnModel().setFormula(fieldId).as(columnId));
    } else {
        // this form has no corresponding attribute
        return Collections.emptyList();
    }
}
Also used : FieldPath(org.activityinfo.model.formTree.FieldPath) ColumnModel(org.activityinfo.model.query.ColumnModel)

Example 12 with ColumnModel

use of org.activityinfo.model.query.ColumnModel in project activityinfo by bedatadriven.

the class EffectiveMapping method getRequiredColumns.

public List<ColumnModel> getRequiredColumns() {
    if (multiValued) {
        List<ColumnModel> columns = new ArrayList<>();
        EnumType enumType = (EnumType) this.formula.getResultType();
        for (EnumItem enumItem : enumType.getValues()) {
            ColumnModel columnModel = new ColumnModel();
            columnModel.setId(getColumnId(enumItem));
            columnModel.setFormula(new CompoundExpr(this.formula.getRootNode(), new SymbolNode(enumItem.getId())));
            columns.add(columnModel);
        }
        return columns;
    }
    if (this.mapping != null && this.formula.isValid()) {
        ColumnModel columnModel = new ColumnModel();
        columnModel.setId(getColumnId());
        columnModel.setFormula(this.formula.getFormula());
        return Collections.singletonList(columnModel);
    }
    return Collections.emptyList();
}
Also used : CompoundExpr(org.activityinfo.model.formula.CompoundExpr) SymbolNode(org.activityinfo.model.formula.SymbolNode) EnumType(org.activityinfo.model.type.enumerated.EnumType) ColumnModel(org.activityinfo.model.query.ColumnModel) EnumItem(org.activityinfo.model.type.enumerated.EnumItem)

Example 13 with ColumnModel

use of org.activityinfo.model.query.ColumnModel in project activityinfo by bedatadriven.

the class PivotAdapter method executeSiteCountQueryOnSite.

private void executeSiteCountQueryOnSite(final Activity activity, LinkedActivity linkedActivity) {
    Preconditions.checkState(!indicatorDimension.isPresent());
    FormTree formTree = formTrees.get(linkedActivity.getSiteFormClassId());
    QueryModel queryModel = new QueryModel(linkedActivity.getSiteFormClassId());
    queryModel.setFilter(composeFilter(formTree));
    // Add dimensions columns as needed
    for (DimBinding dimension : groupBy) {
        for (ColumnModel columnModel : dimension.getColumnQuery(formTree)) {
            queryModel.addColumn(columnModel);
        }
    }
    // Query the table
    enqueueQuery(queryModel, new Function<ColumnSet, Void>() {

        @Nullable
        @Override
        public Void apply(@Nullable ColumnSet columnSet) {
            // Now add the counts to the buckets
            DimensionCategory[][] categories = extractCategories(activity, columnSet);
            for (int i = 0; i < columnSet.getNumRows(); i++) {
                Map<Dimension, DimensionCategory> key = bucketKey(i, categories, null);
                Accumulator bucket = bucketForKey(key, IndicatorDTO.AGGREGATE_SITE_COUNT);
                bucket.addCount(1);
            }
            return null;
        }
    });
}
Also used : DimensionCategory(org.activityinfo.legacy.shared.reports.content.DimensionCategory) FormTree(org.activityinfo.model.formTree.FormTree) ColumnSet(org.activityinfo.model.query.ColumnSet) ColumnModel(org.activityinfo.model.query.ColumnModel) QueryModel(org.activityinfo.model.query.QueryModel) Nullable(javax.annotation.Nullable)

Example 14 with ColumnModel

use of org.activityinfo.model.query.ColumnModel in project activityinfo by bedatadriven.

the class PivotAdapter method executeSiteCountQueryOnReportingPeriod.

private void executeSiteCountQueryOnReportingPeriod(final Activity activity, LinkedActivity linkedActivity) {
    Preconditions.checkArgument(activity.isMonthly());
    // Query the linked activity
    FormTree formTree = formTrees.get(linkedActivity.getLeafFormClassId());
    QueryModel queryModel = new QueryModel(activity.getLeafFormClassId());
    queryModel.setFilter(composeFilter(formTree));
    // Add dimensions columns as needed
    for (DimBinding dimension : groupBy) {
        for (ColumnModel columnModel : dimension.getColumnQuery(formTree)) {
            queryModel.addColumn(columnModel);
        }
    }
    addSiteIdToQuery(activity, queryModel);
    // Query the table
    enqueueQuery(queryModel, new Function<ColumnSet, Void>() {

        @Nullable
        @Override
        public Void apply(@Nullable ColumnSet columnSet) {
            // Now add the counts to the buckets
            DimensionCategory[][] categories = extractCategories(activity, columnSet);
            int[] siteId = extractSiteIds(columnSet);
            for (int i = 0; i < columnSet.getNumRows(); i++) {
                Map<Dimension, DimensionCategory> key = bucketKey(i, categories, null);
                Accumulator bucket = bucketForKey(key, IndicatorDTO.AGGREGATE_SITE_COUNT);
                bucket.addSite(siteId[i]);
            }
            return null;
        }
    });
}
Also used : DimensionCategory(org.activityinfo.legacy.shared.reports.content.DimensionCategory) FormTree(org.activityinfo.model.formTree.FormTree) ColumnSet(org.activityinfo.model.query.ColumnSet) ColumnModel(org.activityinfo.model.query.ColumnModel) QueryModel(org.activityinfo.model.query.QueryModel) Nullable(javax.annotation.Nullable)

Example 15 with ColumnModel

use of org.activityinfo.model.query.ColumnModel 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

ColumnModel (org.activityinfo.model.query.ColumnModel)16 CompoundExpr (org.activityinfo.model.formula.CompoundExpr)6 QueryModel (org.activityinfo.model.query.QueryModel)6 FieldPath (org.activityinfo.model.formTree.FieldPath)5 SymbolNode (org.activityinfo.model.formula.SymbolNode)5 Nullable (javax.annotation.Nullable)4 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)4 ColumnSet (org.activityinfo.model.query.ColumnSet)4 FormTree (org.activityinfo.model.formTree.FormTree)3 ColumnView (org.activityinfo.model.query.ColumnView)2 ResourceId (org.activityinfo.model.resource.ResourceId)2 EnumItem (org.activityinfo.model.type.enumerated.EnumItem)2 ActivityField (org.activityinfo.store.mysql.metadata.ActivityField)2 ArrayList (java.util.ArrayList)1 FormClass (org.activityinfo.model.form.FormClass)1 EnumType (org.activityinfo.model.type.enumerated.EnumType)1 QueryFilter (org.activityinfo.server.command.QueryFilter)1 Activity (org.activityinfo.store.mysql.metadata.Activity)1 LinkedActivity (org.activityinfo.store.mysql.metadata.LinkedActivity)1