Monday, April 7, 2008

Writing a Custom Aggregator for Flex OLAP

Writing a custom aggregator for OLAP involves implementing the IOLAPCustomAggregator interface. It is easy to write a custom one based on the default ones. The default SUM, AVARAGE, MIN, MAX and COUNT aggregators source can be found in the mx.olap.aggregators directory.

The SUM, MIN, MAX and COUNT aggregators are all simple and acutally doesn't require any special handling as they are linear. The AVARAGE aggregator is special because avarage of avarages wouldn't give the correct result and hence we need to maintain proper information to arrive at the correct result.

The following sample shows how this is achieved.

IOLAPCustomAggregator has 6 functions can they can be divided into two sets.

1. computeBegin,computeLoop and computeEnd. These functions are called to initialize, compute and return the simple aggregation value.

Suppose 1..8 are the input values and they need to avaraged as two different sets 1..5 and 6..8. The sequence of function calls is one computeBegin, one or many calls to computeLoop and one call to computeEnd. 1. computeBegin can be used to prepare for a fresh computation. 2. computeLoop is the place where the acutal computation takes place. This function would get called repeatedly to add new input values to the aggregation. 3. computeEnd is supposed to return the value of the computation. This is called when cube decides that no new input values would get added to this aggregation and it is ready to receive the final value.

2. computeObjectBegin, computeObjectLoop, computeObjectEnd. These functions are called to initialize, compute and return the aggregation of aggregations.

The second set of calls would be, one call to computeObjectBegin, one or many calls to computeObjectLoop (depending on the number of aggregated values) and one call to computeObjectEnd.

As shown in the image these functions are supposed to compute the avarage of avarages. As the first set of functions saved the sum and count values separately the task is easy. We need just compute the sum of both these values from each avarage object.

In computeObjectBegin the first value to start the computation is passed. In computeObjectLoop the the computation is carried forward with additional values. In computeObjectEnd the result of the computation should be returned. Hope this helps!

5 comments:

Nancy said...

Great explanation. I'm trying to figure out if I should be using OLAP and was wondering if you could make a percentage aggregator that would calculate what percentage the value is of the entire value of that particular row or column

Sreenivas said...

This is difficult to achieve in the current OLAP implementation because the total value for the row or column gets generated in the end where as the values of the individual cells needs to get computed before it. So it requires support for lazy evaluation.

dns said...

hello, i'm tring to write "runnung totals" aggregator. something is done, but now i have some troubles... i can't resolve object loop

my code -
http://flash-made.blogspot.com/2010/01/flex-olap-running-totals-aggregator.html

grateful for any help

Sreenivas said...

How will running totals work in a OLAP scenario ? I am not able to visualize the output of the queries.

Is it possible to post that ?

dns said...

hello!
thank you for answer! i've posted screenshot of my app with comments in my blog. You can see it - http://flash-made.blogspot.com/2010/01/flex-olap-running-totals-aggregator.html

Logic is (measure - sales):
rta for quarter2 = total sales for quarter 1 + total sales for quarter 2
rta for quarter3 = total sales for quarter 1 + total sales for quarter 2+ total sales for quarter 3

etc.

But for summary groups for all companies and products (red higlighted on screenshot) how i can get value of previous column.

Sorry for my English - it's not my native :)