Search in sources :

Example 46 with QueryModel

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

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

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

Example 49 with QueryModel

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

the class PivotAdapter method executeIndicatorValuesQuery.

private void executeIndicatorValuesQuery(final Activity activity, LinkedActivity linkedActivity) throws SQLException {
    // Double check that this activity has not been deleted
    if (isDeleted(activity, linkedActivity)) {
        return;
    }
    // Query the SOURCE form tree
    FormTree formTree = formTrees.get(linkedActivity.getLeafFormClassId());
    Preconditions.checkNotNull(formTree, "No form tree for form " + linkedActivity.getLeafFormClassId());
    QueryModel queryModel = new QueryModel(linkedActivity.getLeafFormClassId());
    List<ActivityField> indicators = selectedIndicators(activity);
    // Add Indicators to the query
    // Keep track of alias to destination map
    final Multimap<String, ActivityField> aliasToIndicator = HashMultimap.create();
    for (ActivityField indicator : indicators) {
        Collection<Integer> sourceIndicatorIds = linkedActivity.getSourceIndicatorIdsFor(indicator.getId());
        for (Integer sourceIndicatorId : sourceIndicatorIds) {
            String alias = "I" + sourceIndicatorId;
            queryModel.selectExpr(fieldExpression(sourceIndicatorId)).as(alias);
            aliasToIndicator.put(alias, indicator);
        }
    }
    // These are the columns we will use for grouping
    for (DimBinding dimension : groupBy) {
        for (ColumnModel columnModel : dimension.getColumnQuery(formTree)) {
            queryModel.addColumn(columnModel);
        }
    }
    // if any of the indicators are "site count" indicators, then we need
    // to query the site id as well
    addSiteIdToQuery(activity, queryModel);
    // declare the filter
    queryModel.setFilter(composeFilter(formTree));
    // Query the table
    enqueueQuery(queryModel, new Function<ColumnSet, Void>() {

        @Nullable
        @Override
        public Void apply(@Nullable ColumnSet columnSet) {
            // Now add the results to the buckets
            int[] siteIds = extractSiteIds(columnSet);
            DimensionCategory[][] categories = extractCategories(activity, columnSet);
            for (String sourceAlias : aliasToIndicator.keySet()) {
                ColumnView measureView = columnSet.getColumnView(sourceAlias);
                // A single source indicator may be mapped to multiple destination Indicators
                for (ActivityField destinationIndicator : aliasToIndicator.get(sourceAlias)) {
                    DimensionCategory indicatorCategory = null;
                    if (indicatorDimension.isPresent()) {
                        indicatorCategory = indicatorDimension.get().category(destinationIndicator);
                    }
                    for (int i = 0; i < columnSet.getNumRows(); i++) {
                        if (destinationIndicator.getAggregation() == IndicatorDTO.AGGREGATE_SITE_COUNT) {
                            Map<Dimension, DimensionCategory> key = bucketKey(i, categories, indicatorCategory);
                            Accumulator bucket = bucketForKey(key, destinationIndicator.getAggregation());
                            bucket.addSite(siteIds[i]);
                        } else {
                            double value = measureView.getDouble(i);
                            if (!Double.isNaN(value) && !Double.isInfinite(value)) {
                                Map<Dimension, DimensionCategory> key = bucketKey(i, categories, indicatorCategory);
                                if (command.getValueType() == PivotSites.ValueType.INDICATOR) {
                                    Accumulator bucket = bucketForKey(key, destinationIndicator.getAggregation());
                                    bucket.addValue(value);
                                } else {
                                    Accumulator bucket = bucketForKey(key, IndicatorDTO.AGGREGATE_SITE_COUNT);
                                    bucket.addSite(siteIds[i]);
                                }
                            }
                        }
                    }
                }
            }
            return null;
        }
    });
}
Also used : DimensionCategory(org.activityinfo.legacy.shared.reports.content.DimensionCategory) ColumnView(org.activityinfo.model.query.ColumnView) ColumnSet(org.activityinfo.model.query.ColumnSet) QueryModel(org.activityinfo.model.query.QueryModel) FormTree(org.activityinfo.model.formTree.FormTree) ColumnModel(org.activityinfo.model.query.ColumnModel) ActivityField(org.activityinfo.store.mysql.metadata.ActivityField) Nullable(javax.annotation.Nullable)

Example 50 with QueryModel

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

the class MySqlUpdateTest method createFormWithCalculationAndRelevance.

