Remember my post about easy OLAP queries in Rails? I rewrote it almost completely and published is as a Rails plugin for anyone to use on github! It is now called: Active OLAP.
Although it is a complete rewrite, the API I demoed in my previous post should still work with some small changes. The most important: you have to enable it for every class you want to use it on with the enable_active_olap method. You can provide a block to this method with dimension definitions, but is not mandatory:
class User < ActiveRecord::Base enable_active_olap do |olap| # create a simple dimension on the account_type field olap.dimension :account_type # create a dimension with custom categories # the order of the categories will be kept in the results # if you use an array to define the categories. olap.dimension :nationality, :categories => [ [:usa, { :country => 'US' }], [:china, { :country => 'CN' }] # other is automatically added ] # Easily create a trend dimension olap.dimension :created_daily, :trend => { :timestamp_field => :created_at, :period_length => 1.day, :period_count => 20 } end end
Now, we can use these dimensions for our OLAP queries. Multiple dimensions are supported too!
# simple query @result = User.olap_query(:nationality) # @result[:usa] == 123, @result[:china] == 456, @result[:other] = 789 # do drilldown using will_paginate to paginate the results # olap_drilldown is implemented as a named_scope @users = User.olap_drilldown(:nationality => :china).paginate(:page => 1) # multiple dimensions! @result = User.olap_query(:nationality, :created_daily) @users = User.olap_drilldown(:nationality => :china, :created_daily => :period_19)
I am working on a generic controller that can easily be added to your Rails project. Just define dimensions for your models and the controller will let you execute OLAP queries and display the results as a table or a graph.
Keep an eye on this weblog or the github project if you want to stay up-to-date! Or, contact me if you have questions, suggestions or want to help out.
Tags: ActiveRecord, active_olap, OLAP, rails



July 29th, 2008 at 6:23 pm
[...] I rewrote it and released this project on [...]