Search in sources :

Example 36 with ColumnSet

use of org.activityinfo.model.query.ColumnSet 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 37 with ColumnSet

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

the class OfflineStoreGwtTest method verifyWeCanQuery.

private Promise<Void> verifyWeCanQuery(Void input) {
    QueryModel queryModel = new QueryModel(survey.getFormId());
    queryModel.selectResourceId().as("id");
    queryModel.selectExpr(survey.getGenderFieldId().asString()).as("gender");
    queryModel.selectExpr(survey.getAgeFieldId().asString()).as("age");
    queryModel.selectExpr(survey.getNameFieldId().asString()).as("name");
    return offlineStore.query(queryModel).once().then(new Function<ColumnSet, Void>() {

        @Nullable
        @Override
        public Void apply(@Nullable ColumnSet columnSet) {
            assertEquals(survey.getRowCount() + 2, columnSet.getNumRows());
            return null;
        }
    });
}
Also used : ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) Nullable(javax.annotation.Nullable)

Example 38 with ColumnSet

use of org.activityinfo.model.query.ColumnSet 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 39 with ColumnSet

use of org.activityinfo.model.query.ColumnSet 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 40 with ColumnSet

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

ColumnSet (org.activityinfo.model.query.ColumnSet)50 QueryModel (org.activityinfo.model.query.QueryModel)42 Test (org.junit.Test)28 ColumnView (org.activityinfo.model.query.ColumnView)24 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)14 FormTree (org.activityinfo.model.formTree.FormTree)12 NullFormSupervisor (org.activityinfo.store.query.shared.NullFormSupervisor)12 ResourceId (org.activityinfo.model.resource.ResourceId)10 FormClass (org.activityinfo.model.form.FormClass)7 NullFormScanCache (org.activityinfo.store.query.shared.NullFormScanCache)7 Nullable (javax.annotation.Nullable)6 Charsets (com.google.common.base.Charsets)5 Operation (io.swagger.v3.oas.annotations.Operation)5 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)5 ColumnModel (org.activityinfo.model.query.ColumnModel)5 RowBasedJsonWriter (org.activityinfo.store.query.output.RowBasedJsonWriter)5 PrintWriter (java.io.PrintWriter)4 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)4 FormField (org.activityinfo.model.form.FormField)4 RecordRef (org.activityinfo.model.type.RecordRef)4