@Test
public void createFormWithCalculationAndRelevance() {
    userId = 1;
    KeyGenerator generator = new KeyGenerator();
    int activityId = generator.generateInt();
    FormClass formClass = new FormClass(CuidAdapter.activityFormClass(activityId));
    formClass.setDatabaseId(1);
    formClass.setLabel("New Form");
    FormField numberField = new FormField(CuidAdapter.generateIndicatorId()).setType(new QuantityType("widgets")).setLabel("NUM").setRequired(true);
    formClass.addElement(numberField);
    FormField calculatedField = new FormField(CuidAdapter.generateIndicatorId()).setType(new CalculatedFieldType("1")).setLabel("Calculation").setRelevanceConditionExpression("NUM>42").setRequired(true);
    formClass.addElement(calculatedField);
    catalog.createOrUpdateFormSchema(formClass);
    newRequest();
    // Create two records
    RecordTransactionBuilder tx = new RecordTransactionBuilder();
    RecordUpdate site1 = tx.update(formClass.getId(), CuidAdapter.generateSiteCuid());
    site1.setFieldValue(numberField.getId(), new Quantity(10));
    site1.setFieldValue(partnerField(activityId), CuidAdapter.partnerRef(1, 1));
    RecordUpdate site2 = tx.update(formClass.getId(), CuidAdapter.generateSiteCuid());
    site2.setFieldValue(numberField.getId(), new Quantity(60));
    site2.setFieldValue(partnerField(activityId), CuidAdapter.partnerRef(1, 1));
    updater().execute(tx.build());
    newRequest();
    // Query results
    QueryModel queryModel = new QueryModel(formClass.getId());
    queryModel.selectResourceId();
    queryModel.selectExpr("Num").as("num");
    queryModel.selectExpr("Calculation").as("calc");
    query(queryModel);
    ColumnView num = columnSet.getColumnView("num");
    ColumnView calculation = columnSet.getColumnView("calc");
    assertThat(calculation.isMissing(0), equalTo(num.getDouble(0) <= 42));
    assertThat(calculation.isMissing(1), equalTo(num.getDouble(1) <= 42));
}
Also used : RecordTransactionBuilder(org.activityinfo.model.resource.RecordTransactionBuilder) CalculatedFieldType(org.activityinfo.model.type.expr.CalculatedFieldType) RecordUpdate(org.activityinfo.model.resource.RecordUpdate) TypedRecordUpdate(org.activityinfo.store.spi.TypedRecordUpdate) QuantityType(org.activityinfo.model.type.number.QuantityType) FormClass(org.activityinfo.model.form.FormClass) ColumnView(org.activityinfo.model.query.ColumnView) Quantity(org.activityinfo.model.type.number.Quantity) KeyGenerator(org.activityinfo.model.legacy.KeyGenerator) FormField(org.activityinfo.model.form.FormField) QueryModel(org.activityinfo.model.query.QueryModel) GeoPoint(org.activityinfo.model.type.geo.GeoPoint) Test(org.junit.Test)

Aggregations

QueryModel (org.activityinfo.model.query.QueryModel)59 ColumnSet (org.activityinfo.model.query.ColumnSet)40 Test (org.junit.Test)30 ColumnView (org.activityinfo.model.query.ColumnView)21 ColumnSetBuilder (org.activityinfo.store.query.server.ColumnSetBuilder)13 NullFormSupervisor (org.activityinfo.store.query.shared.NullFormSupervisor)11 FormTree (org.activityinfo.model.formTree.FormTree)9 ResourceId (org.activityinfo.model.resource.ResourceId)9 FormClass (org.activityinfo.model.form.FormClass)7 Nullable (javax.annotation.Nullable)6 FormField (org.activityinfo.model.form.FormField)6 ColumnModel (org.activityinfo.model.query.ColumnModel)6 NullFormScanCache (org.activityinfo.store.query.shared.NullFormScanCache)6 QuantityType (org.activityinfo.model.type.number.QuantityType)5 DimensionCategory (org.activityinfo.legacy.shared.reports.content.DimensionCategory)4 RowBasedJsonWriter (org.activityinfo.store.query.output.RowBasedJsonWriter)4 Charsets (com.google.common.base.Charsets)3 Operation (io.swagger.v3.oas.annotations.Operation)3 FieldPath (org.activityinfo.model.formTree.FieldPath)3 FormTreeBuilder (org.activityinfo.model.formTree.FormTreeBuilder)3