Projection Documentation and Examples

Hi,

I’ve read many blog posts and comments about how awesome projections are at doing temporal queries that are otherwise extremely difficult to achieve in SQL.

I’ve also read that projections are beta and experimental, and that this is the reason that there is almost zero documentation and examples. I’ve read the blog posts and seen the overly simplistic “this projection counts messages of type x” and “if the CPU usage goes over 40%, emit another message” examples. These examples are too simple to be useful.

Despite projections being considered beta, is there at least beta documentation that could be released? Any examples of gathering data from multiple messages related by time?

Let’s say I wanted to emit an event that contained the average CPU usage for the past hour, every hour, how would I do that with a projection?

If I already had say, a week’s worth of CPU usage events in my event store, could a projection process those and aggregate that data into new hourly CPU average messages? Could one projection handle both of these cases, or would I need to write separate ones?

Thanks for the help.

Joe,

For average that would be really simple and is much like one of the examples as seen here: http://geteventstore.com/blog/20130217/projections-intermission/

Let’s imagine I have my event coming in measured.

So measured : function(s,e) {

s.measuredtotal+=e.measured:

s.measuredcount += 1;

}

Now you can have “interval occurred”

Intervaloccured :function(s,e) {

Log("avg " + s.measuredtotal/s.measuredcount);

s.measuredtotal = 0;

s.measuredcount = 0;

}

Another way of doing this without the interval occurred is to look at the date times of measurements in the measured function.

There is actually documentation forming for projections on the wiki it’s just not linked from the tic (look from pages)

Greg

I’m trying to get my projection to work, but when I tried to add logging to help see what’s going on, I see this error:

projection failed to process an event. Handler: EventStore.Projections.Core.Services.v8.DefaultV8ProjectionStateHandler
Event Position: WeightStat: 1 Message: ReferenceError: Log is not defined

Do I have to do anything special to get logging to work? I’m calling log like in your example below Log(‘some string’);

I found the Rob Ashton series linked from https://github.com/EventStore/EventStore/wiki#rob-ashton—projections-series to be very helpful. He gives a interesting temporal query example @ http://codeofrob.com/entries/evented-github-adventure—temporal-queries,-who-doesnt-trust-their-hardware.html but it might not make sense outside the context of the entire series (worth it to read it all).