/** * Returns the {@link AnalyticsAggregationType} according to the given value type, * aggregation type, value type aggregation period type and data period type. * * @param aggregationType the aggregation type. * @param valueType the value type. * @param aggregationPeriodType the aggregation period type. * @param dataPeriodType the data period type. */ public static AnalyticsAggregationType getAggregationType( AnalyticsAggregationType aggregationType, ValueType valueType, PeriodType aggregationPeriodType, PeriodType dataPeriodType ) { DataType dataType = DataType.fromValueType( valueType ); boolean disaggregation = isDisaggregation( aggregationType, aggregationPeriodType, dataPeriodType ); return new AnalyticsAggregationType( aggregationType.getAggregationType(), aggregationType.getPeriodAggregationType(), dataType, disaggregation ); }
if ( aggType.isAggregationType( SUM ) && aggType.isPeriodAggregationType( AVERAGE ) && aggType.isNumericDataType() ) else if ( aggType.isAggregationType( AVERAGE ) && aggType.isNumericDataType() ) else if ( aggType.isAggregationType( AVERAGE ) && aggType.isBooleanDataType() ) else if ( aggType.isAggregationType( COUNT ) ) else if ( aggType.isAggregationType( STDDEV ) ) else if ( aggType.isAggregationType( VARIANCE ) ) else if ( aggType.isAggregationType( MIN ) ) else if ( aggType.isAggregationType( MAX ) ) else if ( aggType.isAggregationType( NONE ) )
/** * Indicates whether the aggregation type is of type disaggregation. */ public boolean isDisaggregation() { return aggregationType != null && aggregationType.isDisaggregation(); }
boolean sumAvgNumeric = AggregationType.SUM == type.getAggregationType() && AggregationType.AVERAGE == type.getPeriodAggregationType() && DataType.NUMERIC == type.getDataType();
/** * Returns true if an aggregation type is defined, and this is type is {@link #AggregationType.LAST} */ public boolean isLastPeriodAggregationType() { return getAggregationType() != null && getAggregationType().isLastPeriodAggregationType(); }
/** * Returns the aggregation type for this query, first by looking at the * aggregation type of the query, second by looking at the aggregation type * of the value dimension, third by returning AVERAGE; */ public AnalyticsAggregationType getAggregationTypeFallback() { if ( hasAggregationType() ) { return aggregationType; } else if ( hasValueDimension() && value.getAggregationType() != null ) { return AnalyticsAggregationType.fromAggregationType( value.getAggregationType() ); } return AnalyticsAggregationType.AVERAGE; }
/** * Returns a new instance of this aggregation type. */ public AnalyticsAggregationType instance() { return new AnalyticsAggregationType( this.aggregationType, this.periodAggregationType, this.dataType, this.disaggregation ); }
/** * Indicates whether this object is of the given aggregation type. Based on * {@link getAggregationTypeFallback}. */ @Override public boolean isAggregationType( AggregationType type ) { AnalyticsAggregationType typeFallback = getAggregationTypeFallback(); return typeFallback != null && type.equals( typeFallback.getAggregationType() ); }
/** * Indicates whether this query has the given aggregation type. */ public boolean isAggregationType( AggregationType type ) { return aggregationType != null && aggregationType.isAggregationType( type ); }
params.aggregationType = this.aggregationType != null ? this.aggregationType.instance() : null; params.measureCriteria = new HashMap<>( this.measureCriteria ); params.preAggregateMeasureCriteria = new HashMap<>( this.preAggregateMeasureCriteria );
/** * Indicates whether disaggregation is allowed for the given input. Disaggregation * implies that the frequency order of the aggregation period type is lower than * the data period type. * * @param aggregationPeriodType the aggregation period type. * @param dataPeriodType the data period type. */ public static boolean isDisaggregation( AnalyticsAggregationType aggregationType, PeriodType aggregationPeriodType, PeriodType dataPeriodType ) { if ( dataPeriodType == null || aggregationPeriodType == null ) { return false; } if ( aggregationType == null || AggregationType.AVERAGE != aggregationType.getPeriodAggregationType() ) { return false; } if ( aggregationPeriodType.getFrequencyOrder() < dataPeriodType.getFrequencyOrder() ) { return true; } if ( aggregationPeriodType.getFrequencyOrder() == dataPeriodType.getFrequencyOrder() && !aggregationPeriodType.equals( dataPeriodType ) ) { return true; } return false; }
/** * Makes assertions on the query. * * @param params the data query parameters. */ private void assertQuery( DataQueryParams params ) { Assert.notNull( params.getDataType(), "Data type must be present" ); Assert.notNull( params.getAggregationType(), "Aggregation type must be present" ); Assert.isTrue( !( params.getAggregationType().isLastPeriodAggregationType() && params.getPeriods().size() > 1 ), "Max one dimension period can be present per query for last period aggregation" ); } }
AnalyticsAggregationType degAggType = AnalyticsAggregationType.fromAggregationType( deg.getAggregationType() );
public static AnalyticsAggregationType fromAggregationType( AggregationType aggregationType ) { if ( AggregationType.AVERAGE_SUM_ORG_UNIT == aggregationType ) { return new AnalyticsAggregationType( AggregationType.SUM, AggregationType.AVERAGE ); } else if ( AggregationType.LAST == aggregationType ) { return new AnalyticsAggregationType( AggregationType.SUM, AggregationType.LAST ); } else if ( AggregationType.LAST_AVERAGE_ORG_UNIT == aggregationType ) { return new AnalyticsAggregationType( AggregationType.AVERAGE, AggregationType.LAST ); } else { return new AnalyticsAggregationType( aggregationType, aggregationType ); } }
Assert.isTrue( params.getAggregationTypeFallback().getAggregationType().isAggregateable(), "Event query aggregation type must be aggregatable" ); String function = params.getAggregationTypeFallback().getAggregationType().getValue();
/** * Groups the given query in sub queries for each dimension period. This only applies * if the aggregation type is {@link AggregationType#LAST} or * {@link AggregationType#LAST_AVERAGE_ORG_UNIT}. In this case, each period must be * aggregated individually. * * @param params the data query parameters. * @return a list of {@link DataQueryParams}. */ private List<DataQueryParams> groupByPeriod( DataQueryParams params ) { List<DataQueryParams> queries = new ArrayList<>(); if ( params.getAggregationType().isLastPeriodAggregationType() && !params.getPeriods().isEmpty() ) { for ( DimensionalItemObject period : params.getPeriods() ) { String periodType = ((Period) period).getPeriodType().getName().toLowerCase(); DataQueryParams query = DataQueryParams.newBuilder( params ) .withPeriods( Lists.newArrayList( period ), periodType ).build(); queries.add( query ); } } else { queries.add( params ); } logQuerySplit( queries, "period" ); return queries; }
/** * Creates a mapping between the aggregation type and data element for the * given data elements and period type. * * @param params the data query parameters. */ public static ListMap<AnalyticsAggregationType, DimensionalItemObject> getAggregationTypeDataElementMap( DataQueryParams params ) { List<DimensionalItemObject> dataElements = params.getDataElements(); PeriodType aggregationPeriodType = PeriodType.getPeriodTypeByName( params.getPeriodType() ); ListMap<AnalyticsAggregationType, DimensionalItemObject> map = new ListMap<>(); for ( DimensionalItemObject element : dataElements ) { DataElement de = (DataElement) element; AnalyticsAggregationType aggregationType = ObjectUtils.firstNonNull( params.getAggregationType(), AnalyticsAggregationType.fromAggregationType( de.getAggregationType() ) ); AnalyticsAggregationType analyticsAggregationType = getAggregationType( aggregationType, de.getValueType(), aggregationPeriodType, de.getPeriodType() ); map.putValue( analyticsAggregationType, de ); } return map; }
/** * Generates the from clause of the query SQL. */ private String getFromClause( DataQueryParams params ) { String sql = "from "; if ( params.getAggregationType().isLastPeriodAggregationType() ) { sql += getLastValueSubquerySql( params ); } else if ( params.hasPreAggregateMeasureCriteria() && params.isDataType( DataType.NUMERIC ) ) { sql += getPreMeasureCriteriaSubquerySql( params ); } else { sql += getFromSourceClause( params ); } return sql + " as " + ANALYTICS_TBL_ALIAS + " "; }
params.withAggregationType( AnalyticsAggregationType.fromAggregationType( request.getAggregationType() ) );
if ( params.isAggregateData() && params.hasValueDimension() && params.getAggregationTypeFallback().isLastPeriodAggregationType() )